anypinn.lib.diff
Differential operators for Physics-Informed Neural Networks.
Composable utilities built on torch.autograd.grad for computing
first-order, higher-order, and mixed partial derivatives without
re-implementing autograd boilerplate in every constraint.
All operators default to create_graph=True so their outputs are
differentiable — required when used inside loss functions that must
back-propagate. Pass create_graph=False for detached results
(e.g. visualisation or adaptive sampling).
Note
For even higher performance on large-batch Hessians or Jacobians,
torch.func.jacrev / torch.func.hessian can be composed with
torch.vmap. The operators here intentionally use the simpler
autograd.grad path for broad compatibility and torch.compile
friendliness.
divergence(v: Tensor, x: Tensor, *, create_graph: bool = True) -> Tensor
Compute the divergence \(\nabla \cdot v = \sum_i \partial v_i / \partial x_i\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Tensor
|
Vector field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in src/anypinn/lib/diff.py
grad(u: Tensor, x: Tensor, *, create_graph: bool = True) -> Tensor
Compute the full gradient \(\nabla u\) with respect to coordinates \(x\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Scalar field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Tensor
|
\([\partial u/\partial x_0, \ldots, \partial u/\partial x_{d-1}]\). |
Source code in src/anypinn/lib/diff.py
hessian(u: Tensor, x: Tensor, *, create_graph: bool = True) -> Tensor
Compute the Hessian matrix \(H[u]\) where \(H_{ij} = \partial^2 u / (\partial x_i\partial x_j)\).
Computes the first-order gradient once, then differentiates each
component to build each row — d + 1 autograd calls total.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Scalar field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in src/anypinn/lib/diff.py
laplacian(u: Tensor, x: Tensor, *, create_graph: bool = True) -> Tensor
Compute the Laplacian \(\nabla^2 u = \sum_i \partial^2 u / \partial x_i^2\).
Computes the full first-order gradient once and then differentiates
each component — d + 1 autograd calls total for d dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Scalar field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in src/anypinn/lib/diff.py
mixed_partial(u: Tensor, x: Tensor, dims: tuple[int, ...], *, create_graph: bool = True) -> Tensor
Compute a mixed derivative \(\partial^k u / (\partial x_{d_0} \partial x_{d_1} \cdots)\).
Derivatives are applied left-to-right: first differentiate w.r.t.
dims[0], then the result w.r.t. dims[1], and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Scalar field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
dims
|
tuple[int, ...]
|
Dimension indices to differentiate along, in order. |
required |
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in src/anypinn/lib/diff.py
partial(u: Tensor, x: Tensor, dim: int, *, order: int = 1, create_graph: bool = True) -> Tensor
Compute the order-k derivative \(\partial^k u / \partial x_d^k\) along one dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Scalar field values, shape |
required |
x
|
Tensor
|
Input coordinates, shape |
required |
dim
|
int
|
Spatial dimension index to differentiate along. |
required |
order
|
int
|
Derivative order (≥ 1, default 1). |
1
|
create_graph
|
bool
|
Keep the result in the computation graph (default |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |