sympy_parser.py 43 KB

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