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
)
82 obj
= Basic
.__new
__(cls
, INTEGER
, [])
99 return Integer(self
.i
+o
.i
)
100 return Basic
.__add
__(self
, o
)
102 def __mul__(self
, o
):
104 if o
.type == INTEGER
:
105 return Integer(self
.i
*o
.i
)
106 return Basic
.__mul
__(self
, o
)
111 def __new__(cls
, name
):
112 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
117 return hash(self
.name
)
121 return self
.name
== o
.name
130 def __new__(cls
, args
, canonicalize
=True):
131 if canonicalize
== False:
132 obj
= Basic
.__new
__(cls
, ADD
, args
)
134 args
= [sympify(x
) for x
in args
]
135 return Add
.canonicalize(args
)
138 def canonicalize(cls
, args
):
143 coeff
, key
= b
.as_coeff_rest()
149 coeff
, key
= a
.as_coeff_rest()
156 for a
, b
in d
.iteritems():
157 args
.append(Mul((a
, b
)))
159 return Add(args
, False)
162 s
= str(self
.args
[0])
163 if self
.args
[0].type == ADD
:
165 for x
in self
.args
[1:]:
166 s
= "%s + %s" % (s
, str(x
))
173 def __new__(cls
, args
, canonicalize
=True):
174 if canonicalize
== False:
175 obj
= Basic
.__new
__(cls
, MUL
, args
)
177 args
= [sympify(x
) for x
in args
]
178 return Mul
.canonicalize(args
)
181 def canonicalize(cls
, args
):
185 if a
.type == INTEGER
:
189 coeff
, key
= b
.as_base_exp()
195 coeff
, key
= a
.as_base_exp()
200 if num
.i
== 0 or len(d
)==0:
203 for a
, b
in d
.iteritems():
204 args
.append(Pow((b
, a
)))
210 return Mul(args
, False)
213 a
= list(self
.args
[:])
220 a
= list(self
.args
[:])
229 def as_coeff_rest(self
):
230 if self
.args
[0].type == INTEGER
:
231 return (self
.args
[0], Mul(self
.args
[1:]))
232 return (Integer(1), self
)
235 s
= str(self
.args
[0])
236 if self
.args
[0].type == MUL
:
238 for x
in self
.args
[1:]:
239 s
= "%s*%s" % (s
, str(x
))
246 def __new__(cls
, args
, canonicalize
=True):
247 if canonicalize
== False:
248 obj
= Basic
.__new
__(cls
, POW
, args
)
250 args
= [sympify(x
) for x
in args
]
251 return Pow
.canonicalize(args
)
254 def canonicalize(cls
, args
):
256 if exp
.type == INTEGER
:
261 return Pow(args
, False)
264 s
= str(self
.args
[0])
265 if self
.args
[0].type == ADD
:
267 if self
.args
[1].type == ADD
:
268 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
270 s
= "%s^%s" % (s
, str(self
.args
[1]))
275 if isinstance(x
, int):