-
Notifications
You must be signed in to change notification settings - Fork 31
/
sympy.py
178 lines (129 loc) · 6.17 KB
/
sympy.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from typing import Union, Dict, Tuple, Any, Sequence, Optional
from numbers import Number
from types import CodeType
import builtins
import math
import sympy
import numpy
__all__ = ["sympify", "substitute_with_eval", "to_numpy"]
Sympifyable = Union[str, Number, sympy.Expr, numpy.str_]
class IndexedBasedFinder:
def __init__(self):
self.symbols = set()
self.indexed_base = set()
self.indices = set()
class SubscriptionChecker(sympy.Symbol):
def __getitem__(s, k):
self.indexed_base.add(str(s))
self.indices.add(k)
return sympy.IndexedBase(str(s))[sympy.Symbol(str(k))]
self.SubscriptionChecker = SubscriptionChecker
def __getitem__(self, k) -> sympy.Expr:
self.symbols.add(k)
return self.SubscriptionChecker(k)
def __contains__(self, k) -> bool:
return True
class Len(sympy.Function):
nargs = 1
@classmethod
def eval(cls, arg) -> Optional[sympy.Integer]:
if hasattr(arg, '__len__'):
return sympy.Integer(len(arg))
is_Integer = True
Len.__name__ = 'len'
sympify_namespace = {'len': Len,
'Len': Len}
def numpy_compatible_mul(*args) -> Union[sympy.Mul, sympy.Array]:
if any(isinstance(a, sympy.NDimArray) for a in args):
result = 1
for a in args:
result = result * (numpy.array(a.tolist()) if isinstance(a, sympy.NDimArray) else a)
return sympy.Array(result)
else:
return sympy.Mul(*args)
def numpy_compatible_ceiling(input_value: Any) -> Any:
if isinstance(input_value, numpy.ndarray):
return numpy.ceil(input_value).astype(numpy.int64)
else:
return sympy.ceiling(input_value)
def to_numpy(sympy_array: sympy.NDimArray) -> numpy.ndarray:
if isinstance(sympy_array, sympy.DenseNDimArray):
if len(sympy_array.shape) == 2:
return numpy.asarray(sympy_array.tomatrix())
elif len(sympy_array.shape) == 1:
return numpy.asarray(sympy_array)
return numpy.array(sympy_array.tolist())
def get_subscripted_symbols(expression: str) -> set:
# track all symbols that are subscipted in here
indexed_base_finder = IndexedBasedFinder()
sympy.sympify(expression, locals=indexed_base_finder)
return indexed_base_finder.indexed_base
def sympify(expr: Union[str, Number, sympy.Expr, numpy.str_], **kwargs) -> sympy.Expr:
if isinstance(expr, numpy.str_):
# putting numpy.str_ in sympy.sympify behaves unexpected in version 1.1.1
# It seems to ignore the locals argument
expr = str(expr)
try:
return sympy.sympify(expr, **kwargs, locals=sympify_namespace)
except TypeError as err:
if err.args[0] == "'Symbol' object is not subscriptable":
indexed_base = get_subscripted_symbols(expr)
return sympy.sympify(expr, **kwargs, locals={**{k: sympy.IndexedBase(k)
for k in indexed_base},
**sympify_namespace})
else:
raise
def substitute_with_eval(expression: sympy.Expr,
substitutions: Dict[str, Union[sympy.Expr, numpy.ndarray, str]]) -> sympy.Expr:
"""Substitutes only sympy.Symbols. Workaround for numpy like array behaviour. ~Factor 3 slower compared to subs"""
substitutions = {k: v if isinstance(v, sympy.Expr) else sympify(v)
for k, v in substitutions.items()}
for symbol in expression.free_symbols:
symbol_name = str(symbol)
if symbol_name not in substitutions:
substitutions[symbol_name] = symbol
string_representation = sympy.srepr(expression)
return eval(string_representation, sympy.__dict__, {'Symbol': substitutions.__getitem__,
'Mul': numpy_compatible_mul})
def _recursive_substitution(expression: sympy.Expr,
substitutions: Dict[sympy.Symbol, sympy.Expr]) -> sympy.Expr:
if not expression.free_symbols:
return expression
elif expression.func is sympy.Symbol:
return substitutions.get(expression, expression)
elif expression.func is sympy.Mul:
func = numpy_compatible_mul
else:
func = expression.func
substitutions = {s: substitutions.get(s, s) for s in expression.free_symbols}
return func(*(_recursive_substitution(arg, substitutions) for arg in expression.args))
def recursive_substitution(expression: sympy.Expr,
substitutions: Dict[str, Union[sympy.Expr, numpy.ndarray, str]]) -> sympy.Expr:
substitutions = {sympy.Symbol(k): sympify(v) for k, v in substitutions.items()}
for s in expression.free_symbols:
substitutions.setdefault(s, s)
return _recursive_substitution(expression, substitutions)
_base_environment = {'builtins': builtins, '__builtins__': builtins}
_math_environment = {**_base_environment, **math.__dict__}
_numpy_environment = {**_base_environment, **numpy.__dict__}
_sympy_environment = {**_base_environment, **sympy.__dict__}
def evaluate_compiled(expression: sympy.Expr,
parameters: Dict[str, Union[numpy.ndarray, Number]],
compiled: CodeType=None, mode=None) -> Tuple[any, CodeType]:
if compiled is None:
compiled = compile(sympy.printing.lambdarepr.lambdarepr(expression),
'<string>', 'eval')
if mode == 'numeric' or mode is None:
result = eval(compiled, parameters.copy(), _numpy_environment)
elif mode == 'exact':
result = eval(compiled, parameters.copy(), _sympy_environment)
else:
raise ValueError("Unknown mode: '{}'".format(mode))
return result, compiled
def evaluate_lambdified(expression: Union[sympy.Expr, numpy.ndarray],
variables: Sequence[str],
parameters: Dict[str, Union[numpy.ndarray, Number]],
lambdified) -> Tuple[Any, Any]:
lambdified = lambdified or sympy.lambdify(variables, expression,
[{'ceiling': numpy_compatible_ceiling}, 'numpy'])
return lambdified(**parameters), lambdified