Skip to content

Commit 901b1ab

Browse files
committed
putting examples on math module
1 parent 35cfe7b commit 901b1ab

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

pymove/utils/math.py

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ def is_number(value: Union[int, float, str]):
2828
-------
2929
boolean
3030
True if numerical, otherwise False
31+
32+
Examples
33+
--------
34+
>>> from pymove.utils.math import is_number
35+
>>> a,b,c,d = 50,22.5,'11.25','house'
36+
>>> print(is_number(a), type(is_number(a)))
37+
True <class 'bool'>
38+
>>> print(is_number(b), type(is_number(b)))
39+
True <class 'bool'>-
40+
>>> print(is_number(c), type(is_number(c)))
41+
True <class 'bool'>
42+
>>> print(is_number(d), type(is_number(d)))
43+
False <class 'bool'>
3144
"""
3245
try:
3346
float(value)
@@ -55,6 +68,12 @@ def std(values_array: List[float]) -> float:
5568
squaring with * is over 3 times as fast as with **2
5669
http://stackoverflow.com/questions/29046346/comparison-of-power-to-multiplication-in-python
5770
71+
Example
72+
-------
73+
>>> from pymove.utils.math import std
74+
>>> list = [7.8,9.7,6.4,5.6, 10]
75+
>>> print(std(list), type(std(list)))
76+
1.7435595774162693 <class 'float'>
5877
"""
5978
size = len(values_array)
6079
mean = sum(values_array) / size
@@ -79,6 +98,12 @@ def avg_std(values_array: List[float]) -> Tuple[float, float]:
7998
float
8099
Represents the value of standard deviation.
81100
101+
Example
102+
-------
103+
>>> from pymove.utils.math import avg_std
104+
>>> list = [7.8,9.7,6.4,5.6, 10]
105+
>>> print(avg_std(list), type(avg_std(list)))
106+
1.9493588689617927 <class 'float'>
82107
"""
83108
avg = sum(values_array) / len(values_array)
84109
return avg, std(values_array)
@@ -98,6 +123,12 @@ def std_sample(values_array: List[float]) -> float:
98123
float
99124
Represents the value of standard deviation of sample.
100125
126+
Example
127+
-------
128+
>>> from pymove.utils.math import std_sample
129+
>>> list = [7.8,9.7,6.4,5.6, 10]
130+
>>> print(std_sample(list), type(std_sample(list)))
131+
1.9493588689617927 <class 'float'>
101132
"""
102133
size = len(values_array)
103134
return std(values_array) * math.sqrt(size / (size - 1))
@@ -119,6 +150,12 @@ def avg_std_sample(values_array: List[float]) -> Tuple[float, float]:
119150
float
120151
Represents the standard deviation of sample.
121152
153+
Example
154+
-------
155+
>>> from pymove.utils.math import avg_std_sample
156+
>>> list = [7.8,9.7,6.4,5.6, 10]
157+
>>> print(avg_std_sample(list), type(avg_std_sample(list)))
158+
(7.9, 1.9493588689617927) <class 'tuple'>
122159
"""
123160
avg = sum(values_array) / len(values_array)
124161
return avg, std_sample(values_array)
@@ -143,6 +180,16 @@ def arrays_avg(
143180
float
144181
The mean of the array elements.
145182
183+
Examples
184+
--------
185+
>>> from pymove.utils.math import arrays_avg
186+
>>> list = [7.8,9.7,6.4,5.6, 10]
187+
>>> weights = [0.1,0.3,0.15,0.15,0.3]
188+
>>> print('standard average', arrays_avg(list), type(arrays_avg(list)))
189+
standard average 7.9 <class 'float'>
190+
>>> print('weighted average: ', arrays_avg(list, weights),
191+
type(arrays_avg(list, weights)))
192+
weighted average: 1.6979999999999997 <class 'float'>
146193
"""
147194
n = len(values_array)
148195

@@ -181,7 +228,12 @@ def array_stats(values_array: List[float]) -> Tuple[float, float, int]:
181228
The sum of the square value of each element in the array.
182229
int.
183230
The number of elements in the array.
184-
231+
Example
232+
-------
233+
>>> from pymove.utils.math import array_stats
234+
>>> list = [7.8,9.7,6.4,5.6, 10]
235+
>>> print(array_stats(list), type(array_stats(list)))
236+
(39.5, 327.25, 5) <class 'tuple'>
185237
"""
186238
sum_ = 0
187239
sum_sq = 0
@@ -215,10 +267,11 @@ def interpolation(x0: float, y0: float, x1: float, y1: float, x: float) -> float
215267
float.
216268
Is the interpolated or extrapolated value.
217269
218-
Examples
219-
--------
220-
- interpolation 1: (30, 3, 40, 5, 37) -> 4.4
221-
- interpolation 2: (30, 3, 40, 5, 35) -> 4.0
222-
270+
Example
271+
-------
272+
>>> from pymove.utils.math import interpolation
273+
>>> x0,y0,x1,y1,x = 2,4,3,6,3.5
274+
>>> print(interpolation(x0,y0,x1,y1,x), type(interpolation(x0,y0,x1,y1,x)))
275+
7.0 <class 'float'>
223276
"""
224277
return y0 + (y1 - y0) * ((x - x0) / (x1 - x0))

0 commit comments

Comments
 (0)