Collect not working as desired

Visto 73 veces
Saltar al primer mensaje no leído

Alexander Lindsay

no leída,
9 feb 2016, 22:28:389/2/16
a sympy
Hi all,

I am making my first in-depth foray into sympy (and also my first in-depth foray into using winpdb). I am trying to collect terms that have R in expression. Hopefully the pasted screen shot gives an idea of what the expression looks like. As hopefully you can see, the expression inside the parentheses has multiple terms containing R; however, running collect doesn't collect these terms together. I am wondering whether the fact that expr is listed as being of type sympy.core.add.Add has something to do with this problem. Instinctively I feel like expr should be of type sympy.core.mul.Mul since we have the multiplicative term A outside of the parentheses.


Auto Generated Inline Image 1

Alexander Lindsay

no leída,
11 feb 2016, 13:59:0911/2/16
a sympy
I suspect my initial post was far too ugly for anyone to make sense of. And in fact the expression I was looking at was correctly labeled as type Add.

But I'm still not getting what I want. A much simpler example is:

x,y,z,n,p = symbols('x y z n p')
expr1 = z * (y * x + y * p) + n
expr2 = collect(expr1,y)

Both expr1 and expr2 yield:

n + z*(p*y + x*y)

However, I was hoping that expr2 would yield:

n + z * (y * (p + x))

If I subtract n from either expr1 or expr2, yielding:

expr3 = z*(p*y + x*y)

and then run collect(expr3, y), then I get what I'm looking for:

z * y * (p + x)

Finally:
collect(n + p*y + x*y, y) yields n + y * (p +x) as I would hope.

So it appears that collect doesn't give me the result in the special case where I have addition as well as undistributed multiplication. Sorry for what probably seems like a menial question. However, it's important for me to understand what's going on here as I try to simplify some very complicated expressions to input into a finite element code. Is there a better function for achieving what I want to achieve here?
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos