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))
52 return Mul((Integer(-1), x
))
58 obj
= Basic
.__new
__(cls
, INTEGER
, [])
67 return Integer(self
.i
+o
.i
)
72 return Integer(self
.i
*o
.i
)
78 def __new__(cls
, name
):
79 obj
= Basic
.__new
__(cls
, SYMBOL
, [])
84 return hash(self
.name
)
92 def __new__(cls
, args
, canonicalize
=True):
93 if canonicalize
== False:
94 obj
= Basic
.__new
__(cls
, ADD
, args
)
96 args
= [sympify(x
) for x
in args
]
97 return Add
.canonicalize(args
)
100 def canonicalize(cls
, args
):
105 coeff
, key
= b
.as_coeff_rest()
111 coeff
, key
= a
.as_coeff_rest()
117 for a
, b
in d
.iteritems():
118 args
.append(Mul((a
, b
)))
120 return Add(args
, False)
123 s
= str(self
.args
[0])
124 if self
.args
[0].type == ADD
:
126 for x
in self
.args
[1:]:
127 s
= "%s + %s" % (s
, str(x
))
134 def __new__(cls
, args
, canonicalize
=True):
135 if canonicalize
== False:
136 obj
= Basic
.__new
__(cls
, MUL
, args
)
138 args
= [sympify(x
) for x
in args
]
139 return Mul
.canonicalize(args
)
142 def canonicalize(cls
, args
):
146 if a
.type == INTEGER
:
150 coeff
, key
= b
.as_base_exp()
156 coeff
, key
= a
.as_base_exp()
164 for a
, b
in d
.iteritems():
165 args
.append(Pow((b
, a
)))
171 return Mul(args
, False)
189 def as_coeff_rest(self
):
190 if self
.args
[0].type == INTEGER
:
191 return (self
.args
[0], Mul(self
.args
[1:]))
192 return (Integer(1), self
)
195 s
= str(self
.args
[0])
196 if self
.args
[0].type == MUL
:
198 for x
in self
.args
[1:]:
199 s
= "%s*%s" % (s
, str(x
))
206 def __new__(cls
, args
, canonicalize
=True):
207 if canonicalize
== False:
208 obj
= Basic
.__new
__(cls
, MUL
, args
)
210 args
= [sympify(x
) for x
in args
]
211 return Pow
.canonicalize(args
)
214 def canonicalize(cls
, args
):
216 if exp
.type == INTEGER
:
221 return Pow(args
, False)
224 s
= str(self
.args
[0])
225 if self
.args
[0].type == ADD
:
227 if self
.args
[1].type == ADD
:
228 s
= "%s^(%s)" % (s
, str(self
.args
[1]))
230 s
= "%s^%s" % (s
, str(self
.args
[1]))
235 if isinstance(x
, int):