```python
import numpy as np
import math
import sympy
import sympy as sp
from sympy import latex, Eq, IndexedBase, symbols, Idx, Indexed, Sum, S, diff
from sympy.functions.special.tensor_functions import KroneckerDelta
import matplotlib.pyplot as plt
from sympy.vector import Vector, CoordSys3D, AxisOrienter, BodyOrienter, Del, curl, divergence, gradient, is_conservative, is_solenoidal, scalar_potential, Point, scalar_potential_difference
from sympy.physics.vector import ReferenceFrame
from sympy.vector import express
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from IPython.display import display, display_latex
```


```python
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = sympy.symbols('a:z')
alpha, beta, gamma, theta, phi, rho, omega = sympy.symbols('alpha beta gamma theta phi rho omega')
Alpha, Beta, Gamma, Theta, Phi, Rho, Omega = sympy.symbols('Alpha Beta Gamma Theta Phi Rho Omega')
```

## Setup


```python
sympy.init_printing()
np.set_printoptions(precision=3)
```

### Symbols

Cross product: ^  
Dot product: &  
Outer product: |


```python
a, b, c, d, e, f = symbols('a:f')
u1, u2, u3, v1, v2, v3 = symbols('u_1 u_2 u_3 v_1 v_2 v_3')
E = ReferenceFrame('E')
u = u1 * E.x + u2 * E.y + u3 * E.z
v = v1 * E.x + v2 * E.y + v3 * E.z
u
```

#### Dot Product


```python
u & v
```

#### Cross Product


```python
u ^ v
```


```python
N = CoordSys3D('N')
```


```python
v = 2 * N.i + N.j
v
```


```python
Vector.zero
```


```python
N.origin
```


```python
P = N.origin.locate_new('P', a*N.i + b*N.j + c*N.k)
Q = P.locate_new('Q', -b*N.j)
P
```


```python
P.position_wrt(Q)
```


```python
Q.express_coordinates(N)
```

#### Dyadics


```python
dyad = N.i.outer(N.k)
dyad
```


```python
A = CoordSys3D('A')
B = A.orient_new_axis('B', theta, A.k)
B
```




$\displaystyle CoordSys3D\left(B, \left( \left[\begin{matrix}\cos{\left(\theta \right)} & \sin{\left(\theta \right)} & 0\\- \sin{\left(\theta \right)} & \cos{\left(\theta \right)} & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right), CoordSys3D\left(A, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right)\right)\right)$




```python
axis_orienter = AxisOrienter(theta, A.k)
B = A.orient_new('B', axis_orienter)
B
```




$\displaystyle CoordSys3D\left(B, \left( \left[\begin{matrix}\cos{\left(\theta \right)} & \sin{\left(\theta \right)} & 0\\- \sin{\left(\theta \right)} & \cos{\left(\theta \right)} & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right), CoordSys3D\left(A, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right)\right)\right)$



#### Chain Rotations


```python
A = CoordSys3D('A')
body_orienter = BodyOrienter(a, b, c, 'XYZ')
B = A.orient_new('B', (axis_orienter, body_orienter)).subs(theta, 0)
B
```




$\displaystyle CoordSys3D\left(B, \left( \left[\begin{matrix}\cos{\left(b \right)} \cos{\left(c \right)} & \sin{\left(a \right)} \sin{\left(b \right)} \cos{\left(c \right)} + \sin{\left(c \right)} \cos{\left(a \right)} & \sin{\left(a \right)} \sin{\left(c \right)} - \sin{\left(b \right)} \cos{\left(a \right)} \cos{\left(c \right)}\\- \sin{\left(c \right)} \cos{\left(b \right)} & - \sin{\left(a \right)} \sin{\left(b \right)} \sin{\left(c \right)} + \cos{\left(a \right)} \cos{\left(c \right)} & \sin{\left(a \right)} \cos{\left(c \right)} + \sin{\left(b \right)} \sin{\left(c \right)} \cos{\left(a \right)}\\\sin{\left(b \right)} & - \sin{\left(a \right)} \cos{\left(b \right)} & \cos{\left(a \right)} \cos{\left(b \right)}\end{matrix}\right], \  \mathbf{\hat{0}}\right), CoordSys3D\left(A, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right)\right)\right)$



#### Use Multiple Orienters


```python
C = A.orient_new('C', (axis_orienter, body_orienter))
C
```




$\displaystyle CoordSys3D\left(C, \left( \left[\begin{matrix}- \sin{\left(c \right)} \sin{\left(\theta \right)} \cos{\left(b \right)} + \cos{\left(b \right)} \cos{\left(c \right)} \cos{\left(\theta \right)} & \left(- \sin{\left(a \right)} \sin{\left(b \right)} \sin{\left(c \right)} + \cos{\left(a \right)} \cos{\left(c \right)}\right) \sin{\left(\theta \right)} + \left(\sin{\left(a \right)} \sin{\left(b \right)} \cos{\left(c \right)} + \sin{\left(c \right)} \cos{\left(a \right)}\right) \cos{\left(\theta \right)} & \left(\sin{\left(a \right)} \sin{\left(c \right)} - \sin{\left(b \right)} \cos{\left(a \right)} \cos{\left(c \right)}\right) \cos{\left(\theta \right)} + \left(\sin{\left(a \right)} \cos{\left(c \right)} + \sin{\left(b \right)} \sin{\left(c \right)} \cos{\left(a \right)}\right) \sin{\left(\theta \right)}\\- \sin{\left(c \right)} \cos{\left(b \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(b \right)} \cos{\left(c \right)} & \left(- \sin{\left(a \right)} \sin{\left(b \right)} \sin{\left(c \right)} + \cos{\left(a \right)} \cos{\left(c \right)}\right) \cos{\left(\theta \right)} - \left(\sin{\left(a \right)} \sin{\left(b \right)} \cos{\left(c \right)} + \sin{\left(c \right)} \cos{\left(a \right)}\right) \sin{\left(\theta \right)} & - \left(\sin{\left(a \right)} \sin{\left(c \right)} - \sin{\left(b \right)} \cos{\left(a \right)} \cos{\left(c \right)}\right) \sin{\left(\theta \right)} + \left(\sin{\left(a \right)} \cos{\left(c \right)} + \sin{\left(b \right)} \sin{\left(c \right)} \cos{\left(a \right)}\right) \cos{\left(\theta \right)}\\\sin{\left(b \right)} & - \sin{\left(a \right)} \cos{\left(b \right)} & \cos{\left(a \right)} \cos{\left(b \right)}\end{matrix}\right], \  \mathbf{\hat{0}}\right), CoordSys3D\left(A, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \  \mathbf{\hat{0}}\right)\right)\right)$



#### Get Rotation Matrix W.R.T


```python
B.rotation_matrix(A)
```




$\displaystyle \left[\begin{matrix}- \sin{\left(c \right)} \sin{\left(\theta \right)} \cos{\left(b \right)} + \cos{\left(b \right)} \cos{\left(c \right)} \cos{\left(\theta \right)} & \left(- \sin{\left(a \right)} \sin{\left(b \right)} \sin{\left(c \right)} + \cos{\left(a \right)} \cos{\left(c \right)}\right) \sin{\left(\theta \right)} + \left(\sin{\left(a \right)} \sin{\left(b \right)} \cos{\left(c \right)} + \sin{\left(c \right)} \cos{\left(a \right)}\right) \cos{\left(\theta \right)} & \left(\sin{\left(a \right)} \sin{\left(c \right)} - \sin{\left(b \right)} \cos{\left(a \right)} \cos{\left(c \right)}\right) \cos{\left(\theta \right)} + \left(\sin{\left(a \right)} \cos{\left(c \right)} + \sin{\left(b \right)} \sin{\left(c \right)} \cos{\left(a \right)}\right) \sin{\left(\theta \right)}\\- \sin{\left(c \right)} \cos{\left(b \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(b \right)} \cos{\left(c \right)} & \left(- \sin{\left(a \right)} \sin{\left(b \right)} \sin{\left(c \right)} + \cos{\left(a \right)} \cos{\left(c \right)}\right) \cos{\left(\theta \right)} - \left(\sin{\left(a \right)} \sin{\left(b \right)} \cos{\left(c \right)} + \sin{\left(c \right)} \cos{\left(a \right)}\right) \sin{\left(\theta \right)} & - \left(\sin{\left(a \right)} \sin{\left(c \right)} - \sin{\left(b \right)} \cos{\left(a \right)} \cos{\left(c \right)}\right) \sin{\left(\theta \right)} + \left(\sin{\left(a \right)} \cos{\left(c \right)} + \sin{\left(b \right)} \sin{\left(c \right)} \cos{\left(a \right)}\right) \cos{\left(\theta \right)}\\\sin{\left(b \right)} & - \sin{\left(a \right)} \cos{\left(b \right)} & \cos{\left(a \right)} \cos{\left(b \right)}\end{matrix}\right]$



#### Rotate and Translate


```python
C = A.orient_new_axis('C', a, A.k, location=2*A.j)
C.position_wrt(A)
```

#### Express : Vector instances can be expressed in user defined systems using express.


```python
M = N.orient_new_axis('M', a, N.k)
v1 = N.i + N.j + N.k
express(v1, M)
```


```python
v = N.i + N.j + N.k
v.to_matrix(M)
```




$\displaystyle \left[\begin{matrix}\sin{\left(a \right)} + \cos{\left(a \right)}\\- \sin{\left(a \right)} + \cos{\left(a \right)}\\1\end{matrix}\right]$



### Scalar and Vector Fields and Base Scalar

potential field = $2x^2y$


```python
R = CoordSys3D('R')
electric_potential = 2*R.x**2 * R.y
electric_potential
```


```python
diff(electric_potential, R.x)
```


```python
v = R.x**2*R.i + 2*R.x*R.z*R.k
v
```

#### Gradient


```python
C = CoordSys3D('C')
delop = Del()
gradient_field = delop(C.x*C.y*C.z)
gradient_field
```


```python
gradient_field.doit()
```


```python
gradient(C.x*C.y*C.z)
```


```python
delop.gradient(C.x*C.y*C.z).doit()
```

#### Curl

##### Method 1


```python
delop = Del()
delop.cross(C.x*C.y*C.z*C.i)
```


```python
delop.cross(C.x*C.y*C.z*C.i).doit()
```

##### Method 2


```python
curl(C.x*C.y*C.z*C.i)
```

#### Divergence


```python
divergence(C.x*C.y*C.z*(C.i + C.j + C.k))
```


```python
delop.dot(C.x*C.y*C.z*(C.i + C.j + C.k)).doit()
```

#### Directional Derivative


```python
from sympy.vector import directional_derivative
directional_derivative(C.x*C.y*C.z, 3*C.i + 4*C.j + C.k)
```

#### Conservative Field
* Curl = 0 
* Irrotational 
* Conservation of Energy
* Gradient of some scalar field


```python
R = CoordSys3D('R')
field = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
is_conservative(field)
```




    True




```python
curl(field)
```

#### Solenoidal Field
* Div = 0


```python
R = CoordSys3D('R')
field = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
is_solenoidal(field)
```




    True




```python
divergence(field)
```

#### Scalar Potential Functions


```python
R = CoordSys3D('R')
conservative_field = 4*R.x*R.y*R.z*R.i + 2*R.x**2*R.z*R.j + 2*R.x**2*R.y*R.k
scalar_potential(conservative_field, R)
```


```python
R = CoordSys3D('R')
P = R.origin.locate_new('P', 1*R.i + 2*R.j + 3*R.k)
vectfield = 4*R.x*R.y*R.i + 2*R.x**2*R.j
scalar_potential_difference(vectfield, R, R.origin, P)
```


```python

```

15a37027f6b9fc8b9ffca551d6ec8c040fa4519d
ipynb
Jupyter Notebook
.ipynb_checkpoints/Vectors-checkpoint.ipynb
Valentine-Efagene/Jupyter-Notebooks
91a1d98354a270d214316eba21e4a435b3e17f5d
MIT
.ipynb_checkpoints/Vectors-checkpoint.ipynb
Valentine-Efagene/Jupyter-Notebooks
91a1d98354a270d214316eba21e4a435b3e17f5d
MIT
.ipynb_checkpoints/Vectors-checkpoint.ipynb
Valentine-Efagene/Jupyter-Notebooks
91a1d98354a270d214316eba21e4a435b3e17f5d
MIT
Qwen/Qwen-72B
1. YES
2. YES