Sympy seems unable to factorize a very simple expression

203 views
Skip to first unread message

Clément Bœsch

unread,
Aug 6, 2014, 5:24:02 PM8/6/14
to sy...@googlegroups.com
Hi,
  
    >>> import sympy as sp
    >>> w, x, y, z = sp.symbols("w x y z")
    >>> sp.factor(w*x + w*y + z)         
    w*x + w*y + z              

I would obviously want w*(x+y) + z

Without the "+ z" part it works as expected:
                                           
    >>> sp.factor(w*x + w*y)               
    w*(x + y)              

I tried several thing, like using deep=True argument, or even hinting by
providing the answer:
                    
    >>> sp.factor(w*x + w*y + z, deep=True)
    w*x + w*y + z                         
    >>> sp.factor(w*(x + y) + z)
    w*x + w*y + z              

Strangely, if I mix both it suddenly works:
                                          
    >>> sp.factor(w*(x + y) + z, deep=True)
    w*(x + y) + z                         

But this obviously doesn't help me.

I considered parsing my expression myself at this point, but I'd really love to
avoid doing this...

Regards,

Aaron Meurer

unread,
Aug 6, 2014, 5:48:10 PM8/6/14
to sy...@googlegroups.com
factor() doesn't do such "partial factorizations". In general, the notion is not very well defined. See http://stackoverflow.com/a/21067479/161801 for some more discussion on this.  

In general, what would you expect SymPy to do? Factor the part of the expression that doesn't include z? If it's that, you can use expr.as_independent(z) to give the parts that have and don't have z.

Aaron Meurer




--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d59be48c-a760-4e95-9188-1890d0e6dda6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clément Bœsch

unread,
Aug 7, 2014, 1:47:57 AM8/7/14
to sy...@googlegroups.com
I saw that stackoverflow thread. In my use case, I want to reduce the maximum the redundancy in the expression. Or more specifically, I'm trying to generate code that will produce the minimum amount of costly operation (in my case, in addition to the ugly redundancy of constants, 2mul cost more than 1mul 1add). Note that Wolframalpha seems to be able to propose an appropriate alternate form. But anyway, I just hit another wall:

    >>> sp.factor(0.35*x + 0.35*y)
    0.35*(x + y)
    >>> sp.factor(0.35*x - 0.35*y)
    7.0*(0.05*x - 0.05*y)

Chris Smith

unread,
Aug 14, 2014, 1:42:33 PM8/14/14
to sy...@googlegroups.com
for example:

```
>>> e
x**2 + x*y + 3*x + y*z + z
>>> for s in e.free_symbols:
...  i,d=e.as_independent(s, as_Add=True)
...  c=factor(i)+factor(d)
...  print c.count_ops(),c
...
6 x*(x + y + 3) + z*(y + 1)
6 x*(x + y + 3) + z*(y + 1)
7 x**2 + 3*x + y*(x + z) + z
```
Reply all
Reply to author
Forward
0 new messages