Simplifying expressions with intermediate variables

76 views
Skip to first unread message

Peter Eastman

unread,
Apr 20, 2014, 9:33:33 PM4/20/14
to sy...@googlegroups.com
I'm just learning Sympy, so I apologize if this is a beginner question.  I'm trying to figure out how to use intermediate variables when simplifying expressions.

Here's a simple example of the sort of thing I'm trying to do.  Suppose I have a vector v=(v1,v2,v3).  Next I define u to be a unit vector parallel to v: u=(u1,u2,u3)=v/|v|.  And then I want to compute the derivative du1/dv1.  I can do it like this:

>>> v = Matrix([symbols('v1 v2 v3')])
>>> u = v/sqrt(v.dot(v))
>>> u[0].diff(v[0])

which produces the output

-v1**2/(v1**2 + v2**2 + v3**2)**(3/2) + 1/sqrt(v1**2 + v2**2 + v3**2)

That's a very complicated way of writing it, because it's fully expanded out in terms of the individual components of v.  It could be written much more simply as (1-u1**2)/|v|.  But how do I get Sympy to figure that out?  How do I create intermediate variables like u and v, tell it how they're defined in terms of v1, v2, etc., and then tell it to perform whatever transformations among them leads to the simplest expression?

Thanks!

Peter

Aaron Meurer

unread,
Apr 20, 2014, 10:11:53 PM4/20/14
to sy...@googlegroups.com
Ideally the matrix expressions would let you do this better. I don't think differentiation is fully implemented with them, though. 

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/99e49e60-9db5-4ad3-af0d-449369e4d6ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ondřej Čertík

unread,
Apr 21, 2014, 1:34:23 AM4/21/14
to sympy
Another option might be to use a substitution sqrt(v1**2 + v2**2 +
v3**2) -> |v|.

Peter Eastman

unread,
Apr 21, 2014, 1:30:37 PM4/21/14
to sy...@googlegroups.com
Another option might be to use a substitution sqrt(v1**2 + v2**2 + v3**2) -> |v|.

That works for this toy example because it's so simple.  But in the real calculation I'm trying to do, I have a couple of dozen variables that are all related to each other in different ways, and finding the simplest representation of an expression can require lots of different transformations between them in different directions.  Also, if I do the substitution you suggest I can no longer do any calculations with the resulting expression, because it now includes the variable v along with v1, v2, and v3, and Sympy doesn't know how it's related to them.

Peter

Ondřej Čertík

unread,
Apr 21, 2014, 2:04:07 PM4/21/14
to sympy
Right. Is there any other CAS that can do this better (maybe
Mathematica)? Pretty much you want to calculate the derivatives
in a "clever" way. I am not sure how this would be implemented, other
than SymPy knowing that sqrt(v1**2 + v2**2 + v3**2) and |v|
is the same thing.

Ondrej

Peter Eastman

unread,
Apr 22, 2014, 12:15:58 PM4/22/14
to sy...@googlegroups.com
This isn't really about derivatives.  It's simply the idea that not every symbol is an independent variable.  Some of them are defined in terms of other symbols, and you can use those definitions in everything you do.

I don't know how/if any other program handles this.  I just assumed any CAS would do it, since it's so basic to the way humans do mathematics.  But maybe it's harder than I thought.

Peter

Aaron Meurer

unread,
Apr 22, 2014, 12:21:04 PM4/22/14
to sy...@googlegroups.com
In SymPy if you want to express a relationship between symbols, you
have to use functions, like

f = Function('f')
f(x, y)

creates something that depends on x and y. It is possible to do this
without the explicit syntax, but that requires writing your own class
with the appropriate methods that tell things like diff that it
depends on given variables (in particular, you need to override
free_symbols).

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/76da646f-f921-4291-b66b-9c6283a3f0a6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages