-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
arithmetic_monadic.py
87 lines (68 loc) · 2.15 KB
/
arithmetic_monadic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import numpy as _onp
import casadi as _cas
from aerosandbox.numpy.determine_type import is_casadi_type
def sum(x, axis: int = None):
"""
Sum of array elements over a given axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.sum.html
"""
if not is_casadi_type(x):
return _onp.sum(x, axis=axis)
else:
if axis == 0:
return _cas.sum1(x).T
elif axis == 1:
return _cas.sum2(x)
elif axis is None:
return sum(sum(x, axis=0), axis=0)
else:
raise ValueError(
"CasADi types can only be up to 2D, so `axis` must be None, 0, or 1."
)
def mean(x, axis: int = None):
"""
Compute the arithmetic mean along the specified axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mean.html
"""
if not is_casadi_type(x):
return _onp.mean(x, axis=axis)
else:
if axis == 0:
return sum(x, axis=0) / x.shape[0]
elif axis == 1:
return sum(x, axis=1) / x.shape[1]
elif axis is None:
return mean(mean(x, axis=0), axis=1)
else:
raise ValueError(
"CasADi types can only be up to 2D, so `axis` must be None, 0, or 1."
)
def abs(x):
if not is_casadi_type(x):
return _onp.abs(x)
else:
return _cas.fabs(x)
# TODO trace()
# def cumsum(x, axis: int = None):
# """
# Return the cumulative sum of the elements along a given axis.
#
# See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html
# """
#
# if not is_casadi_type(x):
# return _onp.cumsum(x, axis=axis)
#
# else:
# raise NotImplementedError
# if axis is None:
# return _cas.cumsum(_onp.flatten(x))
def prod(x, axis: int = None):
"""
Return the product of array elements over a given axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.prod.html
"""
if not is_casadi_type(x):
return _onp.prod(x, axis=axis)
else:
return _cas.exp(sum(_cas.log(x), axis=axis))