sympify.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. """sympify -- convert objects SymPy internal format"""
  2. from __future__ import annotations
  3. from typing import Any, Callable, overload, TYPE_CHECKING, TypeVar
  4. import mpmath.libmp as mlib
  5. from inspect import getmro
  6. import string
  7. from sympy.core.random import choice
  8. from .parameters import global_parameters
  9. from sympy.utilities.iterables import iterable
  10. if TYPE_CHECKING:
  11. from sympy.core.basic import Basic
  12. from sympy.core.expr import Expr
  13. from sympy.core.numbers import Integer, Float
  14. Tbasic = TypeVar('Tbasic', bound=Basic)
  15. class SympifyError(ValueError):
  16. def __init__(self, expr, base_exc=None):
  17. self.expr = expr
  18. self.base_exc = base_exc
  19. def __str__(self):
  20. if self.base_exc is None:
  21. return "SympifyError: %r" % (self.expr,)
  22. return ("Sympify of expression '%s' failed, because of exception being "
  23. "raised:\n%s: %s" % (self.expr, self.base_exc.__class__.__name__,
  24. str(self.base_exc)))
  25. converter: dict[type[Any], Callable[[Any], Basic]] = {}
  26. #holds the conversions defined in SymPy itself, i.e. non-user defined conversions
  27. _sympy_converter: dict[type[Any], Callable[[Any], Basic]] = {}
  28. #alias for clearer use in the library
  29. _external_converter = converter
  30. class CantSympify:
  31. """
  32. Mix in this trait to a class to disallow sympification of its instances.
  33. Examples
  34. ========
  35. >>> from sympy import sympify
  36. >>> from sympy.core.sympify import CantSympify
  37. >>> class Something(dict):
  38. ... pass
  39. ...
  40. >>> sympify(Something())
  41. {}
  42. >>> class Something(dict, CantSympify):
  43. ... pass
  44. ...
  45. >>> sympify(Something())
  46. Traceback (most recent call last):
  47. ...
  48. SympifyError: SympifyError: {}
  49. """
  50. __slots__ = ()
  51. def _is_numpy_instance(a):
  52. """
  53. Checks if an object is an instance of a type from the numpy module.
  54. """
  55. # This check avoids unnecessarily importing NumPy. We check the whole
  56. # __mro__ in case any base type is a numpy type.
  57. return any(type_.__module__ == 'numpy'
  58. for type_ in type(a).__mro__)
  59. def _convert_numpy_types(a, **sympify_args):
  60. """
  61. Converts a numpy datatype input to an appropriate SymPy type.
  62. """
  63. import numpy as np
  64. if not isinstance(a, np.floating):
  65. if np.iscomplex(a):
  66. return _sympy_converter[complex](a.item())
  67. else:
  68. return sympify(a.item(), **sympify_args)
  69. else:
  70. from .numbers import Float
  71. prec = np.finfo(a).nmant + 1
  72. # E.g. double precision means prec=53 but nmant=52
  73. # Leading bit of mantissa is always 1, so is not stored
  74. if np.isposinf(a):
  75. return Float('inf')
  76. elif np.isneginf(a):
  77. return Float('-inf')
  78. else:
  79. p, q = a.as_integer_ratio()
  80. a = mlib.from_rational(p, q, prec)
  81. return Float(a, precision=prec)
  82. @overload
  83. def sympify(a: int, *, strict: bool = False) -> Integer: ... # type: ignore
  84. @overload
  85. def sympify(a: float, *, strict: bool = False) -> Float: ...
  86. @overload
  87. def sympify(a: Expr | complex, *, strict: bool = False) -> Expr: ...
  88. @overload
  89. def sympify(a: Tbasic, *, strict: bool = False) -> Tbasic: ...
  90. @overload
  91. def sympify(a: Any, *, strict: bool = False) -> Basic: ...
  92. def sympify(a, locals=None, convert_xor=True, strict=False, rational=False,
  93. evaluate=None):
  94. """
  95. Converts an arbitrary expression to a type that can be used inside SymPy.
  96. Explanation
  97. ===========
  98. It will convert Python ints into instances of :class:`~.Integer`, floats
  99. into instances of :class:`~.Float`, etc. It is also able to coerce
  100. symbolic expressions which inherit from :class:`~.Basic`. This can be
  101. useful in cooperation with SAGE.
  102. .. warning::
  103. Note that this function uses ``eval``, and thus shouldn't be used on
  104. unsanitized input.
  105. If the argument is already a type that SymPy understands, it will do
  106. nothing but return that value. This can be used at the beginning of a
  107. function to ensure you are working with the correct type.
  108. Examples
  109. ========
  110. >>> from sympy import sympify
  111. >>> sympify(2).is_integer
  112. True
  113. >>> sympify(2).is_real
  114. True
  115. >>> sympify(2.0).is_real
  116. True
  117. >>> sympify("2.0").is_real
  118. True
  119. >>> sympify("2e-45").is_real
  120. True
  121. If the expression could not be converted, a SympifyError is raised.
  122. >>> sympify("x***2")
  123. Traceback (most recent call last):
  124. ...
  125. SympifyError: SympifyError: "could not parse 'x***2'"
  126. When attempting to parse non-Python syntax using ``sympify``, it raises a
  127. ``SympifyError``:
  128. >>> sympify("2x+1")
  129. Traceback (most recent call last):
  130. ...
  131. SympifyError: Sympify of expression 'could not parse '2x+1'' failed
  132. To parse non-Python syntax, use ``parse_expr`` from ``sympy.parsing.sympy_parser``.
  133. >>> from sympy.parsing.sympy_parser import parse_expr
  134. >>> parse_expr("2x+1", transformations="all")
  135. 2*x + 1
  136. For more details about ``transformations``: see :func:`~sympy.parsing.sympy_parser.parse_expr`
  137. Locals
  138. ------
  139. The sympification happens with access to everything that is loaded
  140. by ``from sympy import *``; anything used in a string that is not
  141. defined by that import will be converted to a symbol. In the following,
  142. the ``bitcount`` function is treated as a symbol and the ``O`` is
  143. interpreted as the :class:`~.Order` object (used with series) and it raises
  144. an error when used improperly:
  145. >>> s = 'bitcount(42)'
  146. >>> sympify(s)
  147. bitcount(42)
  148. >>> sympify("O(x)")
  149. O(x)
  150. >>> sympify("O + 1")
  151. Traceback (most recent call last):
  152. ...
  153. TypeError: unbound method...
  154. In order to have ``bitcount`` be recognized it can be imported into a
  155. namespace dictionary and passed as locals:
  156. >>> ns = {}
  157. >>> exec('from sympy.core.evalf import bitcount', ns)
  158. >>> sympify(s, locals=ns)
  159. 6
  160. In order to have the ``O`` interpreted as a Symbol, identify it as such
  161. in the namespace dictionary. This can be done in a variety of ways; all
  162. three of the following are possibilities:
  163. >>> from sympy import Symbol
  164. >>> ns["O"] = Symbol("O") # method 1
  165. >>> exec('from sympy.abc import O', ns) # method 2
  166. >>> ns.update(dict(O=Symbol("O"))) # method 3
  167. >>> sympify("O + 1", locals=ns)
  168. O + 1
  169. If you want *all* single-letter and Greek-letter variables to be symbols
  170. then you can use the clashing-symbols dictionaries that have been defined
  171. there as private variables: ``_clash1`` (single-letter variables),
  172. ``_clash2`` (the multi-letter Greek names) or ``_clash`` (both single and
  173. multi-letter names that are defined in ``abc``).
  174. >>> from sympy.abc import _clash1
  175. >>> set(_clash1) # if this fails, see issue #23903
  176. {'E', 'I', 'N', 'O', 'Q', 'S'}
  177. >>> sympify('I & Q', _clash1)
  178. I & Q
  179. Strict
  180. ------
  181. If the option ``strict`` is set to ``True``, only the types for which an
  182. explicit conversion has been defined are converted. In the other
  183. cases, a SympifyError is raised.
  184. >>> print(sympify(None))
  185. None
  186. >>> sympify(None, strict=True)
  187. Traceback (most recent call last):
  188. ...
  189. SympifyError: SympifyError: None
  190. .. deprecated:: 1.6
  191. ``sympify(obj)`` automatically falls back to ``str(obj)`` when all
  192. other conversion methods fail, but this is deprecated. ``strict=True``
  193. will disable this deprecated behavior. See
  194. :ref:`deprecated-sympify-string-fallback`.
  195. Evaluation
  196. ----------
  197. If the option ``evaluate`` is set to ``False``, then arithmetic and
  198. operators will be converted into their SymPy equivalents and the
  199. ``evaluate=False`` option will be added. Nested ``Add`` or ``Mul`` will
  200. be denested first. This is done via an AST transformation that replaces
  201. operators with their SymPy equivalents, so if an operand redefines any
  202. of those operations, the redefined operators will not be used. If
  203. argument a is not a string, the mathematical expression is evaluated
  204. before being passed to sympify, so adding ``evaluate=False`` will still
  205. return the evaluated result of expression.
  206. >>> sympify('2**2 / 3 + 5')
  207. 19/3
  208. >>> sympify('2**2 / 3 + 5', evaluate=False)
  209. 2**2/3 + 5
  210. >>> sympify('4/2+7', evaluate=True)
  211. 9
  212. >>> sympify('4/2+7', evaluate=False)
  213. 4/2 + 7
  214. >>> sympify(4/2+7, evaluate=False)
  215. 9.00000000000000
  216. Extending
  217. ---------
  218. To extend ``sympify`` to convert custom objects (not derived from ``Basic``),
  219. just define a ``_sympy_`` method to your class. You can do that even to
  220. classes that you do not own by subclassing or adding the method at runtime.
  221. >>> from sympy import Matrix
  222. >>> class MyList1(object):
  223. ... def __iter__(self):
  224. ... yield 1
  225. ... yield 2
  226. ... return
  227. ... def __getitem__(self, i): return list(self)[i]
  228. ... def _sympy_(self): return Matrix(self)
  229. >>> sympify(MyList1())
  230. Matrix([
  231. [1],
  232. [2]])
  233. If you do not have control over the class definition you could also use the
  234. ``converter`` global dictionary. The key is the class and the value is a
  235. function that takes a single argument and returns the desired SymPy
  236. object, e.g. ``converter[MyList] = lambda x: Matrix(x)``.
  237. >>> class MyList2(object): # XXX Do not do this if you control the class!
  238. ... def __iter__(self): # Use _sympy_!
  239. ... yield 1
  240. ... yield 2
  241. ... return
  242. ... def __getitem__(self, i): return list(self)[i]
  243. >>> from sympy.core.sympify import converter
  244. >>> converter[MyList2] = lambda x: Matrix(x)
  245. >>> sympify(MyList2())
  246. Matrix([
  247. [1],
  248. [2]])
  249. Notes
  250. =====
  251. The keywords ``rational`` and ``convert_xor`` are only used
  252. when the input is a string.
  253. convert_xor
  254. -----------
  255. >>> sympify('x^y',convert_xor=True)
  256. x**y
  257. >>> sympify('x^y',convert_xor=False)
  258. x ^ y
  259. rational
  260. --------
  261. >>> sympify('0.1',rational=False)
  262. 0.1
  263. >>> sympify('0.1',rational=True)
  264. 1/10
  265. Sometimes autosimplification during sympification results in expressions
  266. that are very different in structure than what was entered. Until such
  267. autosimplification is no longer done, the ``kernS`` function might be of
  268. some use. In the example below you can see how an expression reduces to
  269. $-1$ by autosimplification, but does not do so when ``kernS`` is used.
  270. >>> from sympy.core.sympify import kernS
  271. >>> from sympy.abc import x
  272. >>> -2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1
  273. -1
  274. >>> s = '-2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1'
  275. >>> sympify(s)
  276. -1
  277. >>> kernS(s)
  278. -2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1
  279. Parameters
  280. ==========
  281. a :
  282. - any object defined in SymPy
  283. - standard numeric Python types: ``int``, ``long``, ``float``, ``Decimal``
  284. - strings (like ``"0.09"``, ``"2e-19"`` or ``'sin(x)'``)
  285. - booleans, including ``None`` (will leave ``None`` unchanged)
  286. - dicts, lists, sets or tuples containing any of the above
  287. convert_xor : bool, optional
  288. If true, treats ``^`` as exponentiation.
  289. If False, treats ``^`` as XOR itself.
  290. Used only when input is a string.
  291. locals : any object defined in SymPy, optional
  292. In order to have strings be recognized it can be imported
  293. into a namespace dictionary and passed as locals.
  294. strict : bool, optional
  295. If the option strict is set to ``True``, only the types for which
  296. an explicit conversion has been defined are converted. In the
  297. other cases, a SympifyError is raised.
  298. rational : bool, optional
  299. If ``True``, converts floats into :class:`~.Rational`.
  300. If ``False``, it lets floats remain as it is.
  301. Used only when input is a string.
  302. evaluate : bool, optional
  303. If False, then arithmetic and operators will be converted into
  304. their SymPy equivalents. If True the expression will be evaluated
  305. and the result will be returned.
  306. """
  307. # XXX: If a is a Basic subclass rather than instance (e.g. sin rather than
  308. # sin(x)) then a.__sympy__ will be the property. Only on the instance will
  309. # a.__sympy__ give the *value* of the property (True). Since sympify(sin)
  310. # was used for a long time we allow it to pass. However if strict=True as
  311. # is the case in internal calls to _sympify then we only allow
  312. # is_sympy=True.
  313. #
  314. # https://github.com/sympy/sympy/issues/20124
  315. is_sympy = getattr(a, '__sympy__', None)
  316. if is_sympy is True:
  317. return a
  318. elif is_sympy is not None:
  319. if not strict:
  320. return a
  321. else:
  322. raise SympifyError(a)
  323. if isinstance(a, CantSympify):
  324. raise SympifyError(a)
  325. cls = getattr(a, "__class__", None)
  326. #Check if there exists a converter for any of the types in the mro
  327. for superclass in getmro(cls):
  328. #First check for user defined converters
  329. conv = _external_converter.get(superclass)
  330. if conv is None:
  331. #if none exists, check for SymPy defined converters
  332. conv = _sympy_converter.get(superclass)
  333. if conv is not None:
  334. return conv(a)
  335. if cls is type(None):
  336. if strict:
  337. raise SympifyError(a)
  338. else:
  339. return a
  340. if evaluate is None:
  341. evaluate = global_parameters.evaluate
  342. # Support for basic numpy datatypes
  343. if _is_numpy_instance(a):
  344. import numpy as np
  345. if np.isscalar(a):
  346. return _convert_numpy_types(a, locals=locals,
  347. convert_xor=convert_xor, strict=strict, rational=rational,
  348. evaluate=evaluate)
  349. _sympy_ = getattr(a, "_sympy_", None)
  350. if _sympy_ is not None:
  351. return a._sympy_()
  352. if not strict:
  353. # Put numpy array conversion _before_ float/int, see
  354. # <https://github.com/sympy/sympy/issues/13924>.
  355. flat = getattr(a, "flat", None)
  356. if flat is not None:
  357. shape = getattr(a, "shape", None)
  358. if shape is not None:
  359. from sympy.tensor.array import Array
  360. return Array(a.flat, a.shape) # works with e.g. NumPy arrays
  361. if not isinstance(a, str):
  362. if _is_numpy_instance(a):
  363. import numpy as np
  364. assert not isinstance(a, np.number)
  365. if isinstance(a, np.ndarray):
  366. # Scalar arrays (those with zero dimensions) have sympify
  367. # called on the scalar element.
  368. if a.ndim == 0:
  369. try:
  370. return sympify(a.item(),
  371. locals=locals,
  372. convert_xor=convert_xor,
  373. strict=strict,
  374. rational=rational,
  375. evaluate=evaluate)
  376. except SympifyError:
  377. pass
  378. elif hasattr(a, '__float__'):
  379. # float and int can coerce size-one numpy arrays to their lone
  380. # element. See issue https://github.com/numpy/numpy/issues/10404.
  381. return sympify(float(a))
  382. elif hasattr(a, '__int__'):
  383. return sympify(int(a))
  384. if strict:
  385. raise SympifyError(a)
  386. if iterable(a):
  387. try:
  388. return type(a)([sympify(x, locals=locals, convert_xor=convert_xor,
  389. rational=rational, evaluate=evaluate) for x in a])
  390. except TypeError:
  391. # Not all iterables are rebuildable with their type.
  392. pass
  393. if not isinstance(a, str):
  394. raise SympifyError('cannot sympify object of type %r' % type(a))
  395. from sympy.parsing.sympy_parser import (parse_expr, TokenError,
  396. standard_transformations)
  397. from sympy.parsing.sympy_parser import convert_xor as t_convert_xor
  398. from sympy.parsing.sympy_parser import rationalize as t_rationalize
  399. transformations = standard_transformations
  400. if rational:
  401. transformations += (t_rationalize,)
  402. if convert_xor:
  403. transformations += (t_convert_xor,)
  404. try:
  405. a = a.replace('\n', '')
  406. expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  407. except (TokenError, SyntaxError) as exc:
  408. raise SympifyError('could not parse %r' % a, exc)
  409. return expr
  410. def _sympify(a):
  411. """
  412. Short version of :func:`~.sympify` for internal usage for ``__add__`` and
  413. ``__eq__`` methods where it is ok to allow some things (like Python
  414. integers and floats) in the expression. This excludes things (like strings)
  415. that are unwise to allow into such an expression.
  416. >>> from sympy import Integer
  417. >>> Integer(1) == 1
  418. True
  419. >>> Integer(1) == '1'
  420. False
  421. >>> from sympy.abc import x
  422. >>> x + 1
  423. x + 1
  424. >>> x + '1'
  425. Traceback (most recent call last):
  426. ...
  427. TypeError: unsupported operand type(s) for +: 'Symbol' and 'str'
  428. see: sympify
  429. """
  430. return sympify(a, strict=True)
  431. def kernS(s):
  432. """Use a hack to try keep autosimplification from distributing a
  433. a number into an Add; this modification does not
  434. prevent the 2-arg Mul from becoming an Add, however.
  435. Examples
  436. ========
  437. >>> from sympy.core.sympify import kernS
  438. >>> from sympy.abc import x, y
  439. The 2-arg Mul distributes a number (or minus sign) across the terms
  440. of an expression, but kernS will prevent that:
  441. >>> 2*(x + y), -(x + 1)
  442. (2*x + 2*y, -x - 1)
  443. >>> kernS('2*(x + y)')
  444. 2*(x + y)
  445. >>> kernS('-(x + 1)')
  446. -(x + 1)
  447. If use of the hack fails, the un-hacked string will be passed to sympify...
  448. and you get what you get.
  449. XXX This hack should not be necessary once issue 4596 has been resolved.
  450. """
  451. hit = False
  452. quoted = '"' in s or "'" in s
  453. if '(' in s and not quoted:
  454. if s.count('(') != s.count(")"):
  455. raise SympifyError('unmatched left parenthesis')
  456. # strip all space from s
  457. s = ''.join(s.split())
  458. olds = s
  459. # now use space to represent a symbol that
  460. # will
  461. # step 1. turn potential 2-arg Muls into 3-arg versions
  462. # 1a. *( -> * *(
  463. s = s.replace('*(', '* *(')
  464. # 1b. close up exponentials
  465. s = s.replace('** *', '**')
  466. # 2. handle the implied multiplication of a negated
  467. # parenthesized expression in two steps
  468. # 2a: -(...) --> -( *(...)
  469. target = '-( *('
  470. s = s.replace('-(', target)
  471. # 2b: double the matching closing parenthesis
  472. # -( *(...) --> -( *(...))
  473. i = nest = 0
  474. assert target.endswith('(') # assumption below
  475. while True:
  476. j = s.find(target, i)
  477. if j == -1:
  478. break
  479. j += len(target) - 1
  480. for j in range(j, len(s)):
  481. if s[j] == "(":
  482. nest += 1
  483. elif s[j] == ")":
  484. nest -= 1
  485. if nest == 0:
  486. break
  487. s = s[:j] + ")" + s[j:]
  488. i = j + 2 # the first char after 2nd )
  489. if ' ' in s:
  490. # get a unique kern
  491. kern = '_'
  492. while kern in s:
  493. kern += choice(string.ascii_letters + string.digits)
  494. s = s.replace(' ', kern)
  495. hit = kern in s
  496. else:
  497. hit = False
  498. for i in range(2):
  499. try:
  500. expr = sympify(s)
  501. break
  502. except TypeError: # the kern might cause unknown errors...
  503. if hit:
  504. s = olds # maybe it didn't like the kern; use un-kerned s
  505. hit = False
  506. continue
  507. expr = sympify(s) # let original error raise
  508. if not hit:
  509. return expr
  510. from .symbol import Symbol
  511. rep = {Symbol(kern): 1}
  512. def _clear(expr):
  513. if isinstance(expr, (list, tuple, set)):
  514. return type(expr)([_clear(e) for e in expr])
  515. if hasattr(expr, 'subs'):
  516. return expr.subs(rep, hack2=True)
  517. return expr
  518. expr = _clear(expr)
  519. # hope that kern is not there anymore
  520. return expr
  521. # Avoid circular import
  522. from .basic import Basic