# SymPy Modules Reference

*Arthur Ryman* <br/>
*Last Updated: 2020-04-18*


This notebook contains the examples from the 
[SymPy Modules Reference](https://docs.sympy.org/latest/modules/index.html).

## [Scalar and Vector Field Functionality](https://docs.sympy.org/latest/modules/vector/fields.html)

### Implementation in sympy.vector

#### Scalar and vector fields


```python
from sympy.vector import CoordSys3D
R = CoordSys3D('R')
v = 3*R.i + 4*R.j + 5*R.k
v
```




$\displaystyle (3)\mathbf{\hat{i}_{R}} + (4)\mathbf{\hat{j}_{R}} + (5)\mathbf{\hat{k}_{R}}$




```python
from sympy.vector import CoordSys3D
R = CoordSys3D('R')
electric_potential = 2*R.x**2*R.y
electric_potential
```




$\displaystyle 2 \mathbf{{x}_{R}}^{2} \mathbf{{y}_{R}}$




```python
from sympy.vector import CoordSys3D
R = CoordSys3D('R')
electric_potential = 2*R.x**2*R.y
from sympy import diff
diff(electric_potential, R.x)
```




$\displaystyle 4 \mathbf{{x}_{R}} \mathbf{{y}_{R}}$



#### The Del operator


```python
from sympy.vector import CoordSys3D, Del
C = CoordSys3D('C')
delop = Del()
gradient_field = delop(C.x*C.y*C.z)
gradient_field
```




$\displaystyle (\frac{\partial}{\partial \mathbf{{x}_{C}}} \mathbf{{x}_{C}} \mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}} + (\frac{\partial}{\partial \mathbf{{y}_{C}}} \mathbf{{x}_{C}} \mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{j}_{C}} + (\frac{\partial}{\partial \mathbf{{z}_{C}}} \mathbf{{x}_{C}} \mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{k}_{C}}$




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




$\displaystyle (\mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}} + (\mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{j}_{C}} + (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{k}_{C}}$



### Field operators and related functions

#### Curl


```python
from sympy.vector import CoordSys3D, Del
C = CoordSys3D('C')
delop = Del()
delop.cross(C.x*C.y*C.z*C.i).doit()
```




$\displaystyle (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{j}_{C}} + (- \mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{k}_{C}}$




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




$\displaystyle (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{j}_{C}} + (- \mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{k}_{C}}$




```python
from sympy.vector import curl
curl(C.x*C.y*C.z*C.i)
```




$\displaystyle (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{j}_{C}} + (- \mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{k}_{C}}$



#### Divergence


```python
from sympy.vector import CoordSys3D, Del
C = CoordSys3D('C')
delop = Del()
delop.dot(C.x*C.y*C.z*(C.i + C.j + C.k)).doit()
```




$\displaystyle \mathbf{{x}_{C}} \mathbf{{y}_{C}} + \mathbf{{x}_{C}} \mathbf{{z}_{C}} + \mathbf{{y}_{C}} \mathbf{{z}_{C}}$




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




$\displaystyle \mathbf{{x}_{C}} \mathbf{{y}_{C}} + \mathbf{{x}_{C}} \mathbf{{z}_{C}} + \mathbf{{y}_{C}} \mathbf{{z}_{C}}$




```python
from sympy.vector import divergence
divergence(C.x*C.y*C.z*(C.i + C.j + C.k))
```




$\displaystyle \mathbf{{x}_{C}} \mathbf{{y}_{C}} + \mathbf{{x}_{C}} \mathbf{{z}_{C}} + \mathbf{{y}_{C}} \mathbf{{z}_{C}}$



#### Gradient


```python
from sympy.vector import CoordSys3D, Del
C = CoordSys3D('C')
delop = Del()
delop.gradient(C.x*C.y*C.z).doit()
```




$\displaystyle (\mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}} + (\mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{j}_{C}} + (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{k}_{C}}$




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




$\displaystyle (\mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}} + (\mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{j}_{C}} + (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{k}_{C}}$




```python
from sympy.vector import gradient
gradient(C.x*C.y*C.z)
```




$\displaystyle (\mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}} + (\mathbf{{x}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{j}_{C}} + (\mathbf{{x}_{C}} \mathbf{{y}_{C}})\mathbf{\hat{k}_{C}}$



#### Directional Derivative


```python
from sympy.vector import CoordSys3D, Del
C = CoordSys3D('C')
delop = Del()
vel = C.i + C.j + C.k
scalar_field = C.x*C.y*C.z
vector_field = C.x*C.y*C.z*C.i
(vel.dot(delop))(scalar_field)
```




$\displaystyle \mathbf{{x}_{C}} \mathbf{{y}_{C}} + \mathbf{{x}_{C}} \mathbf{{z}_{C}} + \mathbf{{y}_{C}} \mathbf{{z}_{C}}$




```python
(vel & delop)(vector_field)
```




$\displaystyle (\mathbf{{x}_{C}} \mathbf{{y}_{C}} + \mathbf{{x}_{C}} \mathbf{{z}_{C}} + \mathbf{{y}_{C}} \mathbf{{z}_{C}})\mathbf{\hat{i}_{C}}$




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




$\displaystyle \mathbf{{x}_{C}} \mathbf{{y}_{C}} + 4 \mathbf{{x}_{C}} \mathbf{{z}_{C}} + 3 \mathbf{{y}_{C}} \mathbf{{z}_{C}}$



### Field operator in orthogonal curvilinear coordinate system


```python
from sympy.vector import CoordSys3D
c = CoordSys3D('c', transformation='cylindrical', variable_names=('r', 'theta', 'z'))
gradient(c.r*c.theta*c.z)
```




$\displaystyle (\mathbf{{theta}_{c}} \mathbf{{z}_{c}})\mathbf{\hat{i}_{c}} + (\mathbf{{z}_{c}})\mathbf{\hat{j}_{c}} + (\mathbf{{r}_{c}} \mathbf{{theta}_{c}})\mathbf{\hat{k}_{c}}$



### Conservative and Solenoidal Fields


```python
from sympy.vector import CoordSys3D, is_conservative
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)
```




$\displaystyle \mathbf{\hat{0}}$




```python
from sympy.vector import CoordSys3D, is_solenoidal
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)
```




$\displaystyle 0$



### Scalar potential functions


```python
from sympy.vector import CoordSys3D, scalar_potential
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)
```




$\displaystyle 2 \mathbf{{x}_{R}}^{2} \mathbf{{y}_{R}} \mathbf{{z}_{R}}$




```python
from sympy.vector import CoordSys3D, Point
from sympy.vector import scalar_potential_difference
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
is_conservative(vectfield)
```




    True




```python
scalar_potential_difference(vectfield, R, R.origin, P)
```




$\displaystyle 4$




```python
scalarpot = scalar_potential(vectfield, R)
scalarpot
```




$\displaystyle 2 \mathbf{{x}_{R}}^{2} \mathbf{{y}_{R}}$




```python
scalarpot.subs({R.x: 0})
```




$\displaystyle 0$




```python
P.express_coordinates(R)
```




    (1, 2, 3)




```python
R.origin.express_coordinates(R)
```




    (0, 0, 0)




```python
P_coord = P.express_coordinates(R)
scalarpot.subs({R.x: P_coord[0], R.y: P_coord[1], R.z: P_coord[2]})
```




$\displaystyle 4$



6e53bd8418cf6d6f93c39b081b8d4d192c17b6c9
ipynb
Jupyter Notebook
notes/docs.sympy.org/modules/vector/scalar-and-vector-field-functionality.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
notes/docs.sympy.org/modules/vector/scalar-and-vector-field-functionality.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
2022-01-21T22:17:28.000Z
2022-01-21T22:17:28.000Z
notes/docs.sympy.org/modules/vector/scalar-and-vector-field-functionality.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
Qwen/Qwen-72B
1. YES
2. YES