test_maple.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. from sympy.core import (S, pi, oo, symbols, Function, Rational, Integer,
  2. Tuple, Symbol, Eq, Ne, Le, Lt, Gt, Ge)
  3. from sympy.core import EulerGamma, GoldenRatio, Catalan, Lambda, Mul, Pow
  4. from sympy.functions import Piecewise, sqrt, ceiling, exp, sin, cos, sinc, lucas
  5. from sympy.testing.pytest import raises
  6. from sympy.utilities.lambdify import implemented_function
  7. from sympy.matrices import (eye, Matrix, MatrixSymbol, Identity,
  8. HadamardProduct, SparseMatrix)
  9. from sympy.functions.special.bessel import besseli
  10. from sympy.printing.maple import maple_code
  11. x, y, z = symbols('x,y,z')
  12. def test_Integer():
  13. assert maple_code(Integer(67)) == "67"
  14. assert maple_code(Integer(-1)) == "-1"
  15. def test_Rational():
  16. assert maple_code(Rational(3, 7)) == "3/7"
  17. assert maple_code(Rational(18, 9)) == "2"
  18. assert maple_code(Rational(3, -7)) == "-3/7"
  19. assert maple_code(Rational(-3, -7)) == "3/7"
  20. assert maple_code(x + Rational(3, 7)) == "x + 3/7"
  21. assert maple_code(Rational(3, 7) * x) == '(3/7)*x'
  22. def test_Relational():
  23. assert maple_code(Eq(x, y)) == "x = y"
  24. assert maple_code(Ne(x, y)) == "x <> y"
  25. assert maple_code(Le(x, y)) == "x <= y"
  26. assert maple_code(Lt(x, y)) == "x < y"
  27. assert maple_code(Gt(x, y)) == "x > y"
  28. assert maple_code(Ge(x, y)) == "x >= y"
  29. def test_Function():
  30. assert maple_code(sin(x) ** cos(x)) == "sin(x)^cos(x)"
  31. assert maple_code(abs(x)) == "abs(x)"
  32. assert maple_code(ceiling(x)) == "ceil(x)"
  33. def test_Pow():
  34. assert maple_code(x ** 3) == "x^3"
  35. assert maple_code(x ** (y ** 3)) == "x^(y^3)"
  36. assert maple_code((x ** 3) ** y) == "(x^3)^y"
  37. assert maple_code(x ** Rational(2, 3)) == 'x^(2/3)'
  38. g = implemented_function('g', Lambda(x, 2 * x))
  39. assert maple_code(1 / (g(x) * 3.5) ** (x - y ** x) / (x ** 2 + y)) == \
  40. "(3.5*2*x)^(-x + y^x)/(x^2 + y)"
  41. # For issue 14160
  42. assert maple_code(Mul(-2, x, Pow(Mul(y, y, evaluate=False), -1, evaluate=False),
  43. evaluate=False)) == '-2*x/(y*y)'
  44. def test_basic_ops():
  45. assert maple_code(x * y) == "x*y"
  46. assert maple_code(x + y) == "x + y"
  47. assert maple_code(x - y) == "x - y"
  48. assert maple_code(-x) == "-x"
  49. def test_1_over_x_and_sqrt():
  50. # 1.0 and 0.5 would do something different in regular StrPrinter,
  51. # but these are exact in IEEE floating point so no different here.
  52. assert maple_code(1 / x) == '1/x'
  53. assert maple_code(x ** -1) == maple_code(x ** -1.0) == '1/x'
  54. assert maple_code(1 / sqrt(x)) == '1/sqrt(x)'
  55. assert maple_code(x ** -S.Half) == maple_code(x ** -0.5) == '1/sqrt(x)'
  56. assert maple_code(sqrt(x)) == 'sqrt(x)'
  57. assert maple_code(x ** S.Half) == maple_code(x ** 0.5) == 'sqrt(x)'
  58. assert maple_code(1 / pi) == '1/Pi'
  59. assert maple_code(pi ** -1) == maple_code(pi ** -1.0) == '1/Pi'
  60. assert maple_code(pi ** -0.5) == '1/sqrt(Pi)'
  61. def test_mix_number_mult_symbols():
  62. assert maple_code(3 * x) == "3*x"
  63. assert maple_code(pi * x) == "Pi*x"
  64. assert maple_code(3 / x) == "3/x"
  65. assert maple_code(pi / x) == "Pi/x"
  66. assert maple_code(x / 3) == '(1/3)*x'
  67. assert maple_code(x / pi) == "x/Pi"
  68. assert maple_code(x * y) == "x*y"
  69. assert maple_code(3 * x * y) == "3*x*y"
  70. assert maple_code(3 * pi * x * y) == "3*Pi*x*y"
  71. assert maple_code(x / y) == "x/y"
  72. assert maple_code(3 * x / y) == "3*x/y"
  73. assert maple_code(x * y / z) == "x*y/z"
  74. assert maple_code(x / y * z) == "x*z/y"
  75. assert maple_code(1 / x / y) == "1/(x*y)"
  76. assert maple_code(2 * pi * x / y / z) == "2*Pi*x/(y*z)"
  77. assert maple_code(3 * pi / x) == "3*Pi/x"
  78. assert maple_code(S(3) / 5) == "3/5"
  79. assert maple_code(S(3) / 5 * x) == '(3/5)*x'
  80. assert maple_code(x / y / z) == "x/(y*z)"
  81. assert maple_code((x + y) / z) == "(x + y)/z"
  82. assert maple_code((x + y) / (z + x)) == "(x + y)/(x + z)"
  83. assert maple_code((x + y) / EulerGamma) == '(x + y)/gamma'
  84. assert maple_code(x / 3 / pi) == '(1/3)*x/Pi'
  85. assert maple_code(S(3) / 5 * x * y / pi) == '(3/5)*x*y/Pi'
  86. def test_mix_number_pow_symbols():
  87. assert maple_code(pi ** 3) == 'Pi^3'
  88. assert maple_code(x ** 2) == 'x^2'
  89. assert maple_code(x ** (pi ** 3)) == 'x^(Pi^3)'
  90. assert maple_code(x ** y) == 'x^y'
  91. assert maple_code(x ** (y ** z)) == 'x^(y^z)'
  92. assert maple_code((x ** y) ** z) == '(x^y)^z'
  93. def test_imag():
  94. I = S('I')
  95. assert maple_code(I) == "I"
  96. assert maple_code(5 * I) == "5*I"
  97. assert maple_code((S(3) / 2) * I) == "(3/2)*I"
  98. assert maple_code(3 + 4 * I) == "3 + 4*I"
  99. def test_constants():
  100. assert maple_code(pi) == "Pi"
  101. assert maple_code(oo) == "infinity"
  102. assert maple_code(-oo) == "-infinity"
  103. assert maple_code(S.NegativeInfinity) == "-infinity"
  104. assert maple_code(S.NaN) == "undefined"
  105. assert maple_code(S.Exp1) == "exp(1)"
  106. assert maple_code(exp(1)) == "exp(1)"
  107. def test_constants_other():
  108. assert maple_code(2 * GoldenRatio) == '2*(1/2 + (1/2)*sqrt(5))'
  109. assert maple_code(2 * Catalan) == '2*Catalan'
  110. assert maple_code(2 * EulerGamma) == "2*gamma"
  111. def test_boolean():
  112. assert maple_code(x & y) == "x && y"
  113. assert maple_code(x | y) == "x || y"
  114. assert maple_code(~x) == "!x"
  115. assert maple_code(x & y & z) == "x && y && z"
  116. assert maple_code(x | y | z) == "x || y || z"
  117. assert maple_code((x & y) | z) == "z || x && y"
  118. assert maple_code((x | y) & z) == "z && (x || y)"
  119. def test_Matrices():
  120. assert maple_code(Matrix(1, 1, [10])) == \
  121. 'Matrix([[10]], storage = rectangular)'
  122. A = Matrix([[1, sin(x / 2), abs(x)],
  123. [0, 1, pi],
  124. [0, exp(1), ceiling(x)]])
  125. expected = \
  126. 'Matrix(' \
  127. '[[1, sin((1/2)*x), abs(x)],' \
  128. ' [0, 1, Pi],' \
  129. ' [0, exp(1), ceil(x)]], ' \
  130. 'storage = rectangular)'
  131. assert maple_code(A) == expected
  132. # row and columns
  133. assert maple_code(A[:, 0]) == \
  134. 'Matrix([[1], [0], [0]], storage = rectangular)'
  135. assert maple_code(A[0, :]) == \
  136. 'Matrix([[1, sin((1/2)*x), abs(x)]], storage = rectangular)'
  137. assert maple_code(Matrix([[x, x - y, -y]])) == \
  138. 'Matrix([[x, x - y, -y]], storage = rectangular)'
  139. # empty matrices
  140. assert maple_code(Matrix(0, 0, [])) == \
  141. 'Matrix([], storage = rectangular)'
  142. assert maple_code(Matrix(0, 3, [])) == \
  143. 'Matrix([], storage = rectangular)'
  144. def test_SparseMatrices():
  145. assert maple_code(SparseMatrix(Identity(2))) == 'Matrix([[1, 0], [0, 1]], storage = sparse)'
  146. def test_vector_entries_hadamard():
  147. # For a row or column, user might to use the other dimension
  148. A = Matrix([[1, sin(2 / x), 3 * pi / x / 5]])
  149. assert maple_code(A) == \
  150. 'Matrix([[1, sin(2/x), (3/5)*Pi/x]], storage = rectangular)'
  151. assert maple_code(A.T) == \
  152. 'Matrix([[1], [sin(2/x)], [(3/5)*Pi/x]], storage = rectangular)'
  153. def test_Matrices_entries_not_hadamard():
  154. A = Matrix([[1, sin(2 / x), 3 * pi / x / 5], [1, 2, x * y]])
  155. expected = \
  156. 'Matrix([[1, sin(2/x), (3/5)*Pi/x], [1, 2, x*y]], ' \
  157. 'storage = rectangular)'
  158. assert maple_code(A) == expected
  159. def test_MatrixSymbol():
  160. n = Symbol('n', integer=True)
  161. A = MatrixSymbol('A', n, n)
  162. B = MatrixSymbol('B', n, n)
  163. assert maple_code(A * B) == "A.B"
  164. assert maple_code(B * A) == "B.A"
  165. assert maple_code(2 * A * B) == "2*A.B"
  166. assert maple_code(B * 2 * A) == "2*B.A"
  167. assert maple_code(
  168. A * (B + 3 * Identity(n))) == "A.(3*Matrix(n, shape = identity) + B)"
  169. assert maple_code(A ** (x ** 2)) == "MatrixPower(A, x^2)"
  170. assert maple_code(A ** 3) == "MatrixPower(A, 3)"
  171. assert maple_code(A ** (S.Half)) == "MatrixPower(A, 1/2)"
  172. def test_special_matrices():
  173. assert maple_code(6 * Identity(3)) == "6*Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], storage = sparse)"
  174. assert maple_code(Identity(x)) == 'Matrix(x, shape = identity)'
  175. def test_containers():
  176. assert maple_code([1, 2, 3, [4, 5, [6, 7]], 8, [9, 10], 11]) == \
  177. "[1, 2, 3, [4, 5, [6, 7]], 8, [9, 10], 11]"
  178. assert maple_code((1, 2, (3, 4))) == "[1, 2, [3, 4]]"
  179. assert maple_code([1]) == "[1]"
  180. assert maple_code((1,)) == "[1]"
  181. assert maple_code(Tuple(*[1, 2, 3])) == "[1, 2, 3]"
  182. assert maple_code((1, x * y, (3, x ** 2))) == "[1, x*y, [3, x^2]]"
  183. # scalar, matrix, empty matrix and empty list
  184. assert maple_code((1, eye(3), Matrix(0, 0, []), [])) == \
  185. "[1, Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], storage = rectangular), Matrix([], storage = rectangular), []]"
  186. def test_maple_noninline():
  187. source = maple_code((x + y)/Catalan, assign_to='me', inline=False)
  188. expected = "me := (x + y)/Catalan"
  189. assert source == expected
  190. def test_maple_matrix_assign_to():
  191. A = Matrix([[1, 2, 3]])
  192. assert maple_code(A, assign_to='a') == "a := Matrix([[1, 2, 3]], storage = rectangular)"
  193. A = Matrix([[1, 2], [3, 4]])
  194. assert maple_code(A, assign_to='A') == "A := Matrix([[1, 2], [3, 4]], storage = rectangular)"
  195. def test_maple_matrix_assign_to_more():
  196. # assigning to Symbol or MatrixSymbol requires lhs/rhs match
  197. A = Matrix([[1, 2, 3]])
  198. B = MatrixSymbol('B', 1, 3)
  199. C = MatrixSymbol('C', 2, 3)
  200. assert maple_code(A, assign_to=B) == "B := Matrix([[1, 2, 3]], storage = rectangular)"
  201. raises(ValueError, lambda: maple_code(A, assign_to=x))
  202. raises(ValueError, lambda: maple_code(A, assign_to=C))
  203. def test_maple_matrix_1x1():
  204. A = Matrix([[3]])
  205. assert maple_code(A, assign_to='B') == "B := Matrix([[3]], storage = rectangular)"
  206. def test_maple_matrix_elements():
  207. A = Matrix([[x, 2, x * y]])
  208. assert maple_code(A[0, 0] ** 2 + A[0, 1] + A[0, 2]) == "x^2 + x*y + 2"
  209. AA = MatrixSymbol('AA', 1, 3)
  210. assert maple_code(AA) == "AA"
  211. assert maple_code(AA[0, 0] ** 2 + sin(AA[0, 1]) + AA[0, 2]) == \
  212. "sin(AA[1, 2]) + AA[1, 1]^2 + AA[1, 3]"
  213. assert maple_code(sum(AA)) == "AA[1, 1] + AA[1, 2] + AA[1, 3]"
  214. def test_maple_boolean():
  215. assert maple_code(True) == "true"
  216. assert maple_code(S.true) == "true"
  217. assert maple_code(False) == "false"
  218. assert maple_code(S.false) == "false"
  219. def test_sparse():
  220. M = SparseMatrix(5, 6, {})
  221. M[2, 2] = 10
  222. M[1, 2] = 20
  223. M[1, 3] = 22
  224. M[0, 3] = 30
  225. M[3, 0] = x * y
  226. assert maple_code(M) == \
  227. 'Matrix([[0, 0, 0, 30, 0, 0],' \
  228. ' [0, 0, 20, 22, 0, 0],' \
  229. ' [0, 0, 10, 0, 0, 0],' \
  230. ' [x*y, 0, 0, 0, 0, 0],' \
  231. ' [0, 0, 0, 0, 0, 0]], ' \
  232. 'storage = sparse)'
  233. # Not an important point.
  234. def test_maple_not_supported():
  235. assert maple_code(S.ComplexInfinity) == (
  236. "# Not supported in maple:\n"
  237. "# ComplexInfinity\n"
  238. "zoo"
  239. ) # PROBLEM
  240. def test_MatrixElement_printing():
  241. # test cases for issue #11821
  242. A = MatrixSymbol("A", 1, 3)
  243. B = MatrixSymbol("B", 1, 3)
  244. assert (maple_code(A[0, 0]) == "A[1, 1]")
  245. assert (maple_code(3 * A[0, 0]) == "3*A[1, 1]")
  246. F = A-B
  247. assert (maple_code(F[0,0]) == "A[1, 1] - B[1, 1]")
  248. def test_hadamard():
  249. A = MatrixSymbol('A', 3, 3)
  250. B = MatrixSymbol('B', 3, 3)
  251. v = MatrixSymbol('v', 3, 1)
  252. h = MatrixSymbol('h', 1, 3)
  253. C = HadamardProduct(A, B)
  254. assert maple_code(C) == "A*B"
  255. assert maple_code(C * v) == "(A*B).v"
  256. # HadamardProduct is higher than dot product.
  257. assert maple_code(h * C * v) == "h.(A*B).v"
  258. assert maple_code(C * A) == "(A*B).A"
  259. # mixing Hadamard and scalar strange b/c we vectorize scalars
  260. assert maple_code(C * x * y) == "x*y*(A*B)"
  261. def test_maple_piecewise():
  262. expr = Piecewise((x, x < 1), (x ** 2, True))
  263. assert maple_code(expr) == "piecewise(x < 1, x, x^2)"
  264. assert maple_code(expr, assign_to="r") == (
  265. "r := piecewise(x < 1, x, x^2)")
  266. expr = Piecewise((x ** 2, x < 1), (x ** 3, x < 2), (x ** 4, x < 3), (x ** 5, True))
  267. expected = "piecewise(x < 1, x^2, x < 2, x^3, x < 3, x^4, x^5)"
  268. assert maple_code(expr) == expected
  269. assert maple_code(expr, assign_to="r") == "r := " + expected
  270. # Check that Piecewise without a True (default) condition error
  271. expr = Piecewise((x, x < 1), (x ** 2, x > 1), (sin(x), x > 0))
  272. raises(ValueError, lambda: maple_code(expr))
  273. def test_maple_piecewise_times_const():
  274. pw = Piecewise((x, x < 1), (x ** 2, True))
  275. assert maple_code(2 * pw) == "2*piecewise(x < 1, x, x^2)"
  276. assert maple_code(pw / x) == "piecewise(x < 1, x, x^2)/x"
  277. assert maple_code(pw / (x * y)) == "piecewise(x < 1, x, x^2)/(x*y)"
  278. assert maple_code(pw / 3) == "(1/3)*piecewise(x < 1, x, x^2)"
  279. def test_maple_derivatives():
  280. f = Function('f')
  281. assert maple_code(f(x).diff(x)) == 'diff(f(x), x)'
  282. assert maple_code(f(x).diff(x, 2)) == 'diff(f(x), x$2)'
  283. def test_automatic_rewrites():
  284. assert maple_code(lucas(x)) == '2^(-x)*((1 - sqrt(5))^x + (1 + sqrt(5))^x)'
  285. assert maple_code(sinc(x)) == 'piecewise(x <> 0, sin(x)/x, 1)'
  286. def test_specfun():
  287. assert maple_code('asin(x)') == 'arcsin(x)'
  288. assert maple_code(besseli(x, y)) == 'BesselI(x, y)'