```python
%load_ext autoreload
%autoreload 2
import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d
import matplotlib.cm as cm
from IPython.display import display, Math, clear_output

import sympy  #Sympy1.2 is from conda-forge: [sudo conda install -c conda-forge sympy]
from sympy import *
from sympy.physics.vector import ReferenceFrame, CoordinateSym
from sympy.vector import CoordSys3D, divergence, curl

import ipyvolume as ipv
import time
from ipywidgets import Output

import helpers

np.seterr(divide='ignore', invalid='ignore');
```

## Symbolic Calculation of the Gradient 


```python
#define symbolic vars, function
x,y,z =sympy.symbols('x y z')
fun = x**2

# evaluate the gradient symbolically
gradfun = [sympy.diff(fun, var) for var in (x,y,z)]
print('Gradient of: {}'.format(fun))
print(gradfun)
print(np.shape(gradfun))

#turn into a bivariate lambda for numpy
numgradfun = sympy.lambdify([x,y,z], gradfun)

x = np.linspace(-10, 10, 41)
y = np.linspace(-10, 10, 41)
z = np.linspace(-10, 10, 41)
X, Y, Z = np.meshgrid(x,y,z, indexing='ij')

gradient_eval = sum(numgradfun(X, Y, Z))

print('{}, {}, {}'.format(np.shape(X),np.shape(Y),np.shape(Z)))
print(np.shape(gradient_eval))
print(numgradfun(1,1,2))
print(sum(numgradfun(1,1,2)))
ipv.figure()
ipv.volshow(gradient_eval.T, opacity=0.03, level_width=0.1, data_min=0, data_max=1)
ipv.view(azimuth = 115, elevation = 90)
ipv.show()
```

    Gradient of: x**2
    [2*x, 0, 0]
    (3,)
    (41, 41, 41), (41, 41, 41), (41, 41, 41)
    (41, 41, 41)
    [2, 0, 0]
    2



    A Jupyter Widget


2bc35b751bb649e0956ca9607708f9d5c45e8ae8
ipynb
Jupyter Notebook
gradient.ipynb
lucask07/teaching-notebooks
732638b3bac528f85e0dc649c4671c005f58b22b
MIT
gradient.ipynb
lucask07/teaching-notebooks
732638b3bac528f85e0dc649c4671c005f58b22b
MIT
gradient.ipynb
lucask07/teaching-notebooks
732638b3bac528f85e0dc649c4671c005f58b22b
MIT
Qwen/Qwen-72B
1. YES
2. YES