图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1165 lines
41 KiB

  1. from sympy.algebras.quaternion import Quaternion
  2. from sympy.assumptions.ask import Q
  3. from sympy.calculus.accumulationbounds import AccumBounds
  4. from sympy.combinatorics.partitions import Partition
  5. from sympy.concrete.summations import (Sum, summation)
  6. from sympy.core.add import Add
  7. from sympy.core.containers import (Dict, Tuple)
  8. from sympy.core.expr import UnevaluatedExpr, Expr
  9. from sympy.core.function import (Derivative, Function, Lambda, Subs, WildFunction)
  10. from sympy.core.mul import Mul
  11. from sympy.core import (Catalan, EulerGamma, GoldenRatio, TribonacciConstant)
  12. from sympy.core.numbers import (E, Float, I, Integer, Rational, nan, oo, pi, zoo)
  13. from sympy.core.parameters import _exp_is_pow
  14. from sympy.core.power import Pow
  15. from sympy.core.relational import (Eq, Rel, Ne)
  16. from sympy.core.singleton import S
  17. from sympy.core.symbol import (Dummy, Symbol, Wild, symbols)
  18. from sympy.functions.combinatorial.factorials import (factorial, factorial2, subfactorial)
  19. from sympy.functions.elementary.complexes import Abs
  20. from sympy.functions.elementary.exponential import exp
  21. from sympy.functions.elementary.miscellaneous import sqrt
  22. from sympy.functions.elementary.trigonometric import (cos, sin)
  23. from sympy.functions.special.delta_functions import Heaviside
  24. from sympy.functions.special.zeta_functions import zeta
  25. from sympy.integrals.integrals import Integral
  26. from sympy.logic.boolalg import (Equivalent, false, true, Xor)
  27. from sympy.matrices.dense import Matrix
  28. from sympy.matrices.expressions.matexpr import MatrixSymbol
  29. from sympy.matrices.expressions.slice import MatrixSlice
  30. from sympy.matrices import SparseMatrix
  31. from sympy.polys.polytools import factor
  32. from sympy.series.limits import Limit
  33. from sympy.series.order import O
  34. from sympy.sets.sets import (Complement, FiniteSet, Interval, SymmetricDifference)
  35. from sympy.external import import_module
  36. from sympy.physics.control.lti import TransferFunction, Series, Parallel, \
  37. Feedback, TransferFunctionMatrix, MIMOSeries, MIMOParallel, MIMOFeedback
  38. from sympy.physics.units import second, joule
  39. from sympy.polys import (Poly, rootof, RootSum, groebner, ring, field, ZZ, QQ,
  40. ZZ_I, QQ_I, lex, grlex)
  41. from sympy.geometry import Point, Circle, Polygon, Ellipse, Triangle
  42. from sympy.tensor import NDimArray
  43. from sympy.tensor.array.expressions.array_expressions import ArraySymbol, ArrayElement
  44. from sympy.testing.pytest import raises, warns_deprecated_sympy
  45. from sympy.printing import sstr, sstrrepr, StrPrinter
  46. from sympy.physics.quantum.trace import Tr
  47. x, y, z, w, t = symbols('x,y,z,w,t')
  48. d = Dummy('d')
  49. def test_printmethod():
  50. class R(Abs):
  51. def _sympystr(self, printer):
  52. return "foo(%s)" % printer._print(self.args[0])
  53. assert sstr(R(x)) == "foo(x)"
  54. class R(Abs):
  55. def _sympystr(self, printer):
  56. return "foo"
  57. assert sstr(R(x)) == "foo"
  58. def test_Abs():
  59. assert str(Abs(x)) == "Abs(x)"
  60. assert str(Abs(Rational(1, 6))) == "1/6"
  61. assert str(Abs(Rational(-1, 6))) == "1/6"
  62. def test_Add():
  63. assert str(x + y) == "x + y"
  64. assert str(x + 1) == "x + 1"
  65. assert str(x + x**2) == "x**2 + x"
  66. assert str(Add(0, 1, evaluate=False)) == "0 + 1"
  67. assert str(Add(0, 0, 1, evaluate=False)) == "0 + 0 + 1"
  68. assert str(1.0*x) == "1.0*x"
  69. assert str(5 + x + y + x*y + x**2 + y**2) == "x**2 + x*y + x + y**2 + y + 5"
  70. assert str(1 + x + x**2/2 + x**3/3) == "x**3/3 + x**2/2 + x + 1"
  71. assert str(2*x - 7*x**2 + 2 + 3*y) == "-7*x**2 + 2*x + 3*y + 2"
  72. assert str(x - y) == "x - y"
  73. assert str(2 - x) == "2 - x"
  74. assert str(x - 2) == "x - 2"
  75. assert str(x - y - z - w) == "-w + x - y - z"
  76. assert str(x - z*y**2*z*w) == "-w*y**2*z**2 + x"
  77. assert str(x - 1*y*x*y) == "-x*y**2 + x"
  78. assert str(sin(x).series(x, 0, 15)) == "x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 - x**11/39916800 + x**13/6227020800 + O(x**15)"
  79. def test_Catalan():
  80. assert str(Catalan) == "Catalan"
  81. def test_ComplexInfinity():
  82. assert str(zoo) == "zoo"
  83. def test_Derivative():
  84. assert str(Derivative(x, y)) == "Derivative(x, y)"
  85. assert str(Derivative(x**2, x, evaluate=False)) == "Derivative(x**2, x)"
  86. assert str(Derivative(
  87. x**2/y, x, y, evaluate=False)) == "Derivative(x**2/y, x, y)"
  88. def test_dict():
  89. assert str({1: 1 + x}) == sstr({1: 1 + x}) == "{1: x + 1}"
  90. assert str({1: x**2, 2: y*x}) in ("{1: x**2, 2: x*y}", "{2: x*y, 1: x**2}")
  91. assert sstr({1: x**2, 2: y*x}) == "{1: x**2, 2: x*y}"
  92. def test_Dict():
  93. assert str(Dict({1: 1 + x})) == sstr({1: 1 + x}) == "{1: x + 1}"
  94. assert str(Dict({1: x**2, 2: y*x})) in (
  95. "{1: x**2, 2: x*y}", "{2: x*y, 1: x**2}")
  96. assert sstr(Dict({1: x**2, 2: y*x})) == "{1: x**2, 2: x*y}"
  97. def test_Dummy():
  98. assert str(d) == "_d"
  99. assert str(d + x) == "_d + x"
  100. def test_EulerGamma():
  101. assert str(EulerGamma) == "EulerGamma"
  102. def test_Exp():
  103. assert str(E) == "E"
  104. with _exp_is_pow(True):
  105. assert str(exp(x)) == "E**x"
  106. def test_factorial():
  107. n = Symbol('n', integer=True)
  108. assert str(factorial(-2)) == "zoo"
  109. assert str(factorial(0)) == "1"
  110. assert str(factorial(7)) == "5040"
  111. assert str(factorial(n)) == "factorial(n)"
  112. assert str(factorial(2*n)) == "factorial(2*n)"
  113. assert str(factorial(factorial(n))) == 'factorial(factorial(n))'
  114. assert str(factorial(factorial2(n))) == 'factorial(factorial2(n))'
  115. assert str(factorial2(factorial(n))) == 'factorial2(factorial(n))'
  116. assert str(factorial2(factorial2(n))) == 'factorial2(factorial2(n))'
  117. assert str(subfactorial(3)) == "2"
  118. assert str(subfactorial(n)) == "subfactorial(n)"
  119. assert str(subfactorial(2*n)) == "subfactorial(2*n)"
  120. def test_Function():
  121. f = Function('f')
  122. fx = f(x)
  123. w = WildFunction('w')
  124. assert str(f) == "f"
  125. assert str(fx) == "f(x)"
  126. assert str(w) == "w_"
  127. def test_Geometry():
  128. assert sstr(Point(0, 0)) == 'Point2D(0, 0)'
  129. assert sstr(Circle(Point(0, 0), 3)) == 'Circle(Point2D(0, 0), 3)'
  130. assert sstr(Ellipse(Point(1, 2), 3, 4)) == 'Ellipse(Point2D(1, 2), 3, 4)'
  131. assert sstr(Triangle(Point(1, 1), Point(7, 8), Point(0, -1))) == \
  132. 'Triangle(Point2D(1, 1), Point2D(7, 8), Point2D(0, -1))'
  133. assert sstr(Polygon(Point(5, 6), Point(-2, -3), Point(0, 0), Point(4, 7))) == \
  134. 'Polygon(Point2D(5, 6), Point2D(-2, -3), Point2D(0, 0), Point2D(4, 7))'
  135. assert sstr(Triangle(Point(0, 0), Point(1, 0), Point(0, 1)), sympy_integers=True) == \
  136. 'Triangle(Point2D(S(0), S(0)), Point2D(S(1), S(0)), Point2D(S(0), S(1)))'
  137. assert sstr(Ellipse(Point(1, 2), 3, 4), sympy_integers=True) == \
  138. 'Ellipse(Point2D(S(1), S(2)), S(3), S(4))'
  139. def test_GoldenRatio():
  140. assert str(GoldenRatio) == "GoldenRatio"
  141. def test_Heaviside():
  142. assert str(Heaviside(x)) == str(Heaviside(x, S.Half)) == "Heaviside(x)"
  143. assert str(Heaviside(x, 1)) == "Heaviside(x, 1)"
  144. def test_TribonacciConstant():
  145. assert str(TribonacciConstant) == "TribonacciConstant"
  146. def test_ImaginaryUnit():
  147. assert str(I) == "I"
  148. def test_Infinity():
  149. assert str(oo) == "oo"
  150. assert str(oo*I) == "oo*I"
  151. def test_Integer():
  152. assert str(Integer(-1)) == "-1"
  153. assert str(Integer(1)) == "1"
  154. assert str(Integer(-3)) == "-3"
  155. assert str(Integer(0)) == "0"
  156. assert str(Integer(25)) == "25"
  157. def test_Integral():
  158. assert str(Integral(sin(x), y)) == "Integral(sin(x), y)"
  159. assert str(Integral(sin(x), (y, 0, 1))) == "Integral(sin(x), (y, 0, 1))"
  160. def test_Interval():
  161. n = (S.NegativeInfinity, 1, 2, S.Infinity)
  162. for i in range(len(n)):
  163. for j in range(i + 1, len(n)):
  164. for l in (True, False):
  165. for r in (True, False):
  166. ival = Interval(n[i], n[j], l, r)
  167. assert S(str(ival)) == ival
  168. def test_AccumBounds():
  169. a = Symbol('a', real=True)
  170. assert str(AccumBounds(0, a)) == "AccumBounds(0, a)"
  171. assert str(AccumBounds(0, 1)) == "AccumBounds(0, 1)"
  172. def test_Lambda():
  173. assert str(Lambda(d, d**2)) == "Lambda(_d, _d**2)"
  174. # issue 2908
  175. assert str(Lambda((), 1)) == "Lambda((), 1)"
  176. assert str(Lambda((), x)) == "Lambda((), x)"
  177. assert str(Lambda((x, y), x+y)) == "Lambda((x, y), x + y)"
  178. assert str(Lambda(((x, y),), x+y)) == "Lambda(((x, y),), x + y)"
  179. def test_Limit():
  180. assert str(Limit(sin(x)/x, x, y)) == "Limit(sin(x)/x, x, y)"
  181. assert str(Limit(1/x, x, 0)) == "Limit(1/x, x, 0)"
  182. assert str(
  183. Limit(sin(x)/x, x, y, dir="-")) == "Limit(sin(x)/x, x, y, dir='-')"
  184. def test_list():
  185. assert str([x]) == sstr([x]) == "[x]"
  186. assert str([x**2, x*y + 1]) == sstr([x**2, x*y + 1]) == "[x**2, x*y + 1]"
  187. assert str([x**2, [y + x]]) == sstr([x**2, [y + x]]) == "[x**2, [x + y]]"
  188. def test_Matrix_str():
  189. M = Matrix([[x**+1, 1], [y, x + y]])
  190. assert str(M) == "Matrix([[x, 1], [y, x + y]])"
  191. assert sstr(M) == "Matrix([\n[x, 1],\n[y, x + y]])"
  192. M = Matrix([[1]])
  193. assert str(M) == sstr(M) == "Matrix([[1]])"
  194. M = Matrix([[1, 2]])
  195. assert str(M) == sstr(M) == "Matrix([[1, 2]])"
  196. M = Matrix()
  197. assert str(M) == sstr(M) == "Matrix(0, 0, [])"
  198. M = Matrix(0, 1, lambda i, j: 0)
  199. assert str(M) == sstr(M) == "Matrix(0, 1, [])"
  200. def test_Mul():
  201. assert str(x/y) == "x/y"
  202. assert str(y/x) == "y/x"
  203. assert str(x/y/z) == "x/(y*z)"
  204. assert str((x + 1)/(y + 2)) == "(x + 1)/(y + 2)"
  205. assert str(2*x/3) == '2*x/3'
  206. assert str(-2*x/3) == '-2*x/3'
  207. assert str(-1.0*x) == '-1.0*x'
  208. assert str(1.0*x) == '1.0*x'
  209. assert str(Mul(0, 1, evaluate=False)) == '0*1'
  210. assert str(Mul(1, 0, evaluate=False)) == '1*0'
  211. assert str(Mul(1, 1, evaluate=False)) == '1*1'
  212. assert str(Mul(1, 1, 1, evaluate=False)) == '1*1*1'
  213. assert str(Mul(1, 2, evaluate=False)) == '1*2'
  214. assert str(Mul(1, S.Half, evaluate=False)) == '1*(1/2)'
  215. assert str(Mul(1, 1, S.Half, evaluate=False)) == '1*1*(1/2)'
  216. assert str(Mul(1, 1, 2, 3, x, evaluate=False)) == '1*1*2*3*x'
  217. assert str(Mul(1, -1, evaluate=False)) == '1*(-1)'
  218. assert str(Mul(-1, 1, evaluate=False)) == '-1*1'
  219. assert str(Mul(4, 3, 2, 1, 0, y, x, evaluate=False)) == '4*3*2*1*0*y*x'
  220. assert str(Mul(4, 3, 2, 1+z, 0, y, x, evaluate=False)) == '4*3*2*(z + 1)*0*y*x'
  221. assert str(Mul(Rational(2, 3), Rational(5, 7), evaluate=False)) == '(2/3)*(5/7)'
  222. # For issue 14160
  223. assert str(Mul(-2, x, Pow(Mul(y,y,evaluate=False), -1, evaluate=False),
  224. evaluate=False)) == '-2*x/(y*y)'
  225. # issue 21537
  226. assert str(Mul(x, Pow(1/y, -1, evaluate=False), evaluate=False)) == 'x/(1/y)'
  227. class CustomClass1(Expr):
  228. is_commutative = True
  229. class CustomClass2(Expr):
  230. is_commutative = True
  231. cc1 = CustomClass1()
  232. cc2 = CustomClass2()
  233. assert str(Rational(2)*cc1) == '2*CustomClass1()'
  234. assert str(cc1*Rational(2)) == '2*CustomClass1()'
  235. assert str(cc1*Float("1.5")) == '1.5*CustomClass1()'
  236. assert str(cc2*Rational(2)) == '2*CustomClass2()'
  237. assert str(cc2*Rational(2)*cc1) == '2*CustomClass1()*CustomClass2()'
  238. assert str(cc1*Rational(2)*cc2) == '2*CustomClass1()*CustomClass2()'
  239. def test_NaN():
  240. assert str(nan) == "nan"
  241. def test_NegativeInfinity():
  242. assert str(-oo) == "-oo"
  243. def test_Order():
  244. assert str(O(x)) == "O(x)"
  245. assert str(O(x**2)) == "O(x**2)"
  246. assert str(O(x*y)) == "O(x*y, x, y)"
  247. assert str(O(x, x)) == "O(x)"
  248. assert str(O(x, (x, 0))) == "O(x)"
  249. assert str(O(x, (x, oo))) == "O(x, (x, oo))"
  250. assert str(O(x, x, y)) == "O(x, x, y)"
  251. assert str(O(x, x, y)) == "O(x, x, y)"
  252. assert str(O(x, (x, oo), (y, oo))) == "O(x, (x, oo), (y, oo))"
  253. def test_Permutation_Cycle():
  254. from sympy.combinatorics import Permutation, Cycle
  255. # general principle: economically, canonically show all moved elements
  256. # and the size of the permutation.
  257. for p, s in [
  258. (Cycle(),
  259. '()'),
  260. (Cycle(2),
  261. '(2)'),
  262. (Cycle(2, 1),
  263. '(1 2)'),
  264. (Cycle(1, 2)(5)(6, 7)(10),
  265. '(1 2)(6 7)(10)'),
  266. (Cycle(3, 4)(1, 2)(3, 4),
  267. '(1 2)(4)'),
  268. ]:
  269. assert sstr(p) == s
  270. for p, s in [
  271. (Permutation([]),
  272. 'Permutation([])'),
  273. (Permutation([], size=1),
  274. 'Permutation([0])'),
  275. (Permutation([], size=2),
  276. 'Permutation([0, 1])'),
  277. (Permutation([], size=10),
  278. 'Permutation([], size=10)'),
  279. (Permutation([1, 0, 2]),
  280. 'Permutation([1, 0, 2])'),
  281. (Permutation([1, 0, 2, 3, 4, 5]),
  282. 'Permutation([1, 0], size=6)'),
  283. (Permutation([1, 0, 2, 3, 4, 5], size=10),
  284. 'Permutation([1, 0], size=10)'),
  285. ]:
  286. assert sstr(p, perm_cyclic=False) == s
  287. for p, s in [
  288. (Permutation([]),
  289. '()'),
  290. (Permutation([], size=1),
  291. '(0)'),
  292. (Permutation([], size=2),
  293. '(1)'),
  294. (Permutation([], size=10),
  295. '(9)'),
  296. (Permutation([1, 0, 2]),
  297. '(2)(0 1)'),
  298. (Permutation([1, 0, 2, 3, 4, 5]),
  299. '(5)(0 1)'),
  300. (Permutation([1, 0, 2, 3, 4, 5], size=10),
  301. '(9)(0 1)'),
  302. (Permutation([0, 1, 3, 2, 4, 5], size=10),
  303. '(9)(2 3)'),
  304. ]:
  305. assert sstr(p) == s
  306. with warns_deprecated_sympy():
  307. old_print_cyclic = Permutation.print_cyclic
  308. Permutation.print_cyclic = False
  309. assert sstr(Permutation([1, 0, 2])) == 'Permutation([1, 0, 2])'
  310. Permutation.print_cyclic = old_print_cyclic
  311. def test_Pi():
  312. assert str(pi) == "pi"
  313. def test_Poly():
  314. assert str(Poly(0, x)) == "Poly(0, x, domain='ZZ')"
  315. assert str(Poly(1, x)) == "Poly(1, x, domain='ZZ')"
  316. assert str(Poly(x, x)) == "Poly(x, x, domain='ZZ')"
  317. assert str(Poly(2*x + 1, x)) == "Poly(2*x + 1, x, domain='ZZ')"
  318. assert str(Poly(2*x - 1, x)) == "Poly(2*x - 1, x, domain='ZZ')"
  319. assert str(Poly(-1, x)) == "Poly(-1, x, domain='ZZ')"
  320. assert str(Poly(-x, x)) == "Poly(-x, x, domain='ZZ')"
  321. assert str(Poly(-2*x + 1, x)) == "Poly(-2*x + 1, x, domain='ZZ')"
  322. assert str(Poly(-2*x - 1, x)) == "Poly(-2*x - 1, x, domain='ZZ')"
  323. assert str(Poly(x - 1, x)) == "Poly(x - 1, x, domain='ZZ')"
  324. assert str(Poly(2*x + x**5, x)) == "Poly(x**5 + 2*x, x, domain='ZZ')"
  325. assert str(Poly(3**(2*x), 3**x)) == "Poly((3**x)**2, 3**x, domain='ZZ')"
  326. assert str(Poly((x**2)**x)) == "Poly(((x**2)**x), (x**2)**x, domain='ZZ')"
  327. assert str(Poly((x + y)**3, (x + y), expand=False)
  328. ) == "Poly((x + y)**3, x + y, domain='ZZ')"
  329. assert str(Poly((x - 1)**2, (x - 1), expand=False)
  330. ) == "Poly((x - 1)**2, x - 1, domain='ZZ')"
  331. assert str(
  332. Poly(x**2 + 1 + y, x)) == "Poly(x**2 + y + 1, x, domain='ZZ[y]')"
  333. assert str(
  334. Poly(x**2 - 1 + y, x)) == "Poly(x**2 + y - 1, x, domain='ZZ[y]')"
  335. assert str(Poly(x**2 + I*x, x)) == "Poly(x**2 + I*x, x, domain='ZZ_I')"
  336. assert str(Poly(x**2 - I*x, x)) == "Poly(x**2 - I*x, x, domain='ZZ_I')"
  337. assert str(Poly(-x*y*z + x*y - 1, x, y, z)
  338. ) == "Poly(-x*y*z + x*y - 1, x, y, z, domain='ZZ')"
  339. assert str(Poly(-w*x**21*y**7*z + (1 + w)*z**3 - 2*x*z + 1, x, y, z)) == \
  340. "Poly(-w*x**21*y**7*z - 2*x*z + (w + 1)*z**3 + 1, x, y, z, domain='ZZ[w]')"
  341. assert str(Poly(x**2 + 1, x, modulus=2)) == "Poly(x**2 + 1, x, modulus=2)"
  342. assert str(Poly(2*x**2 + 3*x + 4, x, modulus=17)) == "Poly(2*x**2 + 3*x + 4, x, modulus=17)"
  343. def test_PolyRing():
  344. assert str(ring("x", ZZ, lex)[0]) == "Polynomial ring in x over ZZ with lex order"
  345. assert str(ring("x,y", QQ, grlex)[0]) == "Polynomial ring in x, y over QQ with grlex order"
  346. assert str(ring("x,y,z", ZZ["t"], lex)[0]) == "Polynomial ring in x, y, z over ZZ[t] with lex order"
  347. def test_FracField():
  348. assert str(field("x", ZZ, lex)[0]) == "Rational function field in x over ZZ with lex order"
  349. assert str(field("x,y", QQ, grlex)[0]) == "Rational function field in x, y over QQ with grlex order"
  350. assert str(field("x,y,z", ZZ["t"], lex)[0]) == "Rational function field in x, y, z over ZZ[t] with lex order"
  351. def test_PolyElement():
  352. Ruv, u,v = ring("u,v", ZZ)
  353. Rxyz, x,y,z = ring("x,y,z", Ruv)
  354. Rx_zzi, xz = ring("x", ZZ_I)
  355. assert str(x - x) == "0"
  356. assert str(x - 1) == "x - 1"
  357. assert str(x + 1) == "x + 1"
  358. assert str(x**2) == "x**2"
  359. assert str(x**(-2)) == "x**(-2)"
  360. assert str(x**QQ(1, 2)) == "x**(1/2)"
  361. assert str((u**2 + 3*u*v + 1)*x**2*y + u + 1) == "(u**2 + 3*u*v + 1)*x**2*y + u + 1"
  362. assert str((u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x) == "(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x"
  363. assert str((u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x + 1) == "(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x + 1"
  364. assert str((-u**2 + 3*u*v - 1)*x**2*y - (u + 1)*x - 1) == "-(u**2 - 3*u*v + 1)*x**2*y - (u + 1)*x - 1"
  365. assert str(-(v**2 + v + 1)*x + 3*u*v + 1) == "-(v**2 + v + 1)*x + 3*u*v + 1"
  366. assert str(-(v**2 + v + 1)*x - 3*u*v + 1) == "-(v**2 + v + 1)*x - 3*u*v + 1"
  367. assert str((1+I)*xz + 2) == "(1 + 1*I)*x + (2 + 0*I)"
  368. def test_FracElement():
  369. Fuv, u,v = field("u,v", ZZ)
  370. Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
  371. Rx_zzi, xz = field("x", QQ_I)
  372. i = QQ_I(0, 1)
  373. assert str(x - x) == "0"
  374. assert str(x - 1) == "x - 1"
  375. assert str(x + 1) == "x + 1"
  376. assert str(x/3) == "x/3"
  377. assert str(x/z) == "x/z"
  378. assert str(x*y/z) == "x*y/z"
  379. assert str(x/(z*t)) == "x/(z*t)"
  380. assert str(x*y/(z*t)) == "x*y/(z*t)"
  381. assert str((x - 1)/y) == "(x - 1)/y"
  382. assert str((x + 1)/y) == "(x + 1)/y"
  383. assert str((-x - 1)/y) == "(-x - 1)/y"
  384. assert str((x + 1)/(y*z)) == "(x + 1)/(y*z)"
  385. assert str(-y/(x + 1)) == "-y/(x + 1)"
  386. assert str(y*z/(x + 1)) == "y*z/(x + 1)"
  387. assert str(((u + 1)*x*y + 1)/((v - 1)*z - 1)) == "((u + 1)*x*y + 1)/((v - 1)*z - 1)"
  388. assert str(((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)) == "((u + 1)*x*y + 1)/((v - 1)*z - u*v*t - 1)"
  389. assert str((1+i)/xz) == "(1 + 1*I)/x"
  390. assert str(((1+i)*xz - i)/xz) == "((1 + 1*I)*x + (0 + -1*I))/x"
  391. def test_GaussianInteger():
  392. assert str(ZZ_I(1, 0)) == "1"
  393. assert str(ZZ_I(-1, 0)) == "-1"
  394. assert str(ZZ_I(0, 1)) == "I"
  395. assert str(ZZ_I(0, -1)) == "-I"
  396. assert str(ZZ_I(0, 2)) == "2*I"
  397. assert str(ZZ_I(0, -2)) == "-2*I"
  398. assert str(ZZ_I(1, 1)) == "1 + I"
  399. assert str(ZZ_I(-1, -1)) == "-1 - I"
  400. assert str(ZZ_I(-1, -2)) == "-1 - 2*I"
  401. def test_GaussianRational():
  402. assert str(QQ_I(1, 0)) == "1"
  403. assert str(QQ_I(QQ(2, 3), 0)) == "2/3"
  404. assert str(QQ_I(0, QQ(2, 3))) == "2*I/3"
  405. assert str(QQ_I(QQ(1, 2), QQ(-2, 3))) == "1/2 - 2*I/3"
  406. def test_Pow():
  407. assert str(x**-1) == "1/x"
  408. assert str(x**-2) == "x**(-2)"
  409. assert str(x**2) == "x**2"
  410. assert str((x + y)**-1) == "1/(x + y)"
  411. assert str((x + y)**-2) == "(x + y)**(-2)"
  412. assert str((x + y)**2) == "(x + y)**2"
  413. assert str((x + y)**(1 + x)) == "(x + y)**(x + 1)"
  414. assert str(x**Rational(1, 3)) == "x**(1/3)"
  415. assert str(1/x**Rational(1, 3)) == "x**(-1/3)"
  416. assert str(sqrt(sqrt(x))) == "x**(1/4)"
  417. # not the same as x**-1
  418. assert str(x**-1.0) == 'x**(-1.0)'
  419. # see issue #2860
  420. assert str(Pow(S(2), -1.0, evaluate=False)) == '2**(-1.0)'
  421. def test_sqrt():
  422. assert str(sqrt(x)) == "sqrt(x)"
  423. assert str(sqrt(x**2)) == "sqrt(x**2)"
  424. assert str(1/sqrt(x)) == "1/sqrt(x)"
  425. assert str(1/sqrt(x**2)) == "1/sqrt(x**2)"
  426. assert str(y/sqrt(x)) == "y/sqrt(x)"
  427. assert str(x**0.5) == "x**0.5"
  428. assert str(1/x**0.5) == "x**(-0.5)"
  429. def test_Rational():
  430. n1 = Rational(1, 4)
  431. n2 = Rational(1, 3)
  432. n3 = Rational(2, 4)
  433. n4 = Rational(2, -4)
  434. n5 = Rational(0)
  435. n7 = Rational(3)
  436. n8 = Rational(-3)
  437. assert str(n1*n2) == "1/12"
  438. assert str(n1*n2) == "1/12"
  439. assert str(n3) == "1/2"
  440. assert str(n1*n3) == "1/8"
  441. assert str(n1 + n3) == "3/4"
  442. assert str(n1 + n2) == "7/12"
  443. assert str(n1 + n4) == "-1/4"
  444. assert str(n4*n4) == "1/4"
  445. assert str(n4 + n2) == "-1/6"
  446. assert str(n4 + n5) == "-1/2"
  447. assert str(n4*n5) == "0"
  448. assert str(n3 + n4) == "0"
  449. assert str(n1**n7) == "1/64"
  450. assert str(n2**n7) == "1/27"
  451. assert str(n2**n8) == "27"
  452. assert str(n7**n8) == "1/27"
  453. assert str(Rational("-25")) == "-25"
  454. assert str(Rational("1.25")) == "5/4"
  455. assert str(Rational("-2.6e-2")) == "-13/500"
  456. assert str(S("25/7")) == "25/7"
  457. assert str(S("-123/569")) == "-123/569"
  458. assert str(S("0.1[23]", rational=1)) == "61/495"
  459. assert str(S("5.1[666]", rational=1)) == "31/6"
  460. assert str(S("-5.1[666]", rational=1)) == "-31/6"
  461. assert str(S("0.[9]", rational=1)) == "1"
  462. assert str(S("-0.[9]", rational=1)) == "-1"
  463. assert str(sqrt(Rational(1, 4))) == "1/2"
  464. assert str(sqrt(Rational(1, 36))) == "1/6"
  465. assert str((123**25) ** Rational(1, 25)) == "123"
  466. assert str((123**25 + 1)**Rational(1, 25)) != "123"
  467. assert str((123**25 - 1)**Rational(1, 25)) != "123"
  468. assert str((123**25 - 1)**Rational(1, 25)) != "122"
  469. assert str(sqrt(Rational(81, 36))**3) == "27/8"
  470. assert str(1/sqrt(Rational(81, 36))**3) == "8/27"
  471. assert str(sqrt(-4)) == str(2*I)
  472. assert str(2**Rational(1, 10**10)) == "2**(1/10000000000)"
  473. assert sstr(Rational(2, 3), sympy_integers=True) == "S(2)/3"
  474. x = Symbol("x")
  475. assert sstr(x**Rational(2, 3), sympy_integers=True) == "x**(S(2)/3)"
  476. assert sstr(Eq(x, Rational(2, 3)), sympy_integers=True) == "Eq(x, S(2)/3)"
  477. assert sstr(Limit(x, x, Rational(7, 2)), sympy_integers=True) == \
  478. "Limit(x, x, S(7)/2)"
  479. def test_Float():
  480. # NOTE dps is the whole number of decimal digits
  481. assert str(Float('1.23', dps=1 + 2)) == '1.23'
  482. assert str(Float('1.23456789', dps=1 + 8)) == '1.23456789'
  483. assert str(
  484. Float('1.234567890123456789', dps=1 + 18)) == '1.234567890123456789'
  485. assert str(pi.evalf(1 + 2)) == '3.14'
  486. assert str(pi.evalf(1 + 14)) == '3.14159265358979'
  487. assert str(pi.evalf(1 + 64)) == ('3.141592653589793238462643383279'
  488. '5028841971693993751058209749445923')
  489. assert str(pi.round(-1)) == '0.0'
  490. assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88'
  491. assert sstr(Float("100"), full_prec=False, min=-2, max=2) == '1.0e+2'
  492. assert sstr(Float("100"), full_prec=False, min=-2, max=3) == '100.0'
  493. assert sstr(Float("0.1"), full_prec=False, min=-2, max=3) == '0.1'
  494. assert sstr(Float("0.099"), min=-2, max=3) == '9.90000000000000e-2'
  495. def test_Relational():
  496. assert str(Rel(x, y, "<")) == "x < y"
  497. assert str(Rel(x + y, y, "==")) == "Eq(x + y, y)"
  498. assert str(Rel(x, y, "!=")) == "Ne(x, y)"
  499. assert str(Eq(x, 1) | Eq(x, 2)) == "Eq(x, 1) | Eq(x, 2)"
  500. assert str(Ne(x, 1) & Ne(x, 2)) == "Ne(x, 1) & Ne(x, 2)"
  501. def test_AppliedBinaryRelation():
  502. assert str(Q.eq(x, y)) == "Q.eq(x, y)"
  503. assert str(Q.ne(x, y)) == "Q.ne(x, y)"
  504. def test_CRootOf():
  505. assert str(rootof(x**5 + 2*x - 1, 0)) == "CRootOf(x**5 + 2*x - 1, 0)"
  506. def test_RootSum():
  507. f = x**5 + 2*x - 1
  508. assert str(
  509. RootSum(f, Lambda(z, z), auto=False)) == "RootSum(x**5 + 2*x - 1)"
  510. assert str(RootSum(f, Lambda(
  511. z, z**2), auto=False)) == "RootSum(x**5 + 2*x - 1, Lambda(z, z**2))"
  512. def test_GroebnerBasis():
  513. assert str(groebner(
  514. [], x, y)) == "GroebnerBasis([], x, y, domain='ZZ', order='lex')"
  515. F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
  516. assert str(groebner(F, order='grlex')) == \
  517. "GroebnerBasis([x**2 - x - 3*y + 1, y**2 - 2*x + y - 1], x, y, domain='ZZ', order='grlex')"
  518. assert str(groebner(F, order='lex')) == \
  519. "GroebnerBasis([2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7], x, y, domain='ZZ', order='lex')"
  520. def test_set():
  521. assert sstr(set()) == 'set()'
  522. assert sstr(frozenset()) == 'frozenset()'
  523. assert sstr({1}) == '{1}'
  524. assert sstr(frozenset([1])) == 'frozenset({1})'
  525. assert sstr({1, 2, 3}) == '{1, 2, 3}'
  526. assert sstr(frozenset([1, 2, 3])) == 'frozenset({1, 2, 3})'
  527. assert sstr(
  528. {1, x, x**2, x**3, x**4}) == '{1, x, x**2, x**3, x**4}'
  529. assert sstr(
  530. frozenset([1, x, x**2, x**3, x**4])) == 'frozenset({1, x, x**2, x**3, x**4})'
  531. def test_SparseMatrix():
  532. M = SparseMatrix([[x**+1, 1], [y, x + y]])
  533. assert str(M) == "Matrix([[x, 1], [y, x + y]])"
  534. assert sstr(M) == "Matrix([\n[x, 1],\n[y, x + y]])"
  535. def test_Sum():
  536. assert str(summation(cos(3*z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))"
  537. assert str(Sum(x*y**2, (x, -2, 2), (y, -5, 5))) == \
  538. "Sum(x*y**2, (x, -2, 2), (y, -5, 5))"
  539. def test_Symbol():
  540. assert str(y) == "y"
  541. assert str(x) == "x"
  542. e = x
  543. assert str(e) == "x"
  544. def test_tuple():
  545. assert str((x,)) == sstr((x,)) == "(x,)"
  546. assert str((x + y, 1 + x)) == sstr((x + y, 1 + x)) == "(x + y, x + 1)"
  547. assert str((x + y, (
  548. 1 + x, x**2))) == sstr((x + y, (1 + x, x**2))) == "(x + y, (x + 1, x**2))"
  549. def test_Series_str():
  550. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  551. tf2 = TransferFunction(x - y, x + y, y)
  552. tf3 = TransferFunction(t*x**2 - t**w*x + w, t - y, y)
  553. assert str(Series(tf1, tf2)) == \
  554. "Series(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y))"
  555. assert str(Series(tf1, tf2, tf3)) == \
  556. "Series(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y), TransferFunction(t*x**2 - t**w*x + w, t - y, y))"
  557. assert str(Series(-tf2, tf1)) == \
  558. "Series(TransferFunction(-x + y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y))"
  559. def test_MIMOSeries_str():
  560. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  561. tf2 = TransferFunction(x - y, x + y, y)
  562. tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf2, tf1]])
  563. tfm_2 = TransferFunctionMatrix([[tf2, tf1], [tf1, tf2]])
  564. assert str(MIMOSeries(tfm_1, tfm_2)) == \
  565. "MIMOSeries(TransferFunctionMatrix(((TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)), "\
  566. "(TransferFunction(x - y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y)))), "\
  567. "TransferFunctionMatrix(((TransferFunction(x - y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y)), "\
  568. "(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)))))"
  569. def test_TransferFunction_str():
  570. tf1 = TransferFunction(x - 1, x + 1, x)
  571. assert str(tf1) == "TransferFunction(x - 1, x + 1, x)"
  572. tf2 = TransferFunction(x + 1, 2 - y, x)
  573. assert str(tf2) == "TransferFunction(x + 1, 2 - y, x)"
  574. tf3 = TransferFunction(y, y**2 + 2*y + 3, y)
  575. assert str(tf3) == "TransferFunction(y, y**2 + 2*y + 3, y)"
  576. def test_Parallel_str():
  577. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  578. tf2 = TransferFunction(x - y, x + y, y)
  579. tf3 = TransferFunction(t*x**2 - t**w*x + w, t - y, y)
  580. assert str(Parallel(tf1, tf2)) == \
  581. "Parallel(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y))"
  582. assert str(Parallel(tf1, tf2, tf3)) == \
  583. "Parallel(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y), TransferFunction(t*x**2 - t**w*x + w, t - y, y))"
  584. assert str(Parallel(-tf2, tf1)) == \
  585. "Parallel(TransferFunction(-x + y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y))"
  586. def test_MIMOParallel_str():
  587. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  588. tf2 = TransferFunction(x - y, x + y, y)
  589. tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf2, tf1]])
  590. tfm_2 = TransferFunctionMatrix([[tf2, tf1], [tf1, tf2]])
  591. assert str(MIMOParallel(tfm_1, tfm_2)) == \
  592. "MIMOParallel(TransferFunctionMatrix(((TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)), "\
  593. "(TransferFunction(x - y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y)))), "\
  594. "TransferFunctionMatrix(((TransferFunction(x - y, x + y, y), TransferFunction(x*y**2 - z, -t**3 + y**3, y)), "\
  595. "(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)))))"
  596. def test_Feedback_str():
  597. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  598. tf2 = TransferFunction(x - y, x + y, y)
  599. tf3 = TransferFunction(t*x**2 - t**w*x + w, t - y, y)
  600. assert str(Feedback(tf1*tf2, tf3)) == \
  601. "Feedback(Series(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)), " \
  602. "TransferFunction(t*x**2 - t**w*x + w, t - y, y), -1)"
  603. assert str(Feedback(tf1, TransferFunction(1, 1, y), 1)) == \
  604. "Feedback(TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(1, 1, y), 1)"
  605. def test_MIMOFeedback_str():
  606. tf1 = TransferFunction(x**2 - y**3, y - z, x)
  607. tf2 = TransferFunction(y - x, z + y, x)
  608. tfm_1 = TransferFunctionMatrix([[tf2, tf1], [tf1, tf2]])
  609. tfm_2 = TransferFunctionMatrix([[tf1, tf2], [tf2, tf1]])
  610. assert (str(MIMOFeedback(tfm_1, tfm_2)) \
  611. == "MIMOFeedback(TransferFunctionMatrix(((TransferFunction(-x + y, y + z, x), TransferFunction(x**2 - y**3, y - z, x))," \
  612. " (TransferFunction(x**2 - y**3, y - z, x), TransferFunction(-x + y, y + z, x)))), " \
  613. "TransferFunctionMatrix(((TransferFunction(x**2 - y**3, y - z, x), " \
  614. "TransferFunction(-x + y, y + z, x)), (TransferFunction(-x + y, y + z, x), TransferFunction(x**2 - y**3, y - z, x)))), -1)")
  615. assert (str(MIMOFeedback(tfm_1, tfm_2, 1)) \
  616. == "MIMOFeedback(TransferFunctionMatrix(((TransferFunction(-x + y, y + z, x), TransferFunction(x**2 - y**3, y - z, x)), " \
  617. "(TransferFunction(x**2 - y**3, y - z, x), TransferFunction(-x + y, y + z, x)))), " \
  618. "TransferFunctionMatrix(((TransferFunction(x**2 - y**3, y - z, x), TransferFunction(-x + y, y + z, x)), "\
  619. "(TransferFunction(-x + y, y + z, x), TransferFunction(x**2 - y**3, y - z, x)))), 1)")
  620. def test_TransferFunctionMatrix_str():
  621. tf1 = TransferFunction(x*y**2 - z, y**3 - t**3, y)
  622. tf2 = TransferFunction(x - y, x + y, y)
  623. tf3 = TransferFunction(t*x**2 - t**w*x + w, t - y, y)
  624. assert str(TransferFunctionMatrix([[tf1], [tf2]])) == \
  625. "TransferFunctionMatrix(((TransferFunction(x*y**2 - z, -t**3 + y**3, y),), (TransferFunction(x - y, x + y, y),)))"
  626. assert str(TransferFunctionMatrix([[tf1, tf2], [tf3, tf2]])) == \
  627. "TransferFunctionMatrix(((TransferFunction(x*y**2 - z, -t**3 + y**3, y), TransferFunction(x - y, x + y, y)), (TransferFunction(t*x**2 - t**w*x + w, t - y, y), TransferFunction(x - y, x + y, y))))"
  628. def test_Quaternion_str_printer():
  629. q = Quaternion(x, y, z, t)
  630. assert str(q) == "x + y*i + z*j + t*k"
  631. q = Quaternion(x,y,z,x*t)
  632. assert str(q) == "x + y*i + z*j + t*x*k"
  633. q = Quaternion(x,y,z,x+t)
  634. assert str(q) == "x + y*i + z*j + (t + x)*k"
  635. def test_Quantity_str():
  636. assert sstr(second, abbrev=True) == "s"
  637. assert sstr(joule, abbrev=True) == "J"
  638. assert str(second) == "second"
  639. assert str(joule) == "joule"
  640. def test_wild_str():
  641. # Check expressions containing Wild not causing infinite recursion
  642. w = Wild('x')
  643. assert str(w + 1) == 'x_ + 1'
  644. assert str(exp(2**w) + 5) == 'exp(2**x_) + 5'
  645. assert str(3*w + 1) == '3*x_ + 1'
  646. assert str(1/w + 1) == '1 + 1/x_'
  647. assert str(w**2 + 1) == 'x_**2 + 1'
  648. assert str(1/(1 - w)) == '1/(1 - x_)'
  649. def test_wild_matchpy():
  650. from sympy.utilities.matchpy_connector import WildDot, WildPlus, WildStar
  651. matchpy = import_module("matchpy")
  652. if matchpy is None:
  653. return
  654. wd = WildDot('w_')
  655. wp = WildPlus('w__')
  656. ws = WildStar('w___')
  657. assert str(wd) == 'w_'
  658. assert str(wp) == 'w__'
  659. assert str(ws) == 'w___'
  660. assert str(wp/ws + 2**wd) == '2**w_ + w__/w___'
  661. assert str(sin(wd)*cos(wp)*sqrt(ws)) == 'sqrt(w___)*sin(w_)*cos(w__)'
  662. def test_zeta():
  663. assert str(zeta(3)) == "zeta(3)"
  664. def test_issue_3101():
  665. e = x - y
  666. a = str(e)
  667. b = str(e)
  668. assert a == b
  669. def test_issue_3103():
  670. e = -2*sqrt(x) - y/sqrt(x)/2
  671. assert str(e) not in ["(-2)*x**1/2(-1/2)*x**(-1/2)*y",
  672. "-2*x**1/2(-1/2)*x**(-1/2)*y", "-2*x**1/2-1/2*x**-1/2*w"]
  673. assert str(e) == "-2*sqrt(x) - y/(2*sqrt(x))"
  674. def test_issue_4021():
  675. e = Integral(x, x) + 1
  676. assert str(e) == 'Integral(x, x) + 1'
  677. def test_sstrrepr():
  678. assert sstr('abc') == 'abc'
  679. assert sstrrepr('abc') == "'abc'"
  680. e = ['a', 'b', 'c', x]
  681. assert sstr(e) == "[a, b, c, x]"
  682. assert sstrrepr(e) == "['a', 'b', 'c', x]"
  683. def test_infinity():
  684. assert sstr(oo*I) == "oo*I"
  685. def test_full_prec():
  686. assert sstr(S("0.3"), full_prec=True) == "0.300000000000000"
  687. assert sstr(S("0.3"), full_prec="auto") == "0.300000000000000"
  688. assert sstr(S("0.3"), full_prec=False) == "0.3"
  689. assert sstr(S("0.3")*x, full_prec=True) in [
  690. "0.300000000000000*x",
  691. "x*0.300000000000000"
  692. ]
  693. assert sstr(S("0.3")*x, full_prec="auto") in [
  694. "0.3*x",
  695. "x*0.3"
  696. ]
  697. assert sstr(S("0.3")*x, full_prec=False) in [
  698. "0.3*x",
  699. "x*0.3"
  700. ]
  701. def test_noncommutative():
  702. A, B, C = symbols('A,B,C', commutative=False)
  703. assert sstr(A*B*C**-1) == "A*B*C**(-1)"
  704. assert sstr(C**-1*A*B) == "C**(-1)*A*B"
  705. assert sstr(A*C**-1*B) == "A*C**(-1)*B"
  706. assert sstr(sqrt(A)) == "sqrt(A)"
  707. assert sstr(1/sqrt(A)) == "A**(-1/2)"
  708. def test_empty_printer():
  709. str_printer = StrPrinter()
  710. assert str_printer.emptyPrinter("foo") == "foo"
  711. assert str_printer.emptyPrinter(x*y) == "x*y"
  712. assert str_printer.emptyPrinter(32) == "32"
  713. def test_settings():
  714. raises(TypeError, lambda: sstr(S(4), method="garbage"))
  715. def test_RandomDomain():
  716. from sympy.stats import Normal, Die, Exponential, pspace, where
  717. X = Normal('x1', 0, 1)
  718. assert str(where(X > 0)) == "Domain: (0 < x1) & (x1 < oo)"
  719. D = Die('d1', 6)
  720. assert str(where(D > 4)) == "Domain: Eq(d1, 5) | Eq(d1, 6)"
  721. A = Exponential('a', 1)
  722. B = Exponential('b', 1)
  723. assert str(pspace(Tuple(A, B)).domain) == "Domain: (0 <= a) & (0 <= b) & (a < oo) & (b < oo)"
  724. def test_FiniteSet():
  725. assert str(FiniteSet(*range(1, 51))) == (
  726. '{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,'
  727. ' 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,'
  728. ' 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}'
  729. )
  730. assert str(FiniteSet(*range(1, 6))) == '{1, 2, 3, 4, 5}'
  731. assert str(FiniteSet(*[x*y, x**2])) == '{x**2, x*y}'
  732. assert str(FiniteSet(FiniteSet(FiniteSet(x, y), 5), FiniteSet(x,y), 5)
  733. ) == 'FiniteSet(5, FiniteSet(5, {x, y}), {x, y})'
  734. def test_Partition():
  735. assert str(Partition(FiniteSet(x, y), {z})) == 'Partition({z}, {x, y})'
  736. def test_UniversalSet():
  737. assert str(S.UniversalSet) == 'UniversalSet'
  738. def test_PrettyPoly():
  739. F = QQ.frac_field(x, y)
  740. R = QQ[x, y]
  741. assert sstr(F.convert(x/(x + y))) == sstr(x/(x + y))
  742. assert sstr(R.convert(x + y)) == sstr(x + y)
  743. def test_categories():
  744. from sympy.categories import (Object, NamedMorphism,
  745. IdentityMorphism, Category)
  746. A = Object("A")
  747. B = Object("B")
  748. f = NamedMorphism(A, B, "f")
  749. id_A = IdentityMorphism(A)
  750. K = Category("K")
  751. assert str(A) == 'Object("A")'
  752. assert str(f) == 'NamedMorphism(Object("A"), Object("B"), "f")'
  753. assert str(id_A) == 'IdentityMorphism(Object("A"))'
  754. assert str(K) == 'Category("K")'
  755. def test_Tr():
  756. A, B = symbols('A B', commutative=False)
  757. t = Tr(A*B)
  758. assert str(t) == 'Tr(A*B)'
  759. def test_issue_6387():
  760. assert str(factor(-3.0*z + 3)) == '-3.0*(1.0*z - 1.0)'
  761. def test_MatMul_MatAdd():
  762. X, Y = MatrixSymbol("X", 2, 2), MatrixSymbol("Y", 2, 2)
  763. assert str(2*(X + Y)) == "2*X + 2*Y"
  764. assert str(I*X) == "I*X"
  765. assert str(-I*X) == "-I*X"
  766. assert str((1 + I)*X) == '(1 + I)*X'
  767. assert str(-(1 + I)*X) == '(-1 - I)*X'
  768. def test_MatrixSlice():
  769. n = Symbol('n', integer=True)
  770. X = MatrixSymbol('X', n, n)
  771. Y = MatrixSymbol('Y', 10, 10)
  772. Z = MatrixSymbol('Z', 10, 10)
  773. assert str(MatrixSlice(X, (None, None, None), (None, None, None))) == 'X[:, :]'
  774. assert str(X[x:x + 1, y:y + 1]) == 'X[x:x + 1, y:y + 1]'
  775. assert str(X[x:x + 1:2, y:y + 1:2]) == 'X[x:x + 1:2, y:y + 1:2]'
  776. assert str(X[:x, y:]) == 'X[:x, y:]'
  777. assert str(X[:x, y:]) == 'X[:x, y:]'
  778. assert str(X[x:, :y]) == 'X[x:, :y]'
  779. assert str(X[x:y, z:w]) == 'X[x:y, z:w]'
  780. assert str(X[x:y:t, w:t:x]) == 'X[x:y:t, w:t:x]'
  781. assert str(X[x::y, t::w]) == 'X[x::y, t::w]'
  782. assert str(X[:x:y, :t:w]) == 'X[:x:y, :t:w]'
  783. assert str(X[::x, ::y]) == 'X[::x, ::y]'
  784. assert str(MatrixSlice(X, (0, None, None), (0, None, None))) == 'X[:, :]'
  785. assert str(MatrixSlice(X, (None, n, None), (None, n, None))) == 'X[:, :]'
  786. assert str(MatrixSlice(X, (0, n, None), (0, n, None))) == 'X[:, :]'
  787. assert str(MatrixSlice(X, (0, n, 2), (0, n, 2))) == 'X[::2, ::2]'
  788. assert str(X[1:2:3, 4:5:6]) == 'X[1:2:3, 4:5:6]'
  789. assert str(X[1:3:5, 4:6:8]) == 'X[1:3:5, 4:6:8]'
  790. assert str(X[1:10:2]) == 'X[1:10:2, :]'
  791. assert str(Y[:5, 1:9:2]) == 'Y[:5, 1:9:2]'
  792. assert str(Y[:5, 1:10:2]) == 'Y[:5, 1::2]'
  793. assert str(Y[5, :5:2]) == 'Y[5:6, :5:2]'
  794. assert str(X[0:1, 0:1]) == 'X[:1, :1]'
  795. assert str(X[0:1:2, 0:1:2]) == 'X[:1:2, :1:2]'
  796. assert str((Y + Z)[2:, 2:]) == '(Y + Z)[2:, 2:]'
  797. def test_true_false():
  798. assert str(true) == repr(true) == sstr(true) == "True"
  799. assert str(false) == repr(false) == sstr(false) == "False"
  800. def test_Equivalent():
  801. assert str(Equivalent(y, x)) == "Equivalent(x, y)"
  802. def test_Xor():
  803. assert str(Xor(y, x, evaluate=False)) == "x ^ y"
  804. def test_Complement():
  805. assert str(Complement(S.Reals, S.Naturals)) == 'Complement(Reals, Naturals)'
  806. def test_SymmetricDifference():
  807. assert str(SymmetricDifference(Interval(2, 3), Interval(3, 4),evaluate=False)) == \
  808. 'SymmetricDifference(Interval(2, 3), Interval(3, 4))'
  809. def test_UnevaluatedExpr():
  810. a, b = symbols("a b")
  811. expr1 = 2*UnevaluatedExpr(a+b)
  812. assert str(expr1) == "2*(a + b)"
  813. def test_MatrixElement_printing():
  814. # test cases for issue #11821
  815. A = MatrixSymbol("A", 1, 3)
  816. B = MatrixSymbol("B", 1, 3)
  817. C = MatrixSymbol("C", 1, 3)
  818. assert(str(A[0, 0]) == "A[0, 0]")
  819. assert(str(3 * A[0, 0]) == "3*A[0, 0]")
  820. F = C[0, 0].subs(C, A - B)
  821. assert str(F) == "(A - B)[0, 0]"
  822. def test_MatrixSymbol_printing():
  823. A = MatrixSymbol("A", 3, 3)
  824. B = MatrixSymbol("B", 3, 3)
  825. assert str(A - A*B - B) == "A - A*B - B"
  826. assert str(A*B - (A+B)) == "-A + A*B - B"
  827. assert str(A**(-1)) == "A**(-1)"
  828. assert str(A**3) == "A**3"
  829. def test_MatrixExpressions():
  830. n = Symbol('n', integer=True)
  831. X = MatrixSymbol('X', n, n)
  832. assert str(X) == "X"
  833. # Apply function elementwise (`ElementwiseApplyFunc`):
  834. expr = (X.T*X).applyfunc(sin)
  835. assert str(expr) == 'Lambda(_d, sin(_d)).(X.T*X)'
  836. lamda = Lambda(x, 1/x)
  837. expr = (n*X).applyfunc(lamda)
  838. assert str(expr) == 'Lambda(x, 1/x).(n*X)'
  839. def test_Subs_printing():
  840. assert str(Subs(x, (x,), (1,))) == 'Subs(x, x, 1)'
  841. assert str(Subs(x + y, (x, y), (1, 2))) == 'Subs(x + y, (x, y), (1, 2))'
  842. def test_issue_15716():
  843. e = Integral(factorial(x), (x, -oo, oo))
  844. assert e.as_terms() == ([(e, ((1.0, 0.0), (1,), ()))], [e])
  845. def test_str_special_matrices():
  846. from sympy.matrices import Identity, ZeroMatrix, OneMatrix
  847. assert str(Identity(4)) == 'I'
  848. assert str(ZeroMatrix(2, 2)) == '0'
  849. assert str(OneMatrix(2, 2)) == '1'
  850. def test_issue_14567():
  851. assert factorial(Sum(-1, (x, 0, 0))) + y # doesn't raise an error
  852. def test_issue_21823():
  853. assert str(Partition([1, 2])) == 'Partition({1, 2})'
  854. assert str(Partition({1, 2})) == 'Partition({1, 2})'
  855. def test_issue_22689():
  856. assert str(Mul(Pow(x,-2, evaluate=False), Pow(3,-1,evaluate=False), evaluate=False)) == "1/(x**2*3)"
  857. def test_issue_21119_21460():
  858. ss = lambda x: str(S(x, evaluate=False))
  859. assert ss('4/2') == '4/2'
  860. assert ss('4/-2') == '4/(-2)'
  861. assert ss('-4/2') == '-4/2'
  862. assert ss('-4/-2') == '-4/(-2)'
  863. assert ss('-2*3/-1') == '-2*3/(-1)'
  864. assert ss('-2*3/-1/2') == '-2*3/(-1*2)'
  865. assert ss('4/2/1') == '4/(2*1)'
  866. assert ss('-2/-1/2') == '-2/(-1*2)'
  867. assert ss('2*3*4**(-2*3)') == '2*3/4**(2*3)'
  868. assert ss('2*3*1*4**(-2*3)') == '2*3*1/4**(2*3)'
  869. def test_Str():
  870. from sympy.core.symbol import Str
  871. assert str(Str('x')) == 'x'
  872. assert sstrrepr(Str('x')) == "Str('x')"
  873. def test_diffgeom():
  874. from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField
  875. x,y = symbols('x y', real=True)
  876. m = Manifold('M', 2)
  877. assert str(m) == "M"
  878. p = Patch('P', m)
  879. assert str(p) == "P"
  880. rect = CoordSystem('rect', p, [x, y])
  881. assert str(rect) == "rect"
  882. b = BaseScalarField(rect, 0)
  883. assert str(b) == "x"
  884. def test_NDimArray():
  885. assert sstr(NDimArray(1.0), full_prec=True) == '1.00000000000000'
  886. assert sstr(NDimArray(1.0), full_prec=False) == '1.0'
  887. assert sstr(NDimArray([1.0, 2.0]), full_prec=True) == '[1.00000000000000, 2.00000000000000]'
  888. assert sstr(NDimArray([1.0, 2.0]), full_prec=False) == '[1.0, 2.0]'
  889. def test_Predicate():
  890. assert sstr(Q.even) == 'Q.even'
  891. def test_AppliedPredicate():
  892. assert sstr(Q.even(x)) == 'Q.even(x)'
  893. def test_printing_str_array_expressions():
  894. assert sstr(ArraySymbol("A", (2, 3, 4))) == "A"
  895. assert sstr(ArrayElement("A", (2, 1/(1-x), 0))) == "A[2, 1/(1 - x), 0]"
  896. M = MatrixSymbol("M", 3, 3)
  897. N = MatrixSymbol("N", 3, 3)
  898. assert sstr(ArrayElement(M*N, [x, 0])) == "(M*N)[x, 0]"