m2m模型翻译
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.

7747 lines
177 KiB

6 months ago
  1. # -*- coding: utf-8 -*-
  2. from sympy.concrete.products import Product
  3. from sympy.concrete.summations import Sum
  4. from sympy.core.add import Add
  5. from sympy.core.basic import Basic
  6. from sympy.core.containers import (Dict, Tuple)
  7. from sympy.core.function import (Derivative, Function, Lambda, Subs)
  8. from sympy.core.mul import Mul
  9. from sympy.core import (EulerGamma, GoldenRatio, Catalan)
  10. from sympy.core.numbers import (I, Rational, oo, pi)
  11. from sympy.core.power import Pow
  12. from sympy.core.relational import (Eq, Ge, Gt, Le, Lt, Ne)
  13. from sympy.core.singleton import S
  14. from sympy.core.symbol import (Symbol, symbols)
  15. from sympy.functions.elementary.complexes import conjugate
  16. from sympy.functions.elementary.exponential import LambertW
  17. from sympy.functions.special.bessel import (airyai, airyaiprime, airybi, airybiprime)
  18. from sympy.functions.special.delta_functions import Heaviside
  19. from sympy.functions.special.error_functions import (fresnelc, fresnels)
  20. from sympy.functions.special.singularity_functions import SingularityFunction
  21. from sympy.functions.special.zeta_functions import dirichlet_eta
  22. from sympy.geometry.line import (Ray, Segment)
  23. from sympy.integrals.integrals import Integral
  24. from sympy.logic.boolalg import (And, Equivalent, ITE, Implies, Nand, Nor, Not, Or, Xor)
  25. from sympy.matrices.dense import (Matrix, diag)
  26. from sympy.matrices.expressions.slice import MatrixSlice
  27. from sympy.matrices.expressions.trace import Trace
  28. from sympy.polys.domains.finitefield import FF
  29. from sympy.polys.domains.integerring import ZZ
  30. from sympy.polys.domains.rationalfield import QQ
  31. from sympy.polys.domains.realfield import RR
  32. from sympy.polys.orderings import (grlex, ilex)
  33. from sympy.polys.polytools import groebner
  34. from sympy.polys.rootoftools import (RootSum, rootof)
  35. from sympy.series.formal import fps
  36. from sympy.series.fourier import fourier_series
  37. from sympy.series.limits import Limit
  38. from sympy.series.order import O
  39. from sympy.series.sequences import (SeqAdd, SeqFormula, SeqMul, SeqPer)
  40. from sympy.sets.contains import Contains
  41. from sympy.sets.fancysets import Range
  42. from sympy.sets.sets import (Complement, FiniteSet, Intersection, Interval, Union)
  43. from sympy.codegen.ast import (Assignment, AddAugmentedAssignment,
  44. SubAugmentedAssignment, MulAugmentedAssignment, DivAugmentedAssignment, ModAugmentedAssignment)
  45. from sympy.core.expr import UnevaluatedExpr
  46. from sympy.physics.quantum.trace import Tr
  47. from sympy.functions import (Abs, Chi, Ci, Ei, KroneckerDelta,
  48. Piecewise, Shi, Si, atan2, beta, binomial, catalan, ceiling, cos,
  49. euler, exp, expint, factorial, factorial2, floor, gamma, hyper, log,
  50. meijerg, sin, sqrt, subfactorial, tan, uppergamma, lerchphi,
  51. elliptic_k, elliptic_f, elliptic_e, elliptic_pi, DiracDelta, bell,
  52. bernoulli, fibonacci, tribonacci, lucas, stieltjes, mathieuc, mathieus,
  53. mathieusprime, mathieucprime)
  54. from sympy.matrices import Adjoint, Inverse, MatrixSymbol, Transpose, KroneckerProduct
  55. from sympy.matrices.expressions import hadamard_power
  56. from sympy.physics import mechanics
  57. from sympy.physics.control.lti import (TransferFunction, Feedback, TransferFunctionMatrix,
  58. Series, Parallel, MIMOSeries, MIMOParallel, MIMOFeedback)
  59. from sympy.physics.units import joule, degree
  60. from sympy.printing.pretty import pprint, pretty as xpretty
  61. from sympy.printing.pretty.pretty_symbology import center_accent, is_combining
  62. from sympy.sets.conditionset import ConditionSet
  63. from sympy.sets import ImageSet, ProductSet
  64. from sympy.sets.setexpr import SetExpr
  65. from sympy.stats.crv_types import Normal
  66. from sympy.stats.symbolic_probability import (Covariance, Expectation,
  67. Probability, Variance)
  68. from sympy.tensor.array import (ImmutableDenseNDimArray, ImmutableSparseNDimArray,
  69. MutableDenseNDimArray, MutableSparseNDimArray, tensorproduct)
  70. from sympy.tensor.functions import TensorProduct
  71. from sympy.tensor.tensor import (TensorIndexType, tensor_indices, TensorHead,
  72. TensorElement, tensor_heads)
  73. from sympy.testing.pytest import raises, _both_exp_pow, warns_deprecated_sympy
  74. from sympy.vector import CoordSys3D, Gradient, Curl, Divergence, Dot, Cross, Laplacian
  75. import sympy as sym
  76. class lowergamma(sym.lowergamma):
  77. pass # testing notation inheritance by a subclass with same name
  78. a, b, c, d, x, y, z, k, n, s, p = symbols('a,b,c,d,x,y,z,k,n,s,p')
  79. f = Function("f")
  80. th = Symbol('theta')
  81. ph = Symbol('phi')
  82. """
  83. Expressions whose pretty-printing is tested here:
  84. (A '#' to the right of an expression indicates that its various acceptable
  85. orderings are accounted for by the tests.)
  86. BASIC EXPRESSIONS:
  87. oo
  88. (x**2)
  89. 1/x
  90. y*x**-2
  91. x**Rational(-5,2)
  92. (-2)**x
  93. Pow(3, 1, evaluate=False)
  94. (x**2 + x + 1) #
  95. 1-x #
  96. 1-2*x #
  97. x/y
  98. -x/y
  99. (x+2)/y #
  100. (1+x)*y #3
  101. -5*x/(x+10) # correct placement of negative sign
  102. 1 - Rational(3,2)*(x+1)
  103. -(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5) # issue 5524
  104. ORDERING:
  105. x**2 + x + 1
  106. 1 - x
  107. 1 - 2*x
  108. 2*x**4 + y**2 - x**2 + y**3
  109. RELATIONAL:
  110. Eq(x, y)
  111. Lt(x, y)
  112. Gt(x, y)
  113. Le(x, y)
  114. Ge(x, y)
  115. Ne(x/(y+1), y**2) #
  116. RATIONAL NUMBERS:
  117. y*x**-2
  118. y**Rational(3,2) * x**Rational(-5,2)
  119. sin(x)**3/tan(x)**2
  120. FUNCTIONS (ABS, CONJ, EXP, FUNCTION BRACES, FACTORIAL, FLOOR, CEILING):
  121. (2*x + exp(x)) #
  122. Abs(x)
  123. Abs(x/(x**2+1)) #
  124. Abs(1 / (y - Abs(x)))
  125. factorial(n)
  126. factorial(2*n)
  127. subfactorial(n)
  128. subfactorial(2*n)
  129. factorial(factorial(factorial(n)))
  130. factorial(n+1) #
  131. conjugate(x)
  132. conjugate(f(x+1)) #
  133. f(x)
  134. f(x, y)
  135. f(x/(y+1), y) #
  136. f(x**x**x**x**x**x)
  137. sin(x)**2
  138. conjugate(a+b*I)
  139. conjugate(exp(a+b*I))
  140. conjugate( f(1 + conjugate(f(x))) ) #
  141. f(x/(y+1), y) # denom of first arg
  142. floor(1 / (y - floor(x)))
  143. ceiling(1 / (y - ceiling(x)))
  144. SQRT:
  145. sqrt(2)
  146. 2**Rational(1,3)
  147. 2**Rational(1,1000)
  148. sqrt(x**2 + 1)
  149. (1 + sqrt(5))**Rational(1,3)
  150. 2**(1/x)
  151. sqrt(2+pi)
  152. (2+(1+x**2)/(2+x))**Rational(1,4)+(1+x**Rational(1,1000))/sqrt(3+x**2)
  153. DERIVATIVES:
  154. Derivative(log(x), x, evaluate=False)
  155. Derivative(log(x), x, evaluate=False) + x #
  156. Derivative(log(x) + x**2, x, y, evaluate=False)
  157. Derivative(2*x*y, y, x, evaluate=False) + x**2 #
  158. beta(alpha).diff(alpha)
  159. INTEGRALS:
  160. Integral(log(x), x)
  161. Integral(x**2, x)
  162. Integral((sin(x))**2 / (tan(x))**2)
  163. Integral(x**(2**x), x)
  164. Integral(x**2, (x,1,2))
  165. Integral(x**2, (x,Rational(1,2),10))
  166. Integral(x**2*y**2, x,y)
  167. Integral(x**2, (x, None, 1))
  168. Integral(x**2, (x, 1, None))
  169. Integral(sin(th)/cos(ph), (th,0,pi), (ph, 0, 2*pi))
  170. MATRICES:
  171. Matrix([[x**2+1, 1], [y, x+y]]) #
  172. Matrix([[x/y, y, th], [0, exp(I*k*ph), 1]])
  173. PIECEWISE:
  174. Piecewise((x,x<1),(x**2,True))
  175. ITE:
  176. ITE(x, y, z)
  177. SEQUENCES (TUPLES, LISTS, DICTIONARIES):
  178. ()
  179. []
  180. {}
  181. (1/x,)
  182. [x**2, 1/x, x, y, sin(th)**2/cos(ph)**2]
  183. (x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  184. {x: sin(x)}
  185. {1/x: 1/y, x: sin(x)**2} #
  186. [x**2]
  187. (x**2,)
  188. {x**2: 1}
  189. LIMITS:
  190. Limit(x, x, oo)
  191. Limit(x**2, x, 0)
  192. Limit(1/x, x, 0)
  193. Limit(sin(x)/x, x, 0)
  194. UNITS:
  195. joule => kg*m**2/s
  196. SUBS:
  197. Subs(f(x), x, ph**2)
  198. Subs(f(x).diff(x), x, 0)
  199. Subs(f(x).diff(x)/y, (x, y), (0, Rational(1, 2)))
  200. ORDER:
  201. O(1)
  202. O(1/x)
  203. O(x**2 + y**2)
  204. """
  205. def pretty(expr, order=None):
  206. """ASCII pretty-printing"""
  207. return xpretty(expr, order=order, use_unicode=False, wrap_line=False)
  208. def upretty(expr, order=None):
  209. """Unicode pretty-printing"""
  210. return xpretty(expr, order=order, use_unicode=True, wrap_line=False)
  211. def test_pretty_ascii_str():
  212. assert pretty( 'xxx' ) == 'xxx'
  213. assert pretty( "xxx" ) == 'xxx'
  214. assert pretty( 'xxx\'xxx' ) == 'xxx\'xxx'
  215. assert pretty( 'xxx"xxx' ) == 'xxx\"xxx'
  216. assert pretty( 'xxx\"xxx' ) == 'xxx\"xxx'
  217. assert pretty( "xxx'xxx" ) == 'xxx\'xxx'
  218. assert pretty( "xxx\'xxx" ) == 'xxx\'xxx'
  219. assert pretty( "xxx\"xxx" ) == 'xxx\"xxx'
  220. assert pretty( "xxx\"xxx\'xxx" ) == 'xxx"xxx\'xxx'
  221. assert pretty( "xxx\nxxx" ) == 'xxx\nxxx'
  222. def test_pretty_unicode_str():
  223. assert pretty( 'xxx' ) == 'xxx'
  224. assert pretty( 'xxx' ) == 'xxx'
  225. assert pretty( 'xxx\'xxx' ) == 'xxx\'xxx'
  226. assert pretty( 'xxx"xxx' ) == 'xxx\"xxx'
  227. assert pretty( 'xxx\"xxx' ) == 'xxx\"xxx'
  228. assert pretty( "xxx'xxx" ) == 'xxx\'xxx'
  229. assert pretty( "xxx\'xxx" ) == 'xxx\'xxx'
  230. assert pretty( "xxx\"xxx" ) == 'xxx\"xxx'
  231. assert pretty( "xxx\"xxx\'xxx" ) == 'xxx"xxx\'xxx'
  232. assert pretty( "xxx\nxxx" ) == 'xxx\nxxx'
  233. def test_upretty_greek():
  234. assert upretty( oo ) == ''
  235. assert upretty( Symbol('alpha^+_1') ) == 'α⁺₁'
  236. assert upretty( Symbol('beta') ) == 'β'
  237. assert upretty(Symbol('lambda')) == 'λ'
  238. def test_upretty_multiindex():
  239. assert upretty( Symbol('beta12') ) == 'β₁₂'
  240. assert upretty( Symbol('Y00') ) == 'Y₀₀'
  241. assert upretty( Symbol('Y_00') ) == 'Y₀₀'
  242. assert upretty( Symbol('F^+-') ) == 'F⁺⁻'
  243. def test_upretty_sub_super():
  244. assert upretty( Symbol('beta_1_2') ) == 'β₁ ₂'
  245. assert upretty( Symbol('beta^1^2') ) == 'β¹ ²'
  246. assert upretty( Symbol('beta_1^2') ) == 'β²₁'
  247. assert upretty( Symbol('beta_10_20') ) == 'β₁₀ ₂₀'
  248. assert upretty( Symbol('beta_ax_gamma^i') ) == 'βⁱₐₓ ᵧ'
  249. assert upretty( Symbol("F^1^2_3_4") ) == 'F¹ ²₃ ₄'
  250. assert upretty( Symbol("F_1_2^3^4") ) == 'F³ ⁴₁ ₂'
  251. assert upretty( Symbol("F_1_2_3_4") ) == 'F₁ ₂ ₃ ₄'
  252. assert upretty( Symbol("F^1^2^3^4") ) == 'F¹ ² ³ ⁴'
  253. def test_upretty_subs_missing_in_24():
  254. assert upretty( Symbol('F_beta') ) == 'Fᵦ'
  255. assert upretty( Symbol('F_gamma') ) == 'Fᵧ'
  256. assert upretty( Symbol('F_rho') ) == 'Fᵨ'
  257. assert upretty( Symbol('F_phi') ) == 'Fᵩ'
  258. assert upretty( Symbol('F_chi') ) == 'Fᵪ'
  259. assert upretty( Symbol('F_a') ) == 'Fₐ'
  260. assert upretty( Symbol('F_e') ) == 'Fₑ'
  261. assert upretty( Symbol('F_i') ) == 'Fᵢ'
  262. assert upretty( Symbol('F_o') ) == 'Fₒ'
  263. assert upretty( Symbol('F_u') ) == 'Fᵤ'
  264. assert upretty( Symbol('F_r') ) == 'Fᵣ'
  265. assert upretty( Symbol('F_v') ) == 'Fᵥ'
  266. assert upretty( Symbol('F_x') ) == 'Fₓ'
  267. def test_missing_in_2X_issue_9047():
  268. assert upretty( Symbol('F_h') ) == 'Fₕ'
  269. assert upretty( Symbol('F_k') ) == 'Fₖ'
  270. assert upretty( Symbol('F_l') ) == 'Fₗ'
  271. assert upretty( Symbol('F_m') ) == 'Fₘ'
  272. assert upretty( Symbol('F_n') ) == 'Fₙ'
  273. assert upretty( Symbol('F_p') ) == 'Fₚ'
  274. assert upretty( Symbol('F_s') ) == 'Fₛ'
  275. assert upretty( Symbol('F_t') ) == 'Fₜ'
  276. def test_upretty_modifiers():
  277. # Accents
  278. assert upretty( Symbol('Fmathring') ) == ''
  279. assert upretty( Symbol('Fddddot') ) == 'F⃜'
  280. assert upretty( Symbol('Fdddot') ) == 'F⃛'
  281. assert upretty( Symbol('Fddot') ) == ''
  282. assert upretty( Symbol('Fdot') ) == ''
  283. assert upretty( Symbol('Fcheck') ) == ''
  284. assert upretty( Symbol('Fbreve') ) == ''
  285. assert upretty( Symbol('Facute') ) == ''
  286. assert upretty( Symbol('Fgrave') ) == ''
  287. assert upretty( Symbol('Ftilde') ) == ''
  288. assert upretty( Symbol('Fhat') ) == ''
  289. assert upretty( Symbol('Fbar') ) == ''
  290. assert upretty( Symbol('Fvec') ) == 'F⃗'
  291. assert upretty( Symbol('Fprime') ) == 'F′'
  292. assert upretty( Symbol('Fprm') ) == 'F′'
  293. # No faces are actually implemented, but test to make sure the modifiers are stripped
  294. assert upretty( Symbol('Fbold') ) == 'Fbold'
  295. assert upretty( Symbol('Fbm') ) == 'Fbm'
  296. assert upretty( Symbol('Fcal') ) == 'Fcal'
  297. assert upretty( Symbol('Fscr') ) == 'Fscr'
  298. assert upretty( Symbol('Ffrak') ) == 'Ffrak'
  299. # Brackets
  300. assert upretty( Symbol('Fnorm') ) == '‖F‖'
  301. assert upretty( Symbol('Favg') ) == '⟨F⟩'
  302. assert upretty( Symbol('Fabs') ) == '|F|'
  303. assert upretty( Symbol('Fmag') ) == '|F|'
  304. # Combinations
  305. assert upretty( Symbol('xvecdot') ) == 'x⃗̇'
  306. assert upretty( Symbol('xDotVec') ) == 'ẋ⃗'
  307. assert upretty( Symbol('xHATNorm') ) == '‖x̂‖'
  308. assert upretty( Symbol('xMathring_yCheckPRM__zbreveAbs') ) == 'x̊_y̌′__|z̆|'
  309. assert upretty( Symbol('alphadothat_nVECDOT__tTildePrime') ) == 'α̇̂_n⃗̇__t̃′'
  310. assert upretty( Symbol('x_dot') ) == 'x_dot'
  311. assert upretty( Symbol('x__dot') ) == 'x__dot'
  312. def test_pretty_Cycle():
  313. from sympy.combinatorics.permutations import Cycle
  314. assert pretty(Cycle(1, 2)) == '(1 2)'
  315. assert pretty(Cycle(2)) == '(2)'
  316. assert pretty(Cycle(1, 3)(4, 5)) == '(1 3)(4 5)'
  317. assert pretty(Cycle()) == '()'
  318. def test_pretty_Permutation():
  319. from sympy.combinatorics.permutations import Permutation
  320. p1 = Permutation(1, 2)(3, 4)
  321. assert xpretty(p1, perm_cyclic=True, use_unicode=True) == "(1 2)(3 4)"
  322. assert xpretty(p1, perm_cyclic=True, use_unicode=False) == "(1 2)(3 4)"
  323. assert xpretty(p1, perm_cyclic=False, use_unicode=True) == \
  324. '⎛0 1 2 3 4⎞\n'\
  325. '⎝0 2 1 4 3⎠'
  326. assert xpretty(p1, perm_cyclic=False, use_unicode=False) == \
  327. "/0 1 2 3 4\\\n"\
  328. "\\0 2 1 4 3/"
  329. with warns_deprecated_sympy():
  330. old_print_cyclic = Permutation.print_cyclic
  331. Permutation.print_cyclic = False
  332. assert xpretty(p1, use_unicode=True) == \
  333. '⎛0 1 2 3 4⎞\n'\
  334. '⎝0 2 1 4 3⎠'
  335. assert xpretty(p1, use_unicode=False) == \
  336. "/0 1 2 3 4\\\n"\
  337. "\\0 2 1 4 3/"
  338. Permutation.print_cyclic = old_print_cyclic
  339. def test_pretty_basic():
  340. assert pretty( -Rational(1)/2 ) == '-1/2'
  341. assert pretty( -Rational(13)/22 ) == \
  342. """\
  343. -13 \n\
  344. ----\n\
  345. 22 \
  346. """
  347. expr = oo
  348. ascii_str = \
  349. """\
  350. oo\
  351. """
  352. ucode_str = \
  353. """\
  354. \
  355. """
  356. assert pretty(expr) == ascii_str
  357. assert upretty(expr) == ucode_str
  358. expr = (x**2)
  359. ascii_str = \
  360. """\
  361. 2\n\
  362. x \
  363. """
  364. ucode_str = \
  365. """\
  366. 2\n\
  367. x \
  368. """
  369. assert pretty(expr) == ascii_str
  370. assert upretty(expr) == ucode_str
  371. expr = 1/x
  372. ascii_str = \
  373. """\
  374. 1\n\
  375. -\n\
  376. x\
  377. """
  378. ucode_str = \
  379. """\
  380. 1\n\
  381. \n\
  382. x\
  383. """
  384. assert pretty(expr) == ascii_str
  385. assert upretty(expr) == ucode_str
  386. # not the same as 1/x
  387. expr = x**-1.0
  388. ascii_str = \
  389. """\
  390. -1.0\n\
  391. x \
  392. """
  393. ucode_str = \
  394. """\
  395. -1.0\n\
  396. x \
  397. """
  398. assert pretty(expr) == ascii_str
  399. assert upretty(expr) == ucode_str
  400. # see issue #2860
  401. expr = Pow(S(2), -1.0, evaluate=False)
  402. ascii_str = \
  403. """\
  404. -1.0\n\
  405. 2 \
  406. """
  407. ucode_str = \
  408. """\
  409. -1.0\n\
  410. 2 \
  411. """
  412. assert pretty(expr) == ascii_str
  413. assert upretty(expr) == ucode_str
  414. expr = y*x**-2
  415. ascii_str = \
  416. """\
  417. y \n\
  418. --\n\
  419. 2\n\
  420. x \
  421. """
  422. ucode_str = \
  423. """\
  424. y \n\
  425. \n\
  426. 2\n\
  427. x \
  428. """
  429. assert pretty(expr) == ascii_str
  430. assert upretty(expr) == ucode_str
  431. #see issue #14033
  432. expr = x**Rational(1, 3)
  433. ascii_str = \
  434. """\
  435. 1/3\n\
  436. x \
  437. """
  438. ucode_str = \
  439. """\
  440. 1/3\n\
  441. x \
  442. """
  443. assert xpretty(expr, use_unicode=False, wrap_line=False,\
  444. root_notation = False) == ascii_str
  445. assert xpretty(expr, use_unicode=True, wrap_line=False,\
  446. root_notation = False) == ucode_str
  447. expr = x**Rational(-5, 2)
  448. ascii_str = \
  449. """\
  450. 1 \n\
  451. ----\n\
  452. 5/2\n\
  453. x \
  454. """
  455. ucode_str = \
  456. """\
  457. 1 \n\
  458. \n\
  459. 5/2\n\
  460. x \
  461. """
  462. assert pretty(expr) == ascii_str
  463. assert upretty(expr) == ucode_str
  464. expr = (-2)**x
  465. ascii_str = \
  466. """\
  467. x\n\
  468. (-2) \
  469. """
  470. ucode_str = \
  471. """\
  472. x\n\
  473. (-2) \
  474. """
  475. assert pretty(expr) == ascii_str
  476. assert upretty(expr) == ucode_str
  477. # See issue 4923
  478. expr = Pow(3, 1, evaluate=False)
  479. ascii_str = \
  480. """\
  481. 1\n\
  482. 3 \
  483. """
  484. ucode_str = \
  485. """\
  486. 1\n\
  487. 3 \
  488. """
  489. assert pretty(expr) == ascii_str
  490. assert upretty(expr) == ucode_str
  491. expr = (x**2 + x + 1)
  492. ascii_str_1 = \
  493. """\
  494. 2\n\
  495. 1 + x + x \
  496. """
  497. ascii_str_2 = \
  498. """\
  499. 2 \n\
  500. x + x + 1\
  501. """
  502. ascii_str_3 = \
  503. """\
  504. 2 \n\
  505. x + 1 + x\
  506. """
  507. ucode_str_1 = \
  508. """\
  509. 2\n\
  510. 1 + x + x \
  511. """
  512. ucode_str_2 = \
  513. """\
  514. 2 \n\
  515. x + x + 1\
  516. """
  517. ucode_str_3 = \
  518. """\
  519. 2 \n\
  520. x + 1 + x\
  521. """
  522. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  523. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  524. expr = 1 - x
  525. ascii_str_1 = \
  526. """\
  527. 1 - x\
  528. """
  529. ascii_str_2 = \
  530. """\
  531. -x + 1\
  532. """
  533. ucode_str_1 = \
  534. """\
  535. 1 - x\
  536. """
  537. ucode_str_2 = \
  538. """\
  539. -x + 1\
  540. """
  541. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  542. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  543. expr = 1 - 2*x
  544. ascii_str_1 = \
  545. """\
  546. 1 - 2*x\
  547. """
  548. ascii_str_2 = \
  549. """\
  550. -2*x + 1\
  551. """
  552. ucode_str_1 = \
  553. """\
  554. 1 - 2x\
  555. """
  556. ucode_str_2 = \
  557. """\
  558. -2x + 1\
  559. """
  560. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  561. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  562. expr = x/y
  563. ascii_str = \
  564. """\
  565. x\n\
  566. -\n\
  567. y\
  568. """
  569. ucode_str = \
  570. """\
  571. x\n\
  572. \n\
  573. y\
  574. """
  575. assert pretty(expr) == ascii_str
  576. assert upretty(expr) == ucode_str
  577. expr = -x/y
  578. ascii_str = \
  579. """\
  580. -x \n\
  581. ---\n\
  582. y \
  583. """
  584. ucode_str = \
  585. """\
  586. -x \n\
  587. \n\
  588. y \
  589. """
  590. assert pretty(expr) == ascii_str
  591. assert upretty(expr) == ucode_str
  592. expr = (x + 2)/y
  593. ascii_str_1 = \
  594. """\
  595. 2 + x\n\
  596. -----\n\
  597. y \
  598. """
  599. ascii_str_2 = \
  600. """\
  601. x + 2\n\
  602. -----\n\
  603. y \
  604. """
  605. ucode_str_1 = \
  606. """\
  607. 2 + x\n\
  608. \n\
  609. y \
  610. """
  611. ucode_str_2 = \
  612. """\
  613. x + 2\n\
  614. \n\
  615. y \
  616. """
  617. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  618. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  619. expr = (1 + x)*y
  620. ascii_str_1 = \
  621. """\
  622. y*(1 + x)\
  623. """
  624. ascii_str_2 = \
  625. """\
  626. (1 + x)*y\
  627. """
  628. ascii_str_3 = \
  629. """\
  630. y*(x + 1)\
  631. """
  632. ucode_str_1 = \
  633. """\
  634. y(1 + x)\
  635. """
  636. ucode_str_2 = \
  637. """\
  638. (1 + x)y\
  639. """
  640. ucode_str_3 = \
  641. """\
  642. y(x + 1)\
  643. """
  644. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  645. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  646. # Test for correct placement of the negative sign
  647. expr = -5*x/(x + 10)
  648. ascii_str_1 = \
  649. """\
  650. -5*x \n\
  651. ------\n\
  652. 10 + x\
  653. """
  654. ascii_str_2 = \
  655. """\
  656. -5*x \n\
  657. ------\n\
  658. x + 10\
  659. """
  660. ucode_str_1 = \
  661. """\
  662. -5x \n\
  663. \n\
  664. 10 + x\
  665. """
  666. ucode_str_2 = \
  667. """\
  668. -5x \n\
  669. \n\
  670. x + 10\
  671. """
  672. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  673. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  674. expr = -S.Half - 3*x
  675. ascii_str = \
  676. """\
  677. -3*x - 1/2\
  678. """
  679. ucode_str = \
  680. """\
  681. -3x - 1/2\
  682. """
  683. assert pretty(expr) == ascii_str
  684. assert upretty(expr) == ucode_str
  685. expr = S.Half - 3*x
  686. ascii_str = \
  687. """\
  688. 1/2 - 3*x\
  689. """
  690. ucode_str = \
  691. """\
  692. 1/2 - 3x\
  693. """
  694. assert pretty(expr) == ascii_str
  695. assert upretty(expr) == ucode_str
  696. expr = -S.Half - 3*x/2
  697. ascii_str = \
  698. """\
  699. 3*x 1\n\
  700. - --- - -\n\
  701. 2 2\
  702. """
  703. ucode_str = \
  704. """\
  705. 3x 1\n\
  706. - - \n\
  707. 2 2\
  708. """
  709. assert pretty(expr) == ascii_str
  710. assert upretty(expr) == ucode_str
  711. expr = S.Half - 3*x/2
  712. ascii_str = \
  713. """\
  714. 1 3*x\n\
  715. - - ---\n\
  716. 2 2 \
  717. """
  718. ucode_str = \
  719. """\
  720. 1 3x\n\
  721. - \n\
  722. 2 2 \
  723. """
  724. assert pretty(expr) == ascii_str
  725. assert upretty(expr) == ucode_str
  726. def test_negative_fractions():
  727. expr = -x/y
  728. ascii_str =\
  729. """\
  730. -x \n\
  731. ---\n\
  732. y \
  733. """
  734. ucode_str =\
  735. """\
  736. -x \n\
  737. \n\
  738. y \
  739. """
  740. assert pretty(expr) == ascii_str
  741. assert upretty(expr) == ucode_str
  742. expr = -x*z/y
  743. ascii_str =\
  744. """\
  745. -x*z \n\
  746. -----\n\
  747. y \
  748. """
  749. ucode_str =\
  750. """\
  751. -xz \n\
  752. \n\
  753. y \
  754. """
  755. assert pretty(expr) == ascii_str
  756. assert upretty(expr) == ucode_str
  757. expr = x**2/y
  758. ascii_str =\
  759. """\
  760. 2\n\
  761. x \n\
  762. --\n\
  763. y \
  764. """
  765. ucode_str =\
  766. """\
  767. 2\n\
  768. x \n\
  769. \n\
  770. y \
  771. """
  772. assert pretty(expr) == ascii_str
  773. assert upretty(expr) == ucode_str
  774. expr = -x**2/y
  775. ascii_str =\
  776. """\
  777. 2 \n\
  778. -x \n\
  779. ----\n\
  780. y \
  781. """
  782. ucode_str =\
  783. """\
  784. 2 \n\
  785. -x \n\
  786. \n\
  787. y \
  788. """
  789. assert pretty(expr) == ascii_str
  790. assert upretty(expr) == ucode_str
  791. expr = -x/(y*z)
  792. ascii_str =\
  793. """\
  794. -x \n\
  795. ---\n\
  796. y*z\
  797. """
  798. ucode_str =\
  799. """\
  800. -x \n\
  801. \n\
  802. yz\
  803. """
  804. assert pretty(expr) == ascii_str
  805. assert upretty(expr) == ucode_str
  806. expr = -a/y**2
  807. ascii_str =\
  808. """\
  809. -a \n\
  810. ---\n\
  811. 2\n\
  812. y \
  813. """
  814. ucode_str =\
  815. """\
  816. -a \n\
  817. \n\
  818. 2\n\
  819. y \
  820. """
  821. assert pretty(expr) == ascii_str
  822. assert upretty(expr) == ucode_str
  823. expr = y**(-a/b)
  824. ascii_str =\
  825. """\
  826. -a \n\
  827. ---\n\
  828. b \n\
  829. y \
  830. """
  831. ucode_str =\
  832. """\
  833. -a \n\
  834. \n\
  835. b \n\
  836. y \
  837. """
  838. assert pretty(expr) == ascii_str
  839. assert upretty(expr) == ucode_str
  840. expr = -1/y**2
  841. ascii_str =\
  842. """\
  843. -1 \n\
  844. ---\n\
  845. 2\n\
  846. y \
  847. """
  848. ucode_str =\
  849. """\
  850. -1 \n\
  851. \n\
  852. 2\n\
  853. y \
  854. """
  855. assert pretty(expr) == ascii_str
  856. assert upretty(expr) == ucode_str
  857. expr = -10/b**2
  858. ascii_str =\
  859. """\
  860. -10 \n\
  861. ----\n\
  862. 2 \n\
  863. b \
  864. """
  865. ucode_str =\
  866. """\
  867. -10 \n\
  868. \n\
  869. 2 \n\
  870. b \
  871. """
  872. assert pretty(expr) == ascii_str
  873. assert upretty(expr) == ucode_str
  874. expr = Rational(-200, 37)
  875. ascii_str =\
  876. """\
  877. -200 \n\
  878. -----\n\
  879. 37 \
  880. """
  881. ucode_str =\
  882. """\
  883. -200 \n\
  884. \n\
  885. 37 \
  886. """
  887. assert pretty(expr) == ascii_str
  888. assert upretty(expr) == ucode_str
  889. def test_Mul():
  890. expr = Mul(0, 1, evaluate=False)
  891. assert pretty(expr) == "0*1"
  892. assert upretty(expr) == "0⋅1"
  893. expr = Mul(1, 0, evaluate=False)
  894. assert pretty(expr) == "1*0"
  895. assert upretty(expr) == "1⋅0"
  896. expr = Mul(1, 1, evaluate=False)
  897. assert pretty(expr) == "1*1"
  898. assert upretty(expr) == "1⋅1"
  899. expr = Mul(1, 1, 1, evaluate=False)
  900. assert pretty(expr) == "1*1*1"
  901. assert upretty(expr) == "1⋅1⋅1"
  902. expr = Mul(1, 2, evaluate=False)
  903. assert pretty(expr) == "1*2"
  904. assert upretty(expr) == "1⋅2"
  905. expr = Add(0, 1, evaluate=False)
  906. assert pretty(expr) == "0 + 1"
  907. assert upretty(expr) == "0 + 1"
  908. expr = Mul(1, 1, 2, evaluate=False)
  909. assert pretty(expr) == "1*1*2"
  910. assert upretty(expr) == "1⋅1⋅2"
  911. expr = Add(0, 0, 1, evaluate=False)
  912. assert pretty(expr) == "0 + 0 + 1"
  913. assert upretty(expr) == "0 + 0 + 1"
  914. expr = Mul(1, -1, evaluate=False)
  915. assert pretty(expr) == "1*-1"
  916. assert upretty(expr) == "1⋅-1"
  917. expr = Mul(1.0, x, evaluate=False)
  918. assert pretty(expr) == "1.0*x"
  919. assert upretty(expr) == "1.0⋅x"
  920. expr = Mul(1, 1, 2, 3, x, evaluate=False)
  921. assert pretty(expr) == "1*1*2*3*x"
  922. assert upretty(expr) == "1⋅1⋅2⋅3⋅x"
  923. expr = Mul(-1, 1, evaluate=False)
  924. assert pretty(expr) == "-1*1"
  925. assert upretty(expr) == "-1⋅1"
  926. expr = Mul(4, 3, 2, 1, 0, y, x, evaluate=False)
  927. assert pretty(expr) == "4*3*2*1*0*y*x"
  928. assert upretty(expr) == "4⋅3⋅2⋅1⋅0⋅y⋅x"
  929. expr = Mul(4, 3, 2, 1+z, 0, y, x, evaluate=False)
  930. assert pretty(expr) == "4*3*2*(z + 1)*0*y*x"
  931. assert upretty(expr) == "4⋅3⋅2⋅(z + 1)⋅0⋅y⋅x"
  932. expr = Mul(Rational(2, 3), Rational(5, 7), evaluate=False)
  933. assert pretty(expr) == "2/3*5/7"
  934. assert upretty(expr) == "2/3⋅5/7"
  935. expr = Mul(x + y, Rational(1, 2), evaluate=False)
  936. assert pretty(expr) == "(x + y)*1/2"
  937. assert upretty(expr) == "(x + y)⋅1/2"
  938. expr = Mul(Rational(1, 2), x + y, evaluate=False)
  939. assert pretty(expr) == "x + y\n-----\n 2 "
  940. assert upretty(expr) == "x + y\n─────\n 2 "
  941. expr = Mul(S.One, x + y, evaluate=False)
  942. assert pretty(expr) == "1*(x + y)"
  943. assert upretty(expr) == "1⋅(x + y)"
  944. expr = Mul(x - y, S.One, evaluate=False)
  945. assert pretty(expr) == "(x - y)*1"
  946. assert upretty(expr) == "(x - y)⋅1"
  947. expr = Mul(Rational(1, 2), x - y, S.One, x + y, evaluate=False)
  948. assert pretty(expr) == "1/2*(x - y)*1*(x + y)"
  949. assert upretty(expr) == "1/2⋅(x - y)⋅1⋅(x + y)"
  950. expr = Mul(x + y, Rational(3, 4), S.One, y - z, evaluate=False)
  951. assert pretty(expr) == "(x + y)*3/4*1*(y - z)"
  952. assert upretty(expr) == "(x + y)⋅3/4⋅1⋅(y - z)"
  953. expr = Mul(x + y, Rational(1, 1), Rational(3, 4), Rational(5, 6),evaluate=False)
  954. assert pretty(expr) == "(x + y)*1*3/4*5/6"
  955. assert upretty(expr) == "(x + y)⋅1⋅3/4⋅5/6"
  956. expr = Mul(Rational(3, 4), x + y, S.One, y - z, evaluate=False)
  957. assert pretty(expr) == "3/4*(x + y)*1*(y - z)"
  958. assert upretty(expr) == "3/4⋅(x + y)⋅1⋅(y - z)"
  959. def test_issue_5524():
  960. assert pretty(-(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5)) == \
  961. """\
  962. 2 / ___ \\\n\
  963. - (5 - y) + (x - 5)*\\-x - 2*\\/ 2 + 5/\
  964. """
  965. assert upretty(-(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5)) == \
  966. """\
  967. 2 \n\
  968. - (5 - y) + (x - 5)(-x - 22 + 5)\
  969. """
  970. def test_pretty_ordering():
  971. assert pretty(x**2 + x + 1, order='lex') == \
  972. """\
  973. 2 \n\
  974. x + x + 1\
  975. """
  976. assert pretty(x**2 + x + 1, order='rev-lex') == \
  977. """\
  978. 2\n\
  979. 1 + x + x \
  980. """
  981. assert pretty(1 - x, order='lex') == '-x + 1'
  982. assert pretty(1 - x, order='rev-lex') == '1 - x'
  983. assert pretty(1 - 2*x, order='lex') == '-2*x + 1'
  984. assert pretty(1 - 2*x, order='rev-lex') == '1 - 2*x'
  985. f = 2*x**4 + y**2 - x**2 + y**3
  986. assert pretty(f, order=None) == \
  987. """\
  988. 4 2 3 2\n\
  989. 2*x - x + y + y \
  990. """
  991. assert pretty(f, order='lex') == \
  992. """\
  993. 4 2 3 2\n\
  994. 2*x - x + y + y \
  995. """
  996. assert pretty(f, order='rev-lex') == \
  997. """\
  998. 2 3 2 4\n\
  999. y + y - x + 2*x \
  1000. """
  1001. expr = x - x**3/6 + x**5/120 + O(x**6)
  1002. ascii_str = \
  1003. """\
  1004. 3 5 \n\
  1005. x x / 6\\\n\
  1006. x - -- + --- + O\\x /\n\
  1007. 6 120 \
  1008. """
  1009. ucode_str = \
  1010. """\
  1011. 3 5 \n\
  1012. x x 6\n\
  1013. x - + + Ox \n\
  1014. 6 120 \
  1015. """
  1016. assert pretty(expr, order=None) == ascii_str
  1017. assert upretty(expr, order=None) == ucode_str
  1018. assert pretty(expr, order='lex') == ascii_str
  1019. assert upretty(expr, order='lex') == ucode_str
  1020. assert pretty(expr, order='rev-lex') == ascii_str
  1021. assert upretty(expr, order='rev-lex') == ucode_str
  1022. def test_EulerGamma():
  1023. assert pretty(EulerGamma) == str(EulerGamma) == "EulerGamma"
  1024. assert upretty(EulerGamma) == "γ"
  1025. def test_GoldenRatio():
  1026. assert pretty(GoldenRatio) == str(GoldenRatio) == "GoldenRatio"
  1027. assert upretty(GoldenRatio) == "φ"
  1028. def test_Catalan():
  1029. assert pretty(Catalan) == upretty(Catalan) == "G"
  1030. def test_pretty_relational():
  1031. expr = Eq(x, y)
  1032. ascii_str = \
  1033. """\
  1034. x = y\
  1035. """
  1036. ucode_str = \
  1037. """\
  1038. x = y\
  1039. """
  1040. assert pretty(expr) == ascii_str
  1041. assert upretty(expr) == ucode_str
  1042. expr = Lt(x, y)
  1043. ascii_str = \
  1044. """\
  1045. x < y\
  1046. """
  1047. ucode_str = \
  1048. """\
  1049. x < y\
  1050. """
  1051. assert pretty(expr) == ascii_str
  1052. assert upretty(expr) == ucode_str
  1053. expr = Gt(x, y)
  1054. ascii_str = \
  1055. """\
  1056. x > y\
  1057. """
  1058. ucode_str = \
  1059. """\
  1060. x > y\
  1061. """
  1062. assert pretty(expr) == ascii_str
  1063. assert upretty(expr) == ucode_str
  1064. expr = Le(x, y)
  1065. ascii_str = \
  1066. """\
  1067. x <= y\
  1068. """
  1069. ucode_str = \
  1070. """\
  1071. x y\
  1072. """
  1073. assert pretty(expr) == ascii_str
  1074. assert upretty(expr) == ucode_str
  1075. expr = Ge(x, y)
  1076. ascii_str = \
  1077. """\
  1078. x >= y\
  1079. """
  1080. ucode_str = \
  1081. """\
  1082. x y\
  1083. """
  1084. assert pretty(expr) == ascii_str
  1085. assert upretty(expr) == ucode_str
  1086. expr = Ne(x/(y + 1), y**2)
  1087. ascii_str_1 = \
  1088. """\
  1089. x 2\n\
  1090. ----- != y \n\
  1091. 1 + y \
  1092. """
  1093. ascii_str_2 = \
  1094. """\
  1095. x 2\n\
  1096. ----- != y \n\
  1097. y + 1 \
  1098. """
  1099. ucode_str_1 = \
  1100. """\
  1101. x 2\n\
  1102. y \n\
  1103. 1 + y \
  1104. """
  1105. ucode_str_2 = \
  1106. """\
  1107. x 2\n\
  1108. y \n\
  1109. y + 1 \
  1110. """
  1111. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1112. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1113. def test_Assignment():
  1114. expr = Assignment(x, y)
  1115. ascii_str = \
  1116. """\
  1117. x := y\
  1118. """
  1119. ucode_str = \
  1120. """\
  1121. x := y\
  1122. """
  1123. assert pretty(expr) == ascii_str
  1124. assert upretty(expr) == ucode_str
  1125. def test_AugmentedAssignment():
  1126. expr = AddAugmentedAssignment(x, y)
  1127. ascii_str = \
  1128. """\
  1129. x += y\
  1130. """
  1131. ucode_str = \
  1132. """\
  1133. x += y\
  1134. """
  1135. assert pretty(expr) == ascii_str
  1136. assert upretty(expr) == ucode_str
  1137. expr = SubAugmentedAssignment(x, y)
  1138. ascii_str = \
  1139. """\
  1140. x -= y\
  1141. """
  1142. ucode_str = \
  1143. """\
  1144. x -= y\
  1145. """
  1146. assert pretty(expr) == ascii_str
  1147. assert upretty(expr) == ucode_str
  1148. expr = MulAugmentedAssignment(x, y)
  1149. ascii_str = \
  1150. """\
  1151. x *= y\
  1152. """
  1153. ucode_str = \
  1154. """\
  1155. x *= y\
  1156. """
  1157. assert pretty(expr) == ascii_str
  1158. assert upretty(expr) == ucode_str
  1159. expr = DivAugmentedAssignment(x, y)
  1160. ascii_str = \
  1161. """\
  1162. x /= y\
  1163. """
  1164. ucode_str = \
  1165. """\
  1166. x /= y\
  1167. """
  1168. assert pretty(expr) == ascii_str
  1169. assert upretty(expr) == ucode_str
  1170. expr = ModAugmentedAssignment(x, y)
  1171. ascii_str = \
  1172. """\
  1173. x %= y\
  1174. """
  1175. ucode_str = \
  1176. """\
  1177. x %= y\
  1178. """
  1179. assert pretty(expr) == ascii_str
  1180. assert upretty(expr) == ucode_str
  1181. def test_pretty_rational():
  1182. expr = y*x**-2
  1183. ascii_str = \
  1184. """\
  1185. y \n\
  1186. --\n\
  1187. 2\n\
  1188. x \
  1189. """
  1190. ucode_str = \
  1191. """\
  1192. y \n\
  1193. \n\
  1194. 2\n\
  1195. x \
  1196. """
  1197. assert pretty(expr) == ascii_str
  1198. assert upretty(expr) == ucode_str
  1199. expr = y**Rational(3, 2) * x**Rational(-5, 2)
  1200. ascii_str = \
  1201. """\
  1202. 3/2\n\
  1203. y \n\
  1204. ----\n\
  1205. 5/2\n\
  1206. x \
  1207. """
  1208. ucode_str = \
  1209. """\
  1210. 3/2\n\
  1211. y \n\
  1212. \n\
  1213. 5/2\n\
  1214. x \
  1215. """
  1216. assert pretty(expr) == ascii_str
  1217. assert upretty(expr) == ucode_str
  1218. expr = sin(x)**3/tan(x)**2
  1219. ascii_str = \
  1220. """\
  1221. 3 \n\
  1222. sin (x)\n\
  1223. -------\n\
  1224. 2 \n\
  1225. tan (x)\
  1226. """
  1227. ucode_str = \
  1228. """\
  1229. 3 \n\
  1230. sin (x)\n\
  1231. \n\
  1232. 2 \n\
  1233. tan (x)\
  1234. """
  1235. assert pretty(expr) == ascii_str
  1236. assert upretty(expr) == ucode_str
  1237. @_both_exp_pow
  1238. def test_pretty_functions():
  1239. """Tests for Abs, conjugate, exp, function braces, and factorial."""
  1240. expr = (2*x + exp(x))
  1241. ascii_str_1 = \
  1242. """\
  1243. x\n\
  1244. 2*x + e \
  1245. """
  1246. ascii_str_2 = \
  1247. """\
  1248. x \n\
  1249. e + 2*x\
  1250. """
  1251. ucode_str_1 = \
  1252. """\
  1253. x\n\
  1254. 2x + \
  1255. """
  1256. ucode_str_2 = \
  1257. """\
  1258. x \n\
  1259. + 2x\
  1260. """
  1261. ucode_str_3 = \
  1262. """\
  1263. x \n\
  1264. + 2x\
  1265. """
  1266. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1267. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  1268. expr = Abs(x)
  1269. ascii_str = \
  1270. """\
  1271. |x|\
  1272. """
  1273. ucode_str = \
  1274. """\
  1275. x\
  1276. """
  1277. assert pretty(expr) == ascii_str
  1278. assert upretty(expr) == ucode_str
  1279. expr = Abs(x/(x**2 + 1))
  1280. ascii_str_1 = \
  1281. """\
  1282. | x |\n\
  1283. |------|\n\
  1284. | 2|\n\
  1285. |1 + x |\
  1286. """
  1287. ascii_str_2 = \
  1288. """\
  1289. | x |\n\
  1290. |------|\n\
  1291. | 2 |\n\
  1292. |x + 1|\
  1293. """
  1294. ucode_str_1 = \
  1295. """\
  1296. x \n\
  1297. \n\
  1298. 2\n\
  1299. 1 + x \
  1300. """
  1301. ucode_str_2 = \
  1302. """\
  1303. x \n\
  1304. \n\
  1305. 2 \n\
  1306. x + 1\
  1307. """
  1308. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1309. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1310. expr = Abs(1 / (y - Abs(x)))
  1311. ascii_str = \
  1312. """\
  1313. 1 \n\
  1314. ---------\n\
  1315. |y - |x||\
  1316. """
  1317. ucode_str = \
  1318. """\
  1319. 1 \n\
  1320. \n\
  1321. y - x\
  1322. """
  1323. assert pretty(expr) == ascii_str
  1324. assert upretty(expr) == ucode_str
  1325. n = Symbol('n', integer=True)
  1326. expr = factorial(n)
  1327. ascii_str = \
  1328. """\
  1329. n!\
  1330. """
  1331. ucode_str = \
  1332. """\
  1333. n!\
  1334. """
  1335. assert pretty(expr) == ascii_str
  1336. assert upretty(expr) == ucode_str
  1337. expr = factorial(2*n)
  1338. ascii_str = \
  1339. """\
  1340. (2*n)!\
  1341. """
  1342. ucode_str = \
  1343. """\
  1344. (2n)!\
  1345. """
  1346. assert pretty(expr) == ascii_str
  1347. assert upretty(expr) == ucode_str
  1348. expr = factorial(factorial(factorial(n)))
  1349. ascii_str = \
  1350. """\
  1351. ((n!)!)!\
  1352. """
  1353. ucode_str = \
  1354. """\
  1355. ((n!)!)!\
  1356. """
  1357. assert pretty(expr) == ascii_str
  1358. assert upretty(expr) == ucode_str
  1359. expr = factorial(n + 1)
  1360. ascii_str_1 = \
  1361. """\
  1362. (1 + n)!\
  1363. """
  1364. ascii_str_2 = \
  1365. """\
  1366. (n + 1)!\
  1367. """
  1368. ucode_str_1 = \
  1369. """\
  1370. (1 + n)!\
  1371. """
  1372. ucode_str_2 = \
  1373. """\
  1374. (n + 1)!\
  1375. """
  1376. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1377. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1378. expr = subfactorial(n)
  1379. ascii_str = \
  1380. """\
  1381. !n\
  1382. """
  1383. ucode_str = \
  1384. """\
  1385. !n\
  1386. """
  1387. assert pretty(expr) == ascii_str
  1388. assert upretty(expr) == ucode_str
  1389. expr = subfactorial(2*n)
  1390. ascii_str = \
  1391. """\
  1392. !(2*n)\
  1393. """
  1394. ucode_str = \
  1395. """\
  1396. !(2n)\
  1397. """
  1398. assert pretty(expr) == ascii_str
  1399. assert upretty(expr) == ucode_str
  1400. n = Symbol('n', integer=True)
  1401. expr = factorial2(n)
  1402. ascii_str = \
  1403. """\
  1404. n!!\
  1405. """
  1406. ucode_str = \
  1407. """\
  1408. n!!\
  1409. """
  1410. assert pretty(expr) == ascii_str
  1411. assert upretty(expr) == ucode_str
  1412. expr = factorial2(2*n)
  1413. ascii_str = \
  1414. """\
  1415. (2*n)!!\
  1416. """
  1417. ucode_str = \
  1418. """\
  1419. (2n)!!\
  1420. """
  1421. assert pretty(expr) == ascii_str
  1422. assert upretty(expr) == ucode_str
  1423. expr = factorial2(factorial2(factorial2(n)))
  1424. ascii_str = \
  1425. """\
  1426. ((n!!)!!)!!\
  1427. """
  1428. ucode_str = \
  1429. """\
  1430. ((n!!)!!)!!\
  1431. """
  1432. assert pretty(expr) == ascii_str
  1433. assert upretty(expr) == ucode_str
  1434. expr = factorial2(n + 1)
  1435. ascii_str_1 = \
  1436. """\
  1437. (1 + n)!!\
  1438. """
  1439. ascii_str_2 = \
  1440. """\
  1441. (n + 1)!!\
  1442. """
  1443. ucode_str_1 = \
  1444. """\
  1445. (1 + n)!!\
  1446. """
  1447. ucode_str_2 = \
  1448. """\
  1449. (n + 1)!!\
  1450. """
  1451. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1452. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1453. expr = 2*binomial(n, k)
  1454. ascii_str = \
  1455. """\
  1456. /n\\\n\
  1457. 2*| |\n\
  1458. \\k/\
  1459. """
  1460. ucode_str = \
  1461. """\
  1462. n\n\
  1463. 2 \n\
  1464. k\
  1465. """
  1466. assert pretty(expr) == ascii_str
  1467. assert upretty(expr) == ucode_str
  1468. expr = 2*binomial(2*n, k)
  1469. ascii_str = \
  1470. """\
  1471. /2*n\\\n\
  1472. 2*| |\n\
  1473. \\ k /\
  1474. """
  1475. ucode_str = \
  1476. """\
  1477. 2n\n\
  1478. 2 \n\
  1479. k \
  1480. """
  1481. assert pretty(expr) == ascii_str
  1482. assert upretty(expr) == ucode_str
  1483. expr = 2*binomial(n**2, k)
  1484. ascii_str = \
  1485. """\
  1486. / 2\\\n\
  1487. |n |\n\
  1488. 2*| |\n\
  1489. \\k /\
  1490. """
  1491. ucode_str = \
  1492. """\
  1493. 2\n\
  1494. n \n\
  1495. 2 \n\
  1496. k \
  1497. """
  1498. assert pretty(expr) == ascii_str
  1499. assert upretty(expr) == ucode_str
  1500. expr = catalan(n)
  1501. ascii_str = \
  1502. """\
  1503. C \n\
  1504. n\
  1505. """
  1506. ucode_str = \
  1507. """\
  1508. C \n\
  1509. n\
  1510. """
  1511. assert pretty(expr) == ascii_str
  1512. assert upretty(expr) == ucode_str
  1513. expr = catalan(n)
  1514. ascii_str = \
  1515. """\
  1516. C \n\
  1517. n\
  1518. """
  1519. ucode_str = \
  1520. """\
  1521. C \n\
  1522. n\
  1523. """
  1524. assert pretty(expr) == ascii_str
  1525. assert upretty(expr) == ucode_str
  1526. expr = bell(n)
  1527. ascii_str = \
  1528. """\
  1529. B \n\
  1530. n\
  1531. """
  1532. ucode_str = \
  1533. """\
  1534. B \n\
  1535. n\
  1536. """
  1537. assert pretty(expr) == ascii_str
  1538. assert upretty(expr) == ucode_str
  1539. expr = bernoulli(n)
  1540. ascii_str = \
  1541. """\
  1542. B \n\
  1543. n\
  1544. """
  1545. ucode_str = \
  1546. """\
  1547. B \n\
  1548. n\
  1549. """
  1550. assert pretty(expr) == ascii_str
  1551. assert upretty(expr) == ucode_str
  1552. expr = bernoulli(n, x)
  1553. ascii_str = \
  1554. """\
  1555. B (x)\n\
  1556. n \
  1557. """
  1558. ucode_str = \
  1559. """\
  1560. B (x)\n\
  1561. n \
  1562. """
  1563. assert pretty(expr) == ascii_str
  1564. assert upretty(expr) == ucode_str
  1565. expr = fibonacci(n)
  1566. ascii_str = \
  1567. """\
  1568. F \n\
  1569. n\
  1570. """
  1571. ucode_str = \
  1572. """\
  1573. F \n\
  1574. n\
  1575. """
  1576. assert pretty(expr) == ascii_str
  1577. assert upretty(expr) == ucode_str
  1578. expr = lucas(n)
  1579. ascii_str = \
  1580. """\
  1581. L \n\
  1582. n\
  1583. """
  1584. ucode_str = \
  1585. """\
  1586. L \n\
  1587. n\
  1588. """
  1589. assert pretty(expr) == ascii_str
  1590. assert upretty(expr) == ucode_str
  1591. expr = tribonacci(n)
  1592. ascii_str = \
  1593. """\
  1594. T \n\
  1595. n\
  1596. """
  1597. ucode_str = \
  1598. """\
  1599. T \n\
  1600. n\
  1601. """
  1602. assert pretty(expr) == ascii_str
  1603. assert upretty(expr) == ucode_str
  1604. expr = stieltjes(n)
  1605. ascii_str = \
  1606. """\
  1607. stieltjes \n\
  1608. n\
  1609. """
  1610. ucode_str = \
  1611. """\
  1612. γ \n\
  1613. n\
  1614. """
  1615. assert pretty(expr) == ascii_str
  1616. assert upretty(expr) == ucode_str
  1617. expr = stieltjes(n, x)
  1618. ascii_str = \
  1619. """\
  1620. stieltjes (x)\n\
  1621. n \
  1622. """
  1623. ucode_str = \
  1624. """\
  1625. γ (x)\n\
  1626. n \
  1627. """
  1628. assert pretty(expr) == ascii_str
  1629. assert upretty(expr) == ucode_str
  1630. expr = mathieuc(x, y, z)
  1631. ascii_str = 'C(x, y, z)'
  1632. ucode_str = 'C(x, y, z)'
  1633. assert pretty(expr) == ascii_str
  1634. assert upretty(expr) == ucode_str
  1635. expr = mathieus(x, y, z)
  1636. ascii_str = 'S(x, y, z)'
  1637. ucode_str = 'S(x, y, z)'
  1638. assert pretty(expr) == ascii_str
  1639. assert upretty(expr) == ucode_str
  1640. expr = mathieucprime(x, y, z)
  1641. ascii_str = "C'(x, y, z)"
  1642. ucode_str = "C'(x, y, z)"
  1643. assert pretty(expr) == ascii_str
  1644. assert upretty(expr) == ucode_str
  1645. expr = mathieusprime(x, y, z)
  1646. ascii_str = "S'(x, y, z)"
  1647. ucode_str = "S'(x, y, z)"
  1648. assert pretty(expr) == ascii_str
  1649. assert upretty(expr) == ucode_str
  1650. expr = conjugate(x)
  1651. ascii_str = \
  1652. """\
  1653. _\n\
  1654. x\
  1655. """
  1656. ucode_str = \
  1657. """\
  1658. _\n\
  1659. x\
  1660. """
  1661. assert pretty(expr) == ascii_str
  1662. assert upretty(expr) == ucode_str
  1663. f = Function('f')
  1664. expr = conjugate(f(x + 1))
  1665. ascii_str_1 = \
  1666. """\
  1667. ________\n\
  1668. f(1 + x)\
  1669. """
  1670. ascii_str_2 = \
  1671. """\
  1672. ________\n\
  1673. f(x + 1)\
  1674. """
  1675. ucode_str_1 = \
  1676. """\
  1677. ________\n\
  1678. f(1 + x)\
  1679. """
  1680. ucode_str_2 = \
  1681. """\
  1682. ________\n\
  1683. f(x + 1)\
  1684. """
  1685. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1686. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1687. expr = f(x)
  1688. ascii_str = \
  1689. """\
  1690. f(x)\
  1691. """
  1692. ucode_str = \
  1693. """\
  1694. f(x)\
  1695. """
  1696. assert pretty(expr) == ascii_str
  1697. assert upretty(expr) == ucode_str
  1698. expr = f(x, y)
  1699. ascii_str = \
  1700. """\
  1701. f(x, y)\
  1702. """
  1703. ucode_str = \
  1704. """\
  1705. f(x, y)\
  1706. """
  1707. assert pretty(expr) == ascii_str
  1708. assert upretty(expr) == ucode_str
  1709. expr = f(x/(y + 1), y)
  1710. ascii_str_1 = \
  1711. """\
  1712. / x \\\n\
  1713. f|-----, y|\n\
  1714. \\1 + y /\
  1715. """
  1716. ascii_str_2 = \
  1717. """\
  1718. / x \\\n\
  1719. f|-----, y|\n\
  1720. \\y + 1 /\
  1721. """
  1722. ucode_str_1 = \
  1723. """\
  1724. x \n\
  1725. f, y\n\
  1726. 1 + y \
  1727. """
  1728. ucode_str_2 = \
  1729. """\
  1730. x \n\
  1731. f, y\n\
  1732. y + 1 \
  1733. """
  1734. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1735. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1736. expr = f(x**x**x**x**x**x)
  1737. ascii_str = \
  1738. """\
  1739. / / / / / x\\\\\\\\\\
  1740. | | | | \\x /||||
  1741. | | | \\x /|||
  1742. | | \\x /||
  1743. | \\x /|
  1744. f\\x /\
  1745. """
  1746. ucode_str = \
  1747. """\
  1748. x
  1749. x
  1750. x
  1751. x
  1752. x
  1753. fx \
  1754. """
  1755. assert pretty(expr) == ascii_str
  1756. assert upretty(expr) == ucode_str
  1757. expr = sin(x)**2
  1758. ascii_str = \
  1759. """\
  1760. 2 \n\
  1761. sin (x)\
  1762. """
  1763. ucode_str = \
  1764. """\
  1765. 2 \n\
  1766. sin (x)\
  1767. """
  1768. assert pretty(expr) == ascii_str
  1769. assert upretty(expr) == ucode_str
  1770. expr = conjugate(a + b*I)
  1771. ascii_str = \
  1772. """\
  1773. _ _\n\
  1774. a - I*b\
  1775. """
  1776. ucode_str = \
  1777. """\
  1778. _ _\n\
  1779. a - b\
  1780. """
  1781. assert pretty(expr) == ascii_str
  1782. assert upretty(expr) == ucode_str
  1783. expr = conjugate(exp(a + b*I))
  1784. ascii_str = \
  1785. """\
  1786. _ _\n\
  1787. a - I*b\n\
  1788. e \
  1789. """
  1790. ucode_str = \
  1791. """\
  1792. _ _\n\
  1793. a - b\n\
  1794. \
  1795. """
  1796. assert pretty(expr) == ascii_str
  1797. assert upretty(expr) == ucode_str
  1798. expr = conjugate( f(1 + conjugate(f(x))) )
  1799. ascii_str_1 = \
  1800. """\
  1801. ___________\n\
  1802. / ____\\\n\
  1803. f\\1 + f(x)/\
  1804. """
  1805. ascii_str_2 = \
  1806. """\
  1807. ___________\n\
  1808. /____ \\\n\
  1809. f\\f(x) + 1/\
  1810. """
  1811. ucode_str_1 = \
  1812. """\
  1813. ___________\n\
  1814. ____\n\
  1815. f1 + f(x)\
  1816. """
  1817. ucode_str_2 = \
  1818. """\
  1819. ___________\n\
  1820. ____ \n\
  1821. ff(x) + 1\
  1822. """
  1823. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1824. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1825. expr = f(x/(y + 1), y)
  1826. ascii_str_1 = \
  1827. """\
  1828. / x \\\n\
  1829. f|-----, y|\n\
  1830. \\1 + y /\
  1831. """
  1832. ascii_str_2 = \
  1833. """\
  1834. / x \\\n\
  1835. f|-----, y|\n\
  1836. \\y + 1 /\
  1837. """
  1838. ucode_str_1 = \
  1839. """\
  1840. x \n\
  1841. f, y\n\
  1842. 1 + y \
  1843. """
  1844. ucode_str_2 = \
  1845. """\
  1846. x \n\
  1847. f, y\n\
  1848. y + 1 \
  1849. """
  1850. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1851. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1852. expr = floor(1 / (y - floor(x)))
  1853. ascii_str = \
  1854. """\
  1855. / 1 \\\n\
  1856. floor|------------|\n\
  1857. \\y - floor(x)/\
  1858. """
  1859. ucode_str = \
  1860. """\
  1861. 1 \n\
  1862. \n\
  1863. y - x\
  1864. """
  1865. assert pretty(expr) == ascii_str
  1866. assert upretty(expr) == ucode_str
  1867. expr = ceiling(1 / (y - ceiling(x)))
  1868. ascii_str = \
  1869. """\
  1870. / 1 \\\n\
  1871. ceiling|--------------|\n\
  1872. \\y - ceiling(x)/\
  1873. """
  1874. ucode_str = \
  1875. """\
  1876. 1 \n\
  1877. \n\
  1878. y - x\
  1879. """
  1880. assert pretty(expr) == ascii_str
  1881. assert upretty(expr) == ucode_str
  1882. expr = euler(n)
  1883. ascii_str = \
  1884. """\
  1885. E \n\
  1886. n\
  1887. """
  1888. ucode_str = \
  1889. """\
  1890. E \n\
  1891. n\
  1892. """
  1893. assert pretty(expr) == ascii_str
  1894. assert upretty(expr) == ucode_str
  1895. expr = euler(1/(1 + 1/(1 + 1/n)))
  1896. ascii_str = \
  1897. """\
  1898. E \n\
  1899. 1 \n\
  1900. ---------\n\
  1901. 1 \n\
  1902. 1 + -----\n\
  1903. 1\n\
  1904. 1 + -\n\
  1905. n\
  1906. """
  1907. ucode_str = \
  1908. """\
  1909. E \n\
  1910. 1 \n\
  1911. \n\
  1912. 1 \n\
  1913. 1 + \n\
  1914. 1\n\
  1915. 1 + \n\
  1916. n\
  1917. """
  1918. assert pretty(expr) == ascii_str
  1919. assert upretty(expr) == ucode_str
  1920. expr = euler(n, x)
  1921. ascii_str = \
  1922. """\
  1923. E (x)\n\
  1924. n \
  1925. """
  1926. ucode_str = \
  1927. """\
  1928. E (x)\n\
  1929. n \
  1930. """
  1931. assert pretty(expr) == ascii_str
  1932. assert upretty(expr) == ucode_str
  1933. expr = euler(n, x/2)
  1934. ascii_str = \
  1935. """\
  1936. /x\\\n\
  1937. E |-|\n\
  1938. n\\2/\
  1939. """
  1940. ucode_str = \
  1941. """\
  1942. x\n\
  1943. E \n\
  1944. n2\
  1945. """
  1946. assert pretty(expr) == ascii_str
  1947. assert upretty(expr) == ucode_str
  1948. def test_pretty_sqrt():
  1949. expr = sqrt(2)
  1950. ascii_str = \
  1951. """\
  1952. ___\n\
  1953. \\/ 2 \
  1954. """
  1955. ucode_str = \
  1956. "√2"
  1957. assert pretty(expr) == ascii_str
  1958. assert upretty(expr) == ucode_str
  1959. expr = 2**Rational(1, 3)
  1960. ascii_str = \
  1961. """\
  1962. 3 ___\n\
  1963. \\/ 2 \
  1964. """
  1965. ucode_str = \
  1966. """\
  1967. 3 ___\n\
  1968. 2 \
  1969. """
  1970. assert pretty(expr) == ascii_str
  1971. assert upretty(expr) == ucode_str
  1972. expr = 2**Rational(1, 1000)
  1973. ascii_str = \
  1974. """\
  1975. 1000___\n\
  1976. \\/ 2 \
  1977. """
  1978. ucode_str = \
  1979. """\
  1980. 1000___\n\
  1981. 2 \
  1982. """
  1983. assert pretty(expr) == ascii_str
  1984. assert upretty(expr) == ucode_str
  1985. expr = sqrt(x**2 + 1)
  1986. ascii_str = \
  1987. """\
  1988. ________\n\
  1989. / 2 \n\
  1990. \\/ x + 1 \
  1991. """
  1992. ucode_str = \
  1993. """\
  1994. ________\n\
  1995. 2 \n\
  1996. x + 1 \
  1997. """
  1998. assert pretty(expr) == ascii_str
  1999. assert upretty(expr) == ucode_str
  2000. expr = (1 + sqrt(5))**Rational(1, 3)
  2001. ascii_str = \
  2002. """\
  2003. ___________\n\
  2004. 3 / ___ \n\
  2005. \\/ 1 + \\/ 5 \
  2006. """
  2007. ucode_str = \
  2008. """\
  2009. 3 ________\n\
  2010. 1 + 5 \
  2011. """
  2012. assert pretty(expr) == ascii_str
  2013. assert upretty(expr) == ucode_str
  2014. expr = 2**(1/x)
  2015. ascii_str = \
  2016. """\
  2017. x ___\n\
  2018. \\/ 2 \
  2019. """
  2020. ucode_str = \
  2021. """\
  2022. x ___\n\
  2023. 2 \
  2024. """
  2025. assert pretty(expr) == ascii_str
  2026. assert upretty(expr) == ucode_str
  2027. expr = sqrt(2 + pi)
  2028. ascii_str = \
  2029. """\
  2030. ________\n\
  2031. \\/ 2 + pi \
  2032. """
  2033. ucode_str = \
  2034. """\
  2035. _______\n\
  2036. 2 + π \
  2037. """
  2038. assert pretty(expr) == ascii_str
  2039. assert upretty(expr) == ucode_str
  2040. expr = (2 + (
  2041. 1 + x**2)/(2 + x))**Rational(1, 4) + (1 + x**Rational(1, 1000))/sqrt(3 + x**2)
  2042. ascii_str = \
  2043. """\
  2044. ____________ \n\
  2045. / 2 1000___ \n\
  2046. / x + 1 \\/ x + 1\n\
  2047. 4 / 2 + ------ + -----------\n\
  2048. \\/ x + 2 ________\n\
  2049. / 2 \n\
  2050. \\/ x + 3 \
  2051. """
  2052. ucode_str = \
  2053. """\
  2054. ____________ \n\
  2055. 2 1000___ \n\
  2056. x + 1 x + 1\n\
  2057. 4 2 + + \n\
  2058. x + 2 ________\n\
  2059. 2 \n\
  2060. x + 3 \
  2061. """
  2062. assert pretty(expr) == ascii_str
  2063. assert upretty(expr) == ucode_str
  2064. def test_pretty_sqrt_char_knob():
  2065. # See PR #9234.
  2066. expr = sqrt(2)
  2067. ucode_str1 = \
  2068. """\
  2069. ___\n\
  2070. 2 \
  2071. """
  2072. ucode_str2 = \
  2073. "√2"
  2074. assert xpretty(expr, use_unicode=True,
  2075. use_unicode_sqrt_char=False) == ucode_str1
  2076. assert xpretty(expr, use_unicode=True,
  2077. use_unicode_sqrt_char=True) == ucode_str2
  2078. def test_pretty_sqrt_longsymbol_no_sqrt_char():
  2079. # Do not use unicode sqrt char for long symbols (see PR #9234).
  2080. expr = sqrt(Symbol('C1'))
  2081. ucode_str = \
  2082. """\
  2083. ____\n\
  2084. C \
  2085. """
  2086. assert upretty(expr) == ucode_str
  2087. def test_pretty_KroneckerDelta():
  2088. x, y = symbols("x, y")
  2089. expr = KroneckerDelta(x, y)
  2090. ascii_str = \
  2091. """\
  2092. d \n\
  2093. x,y\
  2094. """
  2095. ucode_str = \
  2096. """\
  2097. δ \n\
  2098. x,y\
  2099. """
  2100. assert pretty(expr) == ascii_str
  2101. assert upretty(expr) == ucode_str
  2102. def test_pretty_product():
  2103. n, m, k, l = symbols('n m k l')
  2104. f = symbols('f', cls=Function)
  2105. expr = Product(f((n/3)**2), (n, k**2, l))
  2106. unicode_str = \
  2107. """\
  2108. l \n\
  2109. \n\
  2110. 2\n\
  2111. n \n\
  2112. f\n\
  2113. 9 \n\
  2114. \n\
  2115. 2 \n\
  2116. n = k """
  2117. ascii_str = \
  2118. """\
  2119. l \n\
  2120. __________ \n\
  2121. | | / 2\\\n\
  2122. | | |n |\n\
  2123. | | f|--|\n\
  2124. | | \\9 /\n\
  2125. | | \n\
  2126. 2 \n\
  2127. n = k """
  2128. expr = Product(f((n/3)**2), (n, k**2, l), (l, 1, m))
  2129. unicode_str = \
  2130. """\
  2131. m l \n\
  2132. \n\
  2133. 2\n\
  2134. n \n\
  2135. f\n\
  2136. 9 \n\
  2137. \n\
  2138. l = 1 2 \n\
  2139. n = k """
  2140. ascii_str = \
  2141. """\
  2142. m l \n\
  2143. __________ __________ \n\
  2144. | | | | / 2\\\n\
  2145. | | | | |n |\n\
  2146. | | | | f|--|\n\
  2147. | | | | \\9 /\n\
  2148. | | | | \n\
  2149. l = 1 2 \n\
  2150. n = k """
  2151. assert pretty(expr) == ascii_str
  2152. assert upretty(expr) == unicode_str
  2153. def test_pretty_Lambda():
  2154. # S.IdentityFunction is a special case
  2155. expr = Lambda(y, y)
  2156. assert pretty(expr) == "x -> x"
  2157. assert upretty(expr) == "x ↦ x"
  2158. expr = Lambda(x, x+1)
  2159. assert pretty(expr) == "x -> x + 1"
  2160. assert upretty(expr) == "x ↦ x + 1"
  2161. expr = Lambda(x, x**2)
  2162. ascii_str = \
  2163. """\
  2164. 2\n\
  2165. x -> x \
  2166. """
  2167. ucode_str = \
  2168. """\
  2169. 2\n\
  2170. x x \
  2171. """
  2172. assert pretty(expr) == ascii_str
  2173. assert upretty(expr) == ucode_str
  2174. expr = Lambda(x, x**2)**2
  2175. ascii_str = \
  2176. """\
  2177. 2
  2178. / 2\\ \n\
  2179. \\x -> x / \
  2180. """
  2181. ucode_str = \
  2182. """\
  2183. 2
  2184. 2 \n\
  2185. x x \
  2186. """
  2187. assert pretty(expr) == ascii_str
  2188. assert upretty(expr) == ucode_str
  2189. expr = Lambda((x, y), x)
  2190. ascii_str = "(x, y) -> x"
  2191. ucode_str = "(x, y) ↦ x"
  2192. assert pretty(expr) == ascii_str
  2193. assert upretty(expr) == ucode_str
  2194. expr = Lambda((x, y), x**2)
  2195. ascii_str = \
  2196. """\
  2197. 2\n\
  2198. (x, y) -> x \
  2199. """
  2200. ucode_str = \
  2201. """\
  2202. 2\n\
  2203. (x, y) x \
  2204. """
  2205. assert pretty(expr) == ascii_str
  2206. assert upretty(expr) == ucode_str
  2207. expr = Lambda(((x, y),), x**2)
  2208. ascii_str = \
  2209. """\
  2210. 2\n\
  2211. ((x, y),) -> x \
  2212. """
  2213. ucode_str = \
  2214. """\
  2215. 2\n\
  2216. ((x, y),) x \
  2217. """
  2218. assert pretty(expr) == ascii_str
  2219. assert upretty(expr) == ucode_str
  2220. def test_pretty_TransferFunction():
  2221. tf1 = TransferFunction(s - 1, s + 1, s)
  2222. assert upretty(tf1) == "s - 1\n─────\ns + 1"
  2223. tf2 = TransferFunction(2*s + 1, 3 - p, s)
  2224. assert upretty(tf2) == "2⋅s + 1\n───────\n 3 - p "
  2225. tf3 = TransferFunction(p, p + 1, p)
  2226. assert upretty(tf3) == " p \n─────\np + 1"
  2227. def test_pretty_Series():
  2228. tf1 = TransferFunction(x + y, x - 2*y, y)
  2229. tf2 = TransferFunction(x - y, x + y, y)
  2230. tf3 = TransferFunction(x**2 + y, y - x, y)
  2231. tf4 = TransferFunction(2, 3, y)
  2232. tfm1 = TransferFunctionMatrix([[tf1, tf2], [tf3, tf4]])
  2233. tfm2 = TransferFunctionMatrix([[tf3], [-tf4]])
  2234. tfm3 = TransferFunctionMatrix([[tf1, -tf2, -tf3], [tf3, -tf4, tf2]])
  2235. tfm4 = TransferFunctionMatrix([[tf1, tf2], [tf3, -tf4], [-tf2, -tf1]])
  2236. tfm5 = TransferFunctionMatrix([[-tf2, -tf1], [tf4, -tf3], [tf1, tf2]])
  2237. expected1 = \
  2238. """\
  2239. 2 \n\
  2240. x + y x + y\n\
  2241. \n\
  2242. x - 2y -x + y\
  2243. """
  2244. expected2 = \
  2245. """\
  2246. -x + y -x - y\n\
  2247. \n\
  2248. x + y x - 2y\
  2249. """
  2250. expected3 = \
  2251. """\
  2252. 2 \n\
  2253. x + y x + y -x - y x - y\n\
  2254. + \n\
  2255. -x + y x - 2y x - 2y x + y\
  2256. """
  2257. expected4 = \
  2258. """\
  2259. 2 \n\
  2260. x + y x - y x - y x + y\n\
  2261. + + \n\
  2262. x - 2y x + y x + y -x + y\
  2263. """
  2264. expected5 = \
  2265. """\
  2266. x + y x - y 2 \n\
  2267. x + y \n\
  2268. x - 2y x + y \n\
  2269. -x + y \n\
  2270. 2 \n\
  2271. x + y 2 -2 \n\
  2272. \n\
  2273. -x + y 3 τ 3 τ\
  2274. """
  2275. expected6 = \
  2276. """\
  2277. x + y x - y x - y x + y \n\
  2278. \n\
  2279. x + y x - y 2 x - 2y x + y x + y x - 2y \n\
  2280. x + y -x + y - x - y \n\
  2281. x - 2y x + y 2 2 \n\
  2282. x - 2y x + y -x + y x + y -2 -2 x + y \n\
  2283. 2 + \n\
  2284. x + y 2 2 -x + y 3 3 -x + y \n\
  2285. x + y -2 x - y \n\
  2286. -x + y 3 τ -x + y -x - y -x - y -x + y \n\
  2287. -x + y 3 x + y τ \n\
  2288. x + y x - 2yτ x - 2y x + y τ\
  2289. """
  2290. assert upretty(Series(tf1, tf3)) == expected1
  2291. assert upretty(Series(-tf2, -tf1)) == expected2
  2292. assert upretty(Series(tf3, tf1, Parallel(-tf1, tf2))) == expected3
  2293. assert upretty(Series(Parallel(tf1, tf2), Parallel(tf2, tf3))) == expected4
  2294. assert upretty(MIMOSeries(tfm2, tfm1)) == expected5
  2295. assert upretty(MIMOSeries(MIMOParallel(tfm4, -tfm5), tfm3, tfm1)) == expected6
  2296. def test_pretty_Parallel():
  2297. tf1 = TransferFunction(x + y, x - 2*y, y)
  2298. tf2 = TransferFunction(x - y, x + y, y)
  2299. tf3 = TransferFunction(x**2 + y, y - x, y)
  2300. tf4 = TransferFunction(y**2 - x, x**3 + x, y)
  2301. tfm1 = TransferFunctionMatrix([[tf1, tf2], [tf3, -tf4], [-tf2, -tf1]])
  2302. tfm2 = TransferFunctionMatrix([[-tf2, -tf1], [tf4, -tf3], [tf1, tf2]])
  2303. tfm3 = TransferFunctionMatrix([[-tf1, tf2], [-tf3, tf4], [tf2, tf1]])
  2304. tfm4 = TransferFunctionMatrix([[-tf1, -tf2], [-tf3, -tf4]])
  2305. expected1 = \
  2306. """\
  2307. x + y x - y\n\
  2308. + \n\
  2309. x - 2y x + y\
  2310. """
  2311. expected2 = \
  2312. """\
  2313. -x + y -x - y\n\
  2314. + \n\
  2315. x + y x - 2y\
  2316. """
  2317. expected3 = \
  2318. """\
  2319. 2 \n\
  2320. x + y x + y -x - y x - y\n\
  2321. + + \n\
  2322. -x + y x - 2y x - 2y x + y\
  2323. """
  2324. expected4 = \
  2325. """\
  2326. 2 \n\
  2327. x + y x - y x - y x + y\n\
  2328. + \n\
  2329. x - 2y x + y x + y -x + y\
  2330. """
  2331. expected5 = \
  2332. """\
  2333. x + y -x + y x - y x + y x + y x - y \n\
  2334. \n\
  2335. x - 2y x + y x + y x - 2y x - 2y x + y \n\
  2336. \n\
  2337. 2 2 2 2 2 2 \n\
  2338. x + y x - y x - y x + y x + y x - y \n\
  2339. + + \n\
  2340. -x + y 3 3 -x + y -x + y 3 \n\
  2341. x + x x + x x + x \n\
  2342. \n\
  2343. -x + y -x - y -x - y -x + y -x + y -x - y \n\
  2344. \n\
  2345. x + y x - 2yτ x - 2y x + y τ x + y x - 2yτ\
  2346. """
  2347. expected6 = \
  2348. """\
  2349. x - y x + y -x + y -x - y \n\
  2350. \n\
  2351. x + y x - 2y -x - y -x + y x + y x - 2y \n\
  2352. \n\
  2353. 2 2 x - 2y x + y 2 2 \n\
  2354. x - y x + y -x + y - x - y \n\
  2355. 2 2 + \n\
  2356. 3 -x + y - x - y x - y 3 -x + y \n\
  2357. x + x x + x \n\
  2358. -x + y 3 \n\
  2359. -x - y -x + y x + xτ x + y x - y \n\
  2360. \n\
  2361. x - 2y x + y τ x - 2y x + y τ\
  2362. """
  2363. assert upretty(Parallel(tf1, tf2)) == expected1
  2364. assert upretty(Parallel(-tf2, -tf1)) == expected2
  2365. assert upretty(Parallel(tf3, tf1, Series(-tf1, tf2))) == expected3
  2366. assert upretty(Parallel(Series(tf1, tf2), Series(tf2, tf3))) == expected4
  2367. assert upretty(MIMOParallel(-tfm3, -tfm2, tfm1)) == expected5
  2368. assert upretty(MIMOParallel(MIMOSeries(tfm4, -tfm2), tfm2)) == expected6
  2369. def test_pretty_Feedback():
  2370. tf = TransferFunction(1, 1, y)
  2371. tf1 = TransferFunction(x + y, x - 2*y, y)
  2372. tf2 = TransferFunction(x - y, x + y, y)
  2373. tf3 = TransferFunction(y**2 - 2*y + 1, y + 5, y)
  2374. tf4 = TransferFunction(x - 2*y**3, x + y, x)
  2375. tf5 = TransferFunction(1 - x, x - y, y)
  2376. tf6 = TransferFunction(2, 2, x)
  2377. expected1 = \
  2378. """\
  2379. 1 \n\
  2380. \n\
  2381. 1 \n\
  2382. \n\
  2383. 1 x + y \n\
  2384. + \n\
  2385. 1 x - 2y\
  2386. """
  2387. expected2 = \
  2388. """\
  2389. 1 \n\
  2390. \n\
  2391. 1 \n\
  2392. \n\
  2393. 2 \n\
  2394. 1 x - y x + y y - 2y + 1\n\
  2395. + \n\
  2396. 1 x + y x - 2y y + 5 \
  2397. """
  2398. expected3 = \
  2399. """\
  2400. x + y \n\
  2401. \n\
  2402. x - 2y \n\
  2403. \n\
  2404. 2 \n\
  2405. 1 x + y x - y y - 2y + 1 1 - x\n\
  2406. + \n\
  2407. 1 x - 2y x + y y + 5 x - y\
  2408. """
  2409. expected4 = \
  2410. """\
  2411. x + y x - y \n\
  2412. \n\
  2413. x - 2y x + y \n\
  2414. \n\
  2415. 1 x + y x - y\n\
  2416. + \n\
  2417. 1 x - 2y x + y\
  2418. """
  2419. expected5 = \
  2420. """\
  2421. x + y x - y \n\
  2422. \n\
  2423. x - 2y x + y \n\
  2424. \n\
  2425. 1 x + y x - y 1 - x\n\
  2426. + \n\
  2427. 1 x - 2y x + y x - y\
  2428. """
  2429. expected6 = \
  2430. """\
  2431. 2 \n\
  2432. y - 2y + 1 1 - x \n\
  2433. \n\
  2434. y + 5 x - y \n\
  2435. \n\
  2436. 2 \n\
  2437. 1 y - 2y + 1 1 - x x - y x + y \n\
  2438. + \n\
  2439. 1 y + 5 x - y x + y x - 2y\
  2440. """
  2441. expected7 = \
  2442. """\
  2443. 3 \n\
  2444. x - 2y \n\
  2445. \n\
  2446. x + y \n\
  2447. \n\
  2448. 3 \n\
  2449. 1 x - 2y 2\n\
  2450. + \n\
  2451. 1 x + y 2\
  2452. """
  2453. expected8 = \
  2454. """\
  2455. 1 - x \n\
  2456. \n\
  2457. x - y \n\
  2458. \n\
  2459. 1 1 - x\n\
  2460. + \n\
  2461. 1 x - y\
  2462. """
  2463. expected9 = \
  2464. """\
  2465. x + y x - y \n\
  2466. \n\
  2467. x - 2y x + y \n\
  2468. \n\
  2469. 1 x + y x - y 1 - x\n\
  2470. - \n\
  2471. 1 x - 2y x + y x - y\
  2472. """
  2473. expected10 = \
  2474. """\
  2475. 1 - x \n\
  2476. \n\
  2477. x - y \n\
  2478. \n\
  2479. 1 1 - x\n\
  2480. - \n\
  2481. 1 x - y\
  2482. """
  2483. assert upretty(Feedback(tf, tf1)) == expected1
  2484. assert upretty(Feedback(tf, tf2*tf1*tf3)) == expected2
  2485. assert upretty(Feedback(tf1, tf2*tf3*tf5)) == expected3
  2486. assert upretty(Feedback(tf1*tf2, tf)) == expected4
  2487. assert upretty(Feedback(tf1*tf2, tf5)) == expected5
  2488. assert upretty(Feedback(tf3*tf5, tf2*tf1)) == expected6
  2489. assert upretty(Feedback(tf4, tf6)) == expected7
  2490. assert upretty(Feedback(tf5, tf)) == expected8
  2491. assert upretty(Feedback(tf1*tf2, tf5, 1)) == expected9
  2492. assert upretty(Feedback(tf5, tf, 1)) == expected10
  2493. def test_pretty_MIMOFeedback():
  2494. tf1 = TransferFunction(x + y, x - 2*y, y)
  2495. tf2 = TransferFunction(x - y, x + y, y)
  2496. tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf2, tf1]])
  2497. tfm_2 = TransferFunctionMatrix([[tf2, tf1], [tf1, tf2]])
  2498. tfm_3 = TransferFunctionMatrix([[tf1, tf1], [tf2, tf2]])
  2499. expected1 = \
  2500. """\
  2501. x + y x - y x - y x + y -1 x + y x - y \n\
  2502. \n\
  2503. x - 2y x + y x + y x - 2y x - 2y x + y \n\
  2504. I - \n\
  2505. x - y x + y x + y x - y x - y x + y \n\
  2506. \n\
  2507. x + y x - 2yτ x - 2y x + y τ x + y x - 2yτ\
  2508. """
  2509. expected2 = \
  2510. """\
  2511. x + y x - y x - y x + y x + y x + y -1 x + y x - y x - y x + y \n\
  2512. \n\
  2513. x - 2y x + y x + y x - 2y x - 2y x - 2y x - 2y x + y x + y x - 2y \n\
  2514. I + \n\
  2515. x - y x + y x + y x - y x - y x - y x - y x + y x + y x - y \n\
  2516. \n\
  2517. x + y x - 2yτ x - 2y x + y τ x + y x + y τ x + y x - 2yτ x - 2y x + y τ\
  2518. """
  2519. assert upretty(MIMOFeedback(tfm_1, tfm_2, 1)) == \
  2520. expected1 # Positive MIMOFeedback
  2521. assert upretty(MIMOFeedback(tfm_1*tfm_2, tfm_3)) == \
  2522. expected2 # Negative MIMOFeedback (Default)
  2523. def test_pretty_TransferFunctionMatrix():
  2524. tf1 = TransferFunction(x + y, x - 2*y, y)
  2525. tf2 = TransferFunction(x - y, x + y, y)
  2526. tf3 = TransferFunction(y**2 - 2*y + 1, y + 5, y)
  2527. tf4 = TransferFunction(y, x**2 + x + 1, y)
  2528. tf5 = TransferFunction(1 - x, x - y, y)
  2529. tf6 = TransferFunction(2, 2, y)
  2530. expected1 = \
  2531. """\
  2532. x + y \n\
  2533. \n\
  2534. x - 2y \n\
  2535. \n\
  2536. x - y \n\
  2537. \n\
  2538. x + y τ\
  2539. """
  2540. expected2 = \
  2541. """\
  2542. x + y \n\
  2543. \n\
  2544. x - 2y \n\
  2545. \n\
  2546. x - y \n\
  2547. \n\
  2548. x + y \n\
  2549. \n\
  2550. 2 \n\
  2551. - y + 2y - 1 \n\
  2552. \n\
  2553. y + 5 τ\
  2554. """
  2555. expected3 = \
  2556. """\
  2557. x + y x - y \n\
  2558. \n\
  2559. x - 2y x + y \n\
  2560. \n\
  2561. 2 \n\
  2562. y - 2y + 1 y \n\
  2563. \n\
  2564. y + 5 2 \n\
  2565. x + x + 1 \n\
  2566. \n\
  2567. 1 - x 2 \n\
  2568. \n\
  2569. x - y 2 τ\
  2570. """
  2571. expected4 = \
  2572. """\
  2573. x - y x + y y \n\
  2574. \n\
  2575. x + y x - 2y 2 \n\
  2576. x + x + 1 \n\
  2577. \n\
  2578. 2 \n\
  2579. - y + 2y - 1 x - 1 -2 \n\
  2580. \n\
  2581. y + 5 x - y 2 τ\
  2582. """
  2583. expected5 = \
  2584. """\
  2585. x + y x - y x + y y \n\
  2586. \n\
  2587. x - 2y x + y x - 2y 2 \n\
  2588. x + x + 1 \n\
  2589. \n\
  2590. 1 - x 2 x + y -2 \n\
  2591. + \n\
  2592. x - y 2 x - 2y 2 τ\
  2593. """
  2594. assert upretty(TransferFunctionMatrix([[tf1], [tf2]])) == expected1
  2595. assert upretty(TransferFunctionMatrix([[tf1], [tf2], [-tf3]])) == expected2
  2596. assert upretty(TransferFunctionMatrix([[tf1, tf2], [tf3, tf4], [tf5, tf6]])) == expected3
  2597. assert upretty(TransferFunctionMatrix([[tf2, tf1, tf4], [-tf3, -tf5, -tf6]])) == expected4
  2598. assert upretty(TransferFunctionMatrix([[Series(tf2, tf1), tf1, tf4], [Parallel(tf6, tf5), tf1, -tf6]])) == \
  2599. expected5
  2600. def test_pretty_order():
  2601. expr = O(1)
  2602. ascii_str = \
  2603. """\
  2604. O(1)\
  2605. """
  2606. ucode_str = \
  2607. """\
  2608. O(1)\
  2609. """
  2610. assert pretty(expr) == ascii_str
  2611. assert upretty(expr) == ucode_str
  2612. expr = O(1/x)
  2613. ascii_str = \
  2614. """\
  2615. /1\\\n\
  2616. O|-|\n\
  2617. \\x/\
  2618. """
  2619. ucode_str = \
  2620. """\
  2621. 1\n\
  2622. O\n\
  2623. x\
  2624. """
  2625. assert pretty(expr) == ascii_str
  2626. assert upretty(expr) == ucode_str
  2627. expr = O(x**2 + y**2)
  2628. ascii_str = \
  2629. """\
  2630. / 2 2 \\\n\
  2631. O\\x + y ; (x, y) -> (0, 0)/\
  2632. """
  2633. ucode_str = \
  2634. """\
  2635. 2 2 \n\
  2636. Ox + y ; (x, y) (0, 0)\
  2637. """
  2638. assert pretty(expr) == ascii_str
  2639. assert upretty(expr) == ucode_str
  2640. expr = O(1, (x, oo))
  2641. ascii_str = \
  2642. """\
  2643. O(1; x -> oo)\
  2644. """
  2645. ucode_str = \
  2646. """\
  2647. O(1; x )\
  2648. """
  2649. assert pretty(expr) == ascii_str
  2650. assert upretty(expr) == ucode_str
  2651. expr = O(1/x, (x, oo))
  2652. ascii_str = \
  2653. """\
  2654. /1 \\\n\
  2655. O|-; x -> oo|\n\
  2656. \\x /\
  2657. """
  2658. ucode_str = \
  2659. """\
  2660. 1 \n\
  2661. O; x \n\
  2662. x \
  2663. """
  2664. assert pretty(expr) == ascii_str
  2665. assert upretty(expr) == ucode_str
  2666. expr = O(x**2 + y**2, (x, oo), (y, oo))
  2667. ascii_str = \
  2668. """\
  2669. / 2 2 \\\n\
  2670. O\\x + y ; (x, y) -> (oo, oo)/\
  2671. """
  2672. ucode_str = \
  2673. """\
  2674. 2 2 \n\
  2675. Ox + y ; (x, y) (, )\
  2676. """
  2677. assert pretty(expr) == ascii_str
  2678. assert upretty(expr) == ucode_str
  2679. def test_pretty_derivatives():
  2680. # Simple
  2681. expr = Derivative(log(x), x, evaluate=False)
  2682. ascii_str = \
  2683. """\
  2684. d \n\
  2685. --(log(x))\n\
  2686. dx \
  2687. """
  2688. ucode_str = \
  2689. """\
  2690. d \n\
  2691. (log(x))\n\
  2692. dx \
  2693. """
  2694. assert pretty(expr) == ascii_str
  2695. assert upretty(expr) == ucode_str
  2696. expr = Derivative(log(x), x, evaluate=False) + x
  2697. ascii_str_1 = \
  2698. """\
  2699. d \n\
  2700. x + --(log(x))\n\
  2701. dx \
  2702. """
  2703. ascii_str_2 = \
  2704. """\
  2705. d \n\
  2706. --(log(x)) + x\n\
  2707. dx \
  2708. """
  2709. ucode_str_1 = \
  2710. """\
  2711. d \n\
  2712. x + (log(x))\n\
  2713. dx \
  2714. """
  2715. ucode_str_2 = \
  2716. """\
  2717. d \n\
  2718. (log(x)) + x\n\
  2719. dx \
  2720. """
  2721. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2722. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2723. # basic partial derivatives
  2724. expr = Derivative(log(x + y) + x, x)
  2725. ascii_str_1 = \
  2726. """\
  2727. d \n\
  2728. --(log(x + y) + x)\n\
  2729. dx \
  2730. """
  2731. ascii_str_2 = \
  2732. """\
  2733. d \n\
  2734. --(x + log(x + y))\n\
  2735. dx \
  2736. """
  2737. ucode_str_1 = \
  2738. """\
  2739. \n\
  2740. (log(x + y) + x)\n\
  2741. x \
  2742. """
  2743. ucode_str_2 = \
  2744. """\
  2745. \n\
  2746. (x + log(x + y))\n\
  2747. x \
  2748. """
  2749. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2750. assert upretty(expr) in [ucode_str_1, ucode_str_2], upretty(expr)
  2751. # Multiple symbols
  2752. expr = Derivative(log(x) + x**2, x, y)
  2753. ascii_str_1 = \
  2754. """\
  2755. 2 \n\
  2756. d / 2\\\n\
  2757. -----\\log(x) + x /\n\
  2758. dy dx \
  2759. """
  2760. ascii_str_2 = \
  2761. """\
  2762. 2 \n\
  2763. d / 2 \\\n\
  2764. -----\\x + log(x)/\n\
  2765. dy dx \
  2766. """
  2767. ucode_str_1 = \
  2768. """\
  2769. 2 \n\
  2770. d 2\n\
  2771. log(x) + x \n\
  2772. dy dx \
  2773. """
  2774. ucode_str_2 = \
  2775. """\
  2776. 2 \n\
  2777. d 2 \n\
  2778. x + log(x)\n\
  2779. dy dx \
  2780. """
  2781. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2782. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2783. expr = Derivative(2*x*y, y, x) + x**2
  2784. ascii_str_1 = \
  2785. """\
  2786. 2 \n\
  2787. d 2\n\
  2788. -----(2*x*y) + x \n\
  2789. dx dy \
  2790. """
  2791. ascii_str_2 = \
  2792. """\
  2793. 2 \n\
  2794. 2 d \n\
  2795. x + -----(2*x*y)\n\
  2796. dx dy \
  2797. """
  2798. ucode_str_1 = \
  2799. """\
  2800. 2 \n\
  2801. 2\n\
  2802. (2xy) + x \n\
  2803. x y \
  2804. """
  2805. ucode_str_2 = \
  2806. """\
  2807. 2 \n\
  2808. 2 \n\
  2809. x + (2xy)\n\
  2810. x y \
  2811. """
  2812. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2813. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2814. expr = Derivative(2*x*y, x, x)
  2815. ascii_str = \
  2816. """\
  2817. 2 \n\
  2818. d \n\
  2819. ---(2*x*y)\n\
  2820. 2 \n\
  2821. dx \
  2822. """
  2823. ucode_str = \
  2824. """\
  2825. 2 \n\
  2826. \n\
  2827. (2xy)\n\
  2828. 2 \n\
  2829. x \
  2830. """
  2831. assert pretty(expr) == ascii_str
  2832. assert upretty(expr) == ucode_str
  2833. expr = Derivative(2*x*y, x, 17)
  2834. ascii_str = \
  2835. """\
  2836. 17 \n\
  2837. d \n\
  2838. ----(2*x*y)\n\
  2839. 17 \n\
  2840. dx \
  2841. """
  2842. ucode_str = \
  2843. """\
  2844. 17 \n\
  2845. \n\
  2846. (2xy)\n\
  2847. 17 \n\
  2848. x \
  2849. """
  2850. assert pretty(expr) == ascii_str
  2851. assert upretty(expr) == ucode_str
  2852. expr = Derivative(2*x*y, x, x, y)
  2853. ascii_str = \
  2854. """\
  2855. 3 \n\
  2856. d \n\
  2857. ------(2*x*y)\n\
  2858. 2 \n\
  2859. dy dx \
  2860. """
  2861. ucode_str = \
  2862. """\
  2863. 3 \n\
  2864. \n\
  2865. (2xy)\n\
  2866. 2 \n\
  2867. y x \
  2868. """
  2869. assert pretty(expr) == ascii_str
  2870. assert upretty(expr) == ucode_str
  2871. # Greek letters
  2872. alpha = Symbol('alpha')
  2873. beta = Function('beta')
  2874. expr = beta(alpha).diff(alpha)
  2875. ascii_str = \
  2876. """\
  2877. d \n\
  2878. ------(beta(alpha))\n\
  2879. dalpha \
  2880. """
  2881. ucode_str = \
  2882. """\
  2883. d \n\
  2884. (β(α))\n\
  2885. \
  2886. """
  2887. assert pretty(expr) == ascii_str
  2888. assert upretty(expr) == ucode_str
  2889. expr = Derivative(f(x), (x, n))
  2890. ascii_str = \
  2891. """\
  2892. n \n\
  2893. d \n\
  2894. ---(f(x))\n\
  2895. n \n\
  2896. dx \
  2897. """
  2898. ucode_str = \
  2899. """\
  2900. n \n\
  2901. d \n\
  2902. (f(x))\n\
  2903. n \n\
  2904. dx \
  2905. """
  2906. assert pretty(expr) == ascii_str
  2907. assert upretty(expr) == ucode_str
  2908. def test_pretty_integrals():
  2909. expr = Integral(log(x), x)
  2910. ascii_str = \
  2911. """\
  2912. / \n\
  2913. | \n\
  2914. | log(x) dx\n\
  2915. | \n\
  2916. / \
  2917. """
  2918. ucode_str = \
  2919. """\
  2920. \n\
  2921. log(x) dx\n\
  2922. \
  2923. """
  2924. assert pretty(expr) == ascii_str
  2925. assert upretty(expr) == ucode_str
  2926. expr = Integral(x**2, x)
  2927. ascii_str = \
  2928. """\
  2929. / \n\
  2930. | \n\
  2931. | 2 \n\
  2932. | x dx\n\
  2933. | \n\
  2934. / \
  2935. """
  2936. ucode_str = \
  2937. """\
  2938. \n\
  2939. 2 \n\
  2940. x dx\n\
  2941. \
  2942. """
  2943. assert pretty(expr) == ascii_str
  2944. assert upretty(expr) == ucode_str
  2945. expr = Integral((sin(x))**2 / (tan(x))**2)
  2946. ascii_str = \
  2947. """\
  2948. / \n\
  2949. | \n\
  2950. | 2 \n\
  2951. | sin (x) \n\
  2952. | ------- dx\n\
  2953. | 2 \n\
  2954. | tan (x) \n\
  2955. | \n\
  2956. / \
  2957. """
  2958. ucode_str = \
  2959. """\
  2960. \n\
  2961. 2 \n\
  2962. sin (x) \n\
  2963. dx\n\
  2964. 2 \n\
  2965. tan (x) \n\
  2966. \
  2967. """
  2968. assert pretty(expr) == ascii_str
  2969. assert upretty(expr) == ucode_str
  2970. expr = Integral(x**(2**x), x)
  2971. ascii_str = \
  2972. """\
  2973. / \n\
  2974. | \n\
  2975. | / x\\ \n\
  2976. | \\2 / \n\
  2977. | x dx\n\
  2978. | \n\
  2979. / \
  2980. """
  2981. ucode_str = \
  2982. """\
  2983. \n\
  2984. x \n\
  2985. 2 \n\
  2986. x dx\n\
  2987. \
  2988. """
  2989. assert pretty(expr) == ascii_str
  2990. assert upretty(expr) == ucode_str
  2991. expr = Integral(x**2, (x, 1, 2))
  2992. ascii_str = \
  2993. """\
  2994. 2 \n\
  2995. / \n\
  2996. | \n\
  2997. | 2 \n\
  2998. | x dx\n\
  2999. | \n\
  3000. / \n\
  3001. 1 \
  3002. """
  3003. ucode_str = \
  3004. """\
  3005. 2 \n\
  3006. \n\
  3007. 2 \n\
  3008. x dx\n\
  3009. \n\
  3010. 1 \
  3011. """
  3012. assert pretty(expr) == ascii_str
  3013. assert upretty(expr) == ucode_str
  3014. expr = Integral(x**2, (x, Rational(1, 2), 10))
  3015. ascii_str = \
  3016. """\
  3017. 10 \n\
  3018. / \n\
  3019. | \n\
  3020. | 2 \n\
  3021. | x dx\n\
  3022. | \n\
  3023. / \n\
  3024. 1/2 \
  3025. """
  3026. ucode_str = \
  3027. """\
  3028. 10 \n\
  3029. \n\
  3030. 2 \n\
  3031. x dx\n\
  3032. \n\
  3033. 1/2 \
  3034. """
  3035. assert pretty(expr) == ascii_str
  3036. assert upretty(expr) == ucode_str
  3037. expr = Integral(x**2*y**2, x, y)
  3038. ascii_str = \
  3039. """\
  3040. / / \n\
  3041. | | \n\
  3042. | | 2 2 \n\
  3043. | | x *y dx dy\n\
  3044. | | \n\
  3045. / / \
  3046. """
  3047. ucode_str = \
  3048. """\
  3049. \n\
  3050. 2 2 \n\
  3051. x y dx dy\n\
  3052. \
  3053. """
  3054. assert pretty(expr) == ascii_str
  3055. assert upretty(expr) == ucode_str
  3056. expr = Integral(sin(th)/cos(ph), (th, 0, pi), (ph, 0, 2*pi))
  3057. ascii_str = \
  3058. """\
  3059. 2*pi pi \n\
  3060. / / \n\
  3061. | | \n\
  3062. | | sin(theta) \n\
  3063. | | ---------- d(theta) d(phi)\n\
  3064. | | cos(phi) \n\
  3065. | | \n\
  3066. / / \n\
  3067. 0 0 \
  3068. """
  3069. ucode_str = \
  3070. """\
  3071. 2π π \n\
  3072. \n\
  3073. sin(θ) \n\
  3074. \n\
  3075. cos(φ) \n\
  3076. \n\
  3077. 0 0 \
  3078. """
  3079. assert pretty(expr) == ascii_str
  3080. assert upretty(expr) == ucode_str
  3081. def test_pretty_matrix():
  3082. # Empty Matrix
  3083. expr = Matrix()
  3084. ascii_str = "[]"
  3085. unicode_str = "[]"
  3086. assert pretty(expr) == ascii_str
  3087. assert upretty(expr) == unicode_str
  3088. expr = Matrix(2, 0, lambda i, j: 0)
  3089. ascii_str = "[]"
  3090. unicode_str = "[]"
  3091. assert pretty(expr) == ascii_str
  3092. assert upretty(expr) == unicode_str
  3093. expr = Matrix(0, 2, lambda i, j: 0)
  3094. ascii_str = "[]"
  3095. unicode_str = "[]"
  3096. assert pretty(expr) == ascii_str
  3097. assert upretty(expr) == unicode_str
  3098. expr = Matrix([[x**2 + 1, 1], [y, x + y]])
  3099. ascii_str_1 = \
  3100. """\
  3101. [ 2 ]
  3102. [1 + x 1 ]
  3103. [ ]
  3104. [ y x + y]\
  3105. """
  3106. ascii_str_2 = \
  3107. """\
  3108. [ 2 ]
  3109. [x + 1 1 ]
  3110. [ ]
  3111. [ y x + y]\
  3112. """
  3113. ucode_str_1 = \
  3114. """\
  3115. 2
  3116. 1 + x 1
  3117. y x + y\
  3118. """
  3119. ucode_str_2 = \
  3120. """\
  3121. 2
  3122. x + 1 1
  3123. y x + y\
  3124. """
  3125. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  3126. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  3127. expr = Matrix([[x/y, y, th], [0, exp(I*k*ph), 1]])
  3128. ascii_str = \
  3129. """\
  3130. [x ]
  3131. [- y theta]
  3132. [y ]
  3133. [ ]
  3134. [ I*k*phi ]
  3135. [0 e 1 ]\
  3136. """
  3137. ucode_str = \
  3138. """\
  3139. x
  3140. y θ
  3141. y
  3142. kφ
  3143. 0 1\
  3144. """
  3145. assert pretty(expr) == ascii_str
  3146. assert upretty(expr) == ucode_str
  3147. unicode_str = \
  3148. """\
  3149. v̇_msc_00 0 0
  3150. 0 v̇_msc_01 0
  3151. 0 0 v̇_msc_02\
  3152. """
  3153. expr = diag(*MatrixSymbol('vdot_msc',1,3))
  3154. assert upretty(expr) == unicode_str
  3155. def test_pretty_ndim_arrays():
  3156. x, y, z, w = symbols("x y z w")
  3157. for ArrayType in (ImmutableDenseNDimArray, ImmutableSparseNDimArray, MutableDenseNDimArray, MutableSparseNDimArray):
  3158. # Basic: scalar array
  3159. M = ArrayType(x)
  3160. assert pretty(M) == "x"
  3161. assert upretty(M) == "x"
  3162. M = ArrayType([[1/x, y], [z, w]])
  3163. M1 = ArrayType([1/x, y, z])
  3164. M2 = tensorproduct(M1, M)
  3165. M3 = tensorproduct(M, M)
  3166. ascii_str = \
  3167. """\
  3168. [1 ]\n\
  3169. [- y]\n\
  3170. [x ]\n\
  3171. [ ]\n\
  3172. [z w]\
  3173. """
  3174. ucode_str = \
  3175. """\
  3176. 1 \n\
  3177. y\n\
  3178. x \n\
  3179. \n\
  3180. z w\
  3181. """
  3182. assert pretty(M) == ascii_str
  3183. assert upretty(M) == ucode_str
  3184. ascii_str = \
  3185. """\
  3186. [1 ]\n\
  3187. [- y z]\n\
  3188. [x ]\
  3189. """
  3190. ucode_str = \
  3191. """\
  3192. 1 \n\
  3193. y z\n\
  3194. x \
  3195. """
  3196. assert pretty(M1) == ascii_str
  3197. assert upretty(M1) == ucode_str
  3198. ascii_str = \
  3199. """\
  3200. [[1 y] ]\n\
  3201. [[-- -] [z ]]\n\
  3202. [[ 2 x] [ y 2 ] [- y*z]]\n\
  3203. [[x ] [ - y ] [x ]]\n\
  3204. [[ ] [ x ] [ ]]\n\
  3205. [[z w] [ ] [ 2 ]]\n\
  3206. [[- -] [y*z w*y] [z w*z]]\n\
  3207. [[x x] ]\
  3208. """
  3209. ucode_str = \
  3210. """\
  3211. 1 y \n\
  3212. z \n\
  3213. 2 x y 2 yz\n\
  3214. x y x \n\
  3215. x \n\
  3216. z w 2 \n\
  3217. yz wy z wz\n\
  3218. x x \
  3219. """
  3220. assert pretty(M2) == ascii_str
  3221. assert upretty(M2) == ucode_str
  3222. ascii_str = \
  3223. """\
  3224. [ [1 y] ]\n\
  3225. [ [-- -] ]\n\
  3226. [ [ 2 x] [ y 2 ]]\n\
  3227. [ [x ] [ - y ]]\n\
  3228. [ [ ] [ x ]]\n\
  3229. [ [z w] [ ]]\n\
  3230. [ [- -] [y*z w*y]]\n\
  3231. [ [x x] ]\n\
  3232. [ ]\n\
  3233. [[z ] [ w ]]\n\
  3234. [[- y*z] [ - w*y]]\n\
  3235. [[x ] [ x ]]\n\
  3236. [[ ] [ ]]\n\
  3237. [[ 2 ] [ 2 ]]\n\
  3238. [[z w*z] [w*z w ]]\
  3239. """
  3240. ucode_str = \
  3241. """\
  3242. 1 y \n\
  3243. \n\
  3244. 2 x y 2 \n\
  3245. x y \n\
  3246. x \n\
  3247. z w \n\
  3248. yz wy\n\
  3249. x x \n\
  3250. \n\
  3251. z w \n\
  3252. yz wy\n\
  3253. x x \n\
  3254. \n\
  3255. 2 2 \n\
  3256. z wz wz w \
  3257. """
  3258. assert pretty(M3) == ascii_str
  3259. assert upretty(M3) == ucode_str
  3260. Mrow = ArrayType([[x, y, 1 / z]])
  3261. Mcolumn = ArrayType([[x], [y], [1 / z]])
  3262. Mcol2 = ArrayType([Mcolumn.tolist()])
  3263. ascii_str = \
  3264. """\
  3265. [[ 1]]\n\
  3266. [[x y -]]\n\
  3267. [[ z]]\
  3268. """
  3269. ucode_str = \
  3270. """\
  3271. 1\n\
  3272. x y \n\
  3273. z\
  3274. """
  3275. assert pretty(Mrow) == ascii_str
  3276. assert upretty(Mrow) == ucode_str
  3277. ascii_str = \
  3278. """\
  3279. [x]\n\
  3280. [ ]\n\
  3281. [y]\n\
  3282. [ ]\n\
  3283. [1]\n\
  3284. [-]\n\
  3285. [z]\
  3286. """
  3287. ucode_str = \
  3288. """\
  3289. x\n\
  3290. \n\
  3291. y\n\
  3292. \n\
  3293. 1\n\
  3294. \n\
  3295. z\
  3296. """
  3297. assert pretty(Mcolumn) == ascii_str
  3298. assert upretty(Mcolumn) == ucode_str
  3299. ascii_str = \
  3300. """\
  3301. [[x]]\n\
  3302. [[ ]]\n\
  3303. [[y]]\n\
  3304. [[ ]]\n\
  3305. [[1]]\n\
  3306. [[-]]\n\
  3307. [[z]]\
  3308. """
  3309. ucode_str = \
  3310. """\
  3311. x\n\
  3312. \n\
  3313. y\n\
  3314. \n\
  3315. 1\n\
  3316. \n\
  3317. z\
  3318. """
  3319. assert pretty(Mcol2) == ascii_str
  3320. assert upretty(Mcol2) == ucode_str
  3321. def test_tensor_TensorProduct():
  3322. A = MatrixSymbol("A", 3, 3)
  3323. B = MatrixSymbol("B", 3, 3)
  3324. assert upretty(TensorProduct(A, B)) == "A\u2297B"
  3325. assert upretty(TensorProduct(A, B, A)) == "A\u2297B\u2297A"
  3326. def test_diffgeom_print_WedgeProduct():
  3327. from sympy.diffgeom.rn import R2
  3328. from sympy.diffgeom import WedgeProduct
  3329. wp = WedgeProduct(R2.dx, R2.dy)
  3330. assert upretty(wp) == "ⅆ x∧ⅆ y"
  3331. assert pretty(wp) == r"d x/\d y"
  3332. def test_Adjoint():
  3333. X = MatrixSymbol('X', 2, 2)
  3334. Y = MatrixSymbol('Y', 2, 2)
  3335. assert pretty(Adjoint(X)) == " +\nX "
  3336. assert pretty(Adjoint(X + Y)) == " +\n(X + Y) "
  3337. assert pretty(Adjoint(X) + Adjoint(Y)) == " + +\nX + Y "
  3338. assert pretty(Adjoint(X*Y)) == " +\n(X*Y) "
  3339. assert pretty(Adjoint(Y)*Adjoint(X)) == " + +\nY *X "
  3340. assert pretty(Adjoint(X**2)) == " +\n/ 2\\ \n\\X / "
  3341. assert pretty(Adjoint(X)**2) == " 2\n/ +\\ \n\\X / "
  3342. assert pretty(Adjoint(Inverse(X))) == " +\n/ -1\\ \n\\X / "
  3343. assert pretty(Inverse(Adjoint(X))) == " -1\n/ +\\ \n\\X / "
  3344. assert pretty(Adjoint(Transpose(X))) == " +\n/ T\\ \n\\X / "
  3345. assert pretty(Transpose(Adjoint(X))) == " T\n/ +\\ \n\\X / "
  3346. assert upretty(Adjoint(X)) == "\nX "
  3347. assert upretty(Adjoint(X + Y)) == "\n(X + Y) "
  3348. assert upretty(Adjoint(X) + Adjoint(Y)) == " † †\nX + Y "
  3349. assert upretty(Adjoint(X*Y)) == "\n(X⋅Y) "
  3350. assert upretty(Adjoint(Y)*Adjoint(X)) == " † †\nY ⋅X "
  3351. assert upretty(Adjoint(X**2)) == \
  3352. "\n⎛ 2⎞ \n⎝X ⎠ "
  3353. assert upretty(Adjoint(X)**2) == \
  3354. " 2\n⎛ †⎞ \n⎝X ⎠ "
  3355. assert upretty(Adjoint(Inverse(X))) == \
  3356. "\n⎛ -1⎞ \n⎝X ⎠ "
  3357. assert upretty(Inverse(Adjoint(X))) == \
  3358. " -1\n⎛ †⎞ \n⎝X ⎠ "
  3359. assert upretty(Adjoint(Transpose(X))) == \
  3360. "\n⎛ T⎞ \n⎝X ⎠ "
  3361. assert upretty(Transpose(Adjoint(X))) == \
  3362. " T\n⎛ †⎞ \n⎝X ⎠ "
  3363. m = Matrix(((1, 2), (3, 4)))
  3364. assert upretty(Adjoint(m)) == \
  3365. '\n'\
  3366. '⎡1 2⎤ \n'\
  3367. '⎢ ⎥ \n'\
  3368. '⎣3 4⎦ '
  3369. assert upretty(Adjoint(m+X)) == \
  3370. '\n'\
  3371. '⎛⎡1 2⎤ ⎞ \n'\
  3372. '⎜⎢ ⎥ + X⎟ \n'\
  3373. '⎝⎣3 4⎦ ⎠ '
  3374. def test_pretty_Trace_issue_9044():
  3375. X = Matrix([[1, 2], [3, 4]])
  3376. Y = Matrix([[2, 4], [6, 8]])
  3377. ascii_str_1 = \
  3378. """\
  3379. /[1 2]\\
  3380. tr|[ ]|
  3381. \\[3 4]/\
  3382. """
  3383. ucode_str_1 = \
  3384. """\
  3385. 1 2
  3386. tr
  3387. 3 4\
  3388. """
  3389. ascii_str_2 = \
  3390. """\
  3391. /[1 2]\\ /[2 4]\\
  3392. tr|[ ]| + tr|[ ]|
  3393. \\[3 4]/ \\[6 8]/\
  3394. """
  3395. ucode_str_2 = \
  3396. """\
  3397. 1 2 2 4
  3398. tr + tr
  3399. 3 4 6 8\
  3400. """
  3401. assert pretty(Trace(X)) == ascii_str_1
  3402. assert upretty(Trace(X)) == ucode_str_1
  3403. assert pretty(Trace(X) + Trace(Y)) == ascii_str_2
  3404. assert upretty(Trace(X) + Trace(Y)) == ucode_str_2
  3405. def test_MatrixSlice():
  3406. n = Symbol('n', integer=True)
  3407. x, y, z, w, t, = symbols('x y z w t')
  3408. X = MatrixSymbol('X', n, n)
  3409. Y = MatrixSymbol('Y', 10, 10)
  3410. Z = MatrixSymbol('Z', 10, 10)
  3411. expr = MatrixSlice(X, (None, None, None), (None, None, None))
  3412. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3413. expr = X[x:x + 1, y:y + 1]
  3414. assert pretty(expr) == upretty(expr) == 'X[x:x + 1, y:y + 1]'
  3415. expr = X[x:x + 1:2, y:y + 1:2]
  3416. assert pretty(expr) == upretty(expr) == 'X[x:x + 1:2, y:y + 1:2]'
  3417. expr = X[:x, y:]
  3418. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3419. expr = X[:x, y:]
  3420. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3421. expr = X[x:, :y]
  3422. assert pretty(expr) == upretty(expr) == 'X[x:, :y]'
  3423. expr = X[x:y, z:w]
  3424. assert pretty(expr) == upretty(expr) == 'X[x:y, z:w]'
  3425. expr = X[x:y:t, w:t:x]
  3426. assert pretty(expr) == upretty(expr) == 'X[x:y:t, w:t:x]'
  3427. expr = X[x::y, t::w]
  3428. assert pretty(expr) == upretty(expr) == 'X[x::y, t::w]'
  3429. expr = X[:x:y, :t:w]
  3430. assert pretty(expr) == upretty(expr) == 'X[:x:y, :t:w]'
  3431. expr = X[::x, ::y]
  3432. assert pretty(expr) == upretty(expr) == 'X[::x, ::y]'
  3433. expr = MatrixSlice(X, (0, None, None), (0, None, None))
  3434. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3435. expr = MatrixSlice(X, (None, n, None), (None, n, None))
  3436. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3437. expr = MatrixSlice(X, (0, n, None), (0, n, None))
  3438. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3439. expr = MatrixSlice(X, (0, n, 2), (0, n, 2))
  3440. assert pretty(expr) == upretty(expr) == 'X[::2, ::2]'
  3441. expr = X[1:2:3, 4:5:6]
  3442. assert pretty(expr) == upretty(expr) == 'X[1:2:3, 4:5:6]'
  3443. expr = X[1:3:5, 4:6:8]
  3444. assert pretty(expr) == upretty(expr) == 'X[1:3:5, 4:6:8]'
  3445. expr = X[1:10:2]
  3446. assert pretty(expr) == upretty(expr) == 'X[1:10:2, :]'
  3447. expr = Y[:5, 1:9:2]
  3448. assert pretty(expr) == upretty(expr) == 'Y[:5, 1:9:2]'
  3449. expr = Y[:5, 1:10:2]
  3450. assert pretty(expr) == upretty(expr) == 'Y[:5, 1::2]'
  3451. expr = Y[5, :5:2]
  3452. assert pretty(expr) == upretty(expr) == 'Y[5:6, :5:2]'
  3453. expr = X[0:1, 0:1]
  3454. assert pretty(expr) == upretty(expr) == 'X[:1, :1]'
  3455. expr = X[0:1:2, 0:1:2]
  3456. assert pretty(expr) == upretty(expr) == 'X[:1:2, :1:2]'
  3457. expr = (Y + Z)[2:, 2:]
  3458. assert pretty(expr) == upretty(expr) == '(Y + Z)[2:, 2:]'
  3459. def test_MatrixExpressions():
  3460. n = Symbol('n', integer=True)
  3461. X = MatrixSymbol('X', n, n)
  3462. assert pretty(X) == upretty(X) == "X"
  3463. # Apply function elementwise (`ElementwiseApplyFunc`):
  3464. expr = (X.T*X).applyfunc(sin)
  3465. ascii_str = """\
  3466. / T \\\n\
  3467. (d -> sin(d)).\\X *X/\
  3468. """
  3469. ucode_str = """\
  3470. T \n\
  3471. (d sin(d))˳X X\
  3472. """
  3473. assert pretty(expr) == ascii_str
  3474. assert upretty(expr) == ucode_str
  3475. lamda = Lambda(x, 1/x)
  3476. expr = (n*X).applyfunc(lamda)
  3477. ascii_str = """\
  3478. / 1\\ \n\
  3479. |x -> -|.(n*X)\n\
  3480. \\ x/ \
  3481. """
  3482. ucode_str = """\
  3483. 1 \n\
  3484. x ˳(nX)\n\
  3485. x \
  3486. """
  3487. assert pretty(expr) == ascii_str
  3488. assert upretty(expr) == ucode_str
  3489. def test_pretty_dotproduct():
  3490. from sympy.matrices.expressions.dotproduct import DotProduct
  3491. n = symbols("n", integer=True)
  3492. A = MatrixSymbol('A', n, 1)
  3493. B = MatrixSymbol('B', n, 1)
  3494. C = Matrix(1, 3, [1, 2, 3])
  3495. D = Matrix(1, 3, [1, 3, 4])
  3496. assert pretty(DotProduct(A, B)) == "A*B"
  3497. assert pretty(DotProduct(C, D)) == "[1 2 3]*[1 3 4]"
  3498. assert upretty(DotProduct(A, B)) == "A⋅B"
  3499. assert upretty(DotProduct(C, D)) == "[1 2 3]⋅[1 3 4]"
  3500. def test_pretty_piecewise():
  3501. expr = Piecewise((x, x < 1), (x**2, True))
  3502. ascii_str = \
  3503. """\
  3504. /x for x < 1\n\
  3505. | \n\
  3506. < 2 \n\
  3507. |x otherwise\n\
  3508. \\ \
  3509. """
  3510. ucode_str = \
  3511. """\
  3512. x for x < 1\n\
  3513. \n\
  3514. 2 \n\
  3515. x otherwise\n\
  3516. \
  3517. """
  3518. assert pretty(expr) == ascii_str
  3519. assert upretty(expr) == ucode_str
  3520. expr = -Piecewise((x, x < 1), (x**2, True))
  3521. ascii_str = \
  3522. """\
  3523. //x for x < 1\\\n\
  3524. || |\n\
  3525. -|< 2 |\n\
  3526. ||x otherwise|\n\
  3527. \\\\ /\
  3528. """
  3529. ucode_str = \
  3530. """\
  3531. x for x < 1\n\
  3532. \n\
  3533. - 2 \n\
  3534. x otherwise\n\
  3535. \
  3536. """
  3537. assert pretty(expr) == ascii_str
  3538. assert upretty(expr) == ucode_str
  3539. expr = x + Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3540. (y**2, x > 2), (1, True)) + 1
  3541. ascii_str = \
  3542. """\
  3543. //x \\ \n\
  3544. ||- for x < 2| \n\
  3545. ||y | \n\
  3546. //x for x > 0\\ || | \n\
  3547. x + |< | + |< 2 | + 1\n\
  3548. \\\\y otherwise/ ||y for x > 2| \n\
  3549. || | \n\
  3550. ||1 otherwise| \n\
  3551. \\\\ / \
  3552. """
  3553. ucode_str = \
  3554. """\
  3555. x \n\
  3556. for x < 2 \n\
  3557. y \n\
  3558. x for x > 0 \n\
  3559. x + + 2 + 1\n\
  3560. y otherwise y for x > 2 \n\
  3561. \n\
  3562. 1 otherwise \n\
  3563. \
  3564. """
  3565. assert pretty(expr) == ascii_str
  3566. assert upretty(expr) == ucode_str
  3567. expr = x - Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3568. (y**2, x > 2), (1, True)) + 1
  3569. ascii_str = \
  3570. """\
  3571. //x \\ \n\
  3572. ||- for x < 2| \n\
  3573. ||y | \n\
  3574. //x for x > 0\\ || | \n\
  3575. x - |< | + |< 2 | + 1\n\
  3576. \\\\y otherwise/ ||y for x > 2| \n\
  3577. || | \n\
  3578. ||1 otherwise| \n\
  3579. \\\\ / \
  3580. """
  3581. ucode_str = \
  3582. """\
  3583. x \n\
  3584. for x < 2 \n\
  3585. y \n\
  3586. x for x > 0 \n\
  3587. x - + 2 + 1\n\
  3588. y otherwise y for x > 2 \n\
  3589. \n\
  3590. 1 otherwise \n\
  3591. \
  3592. """
  3593. assert pretty(expr) == ascii_str
  3594. assert upretty(expr) == ucode_str
  3595. expr = x*Piecewise((x, x > 0), (y, True))
  3596. ascii_str = \
  3597. """\
  3598. //x for x > 0\\\n\
  3599. x*|< |\n\
  3600. \\\\y otherwise/\
  3601. """
  3602. ucode_str = \
  3603. """\
  3604. x for x > 0\n\
  3605. x \n\
  3606. y otherwise\
  3607. """
  3608. assert pretty(expr) == ascii_str
  3609. assert upretty(expr) == ucode_str
  3610. expr = Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x >
  3611. 2), (1, True))
  3612. ascii_str = \
  3613. """\
  3614. //x \\\n\
  3615. ||- for x < 2|\n\
  3616. ||y |\n\
  3617. //x for x > 0\\ || |\n\
  3618. |< |*|< 2 |\n\
  3619. \\\\y otherwise/ ||y for x > 2|\n\
  3620. || |\n\
  3621. ||1 otherwise|\n\
  3622. \\\\ /\
  3623. """
  3624. ucode_str = \
  3625. """\
  3626. x \n\
  3627. for x < 2\n\
  3628. y \n\
  3629. x for x > 0 \n\
  3630. 2 \n\
  3631. y otherwise y for x > 2\n\
  3632. \n\
  3633. 1 otherwise\n\
  3634. \
  3635. """
  3636. assert pretty(expr) == ascii_str
  3637. assert upretty(expr) == ucode_str
  3638. expr = -Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x
  3639. > 2), (1, True))
  3640. ascii_str = \
  3641. """\
  3642. //x \\\n\
  3643. ||- for x < 2|\n\
  3644. ||y |\n\
  3645. //x for x > 0\\ || |\n\
  3646. -|< |*|< 2 |\n\
  3647. \\\\y otherwise/ ||y for x > 2|\n\
  3648. || |\n\
  3649. ||1 otherwise|\n\
  3650. \\\\ /\
  3651. """
  3652. ucode_str = \
  3653. """\
  3654. x \n\
  3655. for x < 2\n\
  3656. y \n\
  3657. x for x > 0 \n\
  3658. - 2 \n\
  3659. y otherwise y for x > 2\n\
  3660. \n\
  3661. 1 otherwise\n\
  3662. \
  3663. """
  3664. assert pretty(expr) == ascii_str
  3665. assert upretty(expr) == ucode_str
  3666. expr = Piecewise((0, Abs(1/y) < 1), (1, Abs(y) < 1), (y*meijerg(((2, 1),
  3667. ()), ((), (1, 0)), 1/y), True))
  3668. ascii_str = \
  3669. """\
  3670. / 1 \n\
  3671. | 0 for --- < 1\n\
  3672. | |y| \n\
  3673. | \n\
  3674. < 1 for |y| < 1\n\
  3675. | \n\
  3676. | __0, 2 /2, 1 | 1\\ \n\
  3677. |y*/__ | | -| otherwise \n\
  3678. \\ \\_|2, 2 \\ 1, 0 | y/ \
  3679. """
  3680. ucode_str = \
  3681. """\
  3682. 1 \n\
  3683. 0 for < 1\n\
  3684. y \n\
  3685. \n\
  3686. 1 for y < 1\n\
  3687. \n\
  3688. 0, 2 2, 1 1 \n\
  3689. y otherwise \n\
  3690. 2, 2 1, 0 y \
  3691. """
  3692. assert pretty(expr) == ascii_str
  3693. assert upretty(expr) == ucode_str
  3694. # XXX: We have to use evaluate=False here because Piecewise._eval_power
  3695. # denests the power.
  3696. expr = Pow(Piecewise((x, x > 0), (y, True)), 2, evaluate=False)
  3697. ascii_str = \
  3698. """\
  3699. 2\n\
  3700. //x for x > 0\\ \n\
  3701. |< | \n\
  3702. \\\\y otherwise/ \
  3703. """
  3704. ucode_str = \
  3705. """\
  3706. 2\n\
  3707. x for x > 0 \n\
  3708. \n\
  3709. y otherwise \
  3710. """
  3711. assert pretty(expr) == ascii_str
  3712. assert upretty(expr) == ucode_str
  3713. def test_pretty_ITE():
  3714. expr = ITE(x, y, z)
  3715. assert pretty(expr) == (
  3716. '/y for x \n'
  3717. '< \n'
  3718. '\\z otherwise'
  3719. )
  3720. assert upretty(expr) == """\
  3721. y for x \n\
  3722. \n\
  3723. z otherwise\
  3724. """
  3725. def test_pretty_seq():
  3726. expr = ()
  3727. ascii_str = \
  3728. """\
  3729. ()\
  3730. """
  3731. ucode_str = \
  3732. """\
  3733. ()\
  3734. """
  3735. assert pretty(expr) == ascii_str
  3736. assert upretty(expr) == ucode_str
  3737. expr = []
  3738. ascii_str = \
  3739. """\
  3740. []\
  3741. """
  3742. ucode_str = \
  3743. """\
  3744. []\
  3745. """
  3746. assert pretty(expr) == ascii_str
  3747. assert upretty(expr) == ucode_str
  3748. expr = {}
  3749. expr_2 = {}
  3750. ascii_str = \
  3751. """\
  3752. {}\
  3753. """
  3754. ucode_str = \
  3755. """\
  3756. {}\
  3757. """
  3758. assert pretty(expr) == ascii_str
  3759. assert pretty(expr_2) == ascii_str
  3760. assert upretty(expr) == ucode_str
  3761. assert upretty(expr_2) == ucode_str
  3762. expr = (1/x,)
  3763. ascii_str = \
  3764. """\
  3765. 1 \n\
  3766. (-,)\n\
  3767. x \
  3768. """
  3769. ucode_str = \
  3770. """\
  3771. 1 \n\
  3772. ,\n\
  3773. x \
  3774. """
  3775. assert pretty(expr) == ascii_str
  3776. assert upretty(expr) == ucode_str
  3777. expr = [x**2, 1/x, x, y, sin(th)**2/cos(ph)**2]
  3778. ascii_str = \
  3779. """\
  3780. 2 \n\
  3781. 2 1 sin (theta) \n\
  3782. [x , -, x, y, -----------]\n\
  3783. x 2 \n\
  3784. cos (phi) \
  3785. """
  3786. ucode_str = \
  3787. """\
  3788. 2 \n\
  3789. 2 1 sin (θ)\n\
  3790. x , , x, y, \n\
  3791. x 2 \n\
  3792. cos (φ)\
  3793. """
  3794. assert pretty(expr) == ascii_str
  3795. assert upretty(expr) == ucode_str
  3796. expr = (x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3797. ascii_str = \
  3798. """\
  3799. 2 \n\
  3800. 2 1 sin (theta) \n\
  3801. (x , -, x, y, -----------)\n\
  3802. x 2 \n\
  3803. cos (phi) \
  3804. """
  3805. ucode_str = \
  3806. """\
  3807. 2 \n\
  3808. 2 1 sin (θ)\n\
  3809. x , , x, y, \n\
  3810. x 2 \n\
  3811. cos (φ)\
  3812. """
  3813. assert pretty(expr) == ascii_str
  3814. assert upretty(expr) == ucode_str
  3815. expr = Tuple(x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3816. ascii_str = \
  3817. """\
  3818. 2 \n\
  3819. 2 1 sin (theta) \n\
  3820. (x , -, x, y, -----------)\n\
  3821. x 2 \n\
  3822. cos (phi) \
  3823. """
  3824. ucode_str = \
  3825. """\
  3826. 2 \n\
  3827. 2 1 sin (θ)\n\
  3828. x , , x, y, \n\
  3829. x 2 \n\
  3830. cos (φ)\
  3831. """
  3832. assert pretty(expr) == ascii_str
  3833. assert upretty(expr) == ucode_str
  3834. expr = {x: sin(x)}
  3835. expr_2 = Dict({x: sin(x)})
  3836. ascii_str = \
  3837. """\
  3838. {x: sin(x)}\
  3839. """
  3840. ucode_str = \
  3841. """\
  3842. {x: sin(x)}\
  3843. """
  3844. assert pretty(expr) == ascii_str
  3845. assert pretty(expr_2) == ascii_str
  3846. assert upretty(expr) == ucode_str
  3847. assert upretty(expr_2) == ucode_str
  3848. expr = {1/x: 1/y, x: sin(x)**2}
  3849. expr_2 = Dict({1/x: 1/y, x: sin(x)**2})
  3850. ascii_str = \
  3851. """\
  3852. 1 1 2 \n\
  3853. {-: -, x: sin (x)}\n\
  3854. x y \
  3855. """
  3856. ucode_str = \
  3857. """\
  3858. 1 1 2 \n\
  3859. : , x: sin (x)\n\
  3860. x y \
  3861. """
  3862. assert pretty(expr) == ascii_str
  3863. assert pretty(expr_2) == ascii_str
  3864. assert upretty(expr) == ucode_str
  3865. assert upretty(expr_2) == ucode_str
  3866. # There used to be a bug with pretty-printing sequences of even height.
  3867. expr = [x**2]
  3868. ascii_str = \
  3869. """\
  3870. 2 \n\
  3871. [x ]\
  3872. """
  3873. ucode_str = \
  3874. """\
  3875. 2\n\
  3876. x \
  3877. """
  3878. assert pretty(expr) == ascii_str
  3879. assert upretty(expr) == ucode_str
  3880. expr = (x**2,)
  3881. ascii_str = \
  3882. """\
  3883. 2 \n\
  3884. (x ,)\
  3885. """
  3886. ucode_str = \
  3887. """\
  3888. 2 \n\
  3889. x ,\
  3890. """
  3891. assert pretty(expr) == ascii_str
  3892. assert upretty(expr) == ucode_str
  3893. expr = Tuple(x**2)
  3894. ascii_str = \
  3895. """\
  3896. 2 \n\
  3897. (x ,)\
  3898. """
  3899. ucode_str = \
  3900. """\
  3901. 2 \n\
  3902. x ,\
  3903. """
  3904. assert pretty(expr) == ascii_str
  3905. assert upretty(expr) == ucode_str
  3906. expr = {x**2: 1}
  3907. expr_2 = Dict({x**2: 1})
  3908. ascii_str = \
  3909. """\
  3910. 2 \n\
  3911. {x : 1}\
  3912. """
  3913. ucode_str = \
  3914. """\
  3915. 2 \n\
  3916. x : 1\n\
  3917. \
  3918. """
  3919. assert pretty(expr) == ascii_str
  3920. assert pretty(expr_2) == ascii_str
  3921. assert upretty(expr) == ucode_str
  3922. assert upretty(expr_2) == ucode_str
  3923. def test_any_object_in_sequence():
  3924. # Cf. issue 5306
  3925. b1 = Basic()
  3926. b2 = Basic(Basic())
  3927. expr = [b2, b1]
  3928. assert pretty(expr) == "[Basic(Basic()), Basic()]"
  3929. assert upretty(expr) == "[Basic(Basic()), Basic()]"
  3930. expr = {b2, b1}
  3931. assert pretty(expr) == "{Basic(), Basic(Basic())}"
  3932. assert upretty(expr) == "{Basic(), Basic(Basic())}"
  3933. expr = {b2: b1, b1: b2}
  3934. expr2 = Dict({b2: b1, b1: b2})
  3935. assert pretty(expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  3936. assert pretty(
  3937. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  3938. assert upretty(
  3939. expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  3940. assert upretty(
  3941. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  3942. def test_print_builtin_set():
  3943. assert pretty(set()) == 'set()'
  3944. assert upretty(set()) == 'set()'
  3945. assert pretty(frozenset()) == 'frozenset()'
  3946. assert upretty(frozenset()) == 'frozenset()'
  3947. s1 = {1/x, x}
  3948. s2 = frozenset(s1)
  3949. assert pretty(s1) == \
  3950. """\
  3951. 1 \n\
  3952. {-, x}
  3953. x \
  3954. """
  3955. assert upretty(s1) == \
  3956. """\
  3957. 1
  3958. , x
  3959. x \
  3960. """
  3961. assert pretty(s2) == \
  3962. """\
  3963. 1 \n\
  3964. frozenset({-, x})
  3965. x \
  3966. """
  3967. assert upretty(s2) == \
  3968. """\
  3969. 1
  3970. frozenset, x
  3971. x \
  3972. """
  3973. def test_pretty_sets():
  3974. s = FiniteSet
  3975. assert pretty(s(*[x*y, x**2])) == \
  3976. """\
  3977. 2 \n\
  3978. {x , x*y}\
  3979. """
  3980. assert pretty(s(*range(1, 6))) == "{1, 2, 3, 4, 5}"
  3981. assert pretty(s(*range(1, 13))) == "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  3982. assert pretty({x*y, x**2}) == \
  3983. """\
  3984. 2 \n\
  3985. {x , x*y}\
  3986. """
  3987. assert pretty(set(range(1, 6))) == "{1, 2, 3, 4, 5}"
  3988. assert pretty(set(range(1, 13))) == \
  3989. "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  3990. assert pretty(frozenset([x*y, x**2])) == \
  3991. """\
  3992. 2 \n\
  3993. frozenset({x , x*y})\
  3994. """
  3995. assert pretty(frozenset(range(1, 6))) == "frozenset({1, 2, 3, 4, 5})"
  3996. assert pretty(frozenset(range(1, 13))) == \
  3997. "frozenset({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})"
  3998. assert pretty(Range(0, 3, 1)) == '{0, 1, 2}'
  3999. ascii_str = '{0, 1, ..., 29}'
  4000. ucode_str = '{0, 1, …, 29}'
  4001. assert pretty(Range(0, 30, 1)) == ascii_str
  4002. assert upretty(Range(0, 30, 1)) == ucode_str
  4003. ascii_str = '{30, 29, ..., 2}'
  4004. ucode_str = '{30, 29, …, 2}'
  4005. assert pretty(Range(30, 1, -1)) == ascii_str
  4006. assert upretty(Range(30, 1, -1)) == ucode_str
  4007. ascii_str = '{0, 2, ...}'
  4008. ucode_str = '{0, 2, …}'
  4009. assert pretty(Range(0, oo, 2)) == ascii_str
  4010. assert upretty(Range(0, oo, 2)) == ucode_str
  4011. ascii_str = '{..., 2, 0}'
  4012. ucode_str = '{…, 2, 0}'
  4013. assert pretty(Range(oo, -2, -2)) == ascii_str
  4014. assert upretty(Range(oo, -2, -2)) == ucode_str
  4015. ascii_str = '{-2, -3, ...}'
  4016. ucode_str = '{-2, -3, …}'
  4017. assert pretty(Range(-2, -oo, -1)) == ascii_str
  4018. assert upretty(Range(-2, -oo, -1)) == ucode_str
  4019. def test_pretty_SetExpr():
  4020. iv = Interval(1, 3)
  4021. se = SetExpr(iv)
  4022. ascii_str = "SetExpr([1, 3])"
  4023. ucode_str = "SetExpr([1, 3])"
  4024. assert pretty(se) == ascii_str
  4025. assert upretty(se) == ucode_str
  4026. def test_pretty_ImageSet():
  4027. imgset = ImageSet(Lambda((x, y), x + y), {1, 2, 3}, {3, 4})
  4028. ascii_str = '{x + y | x in {1, 2, 3}, y in {3, 4}}'
  4029. ucode_str = '{x + y │ x ∊ {1, 2, 3}, y ∊ {3, 4}}'
  4030. assert pretty(imgset) == ascii_str
  4031. assert upretty(imgset) == ucode_str
  4032. imgset = ImageSet(Lambda(((x, y),), x + y), ProductSet({1, 2, 3}, {3, 4}))
  4033. ascii_str = '{x + y | (x, y) in {1, 2, 3} x {3, 4}}'
  4034. ucode_str = '{x + y │ (x, y) ∊ {1, 2, 3} × {3, 4}}'
  4035. assert pretty(imgset) == ascii_str
  4036. assert upretty(imgset) == ucode_str
  4037. imgset = ImageSet(Lambda(x, x**2), S.Naturals)
  4038. ascii_str = '''\
  4039. 2 \n\
  4040. {x | x in Naturals}'''
  4041. ucode_str = '''\
  4042. 2 \n\
  4043. x x \n\
  4044. '''
  4045. assert pretty(imgset) == ascii_str
  4046. assert upretty(imgset) == ucode_str
  4047. # TODO: The "x in N" parts below should be centered independently of the
  4048. # 1/x**2 fraction
  4049. imgset = ImageSet(Lambda(x, 1/x**2), S.Naturals)
  4050. ascii_str = '''\
  4051. 1 \n\
  4052. {-- | x in Naturals}
  4053. 2 \n\
  4054. x '''
  4055. ucode_str = '''\
  4056. 1 \n\
  4057. x \n\
  4058. 2 \n\
  4059. x \n\
  4060. '''
  4061. assert pretty(imgset) == ascii_str
  4062. assert upretty(imgset) == ucode_str
  4063. imgset = ImageSet(Lambda((x, y), 1/(x + y)**2), S.Naturals, S.Naturals)
  4064. ascii_str = '''\
  4065. 1 \n\
  4066. {-------- | x in Naturals, y in Naturals}
  4067. 2 \n\
  4068. (x + y) '''
  4069. ucode_str = '''\
  4070. 1
  4071. x , y
  4072. 2
  4073. (x + y)
  4074. '''
  4075. assert pretty(imgset) == ascii_str
  4076. assert upretty(imgset) == ucode_str
  4077. def test_pretty_ConditionSet():
  4078. ascii_str = '{x | x in (-oo, oo) and sin(x) = 0}'
  4079. ucode_str = '{x │ x ∊ ℝ ∧ (sin(x) = 0)}'
  4080. assert pretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ascii_str
  4081. assert upretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ucode_str
  4082. assert pretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4083. assert upretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4084. assert pretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == "EmptySet"
  4085. assert upretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == ""
  4086. assert pretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4087. assert upretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4088. condset = ConditionSet(x, 1/x**2 > 0)
  4089. ascii_str = '''\
  4090. 1 \n\
  4091. {x | -- > 0}
  4092. 2 \n\
  4093. x '''
  4094. ucode_str = '''\
  4095. 1
  4096. x > 0
  4097. 2
  4098. x
  4099. '''
  4100. assert pretty(condset) == ascii_str
  4101. assert upretty(condset) == ucode_str
  4102. condset = ConditionSet(x, 1/x**2 > 0, S.Reals)
  4103. ascii_str = '''\
  4104. 1 \n\
  4105. {x | x in (-oo, oo) and -- > 0}
  4106. 2 \n\
  4107. x '''
  4108. ucode_str = '''\
  4109. 1
  4110. x x > 0
  4111. 2
  4112. x
  4113. '''
  4114. assert pretty(condset) == ascii_str
  4115. assert upretty(condset) == ucode_str
  4116. def test_pretty_ComplexRegion():
  4117. from sympy.sets.fancysets import ComplexRegion
  4118. cregion = ComplexRegion(Interval(3, 5)*Interval(4, 6))
  4119. ascii_str = '{x + y*I | x, y in [3, 5] x [4, 6]}'
  4120. ucode_str = '{x + y⋅ⅈ │ x, y ∊ [3, 5] × [4, 6]}'
  4121. assert pretty(cregion) == ascii_str
  4122. assert upretty(cregion) == ucode_str
  4123. cregion = ComplexRegion(Interval(0, 1)*Interval(0, 2*pi), polar=True)
  4124. ascii_str = '{r*(I*sin(theta) + cos(theta)) | r, theta in [0, 1] x [0, 2*pi)}'
  4125. ucode_str = '{r⋅(ⅈ⋅sin(θ) + cos(θ)) │ r, θ ∊ [0, 1] × [0, 2⋅π)}'
  4126. assert pretty(cregion) == ascii_str
  4127. assert upretty(cregion) == ucode_str
  4128. cregion = ComplexRegion(Interval(3, 1/a**2)*Interval(4, 6))
  4129. ascii_str = '''\
  4130. 1 \n\
  4131. {x + y*I | x, y in [3, --] x [4, 6]}
  4132. 2 \n\
  4133. a '''
  4134. ucode_str = '''\
  4135. 1
  4136. x + y x, y 3, × [4, 6]
  4137. 2
  4138. a
  4139. '''
  4140. assert pretty(cregion) == ascii_str
  4141. assert upretty(cregion) == ucode_str
  4142. cregion = ComplexRegion(Interval(0, 1/a**2)*Interval(0, 2*pi), polar=True)
  4143. ascii_str = '''\
  4144. 1 \n\
  4145. {r*(I*sin(theta) + cos(theta)) | r, theta in [0, --] x [0, 2*pi)}
  4146. 2 \n\
  4147. a '''
  4148. ucode_str = '''\
  4149. 1
  4150. r(sin(θ) + cos(θ)) r, θ 0, × [0, 2π)
  4151. 2
  4152. a
  4153. '''
  4154. assert pretty(cregion) == ascii_str
  4155. assert upretty(cregion) == ucode_str
  4156. def test_pretty_Union_issue_10414():
  4157. a, b = Interval(2, 3), Interval(4, 7)
  4158. ucode_str = '[2, 3] ∪ [4, 7]'
  4159. ascii_str = '[2, 3] U [4, 7]'
  4160. assert upretty(Union(a, b)) == ucode_str
  4161. assert pretty(Union(a, b)) == ascii_str
  4162. def test_pretty_Intersection_issue_10414():
  4163. x, y, z, w = symbols('x, y, z, w')
  4164. a, b = Interval(x, y), Interval(z, w)
  4165. ucode_str = '[x, y] ∩ [z, w]'
  4166. ascii_str = '[x, y] n [z, w]'
  4167. assert upretty(Intersection(a, b)) == ucode_str
  4168. assert pretty(Intersection(a, b)) == ascii_str
  4169. def test_ProductSet_exponent():
  4170. ucode_str = ' 1\n[0, 1] '
  4171. assert upretty(Interval(0, 1)**1) == ucode_str
  4172. ucode_str = ' 2\n[0, 1] '
  4173. assert upretty(Interval(0, 1)**2) == ucode_str
  4174. def test_ProductSet_parenthesis():
  4175. ucode_str = '([4, 7] × {1, 2}) ∪ ([2, 3] × [4, 7])'
  4176. a, b = Interval(2, 3), Interval(4, 7)
  4177. assert upretty(Union(a*b, b*FiniteSet(1, 2))) == ucode_str
  4178. def test_ProductSet_prod_char_issue_10413():
  4179. ascii_str = '[2, 3] x [4, 7]'
  4180. ucode_str = '[2, 3] × [4, 7]'
  4181. a, b = Interval(2, 3), Interval(4, 7)
  4182. assert pretty(a*b) == ascii_str
  4183. assert upretty(a*b) == ucode_str
  4184. def test_pretty_sequences():
  4185. s1 = SeqFormula(a**2, (0, oo))
  4186. s2 = SeqPer((1, 2))
  4187. ascii_str = '[0, 1, 4, 9, ...]'
  4188. ucode_str = '[0, 1, 4, 9, …]'
  4189. assert pretty(s1) == ascii_str
  4190. assert upretty(s1) == ucode_str
  4191. ascii_str = '[1, 2, 1, 2, ...]'
  4192. ucode_str = '[1, 2, 1, 2, …]'
  4193. assert pretty(s2) == ascii_str
  4194. assert upretty(s2) == ucode_str
  4195. s3 = SeqFormula(a**2, (0, 2))
  4196. s4 = SeqPer((1, 2), (0, 2))
  4197. ascii_str = '[0, 1, 4]'
  4198. ucode_str = '[0, 1, 4]'
  4199. assert pretty(s3) == ascii_str
  4200. assert upretty(s3) == ucode_str
  4201. ascii_str = '[1, 2, 1]'
  4202. ucode_str = '[1, 2, 1]'
  4203. assert pretty(s4) == ascii_str
  4204. assert upretty(s4) == ucode_str
  4205. s5 = SeqFormula(a**2, (-oo, 0))
  4206. s6 = SeqPer((1, 2), (-oo, 0))
  4207. ascii_str = '[..., 9, 4, 1, 0]'
  4208. ucode_str = '[…, 9, 4, 1, 0]'
  4209. assert pretty(s5) == ascii_str
  4210. assert upretty(s5) == ucode_str
  4211. ascii_str = '[..., 2, 1, 2, 1]'
  4212. ucode_str = '[…, 2, 1, 2, 1]'
  4213. assert pretty(s6) == ascii_str
  4214. assert upretty(s6) == ucode_str
  4215. ascii_str = '[1, 3, 5, 11, ...]'
  4216. ucode_str = '[1, 3, 5, 11, …]'
  4217. assert pretty(SeqAdd(s1, s2)) == ascii_str
  4218. assert upretty(SeqAdd(s1, s2)) == ucode_str
  4219. ascii_str = '[1, 3, 5]'
  4220. ucode_str = '[1, 3, 5]'
  4221. assert pretty(SeqAdd(s3, s4)) == ascii_str
  4222. assert upretty(SeqAdd(s3, s4)) == ucode_str
  4223. ascii_str = '[..., 11, 5, 3, 1]'
  4224. ucode_str = '[…, 11, 5, 3, 1]'
  4225. assert pretty(SeqAdd(s5, s6)) == ascii_str
  4226. assert upretty(SeqAdd(s5, s6)) == ucode_str
  4227. ascii_str = '[0, 2, 4, 18, ...]'
  4228. ucode_str = '[0, 2, 4, 18, …]'
  4229. assert pretty(SeqMul(s1, s2)) == ascii_str
  4230. assert upretty(SeqMul(s1, s2)) == ucode_str
  4231. ascii_str = '[0, 2, 4]'
  4232. ucode_str = '[0, 2, 4]'
  4233. assert pretty(SeqMul(s3, s4)) == ascii_str
  4234. assert upretty(SeqMul(s3, s4)) == ucode_str
  4235. ascii_str = '[..., 18, 4, 2, 0]'
  4236. ucode_str = '[…, 18, 4, 2, 0]'
  4237. assert pretty(SeqMul(s5, s6)) == ascii_str
  4238. assert upretty(SeqMul(s5, s6)) == ucode_str
  4239. # Sequences with symbolic limits, issue 12629
  4240. s7 = SeqFormula(a**2, (a, 0, x))
  4241. raises(NotImplementedError, lambda: pretty(s7))
  4242. raises(NotImplementedError, lambda: upretty(s7))
  4243. b = Symbol('b')
  4244. s8 = SeqFormula(b*a**2, (a, 0, 2))
  4245. ascii_str = '[0, b, 4*b]'
  4246. ucode_str = '[0, b, 4⋅b]'
  4247. assert pretty(s8) == ascii_str
  4248. assert upretty(s8) == ucode_str
  4249. def test_pretty_FourierSeries():
  4250. f = fourier_series(x, (x, -pi, pi))
  4251. ascii_str = \
  4252. """\
  4253. 2*sin(3*x) \n\
  4254. 2*sin(x) - sin(2*x) + ---------- + ...\n\
  4255. 3 \
  4256. """
  4257. ucode_str = \
  4258. """\
  4259. 2sin(3x) \n\
  4260. 2sin(x) - sin(2x) + + \n\
  4261. 3 \
  4262. """
  4263. assert pretty(f) == ascii_str
  4264. assert upretty(f) == ucode_str
  4265. def test_pretty_FormalPowerSeries():
  4266. f = fps(log(1 + x))
  4267. ascii_str = \
  4268. """\
  4269. oo \n\
  4270. ____ \n\
  4271. \\ ` \n\
  4272. \\ -k k \n\
  4273. \\ -(-1) *x \n\
  4274. / -----------\n\
  4275. / k \n\
  4276. /___, \n\
  4277. k = 1 \
  4278. """
  4279. ucode_str = \
  4280. """\
  4281. \n\
  4282. ____ \n\
  4283. \n\
  4284. -k k \n\
  4285. -(-1) x \n\
  4286. \n\
  4287. k \n\
  4288. \n\
  4289. \n\
  4290. k = 1 \
  4291. """
  4292. assert pretty(f) == ascii_str
  4293. assert upretty(f) == ucode_str
  4294. def test_pretty_limits():
  4295. expr = Limit(x, x, oo)
  4296. ascii_str = \
  4297. """\
  4298. lim x\n\
  4299. x->oo \
  4300. """
  4301. ucode_str = \
  4302. """\
  4303. lim x\n\
  4304. x \
  4305. """
  4306. assert pretty(expr) == ascii_str
  4307. assert upretty(expr) == ucode_str
  4308. expr = Limit(x**2, x, 0)
  4309. ascii_str = \
  4310. """\
  4311. 2\n\
  4312. lim x \n\
  4313. x->0+ \
  4314. """
  4315. ucode_str = \
  4316. """\
  4317. 2\n\
  4318. lim x \n\
  4319. x0 \
  4320. """
  4321. assert pretty(expr) == ascii_str
  4322. assert upretty(expr) == ucode_str
  4323. expr = Limit(1/x, x, 0)
  4324. ascii_str = \
  4325. """\
  4326. 1\n\
  4327. lim -\n\
  4328. x->0+x\
  4329. """
  4330. ucode_str = \
  4331. """\
  4332. 1\n\
  4333. lim \n\
  4334. x0x\
  4335. """
  4336. assert pretty(expr) == ascii_str
  4337. assert upretty(expr) == ucode_str
  4338. expr = Limit(sin(x)/x, x, 0)
  4339. ascii_str = \
  4340. """\
  4341. /sin(x)\\\n\
  4342. lim |------|\n\
  4343. x->0+\\ x /\
  4344. """
  4345. ucode_str = \
  4346. """\
  4347. sin(x)\n\
  4348. lim \n\
  4349. x0 x \
  4350. """
  4351. assert pretty(expr) == ascii_str
  4352. assert upretty(expr) == ucode_str
  4353. expr = Limit(sin(x)/x, x, 0, "-")
  4354. ascii_str = \
  4355. """\
  4356. /sin(x)\\\n\
  4357. lim |------|\n\
  4358. x->0-\\ x /\
  4359. """
  4360. ucode_str = \
  4361. """\
  4362. sin(x)\n\
  4363. lim \n\
  4364. x0 x \
  4365. """
  4366. assert pretty(expr) == ascii_str
  4367. assert upretty(expr) == ucode_str
  4368. expr = Limit(x + sin(x), x, 0)
  4369. ascii_str = \
  4370. """\
  4371. lim (x + sin(x))\n\
  4372. x->0+ \
  4373. """
  4374. ucode_str = \
  4375. """\
  4376. lim (x + sin(x))\n\
  4377. x0 \
  4378. """
  4379. assert pretty(expr) == ascii_str
  4380. assert upretty(expr) == ucode_str
  4381. expr = Limit(x, x, 0)**2
  4382. ascii_str = \
  4383. """\
  4384. 2\n\
  4385. / lim x\\ \n\
  4386. \\x->0+ / \
  4387. """
  4388. ucode_str = \
  4389. """\
  4390. 2\n\
  4391. lim x \n\
  4392. x0 \
  4393. """
  4394. assert pretty(expr) == ascii_str
  4395. assert upretty(expr) == ucode_str
  4396. expr = Limit(x*Limit(y/2,y,0), x, 0)
  4397. ascii_str = \
  4398. """\
  4399. / /y\\\\\n\
  4400. lim |x* lim |-||\n\
  4401. x->0+\\ y->0+\\2//\
  4402. """
  4403. ucode_str = \
  4404. """\
  4405. y\n\
  4406. lim x lim \n\
  4407. x0 y02\
  4408. """
  4409. assert pretty(expr) == ascii_str
  4410. assert upretty(expr) == ucode_str
  4411. expr = 2*Limit(x*Limit(y/2,y,0), x, 0)
  4412. ascii_str = \
  4413. """\
  4414. / /y\\\\\n\
  4415. 2* lim |x* lim |-||\n\
  4416. x->0+\\ y->0+\\2//\
  4417. """
  4418. ucode_str = \
  4419. """\
  4420. y\n\
  4421. 2 lim x lim \n\
  4422. x0 y02\
  4423. """
  4424. assert pretty(expr) == ascii_str
  4425. assert upretty(expr) == ucode_str
  4426. expr = Limit(sin(x), x, 0, dir='+-')
  4427. ascii_str = \
  4428. """\
  4429. lim sin(x)\n\
  4430. x->0 \
  4431. """
  4432. ucode_str = \
  4433. """\
  4434. lim sin(x)\n\
  4435. x0 \
  4436. """
  4437. assert pretty(expr) == ascii_str
  4438. assert upretty(expr) == ucode_str
  4439. def test_pretty_ComplexRootOf():
  4440. expr = rootof(x**5 + 11*x - 2, 0)
  4441. ascii_str = \
  4442. """\
  4443. / 5 \\\n\
  4444. CRootOf\\x + 11*x - 2, 0/\
  4445. """
  4446. ucode_str = \
  4447. """\
  4448. 5 \n\
  4449. CRootOfx + 11x - 2, 0\
  4450. """
  4451. assert pretty(expr) == ascii_str
  4452. assert upretty(expr) == ucode_str
  4453. def test_pretty_RootSum():
  4454. expr = RootSum(x**5 + 11*x - 2, auto=False)
  4455. ascii_str = \
  4456. """\
  4457. / 5 \\\n\
  4458. RootSum\\x + 11*x - 2/\
  4459. """
  4460. ucode_str = \
  4461. """\
  4462. 5 \n\
  4463. RootSumx + 11x - 2\
  4464. """
  4465. assert pretty(expr) == ascii_str
  4466. assert upretty(expr) == ucode_str
  4467. expr = RootSum(x**5 + 11*x - 2, Lambda(z, exp(z)))
  4468. ascii_str = \
  4469. """\
  4470. / 5 z\\\n\
  4471. RootSum\\x + 11*x - 2, z -> e /\
  4472. """
  4473. ucode_str = \
  4474. """\
  4475. 5 z\n\
  4476. RootSumx + 11x - 2, z \
  4477. """
  4478. assert pretty(expr) == ascii_str
  4479. assert upretty(expr) == ucode_str
  4480. def test_GroebnerBasis():
  4481. expr = groebner([], x, y)
  4482. ascii_str = \
  4483. """\
  4484. GroebnerBasis([], x, y, domain=ZZ, order=lex)\
  4485. """
  4486. ucode_str = \
  4487. """\
  4488. GroebnerBasis([], x, y, domain=, order=lex)\
  4489. """
  4490. assert pretty(expr) == ascii_str
  4491. assert upretty(expr) == ucode_str
  4492. F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
  4493. expr = groebner(F, x, y, order='grlex')
  4494. ascii_str = \
  4495. """\
  4496. /[ 2 2 ] \\\n\
  4497. GroebnerBasis\\[x - x - 3*y + 1, y - 2*x + y - 1], x, y, domain=ZZ, order=grlex/\
  4498. """
  4499. ucode_str = \
  4500. """\
  4501. 2 2 \n\
  4502. GroebnerBasisx - x - 3y + 1, y - 2x + y - 1, x, y, domain=, order=grlex\
  4503. """
  4504. assert pretty(expr) == ascii_str
  4505. assert upretty(expr) == ucode_str
  4506. expr = expr.fglm('lex')
  4507. ascii_str = \
  4508. """\
  4509. /[ 2 4 3 2 ] \\\n\
  4510. GroebnerBasis\\[2*x - y - y + 1, y + 2*y - 3*y - 16*y + 7], x, y, domain=ZZ, order=lex/\
  4511. """
  4512. ucode_str = \
  4513. """\
  4514. 2 4 3 2 \n\
  4515. GroebnerBasis2x - y - y + 1, y + 2y - 3y - 16y + 7, x, y, domain=, order=lex\
  4516. """
  4517. assert pretty(expr) == ascii_str
  4518. assert upretty(expr) == ucode_str
  4519. def test_pretty_UniversalSet():
  4520. assert pretty(S.UniversalSet) == "UniversalSet"
  4521. assert upretty(S.UniversalSet) == '𝕌'
  4522. def test_pretty_Boolean():
  4523. expr = Not(x, evaluate=False)
  4524. assert pretty(expr) == "Not(x)"
  4525. assert upretty(expr) == "¬x"
  4526. expr = And(x, y)
  4527. assert pretty(expr) == "And(x, y)"
  4528. assert upretty(expr) == "x ∧ y"
  4529. expr = Or(x, y)
  4530. assert pretty(expr) == "Or(x, y)"
  4531. assert upretty(expr) == "x ∨ y"
  4532. syms = symbols('a:f')
  4533. expr = And(*syms)
  4534. assert pretty(expr) == "And(a, b, c, d, e, f)"
  4535. assert upretty(expr) == "a ∧ b ∧ c ∧ d ∧ e ∧ f"
  4536. expr = Or(*syms)
  4537. assert pretty(expr) == "Or(a, b, c, d, e, f)"
  4538. assert upretty(expr) == "a ∨ b ∨ c ∨ d ∨ e ∨ f"
  4539. expr = Xor(x, y, evaluate=False)
  4540. assert pretty(expr) == "Xor(x, y)"
  4541. assert upretty(expr) == "x ⊻ y"
  4542. expr = Nand(x, y, evaluate=False)
  4543. assert pretty(expr) == "Nand(x, y)"
  4544. assert upretty(expr) == "x ⊼ y"
  4545. expr = Nor(x, y, evaluate=False)
  4546. assert pretty(expr) == "Nor(x, y)"
  4547. assert upretty(expr) == "x ⊽ y"
  4548. expr = Implies(x, y, evaluate=False)
  4549. assert pretty(expr) == "Implies(x, y)"
  4550. assert upretty(expr) == "x → y"
  4551. # don't sort args
  4552. expr = Implies(y, x, evaluate=False)
  4553. assert pretty(expr) == "Implies(y, x)"
  4554. assert upretty(expr) == "y → x"
  4555. expr = Equivalent(x, y, evaluate=False)
  4556. assert pretty(expr) == "Equivalent(x, y)"
  4557. assert upretty(expr) == "x ⇔ y"
  4558. expr = Equivalent(y, x, evaluate=False)
  4559. assert pretty(expr) == "Equivalent(x, y)"
  4560. assert upretty(expr) == "x ⇔ y"
  4561. def test_pretty_Domain():
  4562. expr = FF(23)
  4563. assert pretty(expr) == "GF(23)"
  4564. assert upretty(expr) == "ℤ₂₃"
  4565. expr = ZZ
  4566. assert pretty(expr) == "ZZ"
  4567. assert upretty(expr) == ""
  4568. expr = QQ
  4569. assert pretty(expr) == "QQ"
  4570. assert upretty(expr) == ""
  4571. expr = RR
  4572. assert pretty(expr) == "RR"
  4573. assert upretty(expr) == ""
  4574. expr = QQ[x]
  4575. assert pretty(expr) == "QQ[x]"
  4576. assert upretty(expr) == "ℚ[x]"
  4577. expr = QQ[x, y]
  4578. assert pretty(expr) == "QQ[x, y]"
  4579. assert upretty(expr) == "ℚ[x, y]"
  4580. expr = ZZ.frac_field(x)
  4581. assert pretty(expr) == "ZZ(x)"
  4582. assert upretty(expr) == "ℤ(x)"
  4583. expr = ZZ.frac_field(x, y)
  4584. assert pretty(expr) == "ZZ(x, y)"
  4585. assert upretty(expr) == "ℤ(x, y)"
  4586. expr = QQ.poly_ring(x, y, order=grlex)
  4587. assert pretty(expr) == "QQ[x, y, order=grlex]"
  4588. assert upretty(expr) == "ℚ[x, y, order=grlex]"
  4589. expr = QQ.poly_ring(x, y, order=ilex)
  4590. assert pretty(expr) == "QQ[x, y, order=ilex]"
  4591. assert upretty(expr) == "ℚ[x, y, order=ilex]"
  4592. def test_pretty_prec():
  4593. assert xpretty(S("0.3"), full_prec=True, wrap_line=False) == "0.300000000000000"
  4594. assert xpretty(S("0.3"), full_prec="auto", wrap_line=False) == "0.300000000000000"
  4595. assert xpretty(S("0.3"), full_prec=False, wrap_line=False) == "0.3"
  4596. assert xpretty(S("0.3")*x, full_prec=True, use_unicode=False, wrap_line=False) in [
  4597. "0.300000000000000*x",
  4598. "x*0.300000000000000"
  4599. ]
  4600. assert xpretty(S("0.3")*x, full_prec="auto", use_unicode=False, wrap_line=False) in [
  4601. "0.3*x",
  4602. "x*0.3"
  4603. ]
  4604. assert xpretty(S("0.3")*x, full_prec=False, use_unicode=False, wrap_line=False) in [
  4605. "0.3*x",
  4606. "x*0.3"
  4607. ]
  4608. def test_pprint():
  4609. import sys
  4610. from io import StringIO
  4611. fd = StringIO()
  4612. sso = sys.stdout
  4613. sys.stdout = fd
  4614. try:
  4615. pprint(pi, use_unicode=False, wrap_line=False)
  4616. finally:
  4617. sys.stdout = sso
  4618. assert fd.getvalue() == 'pi\n'
  4619. def test_pretty_class():
  4620. """Test that the printer dispatcher correctly handles classes."""
  4621. class C:
  4622. pass # C has no .__class__ and this was causing problems
  4623. class D:
  4624. pass
  4625. assert pretty( C ) == str( C )
  4626. assert pretty( D ) == str( D )
  4627. def test_pretty_no_wrap_line():
  4628. huge_expr = 0
  4629. for i in range(20):
  4630. huge_expr += i*sin(i + x)
  4631. assert xpretty(huge_expr ).find('\n') != -1
  4632. assert xpretty(huge_expr, wrap_line=False).find('\n') == -1
  4633. def test_settings():
  4634. raises(TypeError, lambda: pretty(S(4), method="garbage"))
  4635. def test_pretty_sum():
  4636. from sympy.abc import x, a, b, k, m, n
  4637. expr = Sum(k**k, (k, 0, n))
  4638. ascii_str = \
  4639. """\
  4640. n \n\
  4641. ___ \n\
  4642. \\ ` \n\
  4643. \\ k\n\
  4644. / k \n\
  4645. /__, \n\
  4646. k = 0 \
  4647. """
  4648. ucode_str = \
  4649. """\
  4650. n \n\
  4651. ___ \n\
  4652. \n\
  4653. k\n\
  4654. k \n\
  4655. \n\
  4656. \n\
  4657. k = 0 \
  4658. """
  4659. assert pretty(expr) == ascii_str
  4660. assert upretty(expr) == ucode_str
  4661. expr = Sum(k**k, (k, oo, n))
  4662. ascii_str = \
  4663. """\
  4664. n \n\
  4665. ___ \n\
  4666. \\ ` \n\
  4667. \\ k\n\
  4668. / k \n\
  4669. /__, \n\
  4670. k = oo \
  4671. """
  4672. ucode_str = \
  4673. """\
  4674. n \n\
  4675. ___ \n\
  4676. \n\
  4677. k\n\
  4678. k \n\
  4679. \n\
  4680. \n\
  4681. k = \
  4682. """
  4683. assert pretty(expr) == ascii_str
  4684. assert upretty(expr) == ucode_str
  4685. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (k, 0, n**n))
  4686. ascii_str = \
  4687. """\
  4688. n \n\
  4689. n \n\
  4690. ______ \n\
  4691. \\ ` \n\
  4692. \\ oo \n\
  4693. \\ / \n\
  4694. \\ | \n\
  4695. \\ | n \n\
  4696. ) | x dx\n\
  4697. / | \n\
  4698. / / \n\
  4699. / -oo \n\
  4700. / k \n\
  4701. /_____, \n\
  4702. k = 0 \
  4703. """
  4704. ucode_str = \
  4705. """\
  4706. n \n\
  4707. n \n\
  4708. ______ \n\
  4709. \n\
  4710. \n\
  4711. \n\
  4712. \n\
  4713. n \n\
  4714. x dx\n\
  4715. \n\
  4716. - \n\
  4717. k \n\
  4718. \n\
  4719. \n\
  4720. k = 0 \
  4721. """
  4722. assert pretty(expr) == ascii_str
  4723. assert upretty(expr) == ucode_str
  4724. expr = Sum(k**(
  4725. Integral(x**n, (x, -oo, oo))), (k, 0, Integral(x**x, (x, -oo, oo))))
  4726. ascii_str = \
  4727. """\
  4728. oo \n\
  4729. / \n\
  4730. | \n\
  4731. | x \n\
  4732. | x dx \n\
  4733. | \n\
  4734. / \n\
  4735. -oo \n\
  4736. ______ \n\
  4737. \\ ` \n\
  4738. \\ oo \n\
  4739. \\ / \n\
  4740. \\ | \n\
  4741. \\ | n \n\
  4742. ) | x dx\n\
  4743. / | \n\
  4744. / / \n\
  4745. / -oo \n\
  4746. / k \n\
  4747. /_____, \n\
  4748. k = 0 \
  4749. """
  4750. ucode_str = \
  4751. """\
  4752. \n\
  4753. \n\
  4754. x \n\
  4755. x dx \n\
  4756. \n\
  4757. - \n\
  4758. ______ \n\
  4759. \n\
  4760. \n\
  4761. \n\
  4762. \n\
  4763. n \n\
  4764. x dx\n\
  4765. \n\
  4766. - \n\
  4767. k \n\
  4768. \n\
  4769. \n\
  4770. k = 0 \
  4771. """
  4772. assert pretty(expr) == ascii_str
  4773. assert upretty(expr) == ucode_str
  4774. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (
  4775. k, x + n + x**2 + n**2 + (x/n) + (1/x), Integral(x**x, (x, -oo, oo))))
  4776. ascii_str = \
  4777. """\
  4778. oo \n\
  4779. / \n\
  4780. | \n\
  4781. | x \n\
  4782. | x dx \n\
  4783. | \n\
  4784. / \n\
  4785. -oo \n\
  4786. ______ \n\
  4787. \\ ` \n\
  4788. \\ oo \n\
  4789. \\ / \n\
  4790. \\ | \n\
  4791. \\ | n \n\
  4792. ) | x dx\n\
  4793. / | \n\
  4794. / / \n\
  4795. / -oo \n\
  4796. / k \n\
  4797. /_____, \n\
  4798. 2 2 1 x \n\
  4799. k = n + n + x + x + - + - \n\
  4800. x n \
  4801. """
  4802. ucode_str = \
  4803. """\
  4804. \n\
  4805. \n\
  4806. x \n\
  4807. x dx \n\
  4808. \n\
  4809. - \n\
  4810. ______ \n\
  4811. \n\
  4812. \n\
  4813. \n\
  4814. \n\
  4815. n \n\
  4816. x dx\n\
  4817. \n\
  4818. - \n\
  4819. k \n\
  4820. \n\
  4821. \n\
  4822. 2 2 1 x \n\
  4823. k = n + n + x + x + + \n\
  4824. x n \
  4825. """
  4826. assert pretty(expr) == ascii_str
  4827. assert upretty(expr) == ucode_str
  4828. expr = Sum(k**(
  4829. Integral(x**n, (x, -oo, oo))), (k, 0, x + n + x**2 + n**2 + (x/n) + (1/x)))
  4830. ascii_str = \
  4831. """\
  4832. 2 2 1 x \n\
  4833. n + n + x + x + - + - \n\
  4834. x n \n\
  4835. ______ \n\
  4836. \\ ` \n\
  4837. \\ oo \n\
  4838. \\ / \n\
  4839. \\ | \n\
  4840. \\ | n \n\
  4841. ) | x dx\n\
  4842. / | \n\
  4843. / / \n\
  4844. / -oo \n\
  4845. / k \n\
  4846. /_____, \n\
  4847. k = 0 \
  4848. """
  4849. ucode_str = \
  4850. """\
  4851. 2 2 1 x \n\
  4852. n + n + x + x + + \n\
  4853. x n \n\
  4854. ______ \n\
  4855. \n\
  4856. \n\
  4857. \n\
  4858. \n\
  4859. n \n\
  4860. x dx\n\
  4861. \n\
  4862. - \n\
  4863. k \n\
  4864. \n\
  4865. \n\
  4866. k = 0 \
  4867. """
  4868. assert pretty(expr) == ascii_str
  4869. assert upretty(expr) == ucode_str
  4870. expr = Sum(x, (x, 0, oo))
  4871. ascii_str = \
  4872. """\
  4873. oo \n\
  4874. __ \n\
  4875. \\ ` \n\
  4876. ) x\n\
  4877. /_, \n\
  4878. x = 0 \
  4879. """
  4880. ucode_str = \
  4881. """\
  4882. \n\
  4883. ___ \n\
  4884. \n\
  4885. \n\
  4886. x\n\
  4887. \n\
  4888. \n\
  4889. x = 0 \
  4890. """
  4891. assert pretty(expr) == ascii_str
  4892. assert upretty(expr) == ucode_str
  4893. expr = Sum(x**2, (x, 0, oo))
  4894. ascii_str = \
  4895. """\
  4896. oo \n\
  4897. ___ \n\
  4898. \\ ` \n\
  4899. \\ 2\n\
  4900. / x \n\
  4901. /__, \n\
  4902. x = 0 \
  4903. """
  4904. ucode_str = \
  4905. """\
  4906. \n\
  4907. ___ \n\
  4908. \n\
  4909. 2\n\
  4910. x \n\
  4911. \n\
  4912. \n\
  4913. x = 0 \
  4914. """
  4915. assert pretty(expr) == ascii_str
  4916. assert upretty(expr) == ucode_str
  4917. expr = Sum(x/2, (x, 0, oo))
  4918. ascii_str = \
  4919. """\
  4920. oo \n\
  4921. ___ \n\
  4922. \\ ` \n\
  4923. \\ x\n\
  4924. ) -\n\
  4925. / 2\n\
  4926. /__, \n\
  4927. x = 0 \
  4928. """
  4929. ucode_str = \
  4930. """\
  4931. \n\
  4932. ____ \n\
  4933. \n\
  4934. \n\
  4935. x\n\
  4936. \n\
  4937. 2\n\
  4938. \n\
  4939. \n\
  4940. x = 0 \
  4941. """
  4942. assert pretty(expr) == ascii_str
  4943. assert upretty(expr) == ucode_str
  4944. expr = Sum(x**3/2, (x, 0, oo))
  4945. ascii_str = \
  4946. """\
  4947. oo \n\
  4948. ____ \n\
  4949. \\ ` \n\
  4950. \\ 3\n\
  4951. \\ x \n\
  4952. / --\n\
  4953. / 2 \n\
  4954. /___, \n\
  4955. x = 0 \
  4956. """
  4957. ucode_str = \
  4958. """\
  4959. \n\
  4960. ____ \n\
  4961. \n\
  4962. 3\n\
  4963. x \n\
  4964. \n\
  4965. 2 \n\
  4966. \n\
  4967. \n\
  4968. x = 0 \
  4969. """
  4970. assert pretty(expr) == ascii_str
  4971. assert upretty(expr) == ucode_str
  4972. expr = Sum((x**3*y**(x/2))**n, (x, 0, oo))
  4973. ascii_str = \
  4974. """\
  4975. oo \n\
  4976. ____ \n\
  4977. \\ ` \n\
  4978. \\ n\n\
  4979. \\ / x\\ \n\
  4980. ) | -| \n\
  4981. / | 3 2| \n\
  4982. / \\x *y / \n\
  4983. /___, \n\
  4984. x = 0 \
  4985. """
  4986. ucode_str = \
  4987. """\
  4988. \n\
  4989. _____ \n\
  4990. \n\
  4991. \n\
  4992. n\n\
  4993. x \n\
  4994. \n\
  4995. 3 2 \n\
  4996. x y \n\
  4997. \n\
  4998. \n\
  4999. x = 0 \
  5000. """
  5001. assert pretty(expr) == ascii_str
  5002. assert upretty(expr) == ucode_str
  5003. expr = Sum(1/x**2, (x, 0, oo))
  5004. ascii_str = \
  5005. """\
  5006. oo \n\
  5007. ____ \n\
  5008. \\ ` \n\
  5009. \\ 1 \n\
  5010. \\ --\n\
  5011. / 2\n\
  5012. / x \n\
  5013. /___, \n\
  5014. x = 0 \
  5015. """
  5016. ucode_str = \
  5017. """\
  5018. \n\
  5019. ____ \n\
  5020. \n\
  5021. 1 \n\
  5022. \n\
  5023. 2\n\
  5024. x \n\
  5025. \n\
  5026. \n\
  5027. x = 0 \
  5028. """
  5029. assert pretty(expr) == ascii_str
  5030. assert upretty(expr) == ucode_str
  5031. expr = Sum(1/y**(a/b), (x, 0, oo))
  5032. ascii_str = \
  5033. """\
  5034. oo \n\
  5035. ____ \n\
  5036. \\ ` \n\
  5037. \\ -a \n\
  5038. \\ ---\n\
  5039. / b \n\
  5040. / y \n\
  5041. /___, \n\
  5042. x = 0 \
  5043. """
  5044. ucode_str = \
  5045. """\
  5046. \n\
  5047. ____ \n\
  5048. \n\
  5049. -a \n\
  5050. \n\
  5051. b \n\
  5052. y \n\
  5053. \n\
  5054. \n\
  5055. x = 0 \
  5056. """
  5057. assert pretty(expr) == ascii_str
  5058. assert upretty(expr) == ucode_str
  5059. expr = Sum(1/y**(a/b), (x, 0, oo), (y, 1, 2))
  5060. ascii_str = \
  5061. """\
  5062. 2 oo \n\
  5063. ____ ____ \n\
  5064. \\ ` \\ ` \n\
  5065. \\ \\ -a\n\
  5066. \\ \\ --\n\
  5067. / / b \n\
  5068. / / y \n\
  5069. /___, /___, \n\
  5070. y = 1 x = 0 \
  5071. """
  5072. ucode_str = \
  5073. """\
  5074. 2 \n\
  5075. ____ ____ \n\
  5076. \n\
  5077. -a\n\
  5078. \n\
  5079. b \n\
  5080. y \n\
  5081. \n\
  5082. \n\
  5083. y = 1 x = 0 \
  5084. """
  5085. expr = Sum(1/(1 + 1/(
  5086. 1 + 1/k)) + 1, (k, 111, 1 + 1/n), (k, 1/(1 + m), oo)) + 1/(1 + 1/k)
  5087. ascii_str = \
  5088. """\
  5089. 1 \n\
  5090. 1 + - \n\
  5091. oo n \n\
  5092. _____ _____ \n\
  5093. \\ ` \\ ` \n\
  5094. \\ \\ / 1 \\ \n\
  5095. \\ \\ |1 + ---------| \n\
  5096. \\ \\ | 1 | 1 \n\
  5097. ) ) | 1 + -----| + -----\n\
  5098. / / | 1| 1\n\
  5099. / / | 1 + -| 1 + -\n\
  5100. / / \\ k/ k\n\
  5101. /____, /____, \n\
  5102. 1 k = 111 \n\
  5103. k = ----- \n\
  5104. m + 1 \
  5105. """
  5106. ucode_str = \
  5107. """\
  5108. 1 \n\
  5109. 1 + \n\
  5110. n \n\
  5111. ______ ______ \n\
  5112. \n\
  5113. \n\
  5114. 1 \n\
  5115. 1 + \n\
  5116. 1 1 \n\
  5117. 1 + + \n\
  5118. 1 1\n\
  5119. 1 + 1 + \n\
  5120. k k\n\
  5121. \n\
  5122. \n\
  5123. 1 k = 111 \n\
  5124. k = \n\
  5125. m + 1 \
  5126. """
  5127. assert pretty(expr) == ascii_str
  5128. assert upretty(expr) == ucode_str
  5129. def test_units():
  5130. expr = joule
  5131. ascii_str1 = \
  5132. """\
  5133. 2\n\
  5134. kilogram*meter \n\
  5135. ---------------\n\
  5136. 2 \n\
  5137. second \
  5138. """
  5139. unicode_str1 = \
  5140. """\
  5141. 2\n\
  5142. kilogrammeter \n\
  5143. \n\
  5144. 2 \n\
  5145. second \
  5146. """
  5147. ascii_str2 = \
  5148. """\
  5149. 2\n\
  5150. 3*x*y*kilogram*meter \n\
  5151. ---------------------\n\
  5152. 2 \n\
  5153. second \
  5154. """
  5155. unicode_str2 = \
  5156. """\
  5157. 2\n\
  5158. 3xykilogrammeter \n\
  5159. \n\
  5160. 2 \n\
  5161. second \
  5162. """
  5163. from sympy.physics.units import kg, m, s
  5164. assert upretty(expr) == "joule"
  5165. assert pretty(expr) == "joule"
  5166. assert upretty(expr.convert_to(kg*m**2/s**2)) == unicode_str1
  5167. assert pretty(expr.convert_to(kg*m**2/s**2)) == ascii_str1
  5168. assert upretty(3*kg*x*m**2*y/s**2) == unicode_str2
  5169. assert pretty(3*kg*x*m**2*y/s**2) == ascii_str2
  5170. def test_pretty_Subs():
  5171. f = Function('f')
  5172. expr = Subs(f(x), x, ph**2)
  5173. ascii_str = \
  5174. """\
  5175. (f(x))| 2\n\
  5176. |x=phi \
  5177. """
  5178. unicode_str = \
  5179. """\
  5180. (f(x)) 2\n\
  5181. x=φ \
  5182. """
  5183. assert pretty(expr) == ascii_str
  5184. assert upretty(expr) == unicode_str
  5185. expr = Subs(f(x).diff(x), x, 0)
  5186. ascii_str = \
  5187. """\
  5188. /d \\| \n\
  5189. |--(f(x))|| \n\
  5190. \\dx /|x=0\
  5191. """
  5192. unicode_str = \
  5193. """\
  5194. d \n\
  5195. (f(x)) \n\
  5196. dx x=0\
  5197. """
  5198. assert pretty(expr) == ascii_str
  5199. assert upretty(expr) == unicode_str
  5200. expr = Subs(f(x).diff(x)/y, (x, y), (0, Rational(1, 2)))
  5201. ascii_str = \
  5202. """\
  5203. /d \\| \n\
  5204. |--(f(x))|| \n\
  5205. |dx || \n\
  5206. |--------|| \n\
  5207. \\ y /|x=0, y=1/2\
  5208. """
  5209. unicode_str = \
  5210. """\
  5211. d \n\
  5212. (f(x)) \n\
  5213. dx \n\
  5214. \n\
  5215. y x=0, y=1/2\
  5216. """
  5217. assert pretty(expr) == ascii_str
  5218. assert upretty(expr) == unicode_str
  5219. def test_gammas():
  5220. assert upretty(lowergamma(x, y)) == "γ(x, y)"
  5221. assert upretty(uppergamma(x, y)) == "Γ(x, y)"
  5222. assert xpretty(gamma(x), use_unicode=True) == 'Γ(x)'
  5223. assert xpretty(gamma, use_unicode=True) == 'Γ'
  5224. assert xpretty(symbols('gamma', cls=Function)(x), use_unicode=True) == 'γ(x)'
  5225. assert xpretty(symbols('gamma', cls=Function), use_unicode=True) == 'γ'
  5226. def test_beta():
  5227. assert xpretty(beta(x,y), use_unicode=True) == 'Β(x, y)'
  5228. assert xpretty(beta(x,y), use_unicode=False) == 'B(x, y)'
  5229. assert xpretty(beta, use_unicode=True) == 'Β'
  5230. assert xpretty(beta, use_unicode=False) == 'B'
  5231. mybeta = Function('beta')
  5232. assert xpretty(mybeta(x), use_unicode=True) == 'β(x)'
  5233. assert xpretty(mybeta(x, y, z), use_unicode=False) == 'beta(x, y, z)'
  5234. assert xpretty(mybeta, use_unicode=True) == 'β'
  5235. # test that notation passes to subclasses of the same name only
  5236. def test_function_subclass_different_name():
  5237. class mygamma(gamma):
  5238. pass
  5239. assert xpretty(mygamma, use_unicode=True) == r"mygamma"
  5240. assert xpretty(mygamma(x), use_unicode=True) == r"mygamma(x)"
  5241. def test_SingularityFunction():
  5242. assert xpretty(SingularityFunction(x, 0, n), use_unicode=True) == (
  5243. """\
  5244. n\n\
  5245. <x> \
  5246. """)
  5247. assert xpretty(SingularityFunction(x, 1, n), use_unicode=True) == (
  5248. """\
  5249. n\n\
  5250. <x - 1> \
  5251. """)
  5252. assert xpretty(SingularityFunction(x, -1, n), use_unicode=True) == (
  5253. """\
  5254. n\n\
  5255. <x + 1> \
  5256. """)
  5257. assert xpretty(SingularityFunction(x, a, n), use_unicode=True) == (
  5258. """\
  5259. n\n\
  5260. <-a + x> \
  5261. """)
  5262. assert xpretty(SingularityFunction(x, y, n), use_unicode=True) == (
  5263. """\
  5264. n\n\
  5265. <x - y> \
  5266. """)
  5267. assert xpretty(SingularityFunction(x, 0, n), use_unicode=False) == (
  5268. """\
  5269. n\n\
  5270. <x> \
  5271. """)
  5272. assert xpretty(SingularityFunction(x, 1, n), use_unicode=False) == (
  5273. """\
  5274. n\n\
  5275. <x - 1> \
  5276. """)
  5277. assert xpretty(SingularityFunction(x, -1, n), use_unicode=False) == (
  5278. """\
  5279. n\n\
  5280. <x + 1> \
  5281. """)
  5282. assert xpretty(SingularityFunction(x, a, n), use_unicode=False) == (
  5283. """\
  5284. n\n\
  5285. <-a + x> \
  5286. """)
  5287. assert xpretty(SingularityFunction(x, y, n), use_unicode=False) == (
  5288. """\
  5289. n\n\
  5290. <x - y> \
  5291. """)
  5292. def test_deltas():
  5293. assert xpretty(DiracDelta(x), use_unicode=True) == 'δ(x)'
  5294. assert xpretty(DiracDelta(x, 1), use_unicode=True) == \
  5295. """\
  5296. (1) \n\
  5297. δ (x)\
  5298. """
  5299. assert xpretty(x*DiracDelta(x, 1), use_unicode=True) == \
  5300. """\
  5301. (1) \n\
  5302. xδ (x)\
  5303. """
  5304. def test_hyper():
  5305. expr = hyper((), (), z)
  5306. ucode_str = \
  5307. """\
  5308. \n\
  5309. z\n\
  5310. 0 0 \
  5311. """
  5312. ascii_str = \
  5313. """\
  5314. _ \n\
  5315. |_ / | \\\n\
  5316. | | | z|\n\
  5317. 0 0 \\ | /\
  5318. """
  5319. assert pretty(expr) == ascii_str
  5320. assert upretty(expr) == ucode_str
  5321. expr = hyper((), (1,), x)
  5322. ucode_str = \
  5323. """\
  5324. \n\
  5325. x\n\
  5326. 0 1 1 \
  5327. """
  5328. ascii_str = \
  5329. """\
  5330. _ \n\
  5331. |_ / | \\\n\
  5332. | | | x|\n\
  5333. 0 1 \\1 | /\
  5334. """
  5335. assert pretty(expr) == ascii_str
  5336. assert upretty(expr) == ucode_str
  5337. expr = hyper([2], [1], x)
  5338. ucode_str = \
  5339. """\
  5340. 2 \n\
  5341. x\n\
  5342. 1 1 1 \
  5343. """
  5344. ascii_str = \
  5345. """\
  5346. _ \n\
  5347. |_ /2 | \\\n\
  5348. | | | x|\n\
  5349. 1 1 \\1 | /\
  5350. """
  5351. assert pretty(expr) == ascii_str
  5352. assert upretty(expr) == ucode_str
  5353. expr = hyper((pi/3, -2*k), (3, 4, 5, -3), x)
  5354. ucode_str = \
  5355. """\
  5356. π \n\
  5357. , -2k \n\
  5358. 3 x\n\
  5359. 2 4 \n\
  5360. 3, 4, 5, -3 \
  5361. """
  5362. ascii_str = \
  5363. """\
  5364. \n\
  5365. _ / pi | \\\n\
  5366. |_ | --, -2*k | |\n\
  5367. | | 3 | x|\n\
  5368. 2 4 | | |\n\
  5369. \\3, 4, 5, -3 | /\
  5370. """
  5371. assert pretty(expr) == ascii_str
  5372. assert upretty(expr) == ucode_str
  5373. expr = hyper((pi, S('2/3'), -2*k), (3, 4, 5, -3), x**2)
  5374. ucode_str = \
  5375. """\
  5376. π, 2/3, -2k 2\n\
  5377. x \n\
  5378. 3 4 3, 4, 5, -3 \
  5379. """
  5380. ascii_str = \
  5381. """\
  5382. _ \n\
  5383. |_ /pi, 2/3, -2*k | 2\\\n\
  5384. | | | x |\n\
  5385. 3 4 \\ 3, 4, 5, -3 | /\
  5386. """
  5387. assert pretty(expr) == ascii_str
  5388. assert upretty(expr) == ucode_str
  5389. expr = hyper([1, 2], [3, 4], 1/(1/(1/(1/x + 1) + 1) + 1))
  5390. ucode_str = \
  5391. """\
  5392. 1 \n\
  5393. \n\
  5394. 1 \n\
  5395. 1, 2 1 + \n\
  5396. 1 \n\
  5397. 2 2 3, 4 1 + \n\
  5398. 1\n\
  5399. 1 + \n\
  5400. x\
  5401. """
  5402. ascii_str = \
  5403. """\
  5404. \n\
  5405. / | 1 \\\n\
  5406. | | -------------|\n\
  5407. _ | | 1 |\n\
  5408. |_ |1, 2 | 1 + ---------|\n\
  5409. | | | 1 |\n\
  5410. 2 2 |3, 4 | 1 + -----|\n\
  5411. | | 1|\n\
  5412. | | 1 + -|\n\
  5413. \\ | x/\
  5414. """
  5415. assert pretty(expr) == ascii_str
  5416. assert upretty(expr) == ucode_str
  5417. def test_meijerg():
  5418. expr = meijerg([pi, pi, x], [1], [0, 1], [1, 2, 3], z)
  5419. ucode_str = \
  5420. """\
  5421. 2, 3 π, π, x 1 \n\
  5422. z\n\
  5423. 4, 5 0, 1 1, 2, 3 \
  5424. """
  5425. ascii_str = \
  5426. """\
  5427. __2, 3 /pi, pi, x 1 | \\\n\
  5428. /__ | | z|\n\
  5429. \\_|4, 5 \\ 0, 1 1, 2, 3 | /\
  5430. """
  5431. assert pretty(expr) == ascii_str
  5432. assert upretty(expr) == ucode_str
  5433. expr = meijerg([1, pi/7], [2, pi, 5], [], [], z**2)
  5434. ucode_str = \
  5435. """\
  5436. π \n\
  5437. 0, 2 1, 2, π, 5 2\n\
  5438. 7 z \n\
  5439. 5, 0 \n\
  5440. \
  5441. """
  5442. ascii_str = \
  5443. """\
  5444. / pi | \\\n\
  5445. __0, 2 |1, -- 2, pi, 5 | 2|\n\
  5446. /__ | 7 | z |\n\
  5447. \\_|5, 0 | | |\n\
  5448. \\ | /\
  5449. """
  5450. assert pretty(expr) == ascii_str
  5451. assert upretty(expr) == ucode_str
  5452. ucode_str = \
  5453. """\
  5454. 1, 10 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 \n\
  5455. z\n\
  5456. 11, 2 1 1 \
  5457. """
  5458. ascii_str = \
  5459. """\
  5460. __ 1, 10 /1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 | \\\n\
  5461. /__ | | z|\n\
  5462. \\_|11, 2 \\ 1 1 | /\
  5463. """
  5464. expr = meijerg([1]*10, [1], [1], [1], z)
  5465. assert pretty(expr) == ascii_str
  5466. assert upretty(expr) == ucode_str
  5467. expr = meijerg([1, 2, ], [4, 3], [3], [4, 5], 1/(1/(1/(1/x + 1) + 1) + 1))
  5468. ucode_str = \
  5469. """\
  5470. 1 \n\
  5471. \n\
  5472. 1 \n\
  5473. 1, 2 1, 2 4, 3 1 + \n\
  5474. 1 \n\
  5475. 4, 3 3 4, 5 1 + \n\
  5476. 1\n\
  5477. 1 + \n\
  5478. x\
  5479. """
  5480. ascii_str = \
  5481. """\
  5482. / | 1 \\\n\
  5483. | | -------------|\n\
  5484. | | 1 |\n\
  5485. __1, 2 |1, 2 4, 3 | 1 + ---------|\n\
  5486. /__ | | 1 |\n\
  5487. \\_|4, 3 | 3 4, 5 | 1 + -----|\n\
  5488. | | 1|\n\
  5489. | | 1 + -|\n\
  5490. \\ | x/\
  5491. """
  5492. assert pretty(expr) == ascii_str
  5493. assert upretty(expr) == ucode_str
  5494. expr = Integral(expr, x)
  5495. ucode_str = \
  5496. """\
  5497. \n\
  5498. 1 \n\
  5499. \n\
  5500. 1 \n\
  5501. 1, 2 1, 2 4, 3 1 + \n\
  5502. 1 dx\n\
  5503. 4, 3 3 4, 5 1 + \n\
  5504. 1 \n\
  5505. 1 + \n\
  5506. x \n\
  5507. \
  5508. """
  5509. ascii_str = \
  5510. """\
  5511. / \n\
  5512. | \n\
  5513. | / | 1 \\ \n\
  5514. | | | -------------| \n\
  5515. | | | 1 | \n\
  5516. | __1, 2 |1, 2 4, 3 | 1 + ---------| \n\
  5517. | /__ | | 1 | dx\n\
  5518. | \\_|4, 3 | 3 4, 5 | 1 + -----| \n\
  5519. | | | 1| \n\
  5520. | | | 1 + -| \n\
  5521. | \\ | x/ \n\
  5522. | \n\
  5523. / \
  5524. """
  5525. assert pretty(expr) == ascii_str
  5526. assert upretty(expr) == ucode_str
  5527. def test_noncommutative():
  5528. A, B, C = symbols('A,B,C', commutative=False)
  5529. expr = A*B*C**-1
  5530. ascii_str = \
  5531. """\
  5532. -1\n\
  5533. A*B*C \
  5534. """
  5535. ucode_str = \
  5536. """\
  5537. -1\n\
  5538. ABC \
  5539. """
  5540. assert pretty(expr) == ascii_str
  5541. assert upretty(expr) == ucode_str
  5542. expr = C**-1*A*B
  5543. ascii_str = \
  5544. """\
  5545. -1 \n\
  5546. C *A*B\
  5547. """
  5548. ucode_str = \
  5549. """\
  5550. -1 \n\
  5551. C AB\
  5552. """
  5553. assert pretty(expr) == ascii_str
  5554. assert upretty(expr) == ucode_str
  5555. expr = A*C**-1*B
  5556. ascii_str = \
  5557. """\
  5558. -1 \n\
  5559. A*C *B\
  5560. """
  5561. ucode_str = \
  5562. """\
  5563. -1 \n\
  5564. AC B\
  5565. """
  5566. assert pretty(expr) == ascii_str
  5567. assert upretty(expr) == ucode_str
  5568. expr = A*C**-1*B/x
  5569. ascii_str = \
  5570. """\
  5571. -1 \n\
  5572. A*C *B\n\
  5573. -------\n\
  5574. x \
  5575. """
  5576. ucode_str = \
  5577. """\
  5578. -1 \n\
  5579. AC B\n\
  5580. \n\
  5581. x \
  5582. """
  5583. assert pretty(expr) == ascii_str
  5584. assert upretty(expr) == ucode_str
  5585. def test_pretty_special_functions():
  5586. x, y = symbols("x y")
  5587. # atan2
  5588. expr = atan2(y/sqrt(200), sqrt(x))
  5589. ascii_str = \
  5590. """\
  5591. / ___ \\\n\
  5592. |\\/ 2 *y ___|\n\
  5593. atan2|-------, \\/ x |\n\
  5594. \\ 20 /\
  5595. """
  5596. ucode_str = \
  5597. """\
  5598. 2y \n\
  5599. atan2, x\n\
  5600. 20 \
  5601. """
  5602. assert pretty(expr) == ascii_str
  5603. assert upretty(expr) == ucode_str
  5604. def test_pretty_geometry():
  5605. e = Segment((0, 1), (0, 2))
  5606. assert pretty(e) == 'Segment2D(Point2D(0, 1), Point2D(0, 2))'
  5607. e = Ray((1, 1), angle=4.02*pi)
  5608. assert pretty(e) == 'Ray2D(Point2D(1, 1), Point2D(2, tan(pi/50) + 1))'
  5609. def test_expint():
  5610. expr = Ei(x)
  5611. string = 'Ei(x)'
  5612. assert pretty(expr) == string
  5613. assert upretty(expr) == string
  5614. expr = expint(1, z)
  5615. ucode_str = "E₁(z)"
  5616. ascii_str = "expint(1, z)"
  5617. assert pretty(expr) == ascii_str
  5618. assert upretty(expr) == ucode_str
  5619. assert pretty(Shi(x)) == 'Shi(x)'
  5620. assert pretty(Si(x)) == 'Si(x)'
  5621. assert pretty(Ci(x)) == 'Ci(x)'
  5622. assert pretty(Chi(x)) == 'Chi(x)'
  5623. assert upretty(Shi(x)) == 'Shi(x)'
  5624. assert upretty(Si(x)) == 'Si(x)'
  5625. assert upretty(Ci(x)) == 'Ci(x)'
  5626. assert upretty(Chi(x)) == 'Chi(x)'
  5627. def test_elliptic_functions():
  5628. ascii_str = \
  5629. """\
  5630. / 1 \\\n\
  5631. K|-----|\n\
  5632. \\z + 1/\
  5633. """
  5634. ucode_str = \
  5635. """\
  5636. 1 \n\
  5637. K\n\
  5638. z + 1\
  5639. """
  5640. expr = elliptic_k(1/(z + 1))
  5641. assert pretty(expr) == ascii_str
  5642. assert upretty(expr) == ucode_str
  5643. ascii_str = \
  5644. """\
  5645. / | 1 \\\n\
  5646. F|1|-----|\n\
  5647. \\ |z + 1/\
  5648. """
  5649. ucode_str = \
  5650. """\
  5651. 1 \n\
  5652. F1\n\
  5653. z + 1\
  5654. """
  5655. expr = elliptic_f(1, 1/(1 + z))
  5656. assert pretty(expr) == ascii_str
  5657. assert upretty(expr) == ucode_str
  5658. ascii_str = \
  5659. """\
  5660. / 1 \\\n\
  5661. E|-----|\n\
  5662. \\z + 1/\
  5663. """
  5664. ucode_str = \
  5665. """\
  5666. 1 \n\
  5667. E\n\
  5668. z + 1\
  5669. """
  5670. expr = elliptic_e(1/(z + 1))
  5671. assert pretty(expr) == ascii_str
  5672. assert upretty(expr) == ucode_str
  5673. ascii_str = \
  5674. """\
  5675. / | 1 \\\n\
  5676. E|1|-----|\n\
  5677. \\ |z + 1/\
  5678. """
  5679. ucode_str = \
  5680. """\
  5681. 1 \n\
  5682. E1\n\
  5683. z + 1\
  5684. """
  5685. expr = elliptic_e(1, 1/(1 + z))
  5686. assert pretty(expr) == ascii_str
  5687. assert upretty(expr) == ucode_str
  5688. ascii_str = \
  5689. """\
  5690. / |4\\\n\
  5691. Pi|3|-|\n\
  5692. \\ |x/\
  5693. """
  5694. ucode_str = \
  5695. """\
  5696. 4\n\
  5697. Π3\n\
  5698. x\
  5699. """
  5700. expr = elliptic_pi(3, 4/x)
  5701. assert pretty(expr) == ascii_str
  5702. assert upretty(expr) == ucode_str
  5703. ascii_str = \
  5704. """\
  5705. / 4| \\\n\
  5706. Pi|3; -|6|\n\
  5707. \\ x| /\
  5708. """
  5709. ucode_str = \
  5710. """\
  5711. 4 \n\
  5712. Π3; 6\n\
  5713. x \
  5714. """
  5715. expr = elliptic_pi(3, 4/x, 6)
  5716. assert pretty(expr) == ascii_str
  5717. assert upretty(expr) == ucode_str
  5718. def test_RandomDomain():
  5719. from sympy.stats import Normal, Die, Exponential, pspace, where
  5720. X = Normal('x1', 0, 1)
  5721. assert upretty(where(X > 0)) == "Domain: 0 < x₁ ∧ x₁ < ∞"
  5722. D = Die('d1', 6)
  5723. assert upretty(where(D > 4)) == 'Domain: d₁ = 5 ∨ d₁ = 6'
  5724. A = Exponential('a', 1)
  5725. B = Exponential('b', 1)
  5726. assert upretty(pspace(Tuple(A, B)).domain) == \
  5727. 'Domain: 0 ≤ a ∧ 0 ≤ b ∧ a < ∞ ∧ b < ∞'
  5728. def test_PrettyPoly():
  5729. F = QQ.frac_field(x, y)
  5730. R = QQ.poly_ring(x, y)
  5731. expr = F.convert(x/(x + y))
  5732. assert pretty(expr) == "x/(x + y)"
  5733. assert upretty(expr) == "x/(x + y)"
  5734. expr = R.convert(x + y)
  5735. assert pretty(expr) == "x + y"
  5736. assert upretty(expr) == "x + y"
  5737. def test_issue_6285():
  5738. assert pretty(Pow(2, -5, evaluate=False)) == '1 \n--\n 5\n2 '
  5739. assert pretty(Pow(x, (1/pi))) == \
  5740. ' 1 \n'\
  5741. ' --\n'\
  5742. ' pi\n'\
  5743. 'x '
  5744. def test_issue_6359():
  5745. assert pretty(Integral(x**2, x)**2) == \
  5746. """\
  5747. 2
  5748. / / \\ \n\
  5749. | | | \n\
  5750. | | 2 | \n\
  5751. | | x dx| \n\
  5752. | | | \n\
  5753. \\/ / \
  5754. """
  5755. assert upretty(Integral(x**2, x)**2) == \
  5756. """\
  5757. 2
  5758. \n\
  5759. 2 \n\
  5760. x dx \n\
  5761. \
  5762. """
  5763. assert pretty(Sum(x**2, (x, 0, 1))**2) == \
  5764. """\
  5765. 2
  5766. / 1 \\ \n\
  5767. | ___ | \n\
  5768. | \\ ` | \n\
  5769. | \\ 2| \n\
  5770. | / x | \n\
  5771. | /__, | \n\
  5772. \\x = 0 / \
  5773. """
  5774. assert upretty(Sum(x**2, (x, 0, 1))**2) == \
  5775. """\
  5776. 2
  5777. 1 \n\
  5778. ___ \n\
  5779. \n\
  5780. 2 \n\
  5781. x \n\
  5782. \n\
  5783. \n\
  5784. x = 0 \
  5785. """
  5786. assert pretty(Product(x**2, (x, 1, 2))**2) == \
  5787. """\
  5788. 2
  5789. / 2 \\ \n\
  5790. |______ | \n\
  5791. | | | 2| \n\
  5792. | | | x | \n\
  5793. | | | | \n\
  5794. \\x = 1 / \
  5795. """
  5796. assert upretty(Product(x**2, (x, 1, 2))**2) == \
  5797. """\
  5798. 2
  5799. 2 \n\
  5800. \n\
  5801. 2 \n\
  5802. x \n\
  5803. \n\
  5804. x = 1 \
  5805. """
  5806. f = Function('f')
  5807. assert pretty(Derivative(f(x), x)**2) == \
  5808. """\
  5809. 2
  5810. /d \\ \n\
  5811. |--(f(x))| \n\
  5812. \\dx / \
  5813. """
  5814. assert upretty(Derivative(f(x), x)**2) == \
  5815. """\
  5816. 2
  5817. d \n\
  5818. (f(x)) \n\
  5819. dx \
  5820. """
  5821. def test_issue_6739():
  5822. ascii_str = \
  5823. """\
  5824. 1 \n\
  5825. -----\n\
  5826. ___\n\
  5827. \\/ x \
  5828. """
  5829. ucode_str = \
  5830. """\
  5831. 1 \n\
  5832. \n\
  5833. x\
  5834. """
  5835. assert pretty(1/sqrt(x)) == ascii_str
  5836. assert upretty(1/sqrt(x)) == ucode_str
  5837. def test_complicated_symbol_unchanged():
  5838. for symb_name in ["dexpr2_d1tau", "dexpr2^d1tau"]:
  5839. assert pretty(Symbol(symb_name)) == symb_name
  5840. def test_categories():
  5841. from sympy.categories import (Object, IdentityMorphism,
  5842. NamedMorphism, Category, Diagram, DiagramGrid)
  5843. A1 = Object("A1")
  5844. A2 = Object("A2")
  5845. A3 = Object("A3")
  5846. f1 = NamedMorphism(A1, A2, "f1")
  5847. f2 = NamedMorphism(A2, A3, "f2")
  5848. id_A1 = IdentityMorphism(A1)
  5849. K1 = Category("K1")
  5850. assert pretty(A1) == "A1"
  5851. assert upretty(A1) == "A₁"
  5852. assert pretty(f1) == "f1:A1-->A2"
  5853. assert upretty(f1) == "f₁:A₁——▶A₂"
  5854. assert pretty(id_A1) == "id:A1-->A1"
  5855. assert upretty(id_A1) == "id:A₁——▶A₁"
  5856. assert pretty(f2*f1) == "f2*f1:A1-->A3"
  5857. assert upretty(f2*f1) == "f₂∘f₁:A₁——▶A₃"
  5858. assert pretty(K1) == "K1"
  5859. assert upretty(K1) == "K₁"
  5860. # Test how diagrams are printed.
  5861. d = Diagram()
  5862. assert pretty(d) == "EmptySet"
  5863. assert upretty(d) == ""
  5864. d = Diagram({f1: "unique", f2: S.EmptySet})
  5865. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  5866. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  5867. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}"
  5868. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, " \
  5869. "id:A₂——▶A₂: ∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}"
  5870. d = Diagram({f1: "unique", f2: S.EmptySet}, {f2 * f1: "unique"})
  5871. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  5872. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  5873. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}" \
  5874. " ==> {f2*f1:A1-->A3: {unique}}"
  5875. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, id:A₂——▶A₂: " \
  5876. "∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}" \
  5877. " ══▶ {f₂∘f₁:A₁——▶A₃: {unique}}"
  5878. grid = DiagramGrid(d)
  5879. assert pretty(grid) == "A1 A2\n \nA3 "
  5880. assert upretty(grid) == "A₁ A₂\n \nA₃ "
  5881. def test_PrettyModules():
  5882. R = QQ.old_poly_ring(x, y)
  5883. F = R.free_module(2)
  5884. M = F.submodule([x, y], [1, x**2])
  5885. ucode_str = \
  5886. """\
  5887. 2\n\
  5888. [x, y] \
  5889. """
  5890. ascii_str = \
  5891. """\
  5892. 2\n\
  5893. QQ[x, y] \
  5894. """
  5895. assert upretty(F) == ucode_str
  5896. assert pretty(F) == ascii_str
  5897. ucode_str = \
  5898. """\
  5899. 2\n\
  5900. [x, y], 1, x \
  5901. """
  5902. ascii_str = \
  5903. """\
  5904. 2 \n\
  5905. <[x, y], [1, x ]>\
  5906. """
  5907. assert upretty(M) == ucode_str
  5908. assert pretty(M) == ascii_str
  5909. I = R.ideal(x**2, y)
  5910. ucode_str = \
  5911. """\
  5912. 2 \n\
  5913. x , y\
  5914. """
  5915. ascii_str = \
  5916. """\
  5917. 2 \n\
  5918. <x , y>\
  5919. """
  5920. assert upretty(I) == ucode_str
  5921. assert pretty(I) == ascii_str
  5922. Q = F / M
  5923. ucode_str = \
  5924. """\
  5925. 2 \n\
  5926. [x, y] \n\
  5927. \n\
  5928. 2\n\
  5929. [x, y], 1, x \
  5930. """
  5931. ascii_str = \
  5932. """\
  5933. 2 \n\
  5934. QQ[x, y] \n\
  5935. -----------------\n\
  5936. 2 \n\
  5937. <[x, y], [1, x ]>\
  5938. """
  5939. assert upretty(Q) == ucode_str
  5940. assert pretty(Q) == ascii_str
  5941. ucode_str = \
  5942. """\
  5943. 3 \n\
  5944. x 2 2\n\
  5945. 1, + [x, y], 1, x , [2, y] + [x, y], 1, x \n\
  5946. 2 \
  5947. """
  5948. ascii_str = \
  5949. """\
  5950. 3 \n\
  5951. x 2 2 \n\
  5952. <[1, --] + <[x, y], [1, x ]>, [2, y] + <[x, y], [1, x ]>>\n\
  5953. 2 \
  5954. """
  5955. def test_QuotientRing():
  5956. R = QQ.old_poly_ring(x)/[x**2 + 1]
  5957. ucode_str = \
  5958. """\
  5959. [x] \n\
  5960. \n\
  5961. 2 \n\
  5962. x + 1\
  5963. """
  5964. ascii_str = \
  5965. """\
  5966. QQ[x] \n\
  5967. --------\n\
  5968. 2 \n\
  5969. <x + 1>\
  5970. """
  5971. assert upretty(R) == ucode_str
  5972. assert pretty(R) == ascii_str
  5973. ucode_str = \
  5974. """\
  5975. 2 \n\
  5976. 1 + x + 1\
  5977. """
  5978. ascii_str = \
  5979. """\
  5980. 2 \n\
  5981. 1 + <x + 1>\
  5982. """
  5983. assert upretty(R.one) == ucode_str
  5984. assert pretty(R.one) == ascii_str
  5985. def test_Homomorphism():
  5986. from sympy.polys.agca import homomorphism
  5987. R = QQ.old_poly_ring(x)
  5988. expr = homomorphism(R.free_module(1), R.free_module(1), [0])
  5989. ucode_str = \
  5990. """\
  5991. 1 1\n\
  5992. [0] : [x] > [x] \
  5993. """
  5994. ascii_str = \
  5995. """\
  5996. 1 1\n\
  5997. [0] : QQ[x] --> QQ[x] \
  5998. """
  5999. assert upretty(expr) == ucode_str
  6000. assert pretty(expr) == ascii_str
  6001. expr = homomorphism(R.free_module(2), R.free_module(2), [0, 0])
  6002. ucode_str = \
  6003. """\
  6004. 0 0 2 2\n\
  6005. : [x] > [x] \n\
  6006. 0 0 \
  6007. """
  6008. ascii_str = \
  6009. """\
  6010. [0 0] 2 2\n\
  6011. [ ] : QQ[x] --> QQ[x] \n\
  6012. [0 0] \
  6013. """
  6014. assert upretty(expr) == ucode_str
  6015. assert pretty(expr) == ascii_str
  6016. expr = homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])
  6017. ucode_str = \
  6018. """\
  6019. 1\n\
  6020. 1 [x] \n\
  6021. [0] : [x] > \n\
  6022. <[x]>\
  6023. """
  6024. ascii_str = \
  6025. """\
  6026. 1\n\
  6027. 1 QQ[x] \n\
  6028. [0] : QQ[x] --> ------\n\
  6029. <[x]> \
  6030. """
  6031. assert upretty(expr) == ucode_str
  6032. assert pretty(expr) == ascii_str
  6033. def test_Tr():
  6034. A, B = symbols('A B', commutative=False)
  6035. t = Tr(A*B)
  6036. assert pretty(t) == r'Tr(A*B)'
  6037. assert upretty(t) == 'Tr(A⋅B)'
  6038. def test_pretty_Add():
  6039. eq = Mul(-2, x - 2, evaluate=False) + 5
  6040. assert pretty(eq) == '5 - 2*(x - 2)'
  6041. def test_issue_7179():
  6042. assert upretty(Not(Equivalent(x, y))) == 'x ⇎ y'
  6043. assert upretty(Not(Implies(x, y))) == 'x ↛ y'
  6044. def test_issue_7180():
  6045. assert upretty(Equivalent(x, y)) == 'x ⇔ y'
  6046. def test_pretty_Complement():
  6047. assert pretty(S.Reals - S.Naturals) == '(-oo, oo) \\ Naturals'
  6048. assert upretty(S.Reals - S.Naturals) == '\\'
  6049. assert pretty(S.Reals - S.Naturals0) == '(-oo, oo) \\ Naturals0'
  6050. assert upretty(S.Reals - S.Naturals0) == '\\ ℕ₀'
  6051. def test_pretty_SymmetricDifference():
  6052. from sympy.sets.sets import SymmetricDifference
  6053. assert upretty(SymmetricDifference(Interval(2,3), Interval(3,5), \
  6054. evaluate = False)) == '[2, 3] ∆ [3, 5]'
  6055. with raises(NotImplementedError):
  6056. pretty(SymmetricDifference(Interval(2,3), Interval(3,5), evaluate = False))
  6057. def test_pretty_Contains():
  6058. assert pretty(Contains(x, S.Integers)) == 'Contains(x, Integers)'
  6059. assert upretty(Contains(x, S.Integers)) == 'x ∈ ℤ'
  6060. def test_issue_8292():
  6061. from sympy.core import sympify
  6062. e = sympify('((x+x**4)/(x-1))-(2*(x-1)**4/(x-1)**4)', evaluate=False)
  6063. ucode_str = \
  6064. """\
  6065. 4 4 \n\
  6066. 2(x - 1) x + x\n\
  6067. - + \n\
  6068. 4 x - 1 \n\
  6069. (x - 1) \
  6070. """
  6071. ascii_str = \
  6072. """\
  6073. 4 4 \n\
  6074. 2*(x - 1) x + x\n\
  6075. - ---------- + ------\n\
  6076. 4 x - 1 \n\
  6077. (x - 1) \
  6078. """
  6079. assert pretty(e) == ascii_str
  6080. assert upretty(e) == ucode_str
  6081. def test_issue_4335():
  6082. y = Function('y')
  6083. expr = -y(x).diff(x)
  6084. ucode_str = \
  6085. """\
  6086. d \n\
  6087. -(y(x))\n\
  6088. dx \
  6089. """
  6090. ascii_str = \
  6091. """\
  6092. d \n\
  6093. - --(y(x))\n\
  6094. dx \
  6095. """
  6096. assert pretty(expr) == ascii_str
  6097. assert upretty(expr) == ucode_str
  6098. def test_issue_8344():
  6099. from sympy.core import sympify
  6100. e = sympify('2*x*y**2/1**2 + 1', evaluate=False)
  6101. ucode_str = \
  6102. """\
  6103. 2 \n\
  6104. 2xy \n\
  6105. + 1\n\
  6106. 2 \n\
  6107. 1 \
  6108. """
  6109. assert upretty(e) == ucode_str
  6110. def test_issue_6324():
  6111. x = Pow(2, 3, evaluate=False)
  6112. y = Pow(10, -2, evaluate=False)
  6113. e = Mul(x, y, evaluate=False)
  6114. ucode_str = \
  6115. """\
  6116. 3\n\
  6117. 2 \n\
  6118. \n\
  6119. 2\n\
  6120. 10 \
  6121. """
  6122. assert upretty(e) == ucode_str
  6123. def test_issue_7927():
  6124. e = sin(x/2)**cos(x/2)
  6125. ucode_str = \
  6126. """\
  6127. x\n\
  6128. cos\n\
  6129. 2\n\
  6130. x \n\
  6131. sin \n\
  6132. 2 \
  6133. """
  6134. assert upretty(e) == ucode_str
  6135. e = sin(x)**(S(11)/13)
  6136. ucode_str = \
  6137. """\
  6138. 11\n\
  6139. \n\
  6140. 13\n\
  6141. (sin(x)) \
  6142. """
  6143. assert upretty(e) == ucode_str
  6144. def test_issue_6134():
  6145. from sympy.abc import lamda, t
  6146. phi = Function('phi')
  6147. e = lamda*x*Integral(phi(t)*pi*sin(pi*t), (t, 0, 1)) + lamda*x**2*Integral(phi(t)*2*pi*sin(2*pi*t), (t, 0, 1))
  6148. ucode_str = \
  6149. """\
  6150. 1 1 \n\
  6151. 2 \n\
  6152. λx 2πφ(t)sin(2πt) dt + λx πφ(t)sin(πt) dt\n\
  6153. \n\
  6154. 0 0 \
  6155. """
  6156. assert upretty(e) == ucode_str
  6157. def test_issue_9877():
  6158. ucode_str1 = '(2, 3) ∪ ([1, 2] \\ {x})'
  6159. a, b, c = Interval(2, 3, True, True), Interval(1, 2), FiniteSet(x)
  6160. assert upretty(Union(a, Complement(b, c))) == ucode_str1
  6161. ucode_str2 = '{x} ∩ {y} ∩ ({z} \\ [1, 2])'
  6162. d, e, f, g = FiniteSet(x), FiniteSet(y), FiniteSet(z), Interval(1, 2)
  6163. assert upretty(Intersection(d, e, Complement(f, g))) == ucode_str2
  6164. def test_issue_13651():
  6165. expr1 = c + Mul(-1, a + b, evaluate=False)
  6166. assert pretty(expr1) == 'c - (a + b)'
  6167. expr2 = c + Mul(-1, a - b + d, evaluate=False)
  6168. assert pretty(expr2) == 'c - (a - b + d)'
  6169. def test_pretty_primenu():
  6170. from sympy.ntheory.factor_ import primenu
  6171. ascii_str1 = "nu(n)"
  6172. ucode_str1 = "ν(n)"
  6173. n = symbols('n', integer=True)
  6174. assert pretty(primenu(n)) == ascii_str1
  6175. assert upretty(primenu(n)) == ucode_str1
  6176. def test_pretty_primeomega():
  6177. from sympy.ntheory.factor_ import primeomega
  6178. ascii_str1 = "Omega(n)"
  6179. ucode_str1 = "Ω(n)"
  6180. n = symbols('n', integer=True)
  6181. assert pretty(primeomega(n)) == ascii_str1
  6182. assert upretty(primeomega(n)) == ucode_str1
  6183. def test_pretty_Mod():
  6184. from sympy.core import Mod
  6185. ascii_str1 = "x mod 7"
  6186. ucode_str1 = "x mod 7"
  6187. ascii_str2 = "(x + 1) mod 7"
  6188. ucode_str2 = "(x + 1) mod 7"
  6189. ascii_str3 = "2*x mod 7"
  6190. ucode_str3 = "2⋅x mod 7"
  6191. ascii_str4 = "(x mod 7) + 1"
  6192. ucode_str4 = "(x mod 7) + 1"
  6193. ascii_str5 = "2*(x mod 7)"
  6194. ucode_str5 = "2⋅(x mod 7)"
  6195. x = symbols('x', integer=True)
  6196. assert pretty(Mod(x, 7)) == ascii_str1
  6197. assert upretty(Mod(x, 7)) == ucode_str1
  6198. assert pretty(Mod(x + 1, 7)) == ascii_str2
  6199. assert upretty(Mod(x + 1, 7)) == ucode_str2
  6200. assert pretty(Mod(2 * x, 7)) == ascii_str3
  6201. assert upretty(Mod(2 * x, 7)) == ucode_str3
  6202. assert pretty(Mod(x, 7) + 1) == ascii_str4
  6203. assert upretty(Mod(x, 7) + 1) == ucode_str4
  6204. assert pretty(2 * Mod(x, 7)) == ascii_str5
  6205. assert upretty(2 * Mod(x, 7)) == ucode_str5
  6206. def test_issue_11801():
  6207. assert pretty(Symbol("")) == ""
  6208. assert upretty(Symbol("")) == ""
  6209. def test_pretty_UnevaluatedExpr():
  6210. x = symbols('x')
  6211. he = UnevaluatedExpr(1/x)
  6212. ucode_str = \
  6213. """\
  6214. 1\n\
  6215. \n\
  6216. x\
  6217. """
  6218. assert upretty(he) == ucode_str
  6219. ucode_str = \
  6220. """\
  6221. 2\n\
  6222. 1 \n\
  6223. \n\
  6224. x \
  6225. """
  6226. assert upretty(he**2) == ucode_str
  6227. ucode_str = \
  6228. """\
  6229. 1\n\
  6230. 1 + \n\
  6231. x\
  6232. """
  6233. assert upretty(he + 1) == ucode_str
  6234. ucode_str = \
  6235. ('''\
  6236. 1\n\
  6237. x\n\
  6238. x\
  6239. ''')
  6240. assert upretty(x*he) == ucode_str
  6241. def test_issue_10472():
  6242. M = (Matrix([[0, 0], [0, 0]]), Matrix([0, 0]))
  6243. ucode_str = \
  6244. """\
  6245. 0 0 0
  6246. ,
  6247. 0 0 0\
  6248. """
  6249. assert upretty(M) == ucode_str
  6250. def test_MatrixElement_printing():
  6251. # test cases for issue #11821
  6252. A = MatrixSymbol("A", 1, 3)
  6253. B = MatrixSymbol("B", 1, 3)
  6254. C = MatrixSymbol("C", 1, 3)
  6255. ascii_str1 = "A_00"
  6256. ucode_str1 = "A₀₀"
  6257. assert pretty(A[0, 0]) == ascii_str1
  6258. assert upretty(A[0, 0]) == ucode_str1
  6259. ascii_str1 = "3*A_00"
  6260. ucode_str1 = "3⋅A₀₀"
  6261. assert pretty(3*A[0, 0]) == ascii_str1
  6262. assert upretty(3*A[0, 0]) == ucode_str1
  6263. ascii_str1 = "(-B + A)[0, 0]"
  6264. ucode_str1 = "(-B + A)[0, 0]"
  6265. F = C[0, 0].subs(C, A - B)
  6266. assert pretty(F) == ascii_str1
  6267. assert upretty(F) == ucode_str1
  6268. def test_issue_12675():
  6269. x, y, t, j = symbols('x y t j')
  6270. e = CoordSys3D('e')
  6271. ucode_str = \
  6272. """\
  6273. t \n\
  6274. x j_e\n\
  6275. \n\
  6276. y \
  6277. """
  6278. assert upretty((x/y)**t*e.j) == ucode_str
  6279. ucode_str = \
  6280. """\
  6281. 1 \n\
  6282. j_e\n\
  6283. y \
  6284. """
  6285. assert upretty((1/y)*e.j) == ucode_str
  6286. def test_MatrixSymbol_printing():
  6287. # test cases for issue #14237
  6288. A = MatrixSymbol("A", 3, 3)
  6289. B = MatrixSymbol("B", 3, 3)
  6290. C = MatrixSymbol("C", 3, 3)
  6291. assert pretty(-A*B*C) == "-A*B*C"
  6292. assert pretty(A - B) == "-B + A"
  6293. assert pretty(A*B*C - A*B - B*C) == "-A*B -B*C + A*B*C"
  6294. # issue #14814
  6295. x = MatrixSymbol('x', n, n)
  6296. y = MatrixSymbol('y*', n, n)
  6297. assert pretty(x + y) == "x + y*"
  6298. ascii_str = \
  6299. """\
  6300. 2 \n\
  6301. -2*y* -a*x\
  6302. """
  6303. assert pretty(-a*x + -2*y*y) == ascii_str
  6304. def test_degree_printing():
  6305. expr1 = 90*degree
  6306. assert pretty(expr1) == '90°'
  6307. expr2 = x*degree
  6308. assert pretty(expr2) == ''
  6309. expr3 = cos(x*degree + 90*degree)
  6310. assert pretty(expr3) == 'cos(x° + 90°)'
  6311. def test_vector_expr_pretty_printing():
  6312. A = CoordSys3D('A')
  6313. assert upretty(Cross(A.i, A.x*A.i+3*A.y*A.j)) == "(i_A)×((x_A) i_A + (3⋅y_A) j_A)"
  6314. assert upretty(x*Cross(A.i, A.j)) == 'x⋅(i_A)×(j_A)'
  6315. assert upretty(Curl(A.x*A.i + 3*A.y*A.j)) == "∇×((x_A) i_A + (3⋅y_A) j_A)"
  6316. assert upretty(Divergence(A.x*A.i + 3*A.y*A.j)) == "∇⋅((x_A) i_A + (3⋅y_A) j_A)"
  6317. assert upretty(Dot(A.i, A.x*A.i+3*A.y*A.j)) == "(i_A)⋅((x_A) i_A + (3⋅y_A) j_A)"
  6318. assert upretty(Gradient(A.x+3*A.y)) == "∇(x_A + 3⋅y_A)"
  6319. assert upretty(Laplacian(A.x+3*A.y)) == "∆(x_A + 3⋅y_A)"
  6320. # TODO: add support for ASCII pretty.
  6321. def test_pretty_print_tensor_expr():
  6322. L = TensorIndexType("L")
  6323. i, j, k = tensor_indices("i j k", L)
  6324. i0 = tensor_indices("i_0", L)
  6325. A, B, C, D = tensor_heads("A B C D", [L])
  6326. H = TensorHead("H", [L, L])
  6327. expr = -i
  6328. ascii_str = \
  6329. """\
  6330. -i\
  6331. """
  6332. ucode_str = \
  6333. """\
  6334. -i\
  6335. """
  6336. assert pretty(expr) == ascii_str
  6337. assert upretty(expr) == ucode_str
  6338. expr = A(i)
  6339. ascii_str = \
  6340. """\
  6341. i\n\
  6342. A \n\
  6343. \
  6344. """
  6345. ucode_str = \
  6346. """\
  6347. i\n\
  6348. A \n\
  6349. \
  6350. """
  6351. assert pretty(expr) == ascii_str
  6352. assert upretty(expr) == ucode_str
  6353. expr = A(i0)
  6354. ascii_str = \
  6355. """\
  6356. i_0\n\
  6357. A \n\
  6358. \
  6359. """
  6360. ucode_str = \
  6361. """\
  6362. i\n\
  6363. A \n\
  6364. \
  6365. """
  6366. assert pretty(expr) == ascii_str
  6367. assert upretty(expr) == ucode_str
  6368. expr = A(-i)
  6369. ascii_str = \
  6370. """\
  6371. \n\
  6372. A \n\
  6373. i\
  6374. """
  6375. ucode_str = \
  6376. """\
  6377. \n\
  6378. A \n\
  6379. i\
  6380. """
  6381. assert pretty(expr) == ascii_str
  6382. assert upretty(expr) == ucode_str
  6383. expr = -3*A(-i)
  6384. ascii_str = \
  6385. """\
  6386. \n\
  6387. -3*A \n\
  6388. i\
  6389. """
  6390. ucode_str = \
  6391. """\
  6392. \n\
  6393. -3A \n\
  6394. i\
  6395. """
  6396. assert pretty(expr) == ascii_str
  6397. assert upretty(expr) == ucode_str
  6398. expr = H(i, -j)
  6399. ascii_str = \
  6400. """\
  6401. i \n\
  6402. H \n\
  6403. j\
  6404. """
  6405. ucode_str = \
  6406. """\
  6407. i \n\
  6408. H \n\
  6409. j\
  6410. """
  6411. assert pretty(expr) == ascii_str
  6412. assert upretty(expr) == ucode_str
  6413. expr = H(i, -i)
  6414. ascii_str = \
  6415. """\
  6416. L_0 \n\
  6417. H \n\
  6418. L_0\
  6419. """
  6420. ucode_str = \
  6421. """\
  6422. L \n\
  6423. H \n\
  6424. L\
  6425. """
  6426. assert pretty(expr) == ascii_str
  6427. assert upretty(expr) == ucode_str
  6428. expr = H(i, -j)*A(j)*B(k)
  6429. ascii_str = \
  6430. """\
  6431. i L_0 k\n\
  6432. H *A *B \n\
  6433. L_0 \
  6434. """
  6435. ucode_str = \
  6436. """\
  6437. i L k\n\
  6438. H A B \n\
  6439. L \
  6440. """
  6441. assert pretty(expr) == ascii_str
  6442. assert upretty(expr) == ucode_str
  6443. expr = (1+x)*A(i)
  6444. ascii_str = \
  6445. """\
  6446. i\n\
  6447. (x + 1)*A \n\
  6448. \
  6449. """
  6450. ucode_str = \
  6451. """\
  6452. i\n\
  6453. (x + 1)A \n\
  6454. \
  6455. """
  6456. assert pretty(expr) == ascii_str
  6457. assert upretty(expr) == ucode_str
  6458. expr = A(i) + 3*B(i)
  6459. ascii_str = \
  6460. """\
  6461. i i\n\
  6462. 3*B + A \n\
  6463. \
  6464. """
  6465. ucode_str = \
  6466. """\
  6467. i i\n\
  6468. 3B + A \n\
  6469. \
  6470. """
  6471. assert pretty(expr) == ascii_str
  6472. assert upretty(expr) == ucode_str
  6473. def test_pretty_print_tensor_partial_deriv():
  6474. from sympy.tensor.toperators import PartialDerivative
  6475. L = TensorIndexType("L")
  6476. i, j, k = tensor_indices("i j k", L)
  6477. A, B, C, D = tensor_heads("A B C D", [L])
  6478. H = TensorHead("H", [L, L])
  6479. expr = PartialDerivative(A(i), A(j))
  6480. ascii_str = \
  6481. """\
  6482. d / i\\\n\
  6483. ---|A |\n\
  6484. j\\ /\n\
  6485. dA \n\
  6486. \
  6487. """
  6488. ucode_str = \
  6489. """\
  6490. i\n\
  6491. A \n\
  6492. j \n\
  6493. A \n\
  6494. \
  6495. """
  6496. assert pretty(expr) == ascii_str
  6497. assert upretty(expr) == ucode_str
  6498. expr = A(i)*PartialDerivative(H(k, -i), A(j))
  6499. ascii_str = \
  6500. """\
  6501. L_0 d / k \\\n\
  6502. A *---|H |\n\
  6503. j\\ L_0/\n\
  6504. dA \n\
  6505. \
  6506. """
  6507. ucode_str = \
  6508. """\
  6509. L k \n\
  6510. A H \n\
  6511. j L\n\
  6512. A \n\
  6513. \
  6514. """
  6515. assert pretty(expr) == ascii_str
  6516. assert upretty(expr) == ucode_str
  6517. expr = A(i)*PartialDerivative(B(k)*C(-i) + 3*H(k, -i), A(j))
  6518. ascii_str = \
  6519. """\
  6520. L_0 d / k k \\\n\
  6521. A *---|3*H + B *C |\n\
  6522. j\\ L_0 L_0/\n\
  6523. dA \n\
  6524. \
  6525. """
  6526. ucode_str = \
  6527. """\
  6528. L k k \n\
  6529. A 3H + B C \n\
  6530. j L L\n\
  6531. A \n\
  6532. \
  6533. """
  6534. assert pretty(expr) == ascii_str
  6535. assert upretty(expr) == ucode_str
  6536. expr = (A(i) + B(i))*PartialDerivative(C(j), D(j))
  6537. ascii_str = \
  6538. """\
  6539. / i i\\ d / L_0\\\n\
  6540. |A + B |*-----|C |\n\
  6541. \\ / L_0\\ /\n\
  6542. dD \n\
  6543. \
  6544. """
  6545. ucode_str = \
  6546. """\
  6547. i i L\n\
  6548. A + B C \n\
  6549. L \n\
  6550. D \n\
  6551. \
  6552. """
  6553. assert pretty(expr) == ascii_str
  6554. assert upretty(expr) == ucode_str
  6555. expr = (A(i) + B(i))*PartialDerivative(C(-i), D(j))
  6556. ascii_str = \
  6557. """\
  6558. / L_0 L_0\\ d / \\\n\
  6559. |A + B |*---|C |\n\
  6560. \\ / j\\ L_0/\n\
  6561. dD \n\
  6562. \
  6563. """
  6564. ucode_str = \
  6565. """\
  6566. L L \n\
  6567. A + B C \n\
  6568. j L\n\
  6569. D \n\
  6570. \
  6571. """
  6572. assert pretty(expr) == ascii_str
  6573. assert upretty(expr) == ucode_str
  6574. expr = PartialDerivative(B(-i) + A(-i), A(-j), A(-n))
  6575. ucode_str = """\
  6576. 2 \n\
  6577. \n\
  6578. A + B \n\
  6579. i i\n\
  6580. A A \n\
  6581. n j \
  6582. """
  6583. assert upretty(expr) == ucode_str
  6584. expr = PartialDerivative(3*A(-i), A(-j), A(-n))
  6585. ucode_str = """\
  6586. 2 \n\
  6587. \n\
  6588. 3A \n\
  6589. i\n\
  6590. A A \n\
  6591. n j \
  6592. """
  6593. assert upretty(expr) == ucode_str
  6594. expr = TensorElement(H(i, j), {i:1})
  6595. ascii_str = \
  6596. """\
  6597. i=1,j\n\
  6598. H \n\
  6599. \
  6600. """
  6601. ucode_str = ascii_str
  6602. assert pretty(expr) == ascii_str
  6603. assert upretty(expr) == ucode_str
  6604. expr = TensorElement(H(i, j), {i: 1, j: 1})
  6605. ascii_str = \
  6606. """\
  6607. i=1,j=1\n\
  6608. H \n\
  6609. \
  6610. """
  6611. ucode_str = ascii_str
  6612. assert pretty(expr) == ascii_str
  6613. assert upretty(expr) == ucode_str
  6614. expr = TensorElement(H(i, j), {j: 1})
  6615. ascii_str = \
  6616. """\
  6617. i,j=1\n\
  6618. H \n\
  6619. \
  6620. """
  6621. ucode_str = ascii_str
  6622. expr = TensorElement(H(-i, j), {-i: 1})
  6623. ascii_str = \
  6624. """\
  6625. j\n\
  6626. H \n\
  6627. i=1 \
  6628. """
  6629. ucode_str = ascii_str
  6630. assert pretty(expr) == ascii_str
  6631. assert upretty(expr) == ucode_str
  6632. def test_issue_15560():
  6633. a = MatrixSymbol('a', 1, 1)
  6634. e = pretty(a*(KroneckerProduct(a, a)))
  6635. result = 'a*(a x a)'
  6636. assert e == result
  6637. def test_print_lerchphi():
  6638. # Part of issue 6013
  6639. a = Symbol('a')
  6640. pretty(lerchphi(a, 1, 2))
  6641. uresult = 'Φ(a, 1, 2)'
  6642. aresult = 'lerchphi(a, 1, 2)'
  6643. assert pretty(lerchphi(a, 1, 2)) == aresult
  6644. assert upretty(lerchphi(a, 1, 2)) == uresult
  6645. def test_issue_15583():
  6646. N = mechanics.ReferenceFrame('N')
  6647. result = '(n_x, n_y, n_z)'
  6648. e = pretty((N.x, N.y, N.z))
  6649. assert e == result
  6650. def test_matrixSymbolBold():
  6651. # Issue 15871
  6652. def boldpretty(expr):
  6653. return xpretty(expr, use_unicode=True, wrap_line=False, mat_symbol_style="bold")
  6654. from sympy.matrices.expressions.trace import trace
  6655. A = MatrixSymbol("A", 2, 2)
  6656. assert boldpretty(trace(A)) == 'tr(𝐀)'
  6657. A = MatrixSymbol("A", 3, 3)
  6658. B = MatrixSymbol("B", 3, 3)
  6659. C = MatrixSymbol("C", 3, 3)
  6660. assert boldpretty(-A) == '-𝐀'
  6661. assert boldpretty(A - A*B - B) == '-𝐁 -𝐀⋅𝐁 + 𝐀'
  6662. assert boldpretty(-A*B - A*B*C - B) == '-𝐁 -𝐀⋅𝐁 -𝐀⋅𝐁⋅𝐂'
  6663. A = MatrixSymbol("Addot", 3, 3)
  6664. assert boldpretty(A) == '𝐀̈'
  6665. omega = MatrixSymbol("omega", 3, 3)
  6666. assert boldpretty(omega) == 'ω'
  6667. omega = MatrixSymbol("omeganorm", 3, 3)
  6668. assert boldpretty(omega) == '‖ω‖'
  6669. a = Symbol('alpha')
  6670. b = Symbol('b')
  6671. c = MatrixSymbol("c", 3, 1)
  6672. d = MatrixSymbol("d", 3, 1)
  6673. assert boldpretty(a*B*c+b*d) == 'b⋅𝐝 + α⋅𝐁⋅𝐜'
  6674. d = MatrixSymbol("delta", 3, 1)
  6675. B = MatrixSymbol("Beta", 3, 3)
  6676. assert boldpretty(a*B*c+b*d) == 'b⋅δ + α⋅Β⋅𝐜'
  6677. A = MatrixSymbol("A_2", 3, 3)
  6678. assert boldpretty(A) == '𝐀₂'
  6679. def test_center_accent():
  6680. assert center_accent('a', '\N{COMBINING TILDE}') == ''
  6681. assert center_accent('aa', '\N{COMBINING TILDE}') == 'aã'
  6682. assert center_accent('aaa', '\N{COMBINING TILDE}') == 'aãa'
  6683. assert center_accent('aaaa', '\N{COMBINING TILDE}') == 'aaãa'
  6684. assert center_accent('aaaaa', '\N{COMBINING TILDE}') == 'aaãaa'
  6685. assert center_accent('abcdefg', '\N{COMBINING FOUR DOTS ABOVE}') == 'abcd⃜efg'
  6686. def test_imaginary_unit():
  6687. from sympy.printing.pretty import pretty # b/c it was redefined above
  6688. assert pretty(1 + I, use_unicode=False) == '1 + I'
  6689. assert pretty(1 + I, use_unicode=True) == '1 + ⅈ'
  6690. assert pretty(1 + I, use_unicode=False, imaginary_unit='j') == '1 + I'
  6691. assert pretty(1 + I, use_unicode=True, imaginary_unit='j') == '1 + ⅉ'
  6692. raises(TypeError, lambda: pretty(I, imaginary_unit=I))
  6693. raises(ValueError, lambda: pretty(I, imaginary_unit="kkk"))
  6694. def test_str_special_matrices():
  6695. from sympy.matrices import Identity, ZeroMatrix, OneMatrix
  6696. assert pretty(Identity(4)) == 'I'
  6697. assert upretty(Identity(4)) == '𝕀'
  6698. assert pretty(ZeroMatrix(2, 2)) == '0'
  6699. assert upretty(ZeroMatrix(2, 2)) == '𝟘'
  6700. assert pretty(OneMatrix(2, 2)) == '1'
  6701. assert upretty(OneMatrix(2, 2)) == '𝟙'
  6702. def test_pretty_misc_functions():
  6703. assert pretty(LambertW(x)) == 'W(x)'
  6704. assert upretty(LambertW(x)) == 'W(x)'
  6705. assert pretty(LambertW(x, y)) == 'W(x, y)'
  6706. assert upretty(LambertW(x, y)) == 'W(x, y)'
  6707. assert pretty(airyai(x)) == 'Ai(x)'
  6708. assert upretty(airyai(x)) == 'Ai(x)'
  6709. assert pretty(airybi(x)) == 'Bi(x)'
  6710. assert upretty(airybi(x)) == 'Bi(x)'
  6711. assert pretty(airyaiprime(x)) == "Ai'(x)"
  6712. assert upretty(airyaiprime(x)) == "Ai'(x)"
  6713. assert pretty(airybiprime(x)) == "Bi'(x)"
  6714. assert upretty(airybiprime(x)) == "Bi'(x)"
  6715. assert pretty(fresnelc(x)) == 'C(x)'
  6716. assert upretty(fresnelc(x)) == 'C(x)'
  6717. assert pretty(fresnels(x)) == 'S(x)'
  6718. assert upretty(fresnels(x)) == 'S(x)'
  6719. assert pretty(Heaviside(x)) == 'Heaviside(x)'
  6720. assert upretty(Heaviside(x)) == 'θ(x)'
  6721. assert pretty(Heaviside(x, y)) == 'Heaviside(x, y)'
  6722. assert upretty(Heaviside(x, y)) == 'θ(x, y)'
  6723. assert pretty(dirichlet_eta(x)) == 'dirichlet_eta(x)'
  6724. assert upretty(dirichlet_eta(x)) == 'η(x)'
  6725. def test_hadamard_power():
  6726. m, n, p = symbols('m, n, p', integer=True)
  6727. A = MatrixSymbol('A', m, n)
  6728. B = MatrixSymbol('B', m, n)
  6729. # Testing printer:
  6730. expr = hadamard_power(A, n)
  6731. ascii_str = \
  6732. """\
  6733. .n\n\
  6734. A \
  6735. """
  6736. ucode_str = \
  6737. """\
  6738. n\n\
  6739. A \
  6740. """
  6741. assert pretty(expr) == ascii_str
  6742. assert upretty(expr) == ucode_str
  6743. expr = hadamard_power(A, 1+n)
  6744. ascii_str = \
  6745. """\
  6746. .(n + 1)\n\
  6747. A \
  6748. """
  6749. ucode_str = \
  6750. """\
  6751. (n + 1)\n\
  6752. A \
  6753. """
  6754. assert pretty(expr) == ascii_str
  6755. assert upretty(expr) == ucode_str
  6756. expr = hadamard_power(A*B.T, 1+n)
  6757. ascii_str = \
  6758. """\
  6759. .(n + 1)\n\
  6760. / T\\ \n\
  6761. \\A*B / \
  6762. """
  6763. ucode_str = \
  6764. """\
  6765. (n + 1)\n\
  6766. T \n\
  6767. AB \
  6768. """
  6769. assert pretty(expr) == ascii_str
  6770. assert upretty(expr) == ucode_str
  6771. def test_issue_17258():
  6772. n = Symbol('n', integer=True)
  6773. assert pretty(Sum(n, (n, -oo, 1))) == \
  6774. ' 1 \n'\
  6775. ' __ \n'\
  6776. ' \\ ` \n'\
  6777. ' ) n\n'\
  6778. ' /_, \n'\
  6779. 'n = -oo '
  6780. assert upretty(Sum(n, (n, -oo, 1))) == \
  6781. """\
  6782. 1 \n\
  6783. ___ \n\
  6784. \n\
  6785. \n\
  6786. n\n\
  6787. \n\
  6788. \n\
  6789. n = - \
  6790. """
  6791. def test_is_combining():
  6792. line = "v̇_m"
  6793. assert [is_combining(sym) for sym in line] == \
  6794. [False, True, False, False]
  6795. def test_issue_17616():
  6796. assert pretty(pi**(1/exp(1))) == \
  6797. ' / -1\\\n'\
  6798. ' \\e /\n'\
  6799. 'pi '
  6800. assert upretty(pi**(1/exp(1))) == \
  6801. ' ⎛ -1⎞\n'\
  6802. ' ⎝ℯ ⎠\n'\
  6803. 'π '
  6804. assert pretty(pi**(1/pi)) == \
  6805. ' 1 \n'\
  6806. ' --\n'\
  6807. ' pi\n'\
  6808. 'pi '
  6809. assert upretty(pi**(1/pi)) == \
  6810. ' 1\n'\
  6811. '\n'\
  6812. ' π\n'\
  6813. 'π '
  6814. assert pretty(pi**(1/EulerGamma)) == \
  6815. ' 1 \n'\
  6816. ' ----------\n'\
  6817. ' EulerGamma\n'\
  6818. 'pi '
  6819. assert upretty(pi**(1/EulerGamma)) == \
  6820. ' 1\n'\
  6821. '\n'\
  6822. ' γ\n'\
  6823. 'π '
  6824. z = Symbol("x_17")
  6825. assert upretty(7**(1/z)) == \
  6826. 'x₁₇___\n'\
  6827. ' ╲╱ 7 '
  6828. assert pretty(7**(1/z)) == \
  6829. 'x_17___\n'\
  6830. ' \\/ 7 '
  6831. def test_issue_17857():
  6832. assert pretty(Range(-oo, oo)) == '{..., -1, 0, 1, ...}'
  6833. assert pretty(Range(oo, -oo, -1)) == '{..., 1, 0, -1, ...}'
  6834. def test_issue_18272():
  6835. x = Symbol('x')
  6836. n = Symbol('n')
  6837. assert upretty(ConditionSet(x, Eq(-x + exp(x), 0), S.Complexes)) == \
  6838. '⎧ │ ⎛ x ⎞⎫\n'\
  6839. '⎨x │ x ∊ ℂ ∧ ⎝-x + ℯ = 0⎠⎬\n'\
  6840. '⎩ │ ⎭'
  6841. assert upretty(ConditionSet(x, Contains(n/2, Interval(0, oo)), FiniteSet(-n/2, n/2))) == \
  6842. '⎧ │ ⎧-n n⎫ ⎛n ⎞⎫\n'\
  6843. '⎨x │ x ∊ ⎨───, ─⎬ ∧ ⎜─ ∈ [0, ∞)⎟⎬\n'\
  6844. '⎩ │ ⎩ 2 2⎭ ⎝2 ⎠⎭'
  6845. assert upretty(ConditionSet(x, Eq(Piecewise((1, x >= 3), (x/2 - 1/2, x >= 2), (1/2, x >= 1),
  6846. (x/2, True)) - 1/2, 0), Interval(0, 3))) == \
  6847. '⎧ │ ⎛⎛⎧ 1 for x ≥ 3⎞ ⎞⎫\n'\
  6848. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  6849. '⎪ │ ⎜⎜⎪x ⎟ ⎟⎪\n'\
  6850. '⎪ │ ⎜⎜⎪─ - 0.5 for x ≥ 2⎟ ⎟⎪\n'\
  6851. '⎪ │ ⎜⎜⎪2 ⎟ ⎟⎪\n'\
  6852. '⎨x │ x ∊ [0, 3] ∧ ⎜⎜⎨ ⎟ - 0.5 = 0⎟⎬\n'\
  6853. '⎪ │ ⎜⎜⎪ 0.5 for x ≥ 1⎟ ⎟⎪\n'\
  6854. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  6855. '⎪ │ ⎜⎜⎪ x ⎟ ⎟⎪\n'\
  6856. '⎪ │ ⎜⎜⎪ ─ otherwise⎟ ⎟⎪\n'\
  6857. '⎩ │ ⎝⎝⎩ 2 ⎠ ⎠⎭'
  6858. def test_Str():
  6859. from sympy.core.symbol import Str
  6860. assert pretty(Str('x')) == 'x'
  6861. def test_symbolic_probability():
  6862. mu = symbols("mu")
  6863. sigma = symbols("sigma", positive=True)
  6864. X = Normal("X", mu, sigma)
  6865. assert pretty(Expectation(X)) == r'E[X]'
  6866. assert pretty(Variance(X)) == r'Var(X)'
  6867. assert pretty(Probability(X > 0)) == r'P(X > 0)'
  6868. Y = Normal("Y", mu, sigma)
  6869. assert pretty(Covariance(X, Y)) == 'Cov(X, Y)'
  6870. def test_issue_21758():
  6871. from sympy.functions.elementary.piecewise import piecewise_fold
  6872. from sympy.series.fourier import FourierSeries
  6873. x = Symbol('x')
  6874. k, n = symbols('k n')
  6875. fo = FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)), SeqFormula(
  6876. Piecewise((-2*pi*cos(n*pi)/n + 2*sin(n*pi)/n**2, (n > -oo) & (n < oo) & Ne(n, 0)),
  6877. (0, True))*sin(n*x)/pi, (n, 1, oo))))
  6878. assert upretty(piecewise_fold(fo)) == \
  6879. '⎧ 2⋅sin(3⋅x) \n'\
  6880. '⎪2⋅sin(x) - sin(2⋅x) + ────────── + … for n > -∞ ∧ n < ∞ ∧ n ≠ 0\n'\
  6881. '⎨ 3 \n'\
  6882. '\n'\
  6883. '⎩ 0 otherwise '
  6884. assert pretty(FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)),
  6885. SeqFormula(0, (n, 1, oo))))) == '0'
  6886. def test_diffgeom():
  6887. from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField
  6888. x,y = symbols('x y', real=True)
  6889. m = Manifold('M', 2)
  6890. assert pretty(m) == 'M'
  6891. p = Patch('P', m)
  6892. assert pretty(p) == "P"
  6893. rect = CoordSystem('rect', p, [x, y])
  6894. assert pretty(rect) == "rect"
  6895. b = BaseScalarField(rect, 0)
  6896. assert pretty(b) == "x"
  6897. def test_deprecated_prettyForm():
  6898. with warns_deprecated_sympy():
  6899. from sympy.printing.pretty.pretty_symbology import xstr
  6900. assert xstr(1) == '1'
  6901. with warns_deprecated_sympy():
  6902. from sympy.printing.pretty.stringpict import prettyForm
  6903. p = prettyForm('s', unicode='s')
  6904. with warns_deprecated_sympy():
  6905. assert p.unicode == p.s == 's'