backend.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. USE_SYMENGINE = os.getenv('USE_SYMENGINE', '0')
  3. USE_SYMENGINE = USE_SYMENGINE.lower() in ('1', 't', 'true') # type: ignore
  4. if USE_SYMENGINE:
  5. from symengine import (Symbol, Integer, sympify, S,
  6. SympifyError, exp, log, gamma, sqrt, I, E, pi, Matrix,
  7. sin, cos, tan, cot, csc, sec, asin, acos, atan, acot, acsc, asec,
  8. sinh, cosh, tanh, coth, asinh, acosh, atanh, acoth,
  9. lambdify, symarray, diff, zeros, eye, diag, ones,
  10. expand, Function, symbols, var, Add, Mul, Derivative,
  11. ImmutableMatrix, MatrixBase, Rational, Basic)
  12. from symengine.lib.symengine_wrapper import gcd as igcd
  13. from symengine import AppliedUndef
  14. else:
  15. from sympy.core.add import Add
  16. from sympy.core.basic import Basic
  17. from sympy.core.function import (diff, Function, AppliedUndef,
  18. expand, Derivative)
  19. from sympy.core.mul import Mul
  20. from sympy.core.numbers import igcd, pi, I, Integer, Rational, E
  21. from sympy.core.singleton import S
  22. from sympy.core.symbol import Symbol, var, symbols
  23. from sympy.core.sympify import SympifyError, sympify
  24. from sympy.functions.elementary.exponential import log, exp
  25. from sympy.functions.elementary.hyperbolic import (coth, sinh,
  26. acosh, acoth, tanh, asinh, atanh, cosh)
  27. from sympy.functions.elementary.miscellaneous import sqrt
  28. from sympy.functions.elementary.trigonometric import (csc,
  29. asec, cos, atan, sec, acot, asin, tan, sin, cot, acsc, acos)
  30. from sympy.functions.special.gamma_functions import gamma
  31. from sympy.matrices.dense import (eye, zeros, diag, Matrix,
  32. ones, symarray)
  33. from sympy.matrices.immutable import ImmutableMatrix
  34. from sympy.matrices.matrices import MatrixBase
  35. from sympy.utilities.lambdify import lambdify
  36. #
  37. # XXX: Handling of immutable and mutable matrices in SymEngine is inconsistent
  38. # with SymPy's matrix classes in at least SymEngine version 0.7.0. Until that
  39. # is fixed the function below is needed for consistent behaviour when
  40. # attempting to simplify a matrix.
  41. #
  42. # Expected behaviour of a SymPy mutable/immutable matrix .simplify() method:
  43. #
  44. # Matrix.simplify() : works in place, returns None
  45. # ImmutableMatrix.simplify() : returns a simplified copy
  46. #
  47. # In SymEngine both mutable and immutable matrices simplify in place and return
  48. # None. This is inconsistent with the matrix being "immutable" and also the
  49. # returned None leads to problems in the mechanics module.
  50. #
  51. # The simplify function should not be used because simplify(M) sympifies the
  52. # matrix M and the SymEngine matrices all sympify to SymPy matrices. If we want
  53. # to work with SymEngine matrices then we need to use their .simplify() method
  54. # but that method does not work correctly with immutable matrices.
  55. #
  56. # The _simplify_matrix function can be removed when the SymEngine bug is fixed.
  57. # Since this should be a temporary problem we do not make this function part of
  58. # the public API.
  59. #
  60. # SymEngine issue: https://github.com/symengine/symengine.py/issues/363
  61. #
  62. def _simplify_matrix(M):
  63. """Return a simplified copy of the matrix M"""
  64. assert isinstance(M, (Matrix, ImmutableMatrix))
  65. Mnew = M.as_mutable() # makes a copy if mutable
  66. Mnew.simplify()
  67. if isinstance(M, ImmutableMatrix):
  68. Mnew = Mnew.as_immutable()
  69. return Mnew
  70. __all__ = [
  71. 'Symbol', 'Integer', 'sympify', 'S', 'SympifyError', 'exp', 'log',
  72. 'gamma', 'sqrt', 'I', 'E', 'pi', 'Matrix', 'sin', 'cos', 'tan', 'cot',
  73. 'csc', 'sec', 'asin', 'acos', 'atan', 'acot', 'acsc', 'asec', 'sinh',
  74. 'cosh', 'tanh', 'coth', 'asinh', 'acosh', 'atanh', 'acoth', 'lambdify',
  75. 'symarray', 'diff', 'zeros', 'eye', 'diag', 'ones', 'expand', 'Function',
  76. 'symbols', 'var', 'Add', 'Mul', 'Derivative', 'ImmutableMatrix',
  77. 'MatrixBase', 'Rational', 'Basic', 'igcd', 'AppliedUndef',
  78. ]