9 # make this more robust:
12 m
= hash(m
+ 1001 ^
hash(x
))
17 def __new__(cls
, type, args
):
18 obj
= object.__new
__(cls
)
20 obj
._args
= tuple(args
)
27 return hash_seq(self
.args
)
33 def as_coeff_rest(self
):
34 return (Integer(1), self
)
36 def as_base_exp(self
):
37 return (self
, Integer(1))
58 return Mul((x
, Pow((y
, Integer(-1)))))
61 return Mul((y
, Pow((x
, Integer(-1)))))
70 return Mul((Integer(-1), x
))
76 return not self
.__eq
__(x
)
80 if o
.type == self
.type:
81 return self
.args
== o
.args
89 obj
= Basic
.__new
__(cls
, INTEGER
, [])
106 def __add__(self
, o
):
108 if o
.type == INTEGER
:
109 return Integer(self
.i
+o
.i
)
110 return Basic
.__add
__(self
, o
)
112 def __mul__(self
, o
):
114 if o
.type == INTEGER
:
115 return Integer(self
.i
*o
.i
)
116 return Basic
.__mul
__(self
, o
)
121 def __new__(cls
, name
):
122 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
127 return hash(self
.name
)
132 return self
.name
== o
.name
141 def __new__(cls
, args
, canonicalize
=True):
142 if canonicalize
== False:
143 obj
= Basic
.__new
__(cls
, ADD
, args
)
145 args
= [sympify(x
) for x
in args
]
146 return Add
.canonicalize(args
)
149 def canonicalize(cls
, args
):
153 if a
.type == INTEGER
:
157 coeff
, key
= b
.as_coeff_rest()
163 coeff
, key
= a
.as_coeff_rest()
171 for a
, b
in d
.iteritems():
172 args
.append(Mul((a
, b
)))
178 return Add(args
, False)
183 a
= list(self
.args
[:])
192 s
= str(self
.args
[0])
193 if self
.args
[0].type == ADD
:
195 for x
in self
.args
[1:]:
196 s
= "%s + %s" % (s
, str(x
))
203 def __new__(cls
, args
, canonicalize
=True):
204 if canonicalize
== False:
205 obj
= Basic
.__new
__(cls
, MUL
, args
)
207 args
= [sympify(x
) for x
in args
]
208 return Mul
.canonicalize(args
)
211 def canonicalize(cls
, args
):
215 if a
.type == INTEGER
:
219 key
, coeff
= b
.as_base_exp()
225 key
, coeff
= a
.as_base_exp()
230 if num
.i
== 0 or len(d
)==0:
233 for a
, b
in d
.iteritems():
234 args
.append(Pow((a
, b
)))
240 return Mul(args
, False)
243 a
= list(self
.args
[:])
250 a
= list(self
.args
[:])
259 def as_coeff_rest(self
):
260 if self
.args
[0].type == INTEGER
:
261 return (self
.args
[0], Mul(self
.args
[1:]))
262 return (Integer(1), self
)
265 s
= str(self
.args
[0])
266 if self
.args
[0].type == MUL
:
268 for x
in self
.args
[1:]:
269 s
= "%s*%s" % (s
, str(x
))
276 def __new__(cls
, args
, canonicalize
=True):
277 if canonicalize
== False:
278 obj
= Basic
.__new
__(cls
, POW
, args
)
280 args
= [sympify(x
) for x
in args
]
281 return Pow
.canonicalize(args
)
284 def canonicalize(cls
, args
):
286 if base
.type == INTEGER
:
291 if exp
.type == INTEGER
:
297 return Pow((base
.args
[0], base
.args
[1]*exp
))
298 return Pow(args
, False)
301 s
= str(self
.args
[0])
302 if self
.args
[0].type == ADD
:
304 if self
.args
[1].type == ADD
:
305 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
307 s
= "%s^%s" % (s
, str(self
.args
[1]))
312 if isinstance(x
, int):