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
))
79 obj
= Basic
.__new
__(cls
, INTEGER
, [])
96 return Integer(self
.i
+o
.i
)
97 return Basic
.__add
__(self
, o
)
101 if o
.type == INTEGER
:
102 return Integer(self
.i
*o
.i
)
103 return Basic
.__mul
__(self
, o
)
108 def __new__(cls
, name
):
109 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
114 return hash(self
.name
)
122 def __new__(cls
, args
, canonicalize
=True):
123 if canonicalize
== False:
124 obj
= Basic
.__new
__(cls
, ADD
, args
)
126 args
= [sympify(x
) for x
in args
]
127 return Add
.canonicalize(args
)
130 def canonicalize(cls
, args
):
135 coeff
, key
= b
.as_coeff_rest()
141 coeff
, key
= a
.as_coeff_rest()
148 for a
, b
in d
.iteritems():
149 args
.append(Mul((a
, b
)))
151 return Add(args
, False)
154 s
= str(self
.args
[0])
155 if self
.args
[0].type == ADD
:
157 for x
in self
.args
[1:]:
158 s
= "%s + %s" % (s
, str(x
))
165 def __new__(cls
, args
, canonicalize
=True):
166 if canonicalize
== False:
167 obj
= Basic
.__new
__(cls
, MUL
, args
)
169 args
= [sympify(x
) for x
in args
]
170 return Mul
.canonicalize(args
)
173 def canonicalize(cls
, args
):
177 if a
.type == INTEGER
:
181 coeff
, key
= b
.as_base_exp()
187 coeff
, key
= a
.as_base_exp()
192 if num
.i
== 0 or len(d
)==0:
195 for a
, b
in d
.iteritems():
196 args
.append(Pow((b
, a
)))
202 return Mul(args
, False)
205 a
= list(self
.args
[:])
212 a
= list(self
.args
[:])
221 def as_coeff_rest(self
):
222 if self
.args
[0].type == INTEGER
:
223 return (self
.args
[0], Mul(self
.args
[1:]))
224 return (Integer(1), self
)
227 s
= str(self
.args
[0])
228 if self
.args
[0].type == MUL
:
230 for x
in self
.args
[1:]:
231 s
= "%s*%s" % (s
, str(x
))
238 def __new__(cls
, args
, canonicalize
=True):
239 if canonicalize
== False:
240 obj
= Basic
.__new
__(cls
, POW
, args
)
242 args
= [sympify(x
) for x
in args
]
243 return Pow
.canonicalize(args
)
246 def canonicalize(cls
, args
):
248 if exp
.type == INTEGER
:
253 return Pow(args
, False)
256 s
= str(self
.args
[0])
257 if self
.args
[0].type == ADD
:
259 if self
.args
[1].type == ADD
:
260 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
262 s
= "%s^%s" % (s
, str(self
.args
[1]))
267 if isinstance(x
, int):