10 def __new__(cls
, p
, q
, tnew
=tuple.__new
__):
19 return tnew(cls
, (p
, q
))
26 # not needed when __new__ normalizes to ints
31 def __add__(self
, other
):
33 if isinstance(other
, int):
37 return rational(p
*s
+ q
*r
, q
*s
)
41 def __mul__(self
, other
):
43 if isinstance(other
, int):
45 elif isinstance(other
, rational
):
49 return rational(p
*r
, q
*s
)
58 def make_rational(p
, q
):
59 return (NUMBER
, rational(p
, q
))
64 half
= make_rational(1, 2)
66 def make_symbol(name
):
67 return (SYMBOLIC
, name
)
71 return (TERMS
, [(one
, x
[1])])
73 return (TERMS
, [(x
, 1)])
75 def add_default(a
, b
):
76 if a
[0] is not TERMS
: a
= convert_terms(a
)
77 if b
[0] is not TERMS
: b
= convert_terms(b
)
78 return add_terms_terms(a
, b
)
80 def add_terms_terms(a
, b
):
96 return (TERMS
, frozenset(citems
))
98 def add_symbolic_symbolic(a
, b
):
100 return (TERMS
, frozenset([(a
, 2)]))
101 return (TERMS
, frozenset([(a
, 1), (b
, 1)]))
103 def add_rational_rational(a
, b
):
104 return (NUMBER
, a
[1]+b
[1])
107 (TERMS
, TERMS
) : add_terms_terms
,
108 (SYMBOLIC
, SYMBOLIC
) : add_symbolic_symbolic
,
109 (NUMBER
, NUMBER
) : add_rational_rational
113 def mul_default(a
, b
):
114 if a
[0] is not FACTORS
: a
= (FACTORS
, [(a
, 1)])
115 if b
[0] is not FACTORS
: b
= (FACTORS
, [(b
, 1)])
116 return mul_factors_factors(a
, b
)
118 def mul_factors_factors(a
, b
):
129 return (FACTORS
, frozenset(c
.items()))
131 def mul_rational_symbolic(a
, b
):
132 if a
== zero
: return zero
133 if a
== one
: return b
134 return (TERMS
, frozenset([(b
, a
[1])]))
136 def mul_symbolic_rational(a
, b
):
137 return mul_rational_symbolic(b
, a
)
139 def mul_rational_terms(a
, b
):
140 if a
== zero
: return zero
141 if a
== one
: return b
143 return (TERMS
, frozenset([(e
, p
*x
) for e
, x
in b
[1]]))
145 def mul_terms_rational(a
, b
):
146 return mul_rational_terms(b
, a
)
148 def mul_rational_rational(a
, b
):
149 return (NUMBER
, a
[1]*b
[1])
152 (NUMBER
, SYMBOLIC
) : mul_rational_symbolic
,
153 (SYMBOLIC
, NUMBER
) : mul_symbolic_rational
,
154 (NUMBER
, TERMS
) : mul_rational_terms
,
155 (TERMS
, NUMBER
) : mul_terms_rational
,
156 (FACTORS
, FACTORS
) : mul_factors_factors
,
157 (NUMBER
, NUMBER
) : mul_rational_rational
161 if isinstance(x
, int):
169 return ' + '.join((show(b
)+"*"+show(a
)) for (a
, b
) in x
[1])
171 return ' * '.join(("(%s)**(%i/%i)" % (show(a
), show(b
))) for (a
, b
) in x
[1])
176 def __init__(self
, value
=None, _s
=None):
180 if isinstance(value
, int):
181 self
._s
= (NUMBER
, value
)
182 elif isinstance(value
, str):
183 self
._s
= make_symbol(value
)
188 def __add__(self
, other
):
189 if not isinstance(other
, Expr
):
191 s
, t
= self
._s
, other
._s
192 add
= addition
.get((s
[0], t
[0]), add_default
)
193 return Expr(_s
=add(self
._s
, other
._s
))
197 def __mul__(self
, other
):
198 if not isinstance(other
, Expr
):
200 s
, t
= self
._s
, other
._s
201 mul
= multiplication
.get((s
[0], t
[0]), mul_default
)
202 return Expr(_s
=mul(s
, t
))
207 return Expr(_s
=make_rational(p
, q
))
210 # use time() instead on unix
212 if sys
.platform
=='win32':
213 from time
import clock
215 from time
import time
as clock
217 import sympycore
as sympy
241 Symbol
= sympy
.Symbol
242 Number
= sympy
.Number
268 print "without psyco"
273 from sympycore
import profile_expr
275 profile_expr('time2(1000)')
281 if psyco
is not None: