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