test_mathematica.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. from sympy.core import (S, pi, oo, symbols, Function, Rational, Integer, Tuple,
  2. Derivative, Eq, Ne, Le, Lt, Gt, Ge)
  3. from sympy.integrals import Integral
  4. from sympy.concrete import Sum
  5. from sympy.functions import (exp, sin, cos, fresnelc, fresnels, conjugate, Max,
  6. Min, gamma, polygamma, loggamma, erf, erfi, erfc,
  7. erf2, expint, erfinv, erfcinv, Ei, Si, Ci, li,
  8. Shi, Chi, uppergamma, beta, subfactorial, erf2inv,
  9. factorial, factorial2, catalan, RisingFactorial,
  10. FallingFactorial, harmonic, atan2, sec, acsc,
  11. hermite, laguerre, assoc_laguerre, jacobi,
  12. gegenbauer, chebyshevt, chebyshevu, legendre,
  13. assoc_legendre, Li, LambertW)
  14. from sympy.printing.mathematica import mathematica_code as mcode
  15. x, y, z, w = symbols('x,y,z,w')
  16. f = Function('f')
  17. def test_Integer():
  18. assert mcode(Integer(67)) == "67"
  19. assert mcode(Integer(-1)) == "-1"
  20. def test_Rational():
  21. assert mcode(Rational(3, 7)) == "3/7"
  22. assert mcode(Rational(18, 9)) == "2"
  23. assert mcode(Rational(3, -7)) == "-3/7"
  24. assert mcode(Rational(-3, -7)) == "3/7"
  25. assert mcode(x + Rational(3, 7)) == "x + 3/7"
  26. assert mcode(Rational(3, 7)*x) == "(3/7)*x"
  27. def test_Relational():
  28. assert mcode(Eq(x, y)) == "x == y"
  29. assert mcode(Ne(x, y)) == "x != y"
  30. assert mcode(Le(x, y)) == "x <= y"
  31. assert mcode(Lt(x, y)) == "x < y"
  32. assert mcode(Gt(x, y)) == "x > y"
  33. assert mcode(Ge(x, y)) == "x >= y"
  34. def test_Function():
  35. assert mcode(f(x, y, z)) == "f[x, y, z]"
  36. assert mcode(sin(x) ** cos(x)) == "Sin[x]^Cos[x]"
  37. assert mcode(sec(x) * acsc(x)) == "ArcCsc[x]*Sec[x]"
  38. assert mcode(atan2(x, y)) == "ArcTan[x, y]"
  39. assert mcode(conjugate(x)) == "Conjugate[x]"
  40. assert mcode(Max(x, y, z)*Min(y, z)) == "Max[x, y, z]*Min[y, z]"
  41. assert mcode(fresnelc(x)) == "FresnelC[x]"
  42. assert mcode(fresnels(x)) == "FresnelS[x]"
  43. assert mcode(gamma(x)) == "Gamma[x]"
  44. assert mcode(uppergamma(x, y)) == "Gamma[x, y]"
  45. assert mcode(polygamma(x, y)) == "PolyGamma[x, y]"
  46. assert mcode(loggamma(x)) == "LogGamma[x]"
  47. assert mcode(erf(x)) == "Erf[x]"
  48. assert mcode(erfc(x)) == "Erfc[x]"
  49. assert mcode(erfi(x)) == "Erfi[x]"
  50. assert mcode(erf2(x, y)) == "Erf[x, y]"
  51. assert mcode(expint(x, y)) == "ExpIntegralE[x, y]"
  52. assert mcode(erfcinv(x)) == "InverseErfc[x]"
  53. assert mcode(erfinv(x)) == "InverseErf[x]"
  54. assert mcode(erf2inv(x, y)) == "InverseErf[x, y]"
  55. assert mcode(Ei(x)) == "ExpIntegralEi[x]"
  56. assert mcode(Ci(x)) == "CosIntegral[x]"
  57. assert mcode(li(x)) == "LogIntegral[x]"
  58. assert mcode(Si(x)) == "SinIntegral[x]"
  59. assert mcode(Shi(x)) == "SinhIntegral[x]"
  60. assert mcode(Chi(x)) == "CoshIntegral[x]"
  61. assert mcode(beta(x, y)) == "Beta[x, y]"
  62. assert mcode(factorial(x)) == "Factorial[x]"
  63. assert mcode(factorial2(x)) == "Factorial2[x]"
  64. assert mcode(subfactorial(x)) == "Subfactorial[x]"
  65. assert mcode(FallingFactorial(x, y)) == "FactorialPower[x, y]"
  66. assert mcode(RisingFactorial(x, y)) == "Pochhammer[x, y]"
  67. assert mcode(catalan(x)) == "CatalanNumber[x]"
  68. assert mcode(harmonic(x)) == "HarmonicNumber[x]"
  69. assert mcode(harmonic(x, y)) == "HarmonicNumber[x, y]"
  70. assert mcode(Li(x)) == "LogIntegral[x] - LogIntegral[2]"
  71. assert mcode(LambertW(x)) == "ProductLog[x]"
  72. assert mcode(LambertW(x, -1)) == "ProductLog[-1, x]"
  73. assert mcode(LambertW(x, y)) == "ProductLog[y, x]"
  74. def test_special_polynomials():
  75. assert mcode(hermite(x, y)) == "HermiteH[x, y]"
  76. assert mcode(laguerre(x, y)) == "LaguerreL[x, y]"
  77. assert mcode(assoc_laguerre(x, y, z)) == "LaguerreL[x, y, z]"
  78. assert mcode(jacobi(x, y, z, w)) == "JacobiP[x, y, z, w]"
  79. assert mcode(gegenbauer(x, y, z)) == "GegenbauerC[x, y, z]"
  80. assert mcode(chebyshevt(x, y)) == "ChebyshevT[x, y]"
  81. assert mcode(chebyshevu(x, y)) == "ChebyshevU[x, y]"
  82. assert mcode(legendre(x, y)) == "LegendreP[x, y]"
  83. assert mcode(assoc_legendre(x, y, z)) == "LegendreP[x, y, z]"
  84. def test_Pow():
  85. assert mcode(x**3) == "x^3"
  86. assert mcode(x**(y**3)) == "x^(y^3)"
  87. assert mcode(1/(f(x)*3.5)**(x - y**x)/(x**2 + y)) == \
  88. "(3.5*f[x])^(-x + y^x)/(x^2 + y)"
  89. assert mcode(x**-1.0) == 'x^(-1.0)'
  90. assert mcode(x**Rational(2, 3)) == 'x^(2/3)'
  91. def test_Mul():
  92. A, B, C, D = symbols('A B C D', commutative=False)
  93. assert mcode(x*y*z) == "x*y*z"
  94. assert mcode(x*y*A) == "x*y*A"
  95. assert mcode(x*y*A*B) == "x*y*A**B"
  96. assert mcode(x*y*A*B*C) == "x*y*A**B**C"
  97. assert mcode(x*A*B*(C + D)*A*y) == "x*y*A**B**(C + D)**A"
  98. def test_constants():
  99. assert mcode(S.Zero) == "0"
  100. assert mcode(S.One) == "1"
  101. assert mcode(S.NegativeOne) == "-1"
  102. assert mcode(S.Half) == "1/2"
  103. assert mcode(S.ImaginaryUnit) == "I"
  104. assert mcode(oo) == "Infinity"
  105. assert mcode(S.NegativeInfinity) == "-Infinity"
  106. assert mcode(S.ComplexInfinity) == "ComplexInfinity"
  107. assert mcode(S.NaN) == "Indeterminate"
  108. assert mcode(S.Exp1) == "E"
  109. assert mcode(pi) == "Pi"
  110. assert mcode(S.GoldenRatio) == "GoldenRatio"
  111. assert mcode(S.TribonacciConstant) == \
  112. "(1/3 + (1/3)*(19 - 3*33^(1/2))^(1/3) + " \
  113. "(1/3)*(3*33^(1/2) + 19)^(1/3))"
  114. assert mcode(2*S.TribonacciConstant) == \
  115. "2*(1/3 + (1/3)*(19 - 3*33^(1/2))^(1/3) + " \
  116. "(1/3)*(3*33^(1/2) + 19)^(1/3))"
  117. assert mcode(S.EulerGamma) == "EulerGamma"
  118. assert mcode(S.Catalan) == "Catalan"
  119. def test_containers():
  120. assert mcode([1, 2, 3, [4, 5, [6, 7]], 8, [9, 10], 11]) == \
  121. "{1, 2, 3, {4, 5, {6, 7}}, 8, {9, 10}, 11}"
  122. assert mcode((1, 2, (3, 4))) == "{1, 2, {3, 4}}"
  123. assert mcode([1]) == "{1}"
  124. assert mcode((1,)) == "{1}"
  125. assert mcode(Tuple(*[1, 2, 3])) == "{1, 2, 3}"
  126. def test_matrices():
  127. from sympy.matrices import MutableDenseMatrix, MutableSparseMatrix, \
  128. ImmutableDenseMatrix, ImmutableSparseMatrix
  129. A = MutableDenseMatrix(
  130. [[1, -1, 0, 0],
  131. [0, 1, -1, 0],
  132. [0, 0, 1, -1],
  133. [0, 0, 0, 1]]
  134. )
  135. B = MutableSparseMatrix(A)
  136. C = ImmutableDenseMatrix(A)
  137. D = ImmutableSparseMatrix(A)
  138. assert mcode(C) == mcode(A) == \
  139. "{{1, -1, 0, 0}, " \
  140. "{0, 1, -1, 0}, " \
  141. "{0, 0, 1, -1}, " \
  142. "{0, 0, 0, 1}}"
  143. assert mcode(D) == mcode(B) == \
  144. "SparseArray[{" \
  145. "{1, 1} -> 1, {1, 2} -> -1, {2, 2} -> 1, {2, 3} -> -1, " \
  146. "{3, 3} -> 1, {3, 4} -> -1, {4, 4} -> 1" \
  147. "}, {4, 4}]"
  148. # Trivial cases of matrices
  149. assert mcode(MutableDenseMatrix(0, 0, [])) == '{}'
  150. assert mcode(MutableSparseMatrix(0, 0, [])) == 'SparseArray[{}, {0, 0}]'
  151. assert mcode(MutableDenseMatrix(0, 3, [])) == '{}'
  152. assert mcode(MutableSparseMatrix(0, 3, [])) == 'SparseArray[{}, {0, 3}]'
  153. assert mcode(MutableDenseMatrix(3, 0, [])) == '{{}, {}, {}}'
  154. assert mcode(MutableSparseMatrix(3, 0, [])) == 'SparseArray[{}, {3, 0}]'
  155. def test_NDArray():
  156. from sympy.tensor.array import (
  157. MutableDenseNDimArray, ImmutableDenseNDimArray,
  158. MutableSparseNDimArray, ImmutableSparseNDimArray)
  159. example = MutableDenseNDimArray(
  160. [[[1, 2, 3, 4],
  161. [5, 6, 7, 8],
  162. [9, 10, 11, 12]],
  163. [[13, 14, 15, 16],
  164. [17, 18, 19, 20],
  165. [21, 22, 23, 24]]]
  166. )
  167. assert mcode(example) == \
  168. "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " \
  169. "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}"
  170. example = ImmutableDenseNDimArray(example)
  171. assert mcode(example) == \
  172. "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " \
  173. "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}"
  174. example = MutableSparseNDimArray(example)
  175. assert mcode(example) == \
  176. "SparseArray[{" \
  177. "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " \
  178. "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " \
  179. "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " \
  180. "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " \
  181. "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " \
  182. "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " \
  183. "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " \
  184. "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" \
  185. "}, {2, 3, 4}]"
  186. example = ImmutableSparseNDimArray(example)
  187. assert mcode(example) == \
  188. "SparseArray[{" \
  189. "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " \
  190. "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " \
  191. "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " \
  192. "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " \
  193. "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " \
  194. "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " \
  195. "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " \
  196. "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" \
  197. "}, {2, 3, 4}]"
  198. def test_Integral():
  199. assert mcode(Integral(sin(sin(x)), x)) == "Hold[Integrate[Sin[Sin[x]], x]]"
  200. assert mcode(Integral(exp(-x**2 - y**2),
  201. (x, -oo, oo),
  202. (y, -oo, oo))) == \
  203. "Hold[Integrate[Exp[-x^2 - y^2], {x, -Infinity, Infinity}, " \
  204. "{y, -Infinity, Infinity}]]"
  205. def test_Derivative():
  206. assert mcode(Derivative(sin(x), x)) == "Hold[D[Sin[x], x]]"
  207. assert mcode(Derivative(x, x)) == "Hold[D[x, x]]"
  208. assert mcode(Derivative(sin(x)*y**4, x, 2)) == "Hold[D[y^4*Sin[x], {x, 2}]]"
  209. assert mcode(Derivative(sin(x)*y**4, x, y, x)) == "Hold[D[y^4*Sin[x], x, y, x]]"
  210. assert mcode(Derivative(sin(x)*y**4, x, y, 3, x)) == "Hold[D[y^4*Sin[x], x, {y, 3}, x]]"
  211. def test_Sum():
  212. assert mcode(Sum(sin(x), (x, 0, 10))) == "Hold[Sum[Sin[x], {x, 0, 10}]]"
  213. assert mcode(Sum(exp(-x**2 - y**2),
  214. (x, -oo, oo),
  215. (y, -oo, oo))) == \
  216. "Hold[Sum[Exp[-x^2 - y^2], {x, -Infinity, Infinity}, " \
  217. "{y, -Infinity, Infinity}]]"
  218. def test_comment():
  219. from sympy.printing.mathematica import MCodePrinter
  220. assert MCodePrinter()._get_comment("Hello World") == \
  221. "(* Hello World *)"
  222. def test_userfuncs():
  223. # Dictionary mutation test
  224. some_function = symbols("some_function", cls=Function)
  225. my_user_functions = {"some_function": "SomeFunction"}
  226. assert mcode(
  227. some_function(z),
  228. user_functions=my_user_functions) == \
  229. 'SomeFunction[z]'
  230. assert mcode(
  231. some_function(z),
  232. user_functions=my_user_functions) == \
  233. 'SomeFunction[z]'
  234. # List argument test
  235. my_user_functions = \
  236. {"some_function": [(lambda x: True, "SomeOtherFunction")]}
  237. assert mcode(
  238. some_function(z),
  239. user_functions=my_user_functions) == \
  240. 'SomeOtherFunction[z]'