Skip to content

Commit dc8fd64

Browse files
committed
Fix rational and mul
1 parent e385e2f commit dc8fd64

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,15 +2295,22 @@ class Mul(AssocOp):
22952295

22962296
def as_powers_dict(Basic self):
22972297
cdef RCP[const symengine.Mul] X = symengine.rcp_static_cast_Mul(self.thisptr)
2298-
cdef rcp_const_basic coef = <rcp_const_basic>(deref(X).get_coef())
22992298
cdef map_basic_basic m = deref(X).get_dict()
2300-
d = c2py(coef).as_powers_dict()
2299+
coef = c2py(<rcp_const_basic>(deref(X).get_coef()))
2300+
if coef == 1:
2301+
d = collections.defaultdict(int)
2302+
else:
2303+
d = coef.as_powers_dict()
23012304

23022305
it = m.begin()
23032306
it_end = m.end()
23042307
while it != it_end:
2305-
d[c2py(<rcp_const_basic>(deref(it).first))] =\
2306-
c2py(<rcp_const_basic>(deref(it).second))
2308+
base = c2py(<rcp_const_basic>(deref(it).first))
2309+
exp = c2py(<rcp_const_basic>(deref(it).second))
2310+
if base.is_Rational and base.p < base.q and base.p > 0:
2311+
d[1/base] -= exp
2312+
else:
2313+
d[base] += exp
23072314
inc(it)
23082315

23092316
return d
@@ -2351,9 +2358,13 @@ class Pow(Expr):
23512358
def func(self):
23522359
return self.__class__
23532360

2354-
def as_powers_dict(self):
2361+
def as_powers_dict(Basic self):
23552362
d = collections.defaultdict(int)
2356-
d[self.base] = self.exp
2363+
base, exp = self.as_base_exp()
2364+
if base.is_Rational and base.p < base.q and base.p > 0:
2365+
d[1/base] = -exp
2366+
else:
2367+
d[base] = exp
23572368
return d
23582369

23592370

0 commit comments

Comments
 (0)