PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/sympy/sympy/parsing/maxima.py

https://bitbucket.org/cfbolz/benchmarks-pypy-phd
Python | 66 lines | 51 code | 15 blank | 0 comment | 4 complexity | 8418b50928d0055b23a7987555fe958c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0
  1. import re
  2. from sympy import sympify, Sum, product, sin, cos
  3. class MaximaHelpers:
  4. def maxima_expand(expr):
  5. return expr.expand()
  6. def maxima_float(expr):
  7. return expr.evalf()
  8. def maxima_trigexpand(expr):
  9. return expr.expand(trig=True)
  10. def maxima_sum(a1, a2, a3, a4):
  11. return Sum(a1, (a2, a3, a4)).doit()
  12. def maxima_product(a1,a2,a3,a4):
  13. return product(a1, (a2,a3,a4))
  14. def maxima_csc(expr):
  15. return 1/sin(expr)
  16. def maxima_sec(expr):
  17. return 1/cos(expr)
  18. sub_dict = {
  19. 'pi' : re.compile('%pi'),
  20. 'E' : re.compile('%e'),
  21. 'I' : re.compile('%i'),
  22. '**': re.compile('\^'),
  23. 'oo': re.compile(r'\binf\b'),
  24. '-oo': re.compile(r'\bminf\b'),
  25. "'-'" : re.compile(r'\bminus\b'),
  26. 'maxima_expand' : re.compile(r'\bexpand\b'),
  27. 'maxima_float' : re.compile(r'\bfloat\b'),
  28. 'maxima_trigexpand' : re.compile(r'\btrigexpand'),
  29. 'maxima_sum' : re.compile(r'\bsum\b'),
  30. 'maxima_product' : re.compile(r'\bproduct\b'),
  31. 'cancel' : re.compile(r'\bratsimp\b'),
  32. 'maxima_csc' : re.compile(r'\bcsc\b'),
  33. 'maxima_sec' : re.compile(r'\bsec\b')
  34. }
  35. var_name = re.compile('^\s*(\w+)\s*:')
  36. def parse_maxima(str, globals=None, name_dict={}):
  37. str = str.strip()
  38. str = str.rstrip('; ')
  39. for k,v in sub_dict.items():
  40. str = v.sub(k, str)
  41. assign_var = None
  42. var_match = var_name.search(str)
  43. if var_match:
  44. assign_var = var_match.group(1)
  45. str = str[var_match.end():].strip()
  46. dct = MaximaHelpers.__dict__.copy()
  47. dct.update(name_dict)
  48. obj = sympify(str, locals= dct)
  49. if assign_var and globals:
  50. globals[assign_var] = obj
  51. return obj