On Sun, Apr 20, 2014 at 7:33 PM, Peter Eastman <[email protected]> wrote: > 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?
Another option might be to use a substitution sqrt(v1**2 + v2**2 + v3**2) -> |v|. -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CADDwiVCN%3DmNVsQv7LJjk%3DrjnvzqYL%3D6vvqMPC%2BpzZDWBeHHoxA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
