Skip to content

Differential Operators

Based on https://github.com/vsitzmann/siren/blob/master/diff_operators.py

Note

The convention is that the input tensor always comes before the output tensor. For instance, computing the gradient of Y with relation to X is written gradient(X,Y) and not gradient(Y,X).

divergence(x, y)

Compute divergence of vector field y with respect to x.

Divergence: ∇·y = Σᵢ ∂yᵢ/∂xᵢ Measures the "spreading" of a vector field at each point.

Parameters:

Name Type Description Default
x Tensor

Input coordinates of shape (meta_batch_size, num_observations, dim)

required
y Tensor

Vector field of shape (meta_batch_size, num_observations, dim)

required

Returns:

Type Description

torch.Tensor: Divergence of shape (meta_batch_size, num_observations) Scalar value at each observation point

gradient(x, y, grad_outputs=None)

Compute gradient (first-order derivative) of y with respect to x.

Computes ∇y = [∂y/∂x_1, ∂y/∂x_2, ..., ∂y/∂x_D] using reverse-mode autodiff. Supports custom gradient weights for backpropagation through multiple objectives.

Parameters:

Name Type Description Default
x Tensor

Input tensor of shape (meta_batch_size, num_observations, dim)

required
y Tensor

Output tensor of shape (meta_batch_size, num_observations, channels)

required
grad_outputs Tensor

Custom gradient weights for backpropagation. Shape must match y. Default: ones_like(y)

None

Returns:

Type Description

torch.Tensor: Gradient of shape (meta_batch_size, num_observations, dim, channels)

hessian(y, x)

Compute Hessian matrix (second-order partial derivatives) of y with respect to x.

The Hessian is the matrix of all second partial derivatives: H[i,j] = ∂²y/∂x_i∂x_j

Parameters:

Name Type Description Default
x Tensor

Input tensor of shape (N, D) where D = input dimensions

required
y Tensor

Output tensor of shape (N, C) where N = batch size, C = channels

required

Returns:

Type Description

torch.Tensor: Hessian matrix of shape (N, C, D, D) For each sample n and channel c: H[n,c,:,:] is D×D Hessian matrix

jacobian(x, y)

Compute Jacobian matrix (first-order partial derivatives) of y with respect to x.

The Jacobian is the matrix of all first partial derivatives: J[i,j] = ∂y_i/∂x_j

Parameters:

Name Type Description Default
y Tensor

Output tensor of shape (N, C) where N = batch size, C = output channels

required
x Tensor

Input tensor of shape (N, D) where D = input dimensions

required

Returns:

Type Description

torch.Tensor: Jacobian matrix of shape (N, C, D) For each sample n: J[n,c,d] = ∂y[n,c]/∂x[n,d]

laplace(x, y, normalize=False, eps=0.0, return_grad=False)

Compute Laplacian (divergence of gradient) of y with respect to x.

Laplacian: ∇²y = Σᵢ ∂²y/∂xᵢ² Optionally normalizes the gradient before computing divergence (useful for geometric processing).

Parameters:

Name Type Description Default
x Tensor

Input coordinates of shape (meta_batch_size, num_observations, dim)

required
y Tensor

Scalar field of shape (meta_batch_size, num_observations, channels)

required
normalize bool

Whether to normalize gradient by its magnitude before divergence (default: False)

False
eps float

Small epsilon for numerical stability in normalization (default: 0)

0.0
return_grad bool

Whether to also return the gradient used in computation (default: False)

False

Returns:

Name Type Description

torch.Tensor: Laplacian of shape (meta_batch_size, num_observations, channels)

tuple

(laplacian, gradient) if return_grad=True