Skip to content

Commit 5e83085

Browse files
authored
Merge pull request #193 from InsightLab/examples
Examples on math module
2 parents 9491641 + 9c41ca0 commit 5e83085

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

pymove/utils/math.py

Lines changed: 62 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,19 @@ 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(
191+
>>> 'weighted average: ',
192+
>>> arrays_avg(list, weights),
193+
>>> type(arrays_avg(list, weights))
194+
>>> )
195+
'weighted average: 1.6979999999999997 <class 'float'>'
146196
"""
147197
n = len(values_array)
148198

@@ -181,7 +231,12 @@ def array_stats(values_array: List[float]) -> Tuple[float, float, int]:
181231
The sum of the square value of each element in the array.
182232
int.
183233
The number of elements in the array.
184-
234+
Example
235+
-------
236+
>>> from pymove.utils.math import array_stats
237+
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
238+
>>> print(array_stats(list), type(array_stats(list)))
239+
(39.5, 327.25, 5) <class 'tuple'>
185240
"""
186241
sum_ = 0
187242
sum_sq = 0
@@ -215,10 +270,11 @@ def interpolation(x0: float, y0: float, x1: float, y1: float, x: float) -> float
215270
float.
216271
Is the interpolated or extrapolated value.
217272
218-
Examples
219-
--------
220-
- interpolation 1: (30, 3, 40, 5, 37) -> 4.4
221-
- interpolation 2: (30, 3, 40, 5, 35) -> 4.0
222-
273+
Example
274+
-------
275+
>>> from pymove.utils.math import interpolation
276+
>>> x0, y0, x1, y1, x = 2, 4, 3, 6, 3.5
277+
>>> print(interpolation(x0,y0,x1,y1,x), type(interpolation(x0,y0,x1,y1,x)))
278+
7.0 <class 'float'>
223279
"""
224280
return y0 + (y1 - y0) * ((x - x0) / (x1 - x0))

0 commit comments

Comments
 (0)