sympify.py 20 KB

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