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
, [])
103 def __add__(self
, o
):
105 if o
.type == INTEGER
:
106 return Integer(self
.i
+o
.i
)
107 return Basic
.__add
__(self
, o
)
109 def __mul__(self
, o
):
111 if o
.type == INTEGER
:
112 return Integer(self
.i
*o
.i
)
113 return Basic
.__mul
__(self
, o
)
118 def __new__(cls
, name
):
119 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
124 return hash(self
.name
)
129 return self
.name
== o
.name
138 def __new__(cls
, args
, canonicalize
=True):
139 if canonicalize
== False:
140 obj
= Basic
.__new
__(cls
, ADD
, args
)
142 args
= [sympify(x
) for x
in args
]
143 return Add
.canonicalize(args
)
146 def canonicalize(cls
, args
):
151 coeff
, key
= b
.as_coeff_rest()
157 coeff
, key
= a
.as_coeff_rest()
163 for a
, b
in d
.iteritems():
164 args
.append(Mul((a
, b
)))
166 return Add(args
, False)
171 a
= list(self
.args
[:])
180 s
= str(self
.args
[0])
181 if self
.args
[0].type == ADD
:
183 for x
in self
.args
[1:]:
184 s
= "%s + %s" % (s
, str(x
))
191 def __new__(cls
, args
, canonicalize
=True):
192 if canonicalize
== False:
193 obj
= Basic
.__new
__(cls
, MUL
, args
)
195 args
= [sympify(x
) for x
in args
]
196 return Mul
.canonicalize(args
)
199 def canonicalize(cls
, args
):
203 if a
.type == INTEGER
:
207 coeff
, key
= b
.as_base_exp()
213 coeff
, key
= a
.as_base_exp()
218 if num
.i
== 0 or len(d
)==0:
221 for a
, b
in d
.iteritems():
222 args
.append(Pow((b
, a
)))
228 return Mul(args
, False)
231 a
= list(self
.args
[:])
238 a
= list(self
.args
[:])
247 def as_coeff_rest(self
):
248 if self
.args
[0].type == INTEGER
:
249 return (self
.args
[0], Mul(self
.args
[1:]))
250 return (Integer(1), self
)
253 s
= str(self
.args
[0])
254 if self
.args
[0].type == MUL
:
256 for x
in self
.args
[1:]:
257 s
= "%s*%s" % (s
, str(x
))
264 def __new__(cls
, args
, canonicalize
=True):
265 if canonicalize
== False:
266 obj
= Basic
.__new
__(cls
, POW
, args
)
268 args
= [sympify(x
) for x
in args
]
269 return Pow
.canonicalize(args
)
272 def canonicalize(cls
, args
):
274 if exp
.type == INTEGER
:
279 return Pow(args
, False)
282 s
= str(self
.args
[0])
283 if self
.args
[0].type == ADD
:
285 if self
.args
[1].type == ADD
:
286 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
288 s
= "%s^%s" % (s
, str(self
.args
[1]))
293 if isinstance(x
, int):