1 from sympy
import Symbol
, Add
, Mul
, Pow
, Integer
, SYMBOL
, ADD
, MUL
, POW
, \
27 assert x
** y
== x
** y
28 assert a
** y
== x
** y
29 assert x
** y
!= y
** x
30 assert x
** y
!= y
** z
31 assert a
** y
!= x
** z
33 assert Integer(3) == Integer(3)
34 assert Integer(3) != Integer(4)
41 assert (x
+ y
) + z
== x
+ (y
+ z
)
42 assert (z
+ x
) + y
== x
+ (y
+ z
)
43 assert (z
+ x
) + x
!= x
+ (y
+ z
)
45 assert x
+ x
== Integer(2) * x
46 assert ((x
+ y
) + z
) + x
== (Integer(2)*x
+ y
) + z
53 assert (x
* y
) * z
== x
* (y
* z
)
54 assert (z
* x
) * y
== x
* (y
* z
)
55 assert (z
* x
) * x
!= x
* (y
* z
)
57 assert x
* x
== x
** Integer(2)
58 assert ((x
* y
) * z
) * x
== ((x
** Integer(2)) * y
) * z
66 assert x
+y
+z
== (x
+ y
) + z
68 assert x
-y
== x
+ (Integer(-1) * y
)
69 assert y
-x
== (Integer(-1) * x
) + y
72 assert x
*y
*z
== z
* (x
* y
)
74 assert x
/y
== x
* (y
** Integer(-1))
75 assert y
/x
== (x
** Integer(-1)) * y
77 assert x
**Integer(2) == x
** Integer(2)
79 assert -x
== Integer(-1) * x
82 def test_int_conversion():
87 assert x
/2 == x
* (Integer(2) ** -1)
94 assert ( (x
+y
)**2 ).expand() == x
**2 + 2*x
*y
+ y
**2
95 assert ( (x
+y
)**3 ).expand() == x
**3 + 3*x
**2*y
+3*x
*y
**2 + y
**3
97 assert ( (x
+y
+z
)**2 ).expand() == x
**2 + y
**2 + z
**2 + 2*x
*y
+ 2*x
*z
+ 2*y
*z
104 assert ( 2*x
*y
).expand() == 2*x
*y
105 assert ( (x
+y
) * (x
+z
) ).expand() == x
**2 + x
*y
+ x
*z
+ y
*z
106 assert ( x
*(x
+y
)**2 ).expand() == x
**3 + 2*x
**2*y
+ x
*y
**2
107 assert ( x
*(x
+y
)**2 + z
*(x
+y
)**2 ).expand() == \
108 x
**3 + 2*x
**2*y
+ y
**2*z
+ x
**2*z
+ x
*y
**2 + 2*x
*y
*z
110 assert ( 2*x
* (y
*x
+ y
*z
) ).expand() == 2*x
**2*y
+ 2*x
*y
*z
111 assert ( (x
+y
)**2 * (x
+z
) ).expand() == \
112 x
**3 + 2*x
**2*y
+ y
**2*z
+ x
**2*z
+ x
*y
**2 + 2*x
*y
*z
114 def test_canonicalization():
132 assert (x
**2)**3 == x
**6
133 assert (x
**y
)**3 == x
**(3*y
)
134 # this is maybe not mathematically correct:
135 assert (x
**y
)**z
== x
**(y
*z
)
140 def test_args_type():
145 assert (x
+y
).type == ADD
146 assert set((x
+y
).args
) == set((x
, y
))
147 assert set((x
+y
).args
) != set((x
, z
))
149 assert (x
*y
*z
).type == MUL
150 assert set((x
*y
*z
).args
) == set((x
, y
, z
))
152 assert (x
**y
).type == POW
153 assert (x
**y
).args
== (x
, y
)
154 assert x
.type == SYMBOL
156 assert Integer(5).type == INTEGER
157 assert Integer(5).args
== ()
159 assert ( x
-y
).type == ADD
160 assert set(( x
-y
).args
) == set((x
, -y
))
168 assert hash(x
) != hash(y
)
169 assert hash(x
) != hash(z
)
170 assert hash(x
) == hash(a
)
172 assert hash(Integer(3)) == hash(Integer(3))
173 assert hash(Integer(3)) != hash(Integer(4))
175 assert hash(x
*y
) == hash(y
*x
)
176 assert hash(x
*y
) == hash(y
*a
)
177 assert hash(x
*y
) != hash(y
*z
)
178 assert hash(x
*y
*z
) == hash(y
*z
*x
)
179 assert hash(x
*y
*z
) == hash(y
*z
*a
)
187 assert x
*y
+y
*x
== 2*x
*y
189 assert x
*y
+y
*a
== 2*x
*y