factor giving wrong result

30 views
Skip to first unread message

Chuck Ursenbach

unread,
Jan 25, 2018, 5:07:57 PM1/25/18
to sympy
I'm using isympy from a recent anaconda3 installation.  I find that factor is introducing a spurious factor of 0.25.

If I run this:

from sympy import *
a,b,c,d,x,y = symbols('a,b,c,d,x,y')
y = a + 0.25*b**2 + 1.0*b*c*x + 1.0*c**2*x**2
print('factor')
print(factor(y-a))
print('expand')
print(expand(y-a))
print('factor difference')
print(simplify(y-a-factor(y-a)))
print('expand difference')
print(simplify(y-a-expand(y-a)))

then I get this:

factor
1.0*(0.25*b + 0.5*c*x)**2
expand
0.25*b**2 + 1.0*b*c*x + 1.0*c**2*x**2
factor difference
0.1875*b**2 + 0.75*b*c*x + 0.75*c**2*x**2
expand difference
0

Leonid Kovalev

unread,
Jan 25, 2018, 5:26:48 PM1/25/18
to sympy
This is clearly a bug, hopefully one that will be fixed soon. 

Aside: symbolic manipulations are more robust when expressions involve rational numbers instead of floating point numbers. For example:

y = a + Rational(1, 4)*b**2 + 1*b*c*x + 1*c**2*x**2

or 

y = a + Rational('0.25')*b**2 + 1*b*c*x + 1*c**2*x**2

would avoid the issue. 

Thanks for the report. 

Leonid Kovalev

unread,
Jan 25, 2018, 5:37:49 PM1/25/18
to sympy
The bug is present in 1.1.1 but not in the current master branch on GitHub, so it was fixed already.
 
You can clone SymPy from GitHub to use the current version, or avoid the bug by making the coefficients rational as mentioned earlier (which avoids other potential issues with floating point arithmetics).


Reply all
Reply to author
Forward
0 new messages