2 from sympy
.printing
.pretty
import pretty
7 def test_pretty_basic():
8 # Simple numbers/symbols
9 assert pretty( -Rational(1)/2 ) == '-1/2'
10 assert pretty( -Rational(13)/22 ) == ' 13\n- --\n 22'
11 assert pretty( oo
) == 'oo'
14 assert pretty( (x
**2) ) == ' 2\nx '
15 assert pretty( 1/x
) == '1\n-\nx'
18 assert pretty( (x
**2 + x
+ 1)) == ' 2\n1 + x + x '
19 assert pretty( 1-x
) == '1 - x'
20 assert pretty( 1-2*x
) == '1 - 2*x'
21 assert pretty( 1-Rational(3,2)*y
/x
) == ' 3*y\n1 - ---\n 2*x'
24 assert pretty( x
/y
) == 'x\n-\ny'
25 assert pretty( -x
/y
) == '-x\n--\ny '
26 assert pretty( (x
+2)/y
) == '2 + x\n-----\n y '
27 assert pretty( (1+x
)*y
) in ['(1 + x)*y', 'y*(1 + x)']
29 # Check for proper placement of negative sign
30 assert pretty( -5*x
/(x
+10) ) == ' -5*x \n------\n10 + x'
31 assert pretty( 1 - Rational(3,2)*(x
+1) ) == ' 3*x\n-1/2 - ---\n 2 '
33 def test_pretty_relational():
34 assert pretty(x
== y
) == 'x = y'
35 assert pretty(x
<= y
) == 'x <= y'
36 assert pretty(x
> y
) == 'y < x'
37 assert pretty(x
/(y
+1) != y
**2) == ' x 2\n----- != y \n1 + y '
39 def test_pretty_unicode():
40 assert pretty( oo
, True ) == u
'\u221e'
41 assert pretty( pi
, True ) == u
'\u03c0'
42 assert pretty( pi
+2*x
, True ) == u
'\u03c0 + 2*x'
43 #assert pretty( pi**2+exp(x), True ) == u' 2 x\n\u03c0 + \u212f '
44 assert pretty( x
!= y
, True ) == u
'x \u2260 y'
45 assert pretty( Symbol('beta'), True ) == u
'\u03b2'
46 assert pretty( Symbol('beta12'), True ) == u
'\u03b212'
48 def test_pretty_unicode_defaults():
49 use_unicode
= pprint_use_unicode(True)
50 assert pretty(Symbol('alpha')) == u
'\u03b1'
51 pprint_use_unicode(False)
52 assert pretty(Symbol('alpha')) == 'alpha'
54 pprint_use_unicode(use_unicode
)
57 def test_pretty_functions():
59 #assert pretty( (2*x + exp(x)) ) in [' x \ne + 2*x', ' x\n2*x + e ']
60 assert pretty( sqrt(2) ) == ' ___\n\\/ 2 '
61 assert pretty( sqrt(2+pi
) ) == ' ________\n\\/ 2 + pi '
62 #assert pretty(abs(x)) == '|x|'
63 #assert pretty(abs(x/(x**2+1))) == '| x |\n|------|\n| 2|\n|1 + x |'
65 # Univariate/Multivariate functions
67 assert pretty(f(x
)) == 'f(x)'
68 assert pretty(f(x
, y
)) == 'f(x, y)'
69 assert pretty(f(x
/(y
+1), y
)) == ' x \nf(-----, y)\n 1 + y '
71 # Nesting of square roots
72 assert pretty( sqrt((sqrt(x
+1))+1) ) == ' _______________\n / _______ \n \\/ 1 + \\/ 1 + x '
74 #assert pretty( sin(x)**2 ) == ' 2 \nsin (x)'
77 a
,b
= map(Symbol
, 'ab')
78 #assert pretty( conjugate(a+b*I) ) == '_ _\na - I*b'
79 #assert pretty( conjugate(exp(a+b*I)) ) == ' _ _\n a - I*b\ne '
81 def test_pretty_derivatives():
83 f_1
= Derivative(log(x
), x
, evaluate
=False)
84 assert pretty(f_1
) == 'd \n--(log(x))\ndx '
86 f_2
= Derivative(log(x
), x
, evaluate
=False) + x
87 assert pretty(f_2
) == ' d \nx + --(log(x))\n dx '
90 f_3
= Derivative(log(x
) + x
**2, x
, y
, evaluate
=False)
91 assert pretty(f_3
) == ' 2 \n d / 2 \\\n-----\\x + log(x)/\ndy dx '
93 f_4
= Derivative(2*x
*y
, y
, x
, evaluate
=False) + x
**2
94 assert pretty(f_4
) == ' 2 \n 2 d \nx + -----(2*x*y)\n dx dy '
96 def test_pretty_integrals():
98 f_1
= Integral(log(x
), x
)
99 assert pretty(f_1
) == ' / \n | \n | log(x) dx\n | \n / '
101 f_2
= Integral(x
**2, x
)
102 assert pretty(f_2
) == ' / \n | \n | 2 \n | x dx\n | \n / '
103 # Double nesting of pow
104 f_3
= Integral(x
**(2**x
), x
)
105 assert pretty(f_3
) == ' / \n | \n | \n | / x\\ \n | \\2 / \n | x dx\n / '
108 f_4
= Integral(x
**2, (x
,1,2))
109 assert pretty(f_4
) == ' 2 \n / \n | \n | 2 \n | x dx\n | \n / \n 1 '
111 f_5
= Integral(x
**2, (x
,Rational(1,2),10))
112 assert pretty(f_5
) == ' 10 \n / \n | \n | 2 \n | x dx\n | \n / \n1/2 '
115 f_6
= Integral(x
**2*y
**2, x
,y
)
116 assert pretty(f_6
) == ' / / \n | | \n | | 2 2 \n | | x *y dx dy\n | | \n / / '
118 # Nested integrals with limits+unicode
119 f_7
= Integral(x
**2*sin(y
), (x
,0,1), (y
,0,pi
))
120 assert pretty(f_7
, use_unicode
=True) == u
' \u03c0 1 \n / / \n | | \n | | 2 \n | | x *sin(y) dx dy\n | | \n / / \n 0 0 '
122 def _test_pretty_limits():
123 assert pretty( limit(x
, x
, oo
, evaluate
=False) ) == ' lim x\nx->oo '
124 assert pretty( limit(x
**2, x
, 0, evaluate
=False) ) == ' 2\nlim x \nx->0 '