9 # make this more robust:
12 m
= hash(m
+ 1001 ^
hash(x
))
17 def __new__(cls
, type, args
):
18 obj
= object.__new
__(cls
)
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
))
79 obj
= Basic
.__new
__(cls
, INTEGER
, [])
89 return Integer(self
.i
+o
.i
)
90 return Basic
.__add
__(self
, o
)
95 return Integer(self
.i
*o
.i
)
96 return Basic
.__mul
__(self
, o
)
101 def __new__(cls
, name
):
102 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
107 return hash(self
.name
)
115 def __new__(cls
, args
, canonicalize
=True):
116 if canonicalize
== False:
117 obj
= Basic
.__new
__(cls
, ADD
, args
)
119 args
= [sympify(x
) for x
in args
]
120 return Add
.canonicalize(args
)
123 def canonicalize(cls
, args
):
128 coeff
, key
= b
.as_coeff_rest()
134 coeff
, key
= a
.as_coeff_rest()
140 for a
, b
in d
.iteritems():
141 args
.append(Mul((a
, b
)))
143 return Add(args
, False)
146 s
= str(self
.args
[0])
147 if self
.args
[0].type == ADD
:
149 for x
in self
.args
[1:]:
150 s
= "%s + %s" % (s
, str(x
))
157 def __new__(cls
, args
, canonicalize
=True):
158 if canonicalize
== False:
159 obj
= Basic
.__new
__(cls
, MUL
, args
)
161 args
= [sympify(x
) for x
in args
]
162 return Mul
.canonicalize(args
)
165 def canonicalize(cls
, args
):
169 if a
.type == INTEGER
:
173 coeff
, key
= b
.as_base_exp()
179 coeff
, key
= a
.as_base_exp()
187 for a
, b
in d
.iteritems():
188 args
.append(Pow((b
, a
)))
194 return Mul(args
, False)
212 def as_coeff_rest(self
):
213 if self
.args
[0].type == INTEGER
:
214 return (self
.args
[0], Mul(self
.args
[1:]))
215 return (Integer(1), self
)
218 s
= str(self
.args
[0])
219 if self
.args
[0].type == MUL
:
221 for x
in self
.args
[1:]:
222 s
= "%s*%s" % (s
, str(x
))
229 def __new__(cls
, args
, canonicalize
=True):
230 if canonicalize
== False:
231 obj
= Basic
.__new
__(cls
, MUL
, args
)
233 args
= [sympify(x
) for x
in args
]
234 return Pow
.canonicalize(args
)
237 def canonicalize(cls
, args
):
239 if exp
.type == INTEGER
:
244 return Pow(args
, False)
247 s
= str(self
.args
[0])
248 if self
.args
[0].type == ADD
:
250 if self
.args
[1].type == ADD
:
251 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
253 s
= "%s^%s" % (s
, str(self
.args
[1]))
258 if isinstance(x
, int):