-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
conditionals.py
61 lines (50 loc) · 1.5 KB
/
conditionals.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
import numpy as _onp
import casadi as _cas
from aerosandbox.numpy.determine_type import is_casadi_type
def where(
condition,
value_if_true,
value_if_false,
):
"""
Return elements chosen from x or y depending on condition.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.where.html
"""
if not is_casadi_type([condition, value_if_true, value_if_false], recursive=True):
return _onp.where(condition, value_if_true, value_if_false)
else:
return _cas.if_else(condition, value_if_true, value_if_false)
def maximum(
x1,
x2,
):
"""
Element-wise maximum of two arrays.
Note: not differentiable at the crossover point, will cause issues if you try to optimize across a crossover.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.maximum.html
"""
if not is_casadi_type([x1, x2], recursive=True):
return _onp.maximum(x1, x2)
else:
return where(
x1 >= x2,
x1,
x2,
)
def minimum(
x1,
x2,
):
"""
Element-wise minimum of two arrays.
Note: not differentiable at the crossover point, will cause issues if you try to optimize across a crossover.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.minimum.html
"""
if not is_casadi_type([x1, x2], recursive=True):
return _onp.minimum(x1, x2)
else:
return where(
x1 <= x2,
x1,
x2,
)