### trigonometry

#### Sympy cancelling terms in the Jacobian of polar coordinates transformation

I am preparing examples of how a Jacobian is arrived at using transformations from polar/cartesian parametrisations. My question is twofold. First, I want to know how to force sympy to cancel out terms following substitution. The two matrices are: J1 = Matrix([[(r*cos(theta)).diff(r), (r*cos(theta)).diff(theta)],[(r*sin(theta)).diff(r),(r*sin(theta)).diff(theta)]]) J2 = Matrix([[((x**2+y**2)**(1/2.)).diff(x),((x**2+y**2)**(1/2.)).diff(y)],[atan(y/x).diff(x),atan(y/x).diff(y)]]) #substitute for x,y to have same variables for J1 and J2 J2 = trigsimp(J2.subs({x:r*cos(theta), y:r*sin(theta)})) J2 I expected that using cancel(), or even evalf() would have removed the r/(r^2)^1/2= 1 term, but it did not. Second, how or can one require sympy to recognise simple identities, in this case sin^2+cos^2 = 1? This is so that the result is an evaluated identity matrix from J1*J2. This works, as per documentation: simplify(r/(r**2)**(1/2)*(sin(theta)**2+cos(theta)**2)) This equivalent(ish) equation does not. J = J1*J2 simplify(J[0,0]) It seems that the second error is a consequence of the first.

Mathematically, r/(r^2)^1/2= 1 is not always true. It's true if r is a nonnegative number, which it is in polar coordinates. So you should tell SymPy this: r = Symbol('r', nonnegative=True) theta, x, y = symbols('theta x y') (Mathematically, you can even assume r strictly positive, positive=True, since at the origin derivatives in polar coordinates don't work anyway.) The output will be much more agreeable: [[1.0*cos(theta), 1.0*sin(theta)], [-sin(theta)/r, cos(theta)/r]] Only this 1.0 is annoying, where does it come from? It comes from 1/2. being a float instead of a rational number. Use Rational(1, 2) to have a rational number in the exponent (important for simplification). In this case, the exponent being 1/2, it's more natural to use sqrt which has the same effect of making the exponent rational and is easier to type. sqrt(x**2+y**2).diff(x) The end result is [[cos(theta), sin(theta)], [-sin(theta)/r, cos(theta)/r]] As for trigonometric simplification, trigsimp(J1*J2) does return the identity matrix.

### Related Links

Calculate place of point C to be equal distance away from points A and B

How to compute sum of angles from sine and cossine?

Counting trig ops as measure() function to fu() in sympy

Find a coordinate along a path

Angle calculation resulting in strange behavior

Shift angle orientation using atan2

estimate angle between two lines y = 1000x and y = 999x

Need help revising a simple formula to make it universal

SWIFT trig functions

Simplifying atan(tan(a+b))

2 Different Radians constant calcuations whats the difference?

Trigonometry & Sunset

Calculating collisions with Radians incrementing

How do arbitrary-precision libraries like mpmath evaluate simple trigonometric functions?

How can I calculate the angle

Finding Shortest Distance Between Two Parallel Lines, With Arbitrary Point [closed]