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
51 assert e2
== 6+x
+z
+x
*y
58 assert (x
* y
) * z
== x
* (y
* z
)
59 assert (z
* x
) * y
== x
* (y
* z
)
60 assert (z
* x
) * x
!= x
* (y
* z
)
62 assert x
* x
== x
** Integer(2)
63 assert ((x
* y
) * z
) * x
== ((x
** Integer(2)) * y
) * z
68 assert e2
== 10*x
*z
*y
**x
76 assert x
+y
+z
== (x
+ y
) + z
78 assert x
-y
== x
+ (Integer(-1) * y
)
79 assert y
-x
== (Integer(-1) * x
) + y
82 assert x
*y
*z
== z
* (x
* y
)
84 assert x
/y
== x
* (y
** Integer(-1))
85 assert y
/x
== (x
** Integer(-1)) * y
87 assert x
**Integer(2) == x
** Integer(2)
89 assert -x
== Integer(-1) * x
92 def test_int_conversion():
97 assert x
/2 == x
* (Integer(2) ** -1)
104 assert ( (x
+y
)**2 ).expand() == x
**2 + 2*x
*y
+ y
**2
105 assert ( (x
+y
)**3 ).expand() == x
**3 + 3*x
**2*y
+3*x
*y
**2 + y
**3
107 assert ( (x
+y
+z
)**2 ).expand() == x
**2 + y
**2 + z
**2 + 2*x
*y
+ 2*x
*z
+ 2*y
*z
114 assert ( 2*x
*y
).expand() == 2*x
*y
115 assert ( (x
+y
) * (x
+z
) ).expand() == x
**2 + x
*y
+ x
*z
+ y
*z
116 assert ( x
*(x
+y
)**2 ).expand() == x
**3 + 2*x
**2*y
+ x
*y
**2
117 assert ( x
*(x
+y
)**2 + z
*(x
+y
)**2 ).expand() == \
118 x
**3 + 2*x
**2*y
+ y
**2*z
+ x
**2*z
+ x
*y
**2 + 2*x
*y
*z
120 assert ( 2*x
* (y
*x
+ y
*z
) ).expand() == 2*x
**2*y
+ 2*x
*y
*z
121 assert ( (x
+y
)**2 * (x
+z
) ).expand() == \
122 x
**3 + 2*x
**2*y
+ y
**2*z
+ x
**2*z
+ x
*y
**2 + 2*x
*y
*z
124 def test_canonicalization():
142 assert (x
**2)**3 == x
**6
143 assert (x
**y
)**3 == x
**(3*y
)
144 # this is maybe not mathematically correct:
145 assert (x
**y
)**z
== x
**(y
*z
)
150 def test_args_type():
155 assert (x
+y
).type == ADD
156 assert set((x
+y
).args
) == set((x
, y
))
157 assert set((x
+y
).args
) != set((x
, z
))
159 assert (x
*y
*z
).type == MUL
160 assert set((x
*y
*z
).args
) == set((x
, y
, z
))
162 assert (x
**y
).type == POW
163 assert (x
**y
).args
== (x
, y
)
164 assert x
.type == SYMBOL
166 assert Integer(5).type == INTEGER
167 assert Integer(5).args
== ()
169 assert ( x
-y
).type == ADD
170 assert set(( x
-y
).args
) == set((x
, -y
))
178 assert hash(x
) != hash(y
)
179 assert hash(x
) != hash(z
)
180 assert hash(x
) == hash(a
)
182 assert hash(Integer(3)) == hash(Integer(3))
183 assert hash(Integer(3)) != hash(Integer(4))
185 assert hash(x
*y
) == hash(y
*x
)
186 assert hash(x
*y
) == hash(y
*a
)
187 assert hash(x
*y
) != hash(y
*z
)
188 assert hash(x
*y
*z
) == hash(y
*z
*x
)
189 assert hash(x
*y
*z
) == hash(y
*z
*a
)
197 assert x
*y
+y
*x
== 2*x
*y
199 assert x
*y
+y
*a
== 2*x
*y