A Gentle Tutorial on Vector Calculus
Published on Thursday, 21-08-2025
(Adopted from CS221) (../../images/basics/calculus.png)
๐ A Gentle Tutorial on Vector Calculus
With Real Functions, Vector Inputs, and Matrix Inputs
Vector calculus is the mathematical backbone of modern machine learning, optimization, and physics. In this tutorial, weโll explore how calculus extends beyond single-variable functions to vectors and matrices. Weโll cover:
- Real Functions of Real Inputs
- Functions of Vector Inputs
- Functions of Matrix Inputs
And along the way, weโll implement these ideas with Python and NumPy, plus some visualizations using Matplotlib.
1. ๐ข Real Functions of Real Inputs
This is the classic case you learn in calculus: A function .
Example:
Its derivative:
Python Implementation
import numpy as np
import matplotlib.pyplot as plt
# Define function and derivative
f = lambda x: x**2 + 3*x + 2
f_prime = lambda x: 2*x + 3
# Plot
x = np.linspace(-5, 5, 200)
plt.figure(figsize=(6,4))
plt.plot(x, f(x), label="f(x) = xยฒ + 3x + 2")
plt.plot(x, f_prime(x), label="f'(x) = 2x + 3", linestyle="--")
plt.legend()
plt.title("Real Function and Its Derivative")
plt.xlabel("x")
plt.ylabel("Value")
plt.grid(True)
plt.show() ๐ This simple case generalizes naturally to vectors.
2. ๐งญ Functions of Vector Inputs
Now, consider a function . For example:
Here, .
The gradient is the vector of partial derivatives:
Python Implementation
# Vector function: f(x1, x2, x3) = x1^2 + 2x2^2 + 3x3^2
def f_vec(x):
return x[0]**2 + 2*x[1]**2 + 3*x[2]**2
def grad_f_vec(x):
return np.array([2*x[0], 4*x[1], 6*x[2]])
x = np.array([1.0, 2.0, -1.0])
print("f(x) =", f_vec(x))
print("โf(x) =", grad_f_vec(x)) โ This is the foundation of gradient descent in machine learning.
Visualization in 2D
For a function , the gradient points outward radially.
X, Y = np.meshgrid(np.linspace(-2, 2, 20), np.linspace(-2, 2, 20))
U = 2*X
V = 2*Y
plt.figure(figsize=(6,6))
plt.quiver(X, Y, U, V, color="blue")
plt.title("Gradient Field of f(x,y) = xยฒ + yยฒ")
plt.xlabel("x")
plt.ylabel("y")
plt.show() 3. ๐๏ธ Functions of Matrix Inputs
Now letโs extend further: .
Example:
This is the squared Frobenius norm of a matrix.
The derivative (gradient w.r.t. ) is:
Python Implementation
# Function of a matrix: Frobenius norm squared
def f_matrix(A):
return np.trace(A.T @ A)
def grad_f_matrix(A):
return 2*A
A = np.array([[1., 2.],
[3., -1.]])
print("f(A) =", f_matrix(A))
print("โf(A) =\n", grad_f_matrix(A)) 4. ๐งฎ Jacobian Matrix
If we have a vector-valued function:
with
the Jacobian is the matrix of partial derivatives:
Example
Jacobian:
Python Implementation
import sympy as sp
# Define variables
x, y = sp.symbols('x y')
# Define vector-valued function
f1 = x**2 + y
f2 = sp.exp(x) * y
f = sp.Matrix([f1, f2])
# Jacobian
J = f.jacobian([x, y])
J โ The Jacobian is crucial in neural networks (backpropagation) and multivariable optimization.
5. ๐ Hessian Matrix
The Hessian generalizes the second derivative to multiple dimensions.
For a scalar function , the Hessian is:
Example
Gradient:
Hessian:
Python Implementation
# Define scalar function
f = x**2 + x*y + y**2
# Gradient
grad_f = [sp.diff(f, var) for var in (x, y)]
print("Gradient:", grad_f)
# Hessian
H = sp.hessian(f, (x, y))
H โ The Hessian tells us about curvature:
- Positive definite โ local minimum
- Negative definite โ local maximum
- Mixed signs โ saddle point
6. ๐ Visualization: Hessian as Curvature
Letโs visualize .
from mpl_toolkits.mplot3d import Axes3D
X = np.linspace(-2, 2, 50)
Y = np.linspace(-2, 2, 50)
X, Y = np.meshgrid(X, Y)
Z = X**2 + X*Y + Y**2
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap="viridis", alpha=0.8)
ax.set_title("Surface of f(x,y) = xยฒ + xy + yยฒ")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("f(x,y)")
plt.show() The Hessian explains the bowl-shaped curvature we see in the plot.