sympy_parser.py 41 KB

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