On Tue, Jul 28, 2009 at 5:09 PM, Luke<[email protected]> wrote: > > I'm exploring Sympy's behavior with regard to automatic distribution > of expressions. Purely from experimenting, I noticed a few things: > In [1]: exp1 = x*(x-y) > In [2]: exp2 = x*x-x*y > In [3]: exp3 = 4*(x-y) > In [4]: exp4 = 4*x-4*y > In [5]: exp1 > Out[5]: x⋅(x - y) > In [6]: exp2 > Out[6]: > 2 > -x⋅y + x > In [7]: exp3 > Out[7]: -4⋅y + 4⋅x > In [8]: exp4 > Out[8]: -4⋅y + 4⋅x > In [9]: factor(exp1) > Out[9]: x⋅(x - y) > In [10]: factor(exp2) > Out[10]: x⋅(x - y) > In [11]: factor(exp3) > Out[11]: -4⋅y + 4⋅x > In [12]: factor(exp4) > Out[12]: -4⋅y + 4⋅x > > I compared these four expressions in Sympy, Mathematica 7, Matlab > 2008a (Maple under the hood), Matlab 2008b (Mupad under the hood). > The results are as follows: > Mathematica 7 > In[5]:= x*(x-y) > Out[5]= x (x-y) > In[6]:= x*x-x*y > Out[6]= x^2-x y > In[7]:= 4*(x-y) > Out[7]= 4 (x-y) > In[8]:= 4*x-4*y > Out[8]= 4 x-4 y > In[9]:= Factor[In[5]] > Out[9]= x (x-y) > In[10]:= Factor[In[6]] > Out[10]= x (x-y) > In[11]:= Factor[In[7]] > Out[11]= 4 (x-y) > In[12]:= Factor[In[8]] > Out[12]= 4 (x-y) > > Matlab 2008a (Maple) >>> exp1 = x*(x-y) > exp1 = > x*(x-y) >>> exp2 = x*x-x*y > exp2 = > x^2-x*y >>> exp3 = 4*(x-y) > exp3 = > 4*x-4*y >>> exp4=4*x-4*y > exp4 = > 4*x-4*y >>> factor(exp1) > ans = > x*(x-y) >>> factor(exp2) > ans = > x*(x-y) >>> factor(exp3) > ans = > 4*x-4*y >>> factor(exp4) > ans > 4*x-4*y >>> > > Matlab 2008b (MuPad): >>> exp1 = x*(x-y) > exp1 = > x*(x - y) >>> exp2=x*x-x*y > exp2 = > x^2 - x*y >>> exp3=4*(x-y) > exp3 = > 4*x - 4*y >>> exp4=4*x-4*y > exp4 = > 4*x - 4*y >>> factor(exp1) > ans = > x*(x - y) >>> factor(exp2) > ans = > x*(x - y) >>> factor(exp3) > ans = > 4*(x - y) >>> factor(exp4) > ans = > 4*(x - y) >>> > > So three out of 4 automatically distribute things like 4*(x-y), but > not things like x*(x-y). In the case of Sympy and Matlab 2008a, this > automatic distribution is not easily reversible by a simple call to > factor. It seems to me that: > 1) there should be a way to control whether or not distribution occurs > automatically, and > 2) that if distribution does occur automatically, it should be easily > reversible, i.e. through something like factor > > Is there a way to keep 4*(x-y) as 4*(x-y) instead of -4*y+4*x?
If I understand correctly, the only way that we should possibly fix in sympy is factor(-4*y+4*x) -> 4*(x-y), is that correct? Yes, I think we should fix it. I am not sure what the default behavior of 4*(x-y) should be, but factor(4*x+4*y) should probably factor out the 4. The way to hold the 4 in the sympy expression is by: In [3]: Mul(4, x-y, evaluate=False) Out[3]: 4⋅(x - y) Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---
