Skip to content

Vector Calculus & Matrix

๐Ÿ”ข Vector Calculus & Matrix Operations

Vector calculus extends differential and integral operators to vector fields (functions that assign a vector to every point in space). Matrix calculus generalizes this to high-dimensional tensors.


๐ŸŸข 1. Vector Differential Operators (Del โˆ‡\nabla)

The operator โˆ‡\nabla (nabla) is a vector of partial derivative operators: โˆ‡=[โˆ‚โˆ‚x,โˆ‚โˆ‚y,โˆ‚โˆ‚z]\nabla = \left[ \frac{\partial}{\partial x}, \frac{\partial}{\partial y}, \frac{\partial}{\partial z} \right].

1. The Gradient (โˆ‡f\nabla f)

Applied to a scalar function ff, result is a vector field.

  • Points in direction of steepest ascent.

2. The Divergence (โˆ‡โ‹…F\nabla \cdot \mathbf{F})

Applied to a vector field F\mathbf{F}, result is a scalar.

  • Measures the โ€œoutward flowโ€ from a point.

3. The Curl (โˆ‡ร—F\nabla \times \mathbf{F})

Applied to a vector field F\mathbf{F}, result is a vector field.

  • Measures the โ€œrotationโ€ or โ€œswirlโ€ at a point.

๐ŸŸก 2. Matrix Calculus Rules

In machine learning and advanced engineering, we differentiate vectors with respect to other vectors.

1. The Jacobian Matrix (J\mathbf{J})

When differentiating a vector f(x)\mathbf{f}(\mathbf{x}) by another vector x\mathbf{x}, the result is a matrix: J=โˆ‚fโˆ‚x=[โˆ‚f1โˆ‚x1โ€ฆโˆ‚f1โˆ‚xnโ‹ฎโ‹ฑโ‹ฎโˆ‚fmโˆ‚x1โ€ฆโˆ‚fmโˆ‚xn]\mathbf{J} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \dots & \frac{\partial f_1}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \dots & \frac{\partial f_m}{\partial x_n} \end{bmatrix}

2. Essential Matrix Identities

  • Derivative of Linear Form: โˆ‚(aTx)โˆ‚x=aT\frac{\partial (\mathbf{a}^T \mathbf{x})}{\partial \mathbf{x}} = \mathbf{a}^T
  • Derivative of Quadratic Form: โˆ‚(xTAx)โˆ‚x=xT(A+AT)\frac{\partial (\mathbf{x}^T \mathbf{A} \mathbf{x})}{\partial \mathbf{x}} = \mathbf{x}^T (\mathbf{A} + \mathbf{A}^T)
  • Inverse Matrix: โˆ‚Xโˆ’1โˆ‚X=โˆ’(Xโˆ’1)TโŠ—Xโˆ’1\frac{\partial \mathbf{X}^{-1}}{\partial \mathbf{X}} = -(\mathbf{X}^{-1})^T \otimes \mathbf{X}^{-1} (using Kronecker product โŠ—\otimes).

๐Ÿ”ด 3. The Tensor Chain Rule

For a composition z=f(g(x))\mathbf{z} = \mathbf{f}(\mathbf{g}(\mathbf{x})), the derivative is the product of the individual Jacobians: โˆ‚zโˆ‚x=โˆ‚zโˆ‚gโˆ‚gโˆ‚x\frac{\partial \mathbf{z}}{\partial \mathbf{x}} = \frac{\partial \mathbf{z}}{\partial \mathbf{g}} \frac{\partial \mathbf{g}}{\partial \mathbf{x}}


๐ŸŽฏ 4. Fundamental Theorems of Vector Calculus

  1. Fundamental Theorem for Line Integrals: โˆซCโˆ‡fโ‹…dr=f(r(b))โˆ’f(r(a))\int_C \nabla f \cdot d\mathbf{r} = f(\mathbf{r}(b)) - f(\mathbf{r}(a)).
  2. Greenโ€™s Theorem: Relates line integrals around a closed curve to double integrals over the enclosed region.
  3. Stokeโ€™s Theorem: Relates the curl of a vector field over a surface to a line integral around the boundary.
  4. Divergence Theorem (Gaussโ€™s Theorem): Relates the divergence of a vector field in a volume to the flux through the surface.

๐Ÿ’ก Practical Example: Jacobian calculation in Python

import torch

def f(x):
    # Vector-to-vector function
    # y1 = x1^2, y2 = x1*x2
    return torch.stack([x[0]**2, x[0]*x[1]])

x = torch.tensor([1.0, 2.0], requires_grad=True)
y = f(x)

# Calculating Jacobian manually in PyTorch
jacobian = torch.autograd.functional.jacobian(f, x)
print(f"Jacobian matrix:\n{jacobian}")

# Output should be:
# [[2*x1, 0],   => [[2, 0]
#  [x2,   x1]]  =>  [2, 1]]

๐Ÿš€ Key Takeaways

  • Vector operators (grad, div, curl) describe field properties.
  • The Jacobian represents the โ€œderivativeโ€ of a mapping.
  • Fundamental theorems (Stokeโ€™s, Divergence) link integration in different dimensions.