Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions pymove/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def is_number(value: Union[int, float, str]):
-------
boolean
True if numerical, otherwise False

Examples
--------
>>> from pymove.utils.math import is_number
>>> a, b, c, d = 50, 22.5, '11.25', 'house'
>>> print(is_number(a), type(is_number(a)))
True <class 'bool'>
>>> print(is_number(b), type(is_number(b)))
True <class 'bool'>
>>> print(is_number(c), type(is_number(c)))
True <class 'bool'>
>>> print(is_number(d), type(is_number(d)))
False <class 'bool'>
"""
try:
float(value)
Expand Down Expand Up @@ -55,6 +68,12 @@ def std(values_array: List[float]) -> float:
squaring with * is over 3 times as fast as with **2
http://stackoverflow.com/questions/29046346/comparison-of-power-to-multiplication-in-python

Example
-------
>>> from pymove.utils.math import std
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> print(std(list), type(std(list)))
1.7435595774162693 <class 'float'>
"""
size = len(values_array)
mean = sum(values_array) / size
Expand All @@ -79,6 +98,12 @@ def avg_std(values_array: List[float]) -> Tuple[float, float]:
float
Represents the value of standard deviation.

Example
-------
>>> from pymove.utils.math import avg_std
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> print(avg_std(list), type(avg_std(list)))
1.9493588689617927 <class 'float'>
"""
avg = sum(values_array) / len(values_array)
return avg, std(values_array)
Expand All @@ -98,6 +123,12 @@ def std_sample(values_array: List[float]) -> float:
float
Represents the value of standard deviation of sample.

Example
-------
>>> from pymove.utils.math import std_sample
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> print(std_sample(list), type(std_sample(list)))
1.9493588689617927 <class 'float'>
"""
size = len(values_array)
return std(values_array) * math.sqrt(size / (size - 1))
Expand All @@ -119,6 +150,12 @@ def avg_std_sample(values_array: List[float]) -> Tuple[float, float]:
float
Represents the standard deviation of sample.

Example
-------
>>> from pymove.utils.math import avg_std_sample
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> print(avg_std_sample(list), type(avg_std_sample(list)))
(7.9, 1.9493588689617927) <class 'tuple'>
"""
avg = sum(values_array) / len(values_array)
return avg, std_sample(values_array)
Expand All @@ -143,6 +180,19 @@ def arrays_avg(
float
The mean of the array elements.

Examples
--------
>>> from pymove.utils.math import arrays_avg
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> weights = [0.1, 0.3, 0.15, 0.15, 0.3]
>>> print('standard average', arrays_avg(list), type(arrays_avg(list)))
'standard average 7.9 <class 'float'>'
>>> print(
>>> 'weighted average: ',
>>> arrays_avg(list, weights),
>>> type(arrays_avg(list, weights))
>>> )
'weighted average: 1.6979999999999997 <class 'float'>'
"""
n = len(values_array)

Expand Down Expand Up @@ -181,7 +231,12 @@ def array_stats(values_array: List[float]) -> Tuple[float, float, int]:
The sum of the square value of each element in the array.
int.
The number of elements in the array.

Example
-------
>>> from pymove.utils.math import array_stats
>>> list = [7.8, 9.7, 6.4, 5.6, 10]
>>> print(array_stats(list), type(array_stats(list)))
(39.5, 327.25, 5) <class 'tuple'>
"""
sum_ = 0
sum_sq = 0
Expand Down Expand Up @@ -215,10 +270,11 @@ def interpolation(x0: float, y0: float, x1: float, y1: float, x: float) -> float
float.
Is the interpolated or extrapolated value.

Examples
--------
- interpolation 1: (30, 3, 40, 5, 37) -> 4.4
- interpolation 2: (30, 3, 40, 5, 35) -> 4.0

Example
-------
>>> from pymove.utils.math import interpolation
>>> x0, y0, x1, y1, x = 2, 4, 3, 6, 3.5
>>> print(interpolation(x0,y0,x1,y1,x), type(interpolation(x0,y0,x1,y1,x)))
7.0 <class 'float'>
"""
return y0 + (y1 - y0) * ((x - x0) / (x1 - x0))