sympy_parser.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. """Transform a string with Python-like source code into SymPy expression. """
  2. from __future__ import annotations
  3. from tokenize import (generate_tokens, untokenize, TokenError,
  4. NUMBER, STRING, NAME, OP, ENDMARKER, ERRORTOKEN, NEWLINE)
  5. from keyword import iskeyword
  6. import ast
  7. import unicodedata
  8. from io import StringIO
  9. import builtins
  10. import types
  11. from typing import Any, Callable
  12. from functools import reduce
  13. from sympy.assumptions.ask import AssumptionKeys
  14. from sympy.core.basic import Basic
  15. from sympy.core import Symbol
  16. from sympy.core.function import Function
  17. from sympy.utilities.misc import func_name
  18. from sympy.functions.elementary.miscellaneous import Max, Min
  19. null = ''
  20. TOKEN = tuple[int, str]
  21. DICT = dict[str, Any]
  22. TRANS = Callable[[list[TOKEN], DICT, DICT], list[TOKEN]]
  23. def _token_splittable(token_name: str) -> bool:
  24. """
  25. Predicate for whether a token name can be split into multiple tokens.
  26. A token is splittable if it does not contain an underscore character and
  27. it is not the name of a Greek letter. This is used to implicitly convert
  28. expressions like 'xyz' into 'x*y*z'.
  29. """
  30. if '_' in token_name:
  31. return False
  32. try:
  33. return not unicodedata.lookup('GREEK SMALL LETTER ' + token_name)
  34. except KeyError:
  35. return len(token_name) > 1
  36. def _token_callable(token: TOKEN, local_dict: DICT, global_dict: DICT, nextToken=None):
  37. """
  38. Predicate for whether a token name represents a callable function.
  39. Essentially wraps ``callable``, but looks up the token name in the
  40. locals and globals.
  41. """
  42. func = local_dict.get(token[1])
  43. if not func:
  44. func = global_dict.get(token[1])
  45. return callable(func) and not isinstance(func, Symbol)
  46. def _add_factorial_tokens(name: str, result: list[TOKEN]) -> list[TOKEN]:
  47. if result == [] or result[-1][1] == '(':
  48. raise TokenError()
  49. beginning = [(NAME, name), (OP, '(')]
  50. end = [(OP, ')')]
  51. diff = 0
  52. length = len(result)
  53. for index, token in enumerate(result[::-1]):
  54. toknum, tokval = token
  55. i = length - index - 1
  56. if tokval == ')':
  57. diff += 1
  58. elif tokval == '(':
  59. diff -= 1
  60. if diff == 0:
  61. if i - 1 >= 0 and result[i - 1][0] == NAME:
  62. return result[:i - 1] + beginning + result[i - 1:] + end
  63. else:
  64. return result[:i] + beginning + result[i:] + end
  65. return result
  66. class ParenthesisGroup(list[TOKEN]):
  67. """List of tokens representing an expression in parentheses."""
  68. pass
  69. class AppliedFunction:
  70. """
  71. A group of tokens representing a function and its arguments.
  72. `exponent` is for handling the shorthand sin^2, ln^2, etc.
  73. """
  74. def __init__(self, function: TOKEN, args: ParenthesisGroup, exponent=None):
  75. if exponent is None:
  76. exponent = []
  77. self.function = function
  78. self.args = args
  79. self.exponent = exponent
  80. self.items = ['function', 'args', 'exponent']
  81. def expand(self) -> list[TOKEN]:
  82. """Return a list of tokens representing the function"""
  83. return [self.function, *self.args]
  84. def __getitem__(self, index):
  85. return getattr(self, self.items[index])
  86. def __repr__(self):
  87. return "AppliedFunction(%s, %s, %s)" % (self.function, self.args,
  88. self.exponent)
  89. def _flatten(result: list[TOKEN | AppliedFunction]):
  90. result2: list[TOKEN] = []
  91. for tok in result:
  92. if isinstance(tok, AppliedFunction):
  93. result2.extend(tok.expand())
  94. else:
  95. result2.append(tok)
  96. return result2
  97. def _group_parentheses(recursor: TRANS):
  98. def _inner(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  99. """Group tokens between parentheses with ParenthesisGroup.
  100. Also processes those tokens recursively.
  101. """
  102. result: list[TOKEN | ParenthesisGroup] = []
  103. stacks: list[ParenthesisGroup] = []
  104. stacklevel = 0
  105. for token in tokens:
  106. if token[0] == OP:
  107. if token[1] == '(':
  108. stacks.append(ParenthesisGroup([]))
  109. stacklevel += 1
  110. elif token[1] == ')':
  111. stacks[-1].append(token)
  112. stack = stacks.pop()
  113. if len(stacks) > 0:
  114. # We don't recurse here since the upper-level stack
  115. # would reprocess these tokens
  116. stacks[-1].extend(stack)
  117. else:
  118. # Recurse here to handle nested parentheses
  119. # Strip off the outer parentheses to avoid an infinite loop
  120. inner = stack[1:-1]
  121. inner = recursor(inner,
  122. local_dict,
  123. global_dict)
  124. parenGroup = [stack[0]] + inner + [stack[-1]]
  125. result.append(ParenthesisGroup(parenGroup))
  126. stacklevel -= 1
  127. continue
  128. if stacklevel:
  129. stacks[-1].append(token)
  130. else:
  131. result.append(token)
  132. if stacklevel:
  133. raise TokenError("Mismatched parentheses")
  134. return result
  135. return _inner
  136. def _apply_functions(tokens: list[TOKEN | ParenthesisGroup], local_dict: DICT, global_dict: DICT):
  137. """Convert a NAME token + ParenthesisGroup into an AppliedFunction.
  138. Note that ParenthesisGroups, if not applied to any function, are
  139. converted back into lists of tokens.
  140. """
  141. result: list[TOKEN | AppliedFunction] = []
  142. symbol = None
  143. for tok in tokens:
  144. if isinstance(tok, ParenthesisGroup):
  145. if symbol and _token_callable(symbol, local_dict, global_dict):
  146. result[-1] = AppliedFunction(symbol, tok)
  147. symbol = None
  148. else:
  149. result.extend(tok)
  150. elif tok[0] == NAME:
  151. symbol = tok
  152. result.append(tok)
  153. else:
  154. symbol = None
  155. result.append(tok)
  156. return result
  157. def _implicit_multiplication(tokens: list[TOKEN | AppliedFunction], local_dict: DICT, global_dict: DICT):
  158. """Implicitly adds '*' tokens.
  159. Cases:
  160. - Two AppliedFunctions next to each other ("sin(x)cos(x)")
  161. - AppliedFunction next to an open parenthesis ("sin x (cos x + 1)")
  162. - A close parenthesis next to an AppliedFunction ("(x+2)sin x")\
  163. - A close parenthesis next to an open parenthesis ("(x+2)(x+3)")
  164. - AppliedFunction next to an implicitly applied function ("sin(x)cos x")
  165. """
  166. result: list[TOKEN | AppliedFunction] = []
  167. skip = False
  168. for tok, nextTok in zip(tokens, tokens[1:]):
  169. result.append(tok)
  170. if skip:
  171. skip = False
  172. continue
  173. if tok[0] == OP and tok[1] == '.' and nextTok[0] == NAME:
  174. # Dotted name. Do not do implicit multiplication
  175. skip = True
  176. continue
  177. if isinstance(tok, AppliedFunction):
  178. if isinstance(nextTok, AppliedFunction):
  179. result.append((OP, '*'))
  180. elif nextTok == (OP, '('):
  181. # Applied function followed by an open parenthesis
  182. if tok.function[1] == "Function":
  183. tok.function = (tok.function[0], 'Symbol')
  184. result.append((OP, '*'))
  185. elif nextTok[0] == NAME:
  186. # Applied function followed by implicitly applied function
  187. result.append((OP, '*'))
  188. else:
  189. if tok == (OP, ')'):
  190. if isinstance(nextTok, AppliedFunction):
  191. # Close parenthesis followed by an applied function
  192. result.append((OP, '*'))
  193. elif nextTok[0] == NAME:
  194. # Close parenthesis followed by an implicitly applied function
  195. result.append((OP, '*'))
  196. elif nextTok == (OP, '('):
  197. # Close parenthesis followed by an open parenthesis
  198. result.append((OP, '*'))
  199. elif tok[0] == NAME and not _token_callable(tok, local_dict, global_dict):
  200. if isinstance(nextTok, AppliedFunction) or \
  201. (nextTok[0] == NAME and _token_callable(nextTok, local_dict, global_dict)):
  202. # Constant followed by (implicitly applied) function
  203. result.append((OP, '*'))
  204. elif nextTok == (OP, '('):
  205. # Constant followed by parenthesis
  206. result.append((OP, '*'))
  207. elif nextTok[0] == NAME:
  208. # Constant followed by constant
  209. result.append((OP, '*'))
  210. if tokens:
  211. result.append(tokens[-1])
  212. return result
  213. def _implicit_application(tokens: list[TOKEN | AppliedFunction], local_dict: DICT, global_dict: DICT):
  214. """Adds parentheses as needed after functions."""
  215. result: list[TOKEN | AppliedFunction] = []
  216. appendParen = 0 # number of closing parentheses to add
  217. skip = 0 # number of tokens to delay before adding a ')' (to
  218. # capture **, ^, etc.)
  219. exponentSkip = False # skipping tokens before inserting parentheses to
  220. # work with function exponentiation
  221. for tok, nextTok in zip(tokens, tokens[1:]):
  222. result.append(tok)
  223. if (tok[0] == NAME and nextTok[0] not in [OP, ENDMARKER, NEWLINE]):
  224. if _token_callable(tok, local_dict, global_dict, nextTok): # type: ignore
  225. result.append((OP, '('))
  226. appendParen += 1
  227. # name followed by exponent - function exponentiation
  228. elif (tok[0] == NAME and nextTok[0] == OP and nextTok[1] == '**'):
  229. if _token_callable(tok, local_dict, global_dict): # type: ignore
  230. exponentSkip = True
  231. elif exponentSkip:
  232. # if the last token added was an applied function (i.e. the
  233. # power of the function exponent) OR a multiplication (as
  234. # implicit multiplication would have added an extraneous
  235. # multiplication)
  236. if (isinstance(tok, AppliedFunction)
  237. or (tok[0] == OP and tok[1] == '*')):
  238. # don't add anything if the next token is a multiplication
  239. # or if there's already a parenthesis (if parenthesis, still
  240. # stop skipping tokens)
  241. if not (nextTok[0] == OP and nextTok[1] == '*'):
  242. if not(nextTok[0] == OP and nextTok[1] == '('):
  243. result.append((OP, '('))
  244. appendParen += 1
  245. exponentSkip = False
  246. elif appendParen:
  247. if nextTok[0] == OP and nextTok[1] in ('^', '**', '*'):
  248. skip = 1
  249. continue
  250. if skip:
  251. skip -= 1
  252. continue
  253. result.append((OP, ')'))
  254. appendParen -= 1
  255. if tokens:
  256. result.append(tokens[-1])
  257. if appendParen:
  258. result.extend([(OP, ')')] * appendParen)
  259. return result
  260. def function_exponentiation(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  261. """Allows functions to be exponentiated, e.g. ``cos**2(x)``.
  262. Examples
  263. ========
  264. >>> from sympy.parsing.sympy_parser import (parse_expr,
  265. ... standard_transformations, function_exponentiation)
  266. >>> transformations = standard_transformations + (function_exponentiation,)
  267. >>> parse_expr('sin**4(x)', transformations=transformations)
  268. sin(x)**4
  269. """
  270. result: list[TOKEN] = []
  271. exponent: list[TOKEN] = []
  272. consuming_exponent = False
  273. level = 0
  274. for tok, nextTok in zip(tokens, tokens[1:]):
  275. if tok[0] == NAME and nextTok[0] == OP and nextTok[1] == '**':
  276. if _token_callable(tok, local_dict, global_dict):
  277. consuming_exponent = True
  278. elif consuming_exponent:
  279. if tok[0] == NAME and tok[1] == 'Function':
  280. tok = (NAME, 'Symbol')
  281. exponent.append(tok)
  282. # only want to stop after hitting )
  283. if tok[0] == nextTok[0] == OP and tok[1] == ')' and nextTok[1] == '(':
  284. consuming_exponent = False
  285. # if implicit multiplication was used, we may have )*( instead
  286. if tok[0] == nextTok[0] == OP and tok[1] == '*' and nextTok[1] == '(':
  287. consuming_exponent = False
  288. del exponent[-1]
  289. continue
  290. elif exponent and not consuming_exponent:
  291. if tok[0] == OP:
  292. if tok[1] == '(':
  293. level += 1
  294. elif tok[1] == ')':
  295. level -= 1
  296. if level == 0:
  297. result.append(tok)
  298. result.extend(exponent)
  299. exponent = []
  300. continue
  301. result.append(tok)
  302. if tokens:
  303. result.append(tokens[-1])
  304. if exponent:
  305. result.extend(exponent)
  306. return result
  307. def split_symbols_custom(predicate: Callable[[str], bool]):
  308. """Creates a transformation that splits symbol names.
  309. ``predicate`` should return True if the symbol name is to be split.
  310. For instance, to retain the default behavior but avoid splitting certain
  311. symbol names, a predicate like this would work:
  312. >>> from sympy.parsing.sympy_parser import (parse_expr, _token_splittable,
  313. ... standard_transformations, implicit_multiplication,
  314. ... split_symbols_custom)
  315. >>> def can_split(symbol):
  316. ... if symbol not in ('list', 'of', 'unsplittable', 'names'):
  317. ... return _token_splittable(symbol)
  318. ... return False
  319. ...
  320. >>> transformation = split_symbols_custom(can_split)
  321. >>> parse_expr('unsplittable', transformations=standard_transformations +
  322. ... (transformation, implicit_multiplication))
  323. unsplittable
  324. """
  325. def _split_symbols(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  326. result: list[TOKEN] = []
  327. split = False
  328. split_previous=False
  329. for tok in tokens:
  330. if split_previous:
  331. # throw out closing parenthesis of Symbol that was split
  332. split_previous=False
  333. continue
  334. split_previous=False
  335. if tok[0] == NAME and tok[1] in ['Symbol', 'Function']:
  336. split = True
  337. elif split and tok[0] == NAME:
  338. symbol = tok[1][1:-1]
  339. if predicate(symbol):
  340. tok_type = result[-2][1] # Symbol or Function
  341. del result[-2:] # Get rid of the call to Symbol
  342. i = 0
  343. while i < len(symbol):
  344. char = symbol[i]
  345. if char in local_dict or char in global_dict:
  346. result.append((NAME, "%s" % char))
  347. elif char.isdigit():
  348. chars = [char]
  349. for i in range(i + 1, len(symbol)):
  350. if not symbol[i].isdigit():
  351. i -= 1
  352. break
  353. chars.append(symbol[i])
  354. char = ''.join(chars)
  355. result.extend([(NAME, 'Number'), (OP, '('),
  356. (NAME, "'%s'" % char), (OP, ')')])
  357. else:
  358. use = tok_type if i == len(symbol) else 'Symbol'
  359. result.extend([(NAME, use), (OP, '('),
  360. (NAME, "'%s'" % char), (OP, ')')])
  361. i += 1
  362. # Set split_previous=True so will skip
  363. # the closing parenthesis of the original Symbol
  364. split = False
  365. split_previous = True
  366. continue
  367. else:
  368. split = False
  369. result.append(tok)
  370. return result
  371. return _split_symbols
  372. #: Splits symbol names for implicit multiplication.
  373. #:
  374. #: Intended to let expressions like ``xyz`` be parsed as ``x*y*z``. Does not
  375. #: split Greek character names, so ``theta`` will *not* become
  376. #: ``t*h*e*t*a``. Generally this should be used with
  377. #: ``implicit_multiplication``.
  378. split_symbols = split_symbols_custom(_token_splittable)
  379. def implicit_multiplication(tokens: list[TOKEN], local_dict: DICT,
  380. global_dict: DICT) -> list[TOKEN]:
  381. """Makes the multiplication operator optional in most cases.
  382. Use this before :func:`implicit_application`, otherwise expressions like
  383. ``sin 2x`` will be parsed as ``x * sin(2)`` rather than ``sin(2*x)``.
  384. Examples
  385. ========
  386. >>> from sympy.parsing.sympy_parser import (parse_expr,
  387. ... standard_transformations, implicit_multiplication)
  388. >>> transformations = standard_transformations + (implicit_multiplication,)
  389. >>> parse_expr('3 x y', transformations=transformations)
  390. 3*x*y
  391. """
  392. # These are interdependent steps, so we don't expose them separately
  393. res1 = _group_parentheses(implicit_multiplication)(tokens, local_dict, global_dict)
  394. res2 = _apply_functions(res1, local_dict, global_dict)
  395. res3 = _implicit_multiplication(res2, local_dict, global_dict)
  396. result = _flatten(res3)
  397. return result
  398. def implicit_application(tokens: list[TOKEN], local_dict: DICT,
  399. global_dict: DICT) -> list[TOKEN]:
  400. """Makes parentheses optional in some cases for function calls.
  401. Use this after :func:`implicit_multiplication`, otherwise expressions
  402. like ``sin 2x`` will be parsed as ``x * sin(2)`` rather than
  403. ``sin(2*x)``.
  404. Examples
  405. ========
  406. >>> from sympy.parsing.sympy_parser import (parse_expr,
  407. ... standard_transformations, implicit_application)
  408. >>> transformations = standard_transformations + (implicit_application,)
  409. >>> parse_expr('cot z + csc z', transformations=transformations)
  410. cot(z) + csc(z)
  411. """
  412. res1 = _group_parentheses(implicit_application)(tokens, local_dict, global_dict)
  413. res2 = _apply_functions(res1, local_dict, global_dict)
  414. res3 = _implicit_application(res2, local_dict, global_dict)
  415. result = _flatten(res3)
  416. return result
  417. def implicit_multiplication_application(result: list[TOKEN], local_dict: DICT,
  418. global_dict: DICT) -> list[TOKEN]:
  419. """Allows a slightly relaxed syntax.
  420. - Parentheses for single-argument method calls are optional.
  421. - Multiplication is implicit.
  422. - Symbol names can be split (i.e. spaces are not needed between
  423. symbols).
  424. - Functions can be exponentiated.
  425. Examples
  426. ========
  427. >>> from sympy.parsing.sympy_parser import (parse_expr,
  428. ... standard_transformations, implicit_multiplication_application)
  429. >>> parse_expr("10sin**2 x**2 + 3xyz + tan theta",
  430. ... transformations=(standard_transformations +
  431. ... (implicit_multiplication_application,)))
  432. 3*x*y*z + 10*sin(x**2)**2 + tan(theta)
  433. """
  434. for step in (split_symbols, implicit_multiplication,
  435. implicit_application, function_exponentiation):
  436. result = step(result, local_dict, global_dict)
  437. return result
  438. def auto_symbol(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  439. """Inserts calls to ``Symbol``/``Function`` for undefined variables."""
  440. result: list[TOKEN] = []
  441. prevTok = (-1, '')
  442. tokens.append((-1, '')) # so zip traverses all tokens
  443. for tok, nextTok in zip(tokens, tokens[1:]):
  444. tokNum, tokVal = tok
  445. nextTokNum, nextTokVal = nextTok
  446. if tokNum == NAME:
  447. name = tokVal
  448. if (name in ['True', 'False', 'None']
  449. or iskeyword(name)
  450. # Don't convert attribute access
  451. or (prevTok[0] == OP and prevTok[1] == '.')
  452. # Don't convert keyword arguments
  453. or (prevTok[0] == OP and prevTok[1] in ('(', ',')
  454. and nextTokNum == OP and nextTokVal == '=')
  455. # the name has already been defined
  456. or name in local_dict and local_dict[name] is not null):
  457. result.append((NAME, name))
  458. continue
  459. elif name in local_dict:
  460. local_dict.setdefault(null, set()).add(name)
  461. if nextTokVal == '(':
  462. local_dict[name] = Function(name)
  463. else:
  464. local_dict[name] = Symbol(name)
  465. result.append((NAME, name))
  466. continue
  467. elif name in global_dict:
  468. obj = global_dict[name]
  469. if isinstance(obj, (AssumptionKeys, Basic, type)) or callable(obj):
  470. result.append((NAME, name))
  471. continue
  472. result.extend([
  473. (NAME, 'Symbol' if nextTokVal != '(' else 'Function'),
  474. (OP, '('),
  475. (NAME, repr(str(name))),
  476. (OP, ')'),
  477. ])
  478. else:
  479. result.append((tokNum, tokVal))
  480. prevTok = (tokNum, tokVal)
  481. return result
  482. def lambda_notation(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  483. """Substitutes "lambda" with its SymPy equivalent Lambda().
  484. However, the conversion does not take place if only "lambda"
  485. is passed because that is a syntax error.
  486. """
  487. result: list[TOKEN] = []
  488. flag = False
  489. toknum, tokval = tokens[0]
  490. tokLen = len(tokens)
  491. if toknum == NAME and tokval == 'lambda':
  492. if tokLen == 2 or tokLen == 3 and tokens[1][0] == NEWLINE:
  493. # In Python 3.6.7+, inputs without a newline get NEWLINE added to
  494. # the tokens
  495. result.extend(tokens)
  496. elif tokLen > 2:
  497. result.extend([
  498. (NAME, 'Lambda'),
  499. (OP, '('),
  500. (OP, '('),
  501. (OP, ')'),
  502. (OP, ')'),
  503. ])
  504. for tokNum, tokVal in tokens[1:]:
  505. if tokNum == OP and tokVal == ':':
  506. tokVal = ','
  507. flag = True
  508. if not flag and tokNum == OP and tokVal in ('*', '**'):
  509. raise TokenError("Starred arguments in lambda not supported")
  510. if flag:
  511. result.insert(-1, (tokNum, tokVal))
  512. else:
  513. result.insert(-2, (tokNum, tokVal))
  514. else:
  515. result.extend(tokens)
  516. return result
  517. def factorial_notation(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  518. """Allows standard notation for factorial."""
  519. result: list[TOKEN] = []
  520. nfactorial = 0
  521. for toknum, tokval in tokens:
  522. if toknum == OP and tokval == "!":
  523. # In Python 3.12 "!" are OP instead of ERRORTOKEN
  524. nfactorial += 1
  525. elif toknum == ERRORTOKEN:
  526. op = tokval
  527. if op == '!':
  528. nfactorial += 1
  529. else:
  530. nfactorial = 0
  531. result.append((OP, op))
  532. else:
  533. if nfactorial == 1:
  534. result = _add_factorial_tokens('factorial', result)
  535. elif nfactorial == 2:
  536. result = _add_factorial_tokens('factorial2', result)
  537. elif nfactorial > 2:
  538. raise TokenError
  539. nfactorial = 0
  540. result.append((toknum, tokval))
  541. return result
  542. def convert_xor(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  543. """Treats XOR, ``^``, as exponentiation, ``**``."""
  544. result: list[TOKEN] = []
  545. for toknum, tokval in tokens:
  546. if toknum == OP:
  547. if tokval == '^':
  548. result.append((OP, '**'))
  549. else:
  550. result.append((toknum, tokval))
  551. else:
  552. result.append((toknum, tokval))
  553. return result
  554. def repeated_decimals(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  555. """
  556. Allows 0.2[1] notation to represent the repeated decimal 0.2111... (19/90)
  557. Run this before auto_number.
  558. """
  559. result: list[TOKEN] = []
  560. def is_digit(s):
  561. return all(i in '0123456789_' for i in s)
  562. # num will running match any DECIMAL [ INTEGER ]
  563. num: list[TOKEN] = []
  564. for toknum, tokval in tokens:
  565. if toknum == NUMBER:
  566. if (not num and '.' in tokval and 'e' not in tokval.lower() and
  567. 'j' not in tokval.lower()):
  568. num.append((toknum, tokval))
  569. elif is_digit(tokval) and (len(num) == 2 or
  570. len(num) == 3 and is_digit(num[-1][1])):
  571. num.append((toknum, tokval))
  572. else:
  573. num = []
  574. elif toknum == OP:
  575. if tokval == '[' and len(num) == 1:
  576. num.append((OP, tokval))
  577. elif tokval == ']' and len(num) >= 3:
  578. num.append((OP, tokval))
  579. elif tokval == '.' and not num:
  580. # handle .[1]
  581. num.append((NUMBER, '0.'))
  582. else:
  583. num = []
  584. else:
  585. num = []
  586. result.append((toknum, tokval))
  587. if num and num[-1][1] == ']':
  588. # pre.post[repetend] = a + b/c + d/e where a = pre, b/c = post,
  589. # and d/e = repetend
  590. result = result[:-len(num)]
  591. pre, post = num[0][1].split('.')
  592. repetend = num[2][1]
  593. if len(num) == 5:
  594. repetend += num[3][1]
  595. pre = pre.replace('_', '')
  596. post = post.replace('_', '')
  597. repetend = repetend.replace('_', '')
  598. zeros = '0'*len(post)
  599. post, repetends = [w.lstrip('0') for w in [post, repetend]]
  600. # or else interpreted as octal
  601. a = pre or '0'
  602. b, c = post or '0', '1' + zeros
  603. d, e = repetends, ('9'*len(repetend)) + zeros
  604. seq = [
  605. (OP, '('),
  606. (NAME, 'Integer'),
  607. (OP, '('),
  608. (NUMBER, a),
  609. (OP, ')'),
  610. (OP, '+'),
  611. (NAME, 'Rational'),
  612. (OP, '('),
  613. (NUMBER, b),
  614. (OP, ','),
  615. (NUMBER, c),
  616. (OP, ')'),
  617. (OP, '+'),
  618. (NAME, 'Rational'),
  619. (OP, '('),
  620. (NUMBER, d),
  621. (OP, ','),
  622. (NUMBER, e),
  623. (OP, ')'),
  624. (OP, ')'),
  625. ]
  626. result.extend(seq)
  627. num = []
  628. return result
  629. def auto_number(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  630. """
  631. Converts numeric literals to use SymPy equivalents.
  632. Complex numbers use ``I``, integer literals use ``Integer``, and float
  633. literals use ``Float``.
  634. """
  635. result: list[TOKEN] = []
  636. for toknum, tokval in tokens:
  637. if toknum == NUMBER:
  638. number = tokval
  639. postfix = []
  640. if number.endswith(('j', 'J')):
  641. number = number[:-1]
  642. postfix = [(OP, '*'), (NAME, 'I')]
  643. if '.' in number or (('e' in number or 'E' in number) and
  644. not (number.startswith(('0x', '0X')))):
  645. seq = [(NAME, 'Float'), (OP, '('),
  646. (NUMBER, repr(str(number))), (OP, ')')]
  647. else:
  648. seq = [(NAME, 'Integer'), (OP, '('), (
  649. NUMBER, number), (OP, ')')]
  650. result.extend(seq + postfix)
  651. else:
  652. result.append((toknum, tokval))
  653. return result
  654. def rationalize(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  655. """Converts floats into ``Rational``. Run AFTER ``auto_number``."""
  656. result: list[TOKEN] = []
  657. passed_float = False
  658. for toknum, tokval in tokens:
  659. if toknum == NAME:
  660. if tokval == 'Float':
  661. passed_float = True
  662. tokval = 'Rational'
  663. result.append((toknum, tokval))
  664. elif passed_float == True and toknum == NUMBER:
  665. passed_float = False
  666. result.append((STRING, tokval))
  667. else:
  668. result.append((toknum, tokval))
  669. return result
  670. def _transform_equals_sign(tokens: list[TOKEN], local_dict: DICT, global_dict: DICT):
  671. """Transforms the equals sign ``=`` to instances of Eq.
  672. This is a helper function for ``convert_equals_signs``.
  673. Works with expressions containing one equals sign and no
  674. nesting. Expressions like ``(1=2)=False`` will not work with this
  675. and should be used with ``convert_equals_signs``.
  676. Examples: 1=2 to Eq(1,2)
  677. 1*2=x to Eq(1*2, x)
  678. This does not deal with function arguments yet.
  679. """
  680. result: list[TOKEN] = []
  681. if (OP, "=") in tokens:
  682. result.append((NAME, "Eq"))
  683. result.append((OP, "("))
  684. for token in tokens:
  685. if token == (OP, "="):
  686. result.append((OP, ","))
  687. continue
  688. result.append(token)
  689. result.append((OP, ")"))
  690. else:
  691. result = tokens
  692. return result
  693. def convert_equals_signs(tokens: list[TOKEN], local_dict: DICT,
  694. global_dict: DICT) -> list[TOKEN]:
  695. """ Transforms all the equals signs ``=`` to instances of Eq.
  696. Parses the equals signs in the expression and replaces them with
  697. appropriate Eq instances. Also works with nested equals signs.
  698. Does not yet play well with function arguments.
  699. For example, the expression ``(x=y)`` is ambiguous and can be interpreted
  700. as x being an argument to a function and ``convert_equals_signs`` will not
  701. work for this.
  702. See also
  703. ========
  704. convert_equality_operators
  705. Examples
  706. ========
  707. >>> from sympy.parsing.sympy_parser import (parse_expr,
  708. ... standard_transformations, convert_equals_signs)
  709. >>> parse_expr("1*2=x", transformations=(
  710. ... standard_transformations + (convert_equals_signs,)))
  711. Eq(2, x)
  712. >>> parse_expr("(1*2=x)=False", transformations=(
  713. ... standard_transformations + (convert_equals_signs,)))
  714. Eq(Eq(2, x), False)
  715. """
  716. res1 = _group_parentheses(convert_equals_signs)(tokens, local_dict, global_dict)
  717. res2 = _apply_functions(res1, local_dict, global_dict)
  718. res3 = _transform_equals_sign(res2, local_dict, global_dict)
  719. result = _flatten(res3)
  720. return result
  721. #: Standard transformations for :func:`parse_expr`.
  722. #: Inserts calls to :class:`~.Symbol`, :class:`~.Integer`, and other SymPy
  723. #: datatypes and allows the use of standard factorial notation (e.g. ``x!``).
  724. standard_transformations: tuple[TRANS, ...] \
  725. = (lambda_notation, auto_symbol, repeated_decimals, auto_number,
  726. factorial_notation)
  727. def stringify_expr(s: str, local_dict: DICT, global_dict: DICT,
  728. transformations: tuple[TRANS, ...]) -> str:
  729. """
  730. Converts the string ``s`` to Python code, in ``local_dict``
  731. Generally, ``parse_expr`` should be used.
  732. """
  733. tokens = []
  734. input_code = StringIO(s.strip())
  735. for toknum, tokval, _, _, _ in generate_tokens(input_code.readline):
  736. tokens.append((toknum, tokval))
  737. for transform in transformations:
  738. tokens = transform(tokens, local_dict, global_dict)
  739. return untokenize(tokens)
  740. def eval_expr(code, local_dict: DICT, global_dict: DICT):
  741. """
  742. Evaluate Python code generated by ``stringify_expr``.
  743. Generally, ``parse_expr`` should be used.
  744. """
  745. expr = eval(
  746. code, global_dict, local_dict) # take local objects in preference
  747. return expr
  748. def parse_expr(s: str, local_dict: DICT | None = None,
  749. transformations: tuple[TRANS, ...] | str \
  750. = standard_transformations,
  751. global_dict: DICT | None = None, evaluate=True):
  752. """Converts the string ``s`` to a SymPy expression, in ``local_dict``.
  753. .. warning::
  754. Note that this function uses ``eval``, and thus shouldn't be used on
  755. unsanitized input.
  756. Parameters
  757. ==========
  758. s : str
  759. The string to parse.
  760. local_dict : dict, optional
  761. A dictionary of local variables to use when parsing.
  762. global_dict : dict, optional
  763. A dictionary of global variables. By default, this is initialized
  764. with ``from sympy import *``; provide this parameter to override
  765. this behavior (for instance, to parse ``"Q & S"``).
  766. transformations : tuple or str
  767. A tuple of transformation functions used to modify the tokens of the
  768. parsed expression before evaluation. The default transformations
  769. convert numeric literals into their SymPy equivalents, convert
  770. undefined variables into SymPy symbols, and allow the use of standard
  771. mathematical factorial notation (e.g. ``x!``). Selection via
  772. string is available (see below).
  773. evaluate : bool, optional
  774. When False, the order of the arguments will remain as they were in the
  775. string and automatic simplification that would normally occur is
  776. suppressed. (see examples)
  777. Examples
  778. ========
  779. >>> from sympy.parsing.sympy_parser import parse_expr
  780. >>> parse_expr("1/2")
  781. 1/2
  782. >>> type(_)
  783. <class 'sympy.core.numbers.Half'>
  784. >>> from sympy.parsing.sympy_parser import standard_transformations,\\
  785. ... implicit_multiplication_application
  786. >>> transformations = (standard_transformations +
  787. ... (implicit_multiplication_application,))
  788. >>> parse_expr("2x", transformations=transformations)
  789. 2*x
  790. When evaluate=False, some automatic simplifications will not occur:
  791. >>> parse_expr("2**3"), parse_expr("2**3", evaluate=False)
  792. (8, 2**3)
  793. In addition the order of the arguments will not be made canonical.
  794. This feature allows one to tell exactly how the expression was entered:
  795. >>> a = parse_expr('1 + x', evaluate=False)
  796. >>> b = parse_expr('x + 1', evaluate=False)
  797. >>> a == b
  798. False
  799. >>> a.args
  800. (1, x)
  801. >>> b.args
  802. (x, 1)
  803. Note, however, that when these expressions are printed they will
  804. appear the same:
  805. >>> assert str(a) == str(b)
  806. As a convenience, transformations can be seen by printing ``transformations``:
  807. >>> from sympy.parsing.sympy_parser import transformations
  808. >>> print(transformations)
  809. 0: lambda_notation
  810. 1: auto_symbol
  811. 2: repeated_decimals
  812. 3: auto_number
  813. 4: factorial_notation
  814. 5: implicit_multiplication_application
  815. 6: convert_xor
  816. 7: implicit_application
  817. 8: implicit_multiplication
  818. 9: convert_equals_signs
  819. 10: function_exponentiation
  820. 11: rationalize
  821. The ``T`` object provides a way to select these transformations:
  822. >>> from sympy.parsing.sympy_parser import T
  823. If you print it, you will see the same list as shown above.
  824. >>> str(T) == str(transformations)
  825. True
  826. Standard slicing will return a tuple of transformations:
  827. >>> T[:5] == standard_transformations
  828. True
  829. So ``T`` can be used to specify the parsing transformations:
  830. >>> parse_expr("2x", transformations=T[:5])
  831. Traceback (most recent call last):
  832. ...
  833. SyntaxError: invalid syntax
  834. >>> parse_expr("2x", transformations=T[:6])
  835. 2*x
  836. >>> parse_expr('.3', transformations=T[3, 11])
  837. 3/10
  838. >>> parse_expr('.3x', transformations=T[:])
  839. 3*x/10
  840. As a further convenience, strings 'implicit' and 'all' can be used
  841. to select 0-5 and all the transformations, respectively.
  842. >>> parse_expr('.3x', transformations='all')
  843. 3*x/10
  844. See Also
  845. ========
  846. stringify_expr, eval_expr, standard_transformations,
  847. implicit_multiplication_application
  848. """
  849. if local_dict is None:
  850. local_dict = {}
  851. elif not isinstance(local_dict, dict):
  852. raise TypeError('expecting local_dict to be a dict')
  853. elif null in local_dict:
  854. raise ValueError('cannot use "" in local_dict')
  855. if global_dict is None:
  856. global_dict = {}
  857. exec('from sympy import *', global_dict)
  858. builtins_dict = vars(builtins)
  859. for name, obj in builtins_dict.items():
  860. if isinstance(obj, types.BuiltinFunctionType):
  861. global_dict[name] = obj
  862. global_dict['max'] = Max
  863. global_dict['min'] = Min
  864. elif not isinstance(global_dict, dict):
  865. raise TypeError('expecting global_dict to be a dict')
  866. transformations = transformations or ()
  867. if isinstance(transformations, str):
  868. if transformations == 'all':
  869. _transformations = T[:]
  870. elif transformations == 'implicit':
  871. _transformations = T[:6]
  872. else:
  873. raise ValueError('unknown transformation group name')
  874. else:
  875. _transformations = transformations
  876. code = stringify_expr(s, local_dict, global_dict, _transformations)
  877. if not evaluate:
  878. code = compile(evaluateFalse(code), '<string>', 'eval') # type: ignore
  879. try:
  880. rv = eval_expr(code, local_dict, global_dict)
  881. # restore neutral definitions for names
  882. for i in local_dict.pop(null, ()):
  883. local_dict[i] = null
  884. return rv
  885. except Exception as e:
  886. # restore neutral definitions for names
  887. for i in local_dict.pop(null, ()):
  888. local_dict[i] = null
  889. raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  890. def evaluateFalse(s: str):
  891. """
  892. Replaces operators with the SymPy equivalent and sets evaluate=False.
  893. """
  894. node = ast.parse(s)
  895. transformed_node = EvaluateFalseTransformer().visit(node)
  896. # node is a Module, we want an Expression
  897. transformed_node = ast.Expression(transformed_node.body[0].value)
  898. return ast.fix_missing_locations(transformed_node)
  899. class EvaluateFalseTransformer(ast.NodeTransformer):
  900. operators = {
  901. ast.Add: 'Add',
  902. ast.Mult: 'Mul',
  903. ast.Pow: 'Pow',
  904. ast.Sub: 'Add',
  905. ast.Div: 'Mul',
  906. ast.BitOr: 'Or',
  907. ast.BitAnd: 'And',
  908. ast.BitXor: 'Not',
  909. }
  910. functions = (
  911. 'Abs', 'im', 're', 'sign', 'arg', 'conjugate',
  912. 'acos', 'acot', 'acsc', 'asec', 'asin', 'atan',
  913. 'acosh', 'acoth', 'acsch', 'asech', 'asinh', 'atanh',
  914. 'cos', 'cot', 'csc', 'sec', 'sin', 'tan',
  915. 'cosh', 'coth', 'csch', 'sech', 'sinh', 'tanh',
  916. 'exp', 'ln', 'log', 'sqrt', 'cbrt',
  917. )
  918. relational_operators = {
  919. ast.NotEq: 'Ne',
  920. ast.Lt: 'Lt',
  921. ast.LtE: 'Le',
  922. ast.Gt: 'Gt',
  923. ast.GtE: 'Ge',
  924. ast.Eq: 'Eq'
  925. }
  926. def visit_Compare(self, node):
  927. def reducer(acc, op_right):
  928. result, left = acc
  929. op, right = op_right
  930. if op.__class__ not in self.relational_operators:
  931. raise ValueError("Only equation or inequality operators are supported")
  932. new = ast.Call(
  933. func=ast.Name(
  934. id=self.relational_operators[op.__class__], ctx=ast.Load()
  935. ),
  936. args=[self.visit(left), self.visit(right)],
  937. keywords=[ast.keyword(arg="evaluate", value=ast.Constant(value=False))],
  938. )
  939. return result + [new], right
  940. args, _ = reduce(
  941. reducer, zip(node.ops, node.comparators), ([], node.left)
  942. )
  943. if len(args) == 1:
  944. return args[0]
  945. return ast.Call(
  946. func=ast.Name(id=self.operators[ast.BitAnd], ctx=ast.Load()),
  947. args=args,
  948. keywords=[ast.keyword(arg="evaluate", value=ast.Constant(value=False))],
  949. )
  950. def flatten(self, args, func):
  951. result = []
  952. for arg in args:
  953. if isinstance(arg, ast.Call):
  954. arg_func = arg.func
  955. if isinstance(arg_func, ast.Call):
  956. arg_func = arg_func.func
  957. if arg_func.id == func:
  958. result.extend(self.flatten(arg.args, func))
  959. else:
  960. result.append(arg)
  961. else:
  962. result.append(arg)
  963. return result
  964. def visit_BinOp(self, node):
  965. if node.op.__class__ in self.operators:
  966. sympy_class = self.operators[node.op.__class__]
  967. right = self.visit(node.right)
  968. left = self.visit(node.left)
  969. rev = False
  970. if isinstance(node.op, ast.Sub):
  971. right = ast.Call(
  972. func=ast.Name(id='Mul', ctx=ast.Load()),
  973. args=[ast.UnaryOp(op=ast.USub(), operand=ast.Constant(1)), right],
  974. keywords=[ast.keyword(arg='evaluate', value=ast.Constant(value=False))]
  975. )
  976. elif isinstance(node.op, ast.Div):
  977. if isinstance(node.left, ast.UnaryOp):
  978. left, right = right, left
  979. rev = True
  980. left = ast.Call(
  981. func=ast.Name(id='Pow', ctx=ast.Load()),
  982. args=[left, ast.UnaryOp(op=ast.USub(), operand=ast.Constant(1))],
  983. keywords=[ast.keyword(arg='evaluate', value=ast.Constant(value=False))]
  984. )
  985. else:
  986. right = ast.Call(
  987. func=ast.Name(id='Pow', ctx=ast.Load()),
  988. args=[right, ast.UnaryOp(op=ast.USub(), operand=ast.Constant(1))],
  989. keywords=[ast.keyword(arg='evaluate', value=ast.Constant(value=False))]
  990. )
  991. if rev: # undo reversal
  992. left, right = right, left
  993. new_node = ast.Call(
  994. func=ast.Name(id=sympy_class, ctx=ast.Load()),
  995. args=[left, right],
  996. keywords=[ast.keyword(arg='evaluate', value=ast.Constant(value=False))]
  997. )
  998. if sympy_class in ('Add', 'Mul'):
  999. # Denest Add or Mul as appropriate
  1000. new_node.args = self.flatten(new_node.args, sympy_class)
  1001. return new_node
  1002. return node
  1003. def visit_Call(self, node):
  1004. new_node = self.generic_visit(node)
  1005. if isinstance(node.func, ast.Name) and node.func.id in self.functions:
  1006. new_node.keywords.append(ast.keyword(arg='evaluate', value=ast.Constant(value=False)))
  1007. return new_node
  1008. _transformation = { # items can be added but never re-ordered
  1009. 0: lambda_notation,
  1010. 1: auto_symbol,
  1011. 2: repeated_decimals,
  1012. 3: auto_number,
  1013. 4: factorial_notation,
  1014. 5: implicit_multiplication_application,
  1015. 6: convert_xor,
  1016. 7: implicit_application,
  1017. 8: implicit_multiplication,
  1018. 9: convert_equals_signs,
  1019. 10: function_exponentiation,
  1020. 11: rationalize}
  1021. transformations = '\n'.join('%s: %s' % (i, func_name(f)) for i, f in _transformation.items())
  1022. class _T():
  1023. """class to retrieve transformations from a given slice
  1024. EXAMPLES
  1025. ========
  1026. >>> from sympy.parsing.sympy_parser import T, standard_transformations
  1027. >>> assert T[:5] == standard_transformations
  1028. """
  1029. def __init__(self):
  1030. self.N = len(_transformation)
  1031. def __str__(self):
  1032. return transformations
  1033. def __getitem__(self, t):
  1034. if not type(t) is tuple:
  1035. t = (t,)
  1036. i = []
  1037. for ti in t:
  1038. if type(ti) is int:
  1039. i.append(range(self.N)[ti])
  1040. elif type(ti) is slice:
  1041. i.extend(range(*ti.indices(self.N)))
  1042. else:
  1043. raise TypeError('unexpected slice arg')
  1044. return tuple([_transformation[_] for _ in i])
  1045. T = _T()