# Jacobian.ipynb

In [1]:
from sympy import (symbols, pi, Lambda,
Matrix, sqrt, atan2,
cos, sin, Derivative, Eq)
from IPython.display import HTML

In [2]:
x, y, z = symbols('x y z', real=True)
r, theta, phi = symbols('r theta phi', nonnegative=True)


# 2D Jacobian

In [3]:
car2d = Matrix([x, y])
pol = Matrix([r, theta])
display(car2d, pol)

$\displaystyle \left[\begin{matrix}x\\y\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}r\\\theta\end{matrix}\right]$

## Jacobian notation

from cartesian 2d to polar

In [4]:
J2d_car2d_pol = symbols('\\textrm{J}_{car2d}^{pol}')
J2d_car2d_pol

Out[4]:
$\displaystyle \textrm{J}_{car2d}^{pol}$
In [5]:
J2d_car2d_pol_m = Matrix([[Derivative(r, x), Derivative(r, y)],
[Derivative(theta, x), Derivative(theta, y)]])
J_eq = Eq(J2d_car2d_pol, J2d_car2d_pol_m, evaluate = False)
display(
J_eq
)

$\displaystyle \textrm{J}_{car2d}^{pol} = \left[\begin{matrix}\frac{d}{d x} r & \frac{d}{d y} r\\\frac{d}{d x} \theta & \frac{d}{d y} \theta\end{matrix}\right]$

## Jacobian of r = sqrt(x2+y2) and theta = atan2(y, x)

In [6]:
J2d_car2d_pol_m = J2d_car2d_pol_m.subs({r: sqrt(x**2+y**2), theta: atan2(y, x)})
J2d_car2d_pol_m

Out[6]:
$\displaystyle \left[\begin{matrix}\frac{\partial}{\partial x} \sqrt{x^{2} + y^{2}} & \frac{\partial}{\partial y} \sqrt{x^{2} + y^{2}}\\\frac{\partial}{\partial x} \operatorname{atan_{2}}{\left(y,x \right)} & \frac{\partial}{\partial y} \operatorname{atan_{2}}{\left(y,x \right)}\end{matrix}\right]$
In [7]:
J2d_car2d_pol_m.doit()

Out[7]:
$\displaystyle \left[\begin{matrix}\frac{x}{\sqrt{x^{2} + y^{2}}} & \frac{y}{\sqrt{x^{2} + y^{2}}}\\- \frac{y}{x^{2} + y^{2}} & \frac{x}{x^{2} + y^{2}}\end{matrix}\right]$

## Jacobian notation

from polar to cartesian 2d

In [8]:
J2d_pol_car2d = symbols('\\textrm{J}_{pol}^{car2d}')
J2d_pol_car2d

Out[8]:
$\displaystyle \textrm{J}_{pol}^{car2d}$
In [9]:
J2d_pol_car2d_m = Matrix([[Derivative(x, r), Derivative(x, theta)],
[Derivative(y, r), Derivative(y, theta)]])

J_eq = Eq(J2d_pol_car2d, J2d_pol_car2d_m, evaluate = False)
display(
J_eq
)

$\displaystyle \textrm{J}_{pol}^{car2d} = \left[\begin{matrix}\frac{d}{d r} x & \frac{d}{d \theta} x\\\frac{d}{d r} y & \frac{d}{d \theta} y\end{matrix}\right]$

## Jacobian of x = r cos(theta) and y = r sin(theta)

In [10]:
J2d_pol_car2d_m = J2d_pol_car2d_m.subs({x: r*cos(theta), y: r*sin(theta)})
J2d_pol_car2d_m

Out[10]:
$\displaystyle \left[\begin{matrix}\frac{\partial}{\partial r} r \cos{\left(\theta \right)} & \frac{\partial}{\partial \theta} r \cos{\left(\theta \right)}\\\frac{\partial}{\partial r} r \sin{\left(\theta \right)} & \frac{\partial}{\partial \theta} r \sin{\left(\theta \right)}\end{matrix}\right]$
In [11]:
J2d_pol_car2d_m.doit()

Out[11]:
$\displaystyle \left[\begin{matrix}\cos{\left(\theta \right)} & - r \sin{\left(\theta \right)}\\\sin{\left(\theta \right)} & r \cos{\left(\theta \right)}\end{matrix}\right]$

# 3D Jacobian

In [12]:
car3d = Matrix([x, y, z])
sphe = Matrix([r, theta, phi])
display(car3d, sphe)

$\displaystyle \left[\begin{matrix}x\\y\\z\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}r\\\theta\\\phi\end{matrix}\right]$

## Jacobian notation

from cartesian 3d to spherical

In [13]:
J3d_car3d_sphe = symbols('\\textrm{J}_{car3d}^{sphe}')
J3d_car3d_sphe

Out[13]:
$\displaystyle \textrm{J}_{car3d}^{sphe}$
In [14]:
J3d_car3d_sphe_m = Matrix([[Derivative(r, x), Derivative(r, y), Derivative(r, z)],
[Derivative(theta, x), Derivative(theta, y), Derivative(theta, z)],
[Derivative(phi, x), Derivative(phi, y), Derivative(phi, z)]])
J3d_car3d_sphe_m

Out[14]:
$\displaystyle \left[\begin{matrix}\frac{d}{d x} r & \frac{d}{d y} r & \frac{d}{d z} r\\\frac{d}{d x} \theta & \frac{d}{d y} \theta & \frac{d}{d z} \theta\\\frac{d}{d x} \phi & \frac{d}{d y} \phi & \frac{d}{d z} \phi\end{matrix}\right]$

## Jacobian of r = sqrt(x2 + y2 + z2) and $\theta$ = atan2(y, x) and $\phi$ = atan2(z, sqrt(x2 + y**2))

In [15]:
J3d_car3d_sphe_m = J3d_car3d_sphe_m.subs({r: sqrt(x**2 + y**2 + z**2), theta: atan2(y, x), phi: atan2(z, sqrt(x**2 + y**2))})
J3d_car3d_sphe_m

Out[15]:
$\displaystyle \left[\begin{matrix}\frac{\partial}{\partial x} \sqrt{x^{2} + y^{2} + z^{2}} & \frac{\partial}{\partial y} \sqrt{x^{2} + y^{2} + z^{2}} & \frac{\partial}{\partial z} \sqrt{x^{2} + y^{2} + z^{2}}\\\frac{\partial}{\partial x} \operatorname{atan_{2}}{\left(y,x \right)} & \frac{\partial}{\partial y} \operatorname{atan_{2}}{\left(y,x \right)} & \frac{\partial}{\partial z} \operatorname{atan_{2}}{\left(y,x \right)}\\\frac{\partial}{\partial x} \operatorname{atan_{2}}{\left(z,\sqrt{x^{2} + y^{2}} \right)} & \frac{\partial}{\partial y} \operatorname{atan_{2}}{\left(z,\sqrt{x^{2} + y^{2}} \right)} & \frac{\partial}{\partial z} \operatorname{atan_{2}}{\left(z,\sqrt{x^{2} + y^{2}} \right)}\end{matrix}\right]$
In [16]:
J3d_car3d_sphe_m.doit()

Out[16]:
$\displaystyle \left[\begin{matrix}\frac{x}{\sqrt{x^{2} + y^{2} + z^{2}}} & \frac{y}{\sqrt{x^{2} + y^{2} + z^{2}}} & \frac{z}{\sqrt{x^{2} + y^{2} + z^{2}}}\\- \frac{y}{x^{2} + y^{2}} & \frac{x}{x^{2} + y^{2}} & 0\\- \frac{x z}{\sqrt{x^{2} + y^{2}} \left(x^{2} + y^{2} + z^{2}\right)} & - \frac{y z}{\sqrt{x^{2} + y^{2}} \left(x^{2} + y^{2} + z^{2}\right)} & \frac{\sqrt{x^{2} + y^{2}}}{x^{2} + y^{2} + z^{2}}\end{matrix}\right]$
In [ ]:



## Jacobian notation

from spherical to cartesian 3d

In [17]:
J3d_sphe_car3d = symbols('\\textrm{J}_{sphe}^{car3d}')
J3d_sphe_car3d

Out[17]:
$\displaystyle \textrm{J}_{sphe}^{car3d}$
In [18]:
J3d_sphe_car3d_m = Matrix([[Derivative(x, r), Derivative(x, phi), Derivative(x, theta)],
[Derivative(y, r), Derivative(y, phi), Derivative(y, theta)],
[Derivative(z, r), Derivative(z, phi), Derivative(z, theta)]])
J3d_sphe_car3d_m

Out[18]:
$\displaystyle \left[\begin{matrix}\frac{d}{d r} x & \frac{d}{d \phi} x & \frac{d}{d \theta} x\\\frac{d}{d r} y & \frac{d}{d \phi} y & \frac{d}{d \theta} y\\\frac{d}{d r} z & \frac{d}{d \phi} z & \frac{d}{d \theta} z\end{matrix}\right]$

## Jacobian of x = r cos($\phi$) cos($\theta$) and y = r sin($\phi$) sin($\theta$) and z = r cos($\phi$)

In [19]:
J3d_sphe_car3d_m = J3d_sphe_car3d_m.subs({x: r*cos(phi)*cos(theta), y: r*cos(phi)*sin(theta), z:r*sin(phi)})
J3d_sphe_car3d_m

Out[19]:
$\displaystyle \left[\begin{matrix}\frac{\partial}{\partial r} r \cos{\left(\phi \right)} \cos{\left(\theta \right)} & \frac{\partial}{\partial \phi} r \cos{\left(\phi \right)} \cos{\left(\theta \right)} & \frac{\partial}{\partial \theta} r \cos{\left(\phi \right)} \cos{\left(\theta \right)}\\\frac{\partial}{\partial r} r \sin{\left(\theta \right)} \cos{\left(\phi \right)} & \frac{\partial}{\partial \phi} r \sin{\left(\theta \right)} \cos{\left(\phi \right)} & \frac{\partial}{\partial \theta} r \sin{\left(\theta \right)} \cos{\left(\phi \right)}\\\frac{\partial}{\partial r} r \sin{\left(\phi \right)} & \frac{\partial}{\partial \phi} r \sin{\left(\phi \right)} & \frac{\partial}{\partial \theta} r \sin{\left(\phi \right)}\end{matrix}\right]$
In [20]:
J3d_sphe_car3d_m.doit()

Out[20]:
$\displaystyle \left[\begin{matrix}\cos{\left(\phi \right)} \cos{\left(\theta \right)} & - r \sin{\left(\phi \right)} \cos{\left(\theta \right)} & - r \sin{\left(\theta \right)} \cos{\left(\phi \right)}\\\sin{\left(\theta \right)} \cos{\left(\phi \right)} & - r \sin{\left(\phi \right)} \sin{\left(\theta \right)} & r \cos{\left(\phi \right)} \cos{\left(\theta \right)}\\\sin{\left(\phi \right)} & r \cos{\left(\phi \right)} & 0\end{matrix}\right]$
In [ ]: