test_str.py 41 KB

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