-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
sympify.py
315 lines (253 loc) · 9.55 KB
/
sympify.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""sympify -- convert objects Diofant internal format"""
from __future__ import annotations
import typing
from inspect import getmro
import diofant
from .compatibility import iterable
from .evaluate import global_evaluate
class SympifyError(ValueError):
"""Generic sympification error."""
def __init__(self, expr, base_exc=None):
self.expr = expr
self.base_exc = base_exc
def __str__(self):
try:
s = str(self.expr)
except TypeError:
s = repr(self.expr)
return ("Sympify of expression '%s' failed, because of exception being "
'raised:\n%s: %s' % (s, self.base_exc.__class__.__name__,
str(self.base_exc)))
converter: dict[type[typing.Any],
typing.Callable[[typing.Any],
diofant.core.basic.Basic]] = {} # See sympify docstring.
class CantSympify:
"""
Mix in this trait to a class to disallow sympification of its instances.
Examples
========
>>> class Something(dict):
... pass
...
>>> sympify(Something())
{}
>>> class Something(dict, CantSympify):
... pass
...
>>> sympify(Something())
Traceback (most recent call last):
...
SympifyError: SympifyError: {}
"""
def sympify(a, locals=None, convert_xor=True, strict=False, rational=False,
evaluate=None):
"""Converts an arbitrary expression to a type that can be used inside Diofant.
For example, it will convert Python ints into instance of diofant.Rational,
floats into instances of diofant.Float, etc. It is also able to coerce symbolic
expressions which inherit from Basic. This can be useful in cooperation
with SAGE.
It currently accepts as arguments:
- any object defined in diofant
- standard numeric python types: int, long, float, Decimal
- strings (like "0.09" or "2e-19")
- booleans, including ``None`` (will leave ``None`` unchanged)
- lists, sets or tuples containing any of the above
If the argument is already a type that Diofant understands, it will do
nothing but return that value. This can be used at the beginning of a
function to ensure you are working with the correct type.
>>> sympify(2).is_integer
True
>>> sympify(2).is_real
True
>>> sympify(2.0).is_real
True
>>> sympify('2.0').is_real
True
>>> sympify('2e-45').is_real
True
If the expression could not be converted, a SympifyError is raised.
>>> sympify('x***2')
Traceback (most recent call last):
...
SympifyError: SympifyError: "could not parse u'x***2'"
*Locals*
The sympification happens with access to everything that is loaded
by ``from diofant import *``; anything used in a string that is not
defined by that import will be converted to a symbol. In the following,
the ``bitcount`` function is treated as a symbol and the ``O`` is
interpreted as the Order object (used with series) and it raises
an error when used improperly:
>>> s = 'bitcount(42)'
>>> sympify(s)
bitcount(42)
>>> sympify('O(x)')
O(x)
>>> sympify('O + 1')
Traceback (most recent call last):
...
TypeError: unbound method...
In order to have ``bitcount`` be recognized it can be imported into a
namespace dictionary and passed as locals:
>>> ns = {}
>>> exec('from diofant.core.evalf import bitcount', ns)
>>> sympify(s, locals=ns)
6
In order to have the ``O`` interpreted as a Symbol, identify it as such
in the namespace dictionary. This can be done in a variety of ways; all
three of the following are possibilities:
>>> ns['O'] = Symbol('O') # method 1
>>> exec('from diofant.abc import O', ns) # method 2
>>> ns.update({O: Symbol('O')}) # method 3
>>> sympify('O + 1', locals=ns)
O + 1
If you want *all* single-letter and Greek-letter variables to be symbols
then you can use the clashing-symbols dictionaries that have been defined
there as private variables: _clash1 (single-letter variables), _clash2
(the multi-letter Greek names) or _clash (both single and multi-letter
names that are defined in abc).
>>> from diofant.abc import _clash1
>>> _clash1
{'E': E, 'I': I, 'N': N, 'O': O, 'S': S}
>>> sympify('E & O', _clash1)
E & O
*Strict*
If the option ``strict`` is set to ``True``, only the types for which an
explicit conversion has been defined are converted. In the other
cases, a SympifyError is raised.
>>> print(sympify(None))
None
>>> sympify(None, strict=True)
Traceback (most recent call last):
...
SympifyError: SympifyError: None
*Evaluation*
If the option ``evaluate`` is set to ``False``, then arithmetic and
operators will be converted into their Diofant equivalents and the
``evaluate=False`` option will be added. Nested ``Add`` or ``Mul`` will
be denested first. This is done via an AST transformation that replaces
operators with their Diofant equivalents, so if an operand redefines any
of those operations, the redefined operators will not be used.
>>> sympify('2**2 / 3 + 5')
19/3
>>> sympify('2**2 / 3 + 5', evaluate=False)
2**2/3 + 5
Sometimes autosimplification during sympification results in expressions
that are very different in structure than what was entered. Below you
can see how an expression reduces to -1 by autosimplification, but does
not do so when ``evaluate`` option is used.
>>> -2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1
-1
>>> s = '-2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1'
>>> sympify(s)
-1
>>> sympify(s, evaluate=False)
-2*((x - 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1
*Extending*
To extend ``sympify`` to convert custom objects (not derived from ``Basic``),
just define a ``_diofant_`` method to your class. You can do that even to
classes that you do not own by subclassing or adding the method at runtime.
>>> class MyList1:
... def __iter__(self):
... yield 1
... yield 2
... return
...
... def __getitem__(self, i):
... return list(self)[i]
...
... def _diofant_(self):
... return Matrix(self)
>>> sympify(MyList1())
Matrix([
[1],
[2]])
If you do not have control over the class definition you could also use the
``converter`` global dictionary. The key is the class and the value is a
function that takes a single argument and returns the desired Diofant
object, e.g. ``converter[MyList] = lambda x: Matrix(x)``.
>>> class MyList2: # XXX Do not do this if you control the class!
... def __iter__(self): # Use _diofant_!
... yield 1
... yield 2
... return
...
... def __getitem__(self, i):
... return list(self)[i]
>>> converter[MyList2] = lambda x: Matrix(x)
>>> sympify(MyList2())
Matrix([
[1],
[2]])
"""
from .basic import Basic
if evaluate is None:
evaluate = global_evaluate[0]
try:
if issubclass(a, Basic):
return a
except TypeError: # Type of a is unhashable
pass
cls = a.__class__
if issubclass(cls, Basic):
return a
if issubclass(cls, type(None)):
if strict:
raise SympifyError(a)
else:
return a
try:
return converter[cls](a)
except KeyError:
for superclass in getmro(cls):
try:
return converter[superclass](a)
except KeyError:
continue
if isinstance(a, CantSympify):
raise SympifyError(a)
try:
return a._diofant_()
except AttributeError:
pass
if not isinstance(a, str):
for coerce in (float, int):
try:
return sympify(coerce(a))
except (TypeError, ValueError, AttributeError, SympifyError):
continue
if strict:
raise SympifyError(a)
if iterable(a):
return type(a)([sympify(x, locals=locals, convert_xor=convert_xor,
rational=rational) for x in a])
if isinstance(a, dict):
return type(a)([sympify(x, locals=locals, convert_xor=convert_xor,
rational=rational) for x in a.items()])
# At this point we were given an arbitrary expression
# which does not inherit from Basic and doesn't implement
# _diofant_ (which is a canonical and robust way to convert
# anything to Diofant expression).
#
# As a last chance, we try to take "a"'s normal form via str()
# and try to parse it. If it fails, then we have no luck and
# return an exception
try:
a = str(a)
except Exception as exc:
raise SympifyError(a, exc)
from ..parsing.sympy_parser import TokenError
from ..parsing.sympy_parser import convert_xor as t_convert_xor
from ..parsing.sympy_parser import parse_expr
from ..parsing.sympy_parser import rationalize as t_rationalize
from ..parsing.sympy_parser import standard_transformations
transformations = standard_transformations
if rational:
transformations += t_rationalize,
if convert_xor:
transformations += t_convert_xor,
try:
a = a.replace('\n', '')
expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
except (TokenError, SyntaxError) as exc:
raise SympifyError(f'could not parse {a!r}', exc)
return expr