Problems with simplify on expressions with floating point numbers

38 views
Skip to first unread message

Larry Wigton

unread,
Sep 27, 2012, 11:54:19 AM9/27/12
to sy...@googlegroups.com
*******************************************************************
Python code:

a= Symbol('a')
b= Symbol('b')
c= Symbol('c')

x = a*b+a*c
print "x=",x
y = factor(x)
print "factor(x)=",y
y = simplify(x)
print "simplify(x)=",y

a = sympify(1.2)

print " "
x = a*b+a*c
print "x=",x
y = factor(x)
print "factor(x)=",y
y = simplify(x)
print "simplify(x)=",y

y = simplify(factor(x))
print "simplify(factor(x))=",y
*********************************************************************

Output from code using sympy 7.1 and also bleeding-edge sympy:

x= a*b + a*c
factor(x)= a*(b + c)
simplify(x)= a*(b + c)
 
x= 1.2*b + 1.2*c
factor(x)= 1.2*(b + c)
simplify(x)= 1.2*b + 1.2*c
simplify(factor(x))= 1.2*b + 1.2*c

*************************************************************

why does simplify prefer:

1.2*b + 1.2*c

over:

1.2*(b + c)

especially since it prefers a*(b+c) over a*b + a*c?

   Larry Wigton

Chris Smith

unread,
Sep 27, 2012, 12:07:58 PM9/27/12
to sy...@googlegroups.com
This is the autoexpansion of the 2-arg, commutative mul. factor
returns an unevaluated expression in order to keep the leading
Rational undistributed:

>> 2*(x + y)
2*x + 2*y
>> Mul(2, x + y, evaluate=False)
2*(x + y)
>> (x + y)*x*2 # by the time 2 is handled the expression has 3 args, not 2
2*x*(x + y)
>> 2*(x + y)*x # 2*(x + y) leads to autoexpansion
(2*x + 2*y)*x

It could be that factor is not watching for the extracted Rational and
thus doesn't use the unevaluated Mul option.

Aaron Meurer

unread,
Sep 27, 2012, 2:08:41 PM9/27/12
to sy...@googlegroups.com
In more plain English, rationals and floating points are automatically distributed over addition, and it requires a hack (evaluate=False) to make them not. See http://code.google.com/p/sympy/issues/detail?id=1497

Aaron Meurer
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Larry Wigton

unread,
Sep 27, 2012, 5:06:18 PM9/27/12
to sy...@googlegroups.com
I was forced to replace all the floating point numbers with symbols, do all
the sympy stuff including taking derivatives and applying cse and then in the
end substituting back the floating point numbers.  This seems to work very
well modulo a few noncritical sympy problems I may bring up latter.

Thank you for your help gentlemen!

    Larry Wigton
Reply all
Reply to author
Forward
0 new messages