Differential Operators#
Differential operators (gradient, divergence, Laplacian) in Cartesian and spherical coordinates.
- spherical_inr.diffops.cartesian_divergence(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the divergence of a vector field in Cartesian coordinates.
For a vector field \(\mathbf{F}: \mathbb{R}^n \rightarrow \mathbb{R}^n\) with components \(\mathbf{F}(x) = \left[F_1(x),\, F_2(x),\, \dots,\, F_n(x)\right]\), the divergence is defined as
\[\nabla \cdot \mathbf{F} = \sum_{i=1}^{n} \frac{\partial F_i}{\partial x_i}.\]This function computes the divergence by summing the partial derivatives of each component of the vector field with respect to its corresponding Cartesian coordinate.
- Parameters:
outputs (torch.Tensor) – Tensor representing the vector field, where the last dimension contains the components of \(\mathbf{F}\).
inputs (torch.Tensor) – Tensor representing the Cartesian coordinates \(x\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The divergence \(\nabla \cdot \mathbf{F}\).
- Return type:
torch.Tensor
- spherical_inr.diffops.cartesian_gradient(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the gradient of a scalar function in Cartesian coordinates.
For a scalar function \(f: \mathbb{R}^n \rightarrow \mathbb{R}\), the gradient is given by
\[\nabla f(x) = \left[ \frac{\partial f}{\partial x_1},\, \frac{\partial f}{\partial x_2},\, \dots,\, \frac{\partial f}{\partial x_n} \right].\]This function computes \(\nabla f(x)\) with respect to the Cartesian coordinates provided in
inputs. Enabling thetrackparameter allows for the construction of higher-order derivative graphs.- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(x)\).
inputs (torch.Tensor) – Tensor representing the Cartesian coordinates \(x\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The gradient \(\nabla f(x)\) in Cartesian coordinates.
- Return type:
torch.Tensor
- spherical_inr.diffops.cartesian_laplacian(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the Laplacian of a scalar function in Cartesian coordinates.
The Laplacian is defined as the divergence of the gradient:
\[\Delta f = \nabla \cdot \left(\nabla f\right) \;=\; \sum_{i=1}^{n} \frac{\partial^2 f}{\partial x_i^2}.\]This function computes the gradient of \(f(x)\) with respect to the Cartesian coordinates
xand then evaluates its divergence to obtain the Laplacian. Thetrackparameter enables higher-order derivative tracking.- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(x)\).
inputs (torch.Tensor) – Tensor representing Cartesian coordinates \(x\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The Laplacian \(\Delta f\) of the function.
- Return type:
torch.Tensor
- spherical_inr.diffops.s2_divergence(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the divergence of a vector field defined on the 2-sphere (S²).
For a vector field on S², \(\mathbf{F}(\theta,\phi) = \left[F_\theta,\, F_\phi\right]\), the divergence is defined as
\[\nabla_{\mathbb{S}^2} \cdot \mathbf{F} = \frac{1}{\sin\theta}\frac{\partial}{\partial \theta}\Bigl(\sin\theta F_\theta\Bigr) \;+\; \frac{1}{\sin\theta}\frac{\partial F_\phi}{\partial \phi}.\]This function computes the divergence of the vector field on S². The tensor
outputsmust have two components.- Parameters:
outputs (torch.Tensor) – Tensor representing the vector field on S² with two components.
inputs (torch.Tensor) – Tensor of shape (…, 2) representing spherical coordinates \((\theta, \phi)\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The divergence \(\nabla_{\mathbb{S}^2} \cdot \mathbf{F}\) on the 2-sphere.
- Return type:
torch.Tensor
- Raises:
ValueError: – If
outputsdoes not have two components.
- spherical_inr.diffops.s2_gradient(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the gradient of a function defined on the 2-sphere (S²) with respect to the angles \((\theta, \phi)\).
For a function \(f(\theta, \phi)\) defined on S², the gradient is expressed as
\[\nabla_{\mathbb{S}^2} f = \hat{\theta}\,\frac{\partial f}{\partial \theta} \;+\; \hat{\phi}\,\frac{1}{\sin\theta}\frac{\partial f}{\partial \phi}.\]This function computes the gradient of \(f(\theta, \phi)\) on S². The input tensor
inputsmust have two components corresponding to \((\theta, \phi)\).- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(\theta, \phi)\).
inputs (torch.Tensor) – Tensor of shape (…, 2) representing spherical coordinates \((\theta, \phi)\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The gradient on S², \(\nabla_{\mathbb{S}^2} f\).
- Return type:
torch.Tensor
- Raises:
ValueError: – If
inputsdoes not have two components.
- spherical_inr.diffops.s2_laplacian(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the Laplacian of a function defined on the 2-sphere (S²).
The Laplacian on S² is defined as the divergence of the gradient on the sphere:
\[\Delta_{\mathbb{S}^2} f = \nabla_{\mathbb{S}^2} \cdot \left(\nabla_{\mathbb{S}^2} f\right).\]For a function \(f(\theta,\phi)\) on S², the Laplacian expands to
\[\Delta_{\mathbb{S}^2} f = \frac{1}{\sin\theta}\frac{\partial}{\partial \theta}\Bigl(\sin\theta \frac{\partial f}{\partial \theta}\Bigr) \;+\; \frac{1}{\sin^2\theta}\frac{\partial^2 f}{\partial \phi^2}.\]This function computes the gradient of \(f(\theta,\phi)\) on S² and then evaluates its divergence to obtain the Laplacian.
- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(\theta,\phi)\).
inputs (torch.Tensor) – Tensor representing spherical coordinates \((\theta, \phi)\) on S².
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The Laplacian \(\Delta_{\mathbb{S}^2} f\) on the 2-sphere.
- Return type:
torch.Tensor
- spherical_inr.diffops.spherical_divergence(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the divergence of a vector field in spherical coordinates.
For a vector field \(\mathbf{F}(r,\theta,\phi) = \left[F_r,\, F_\theta,\, F_\phi\right]\), the divergence is given by
\[\nabla \cdot \mathbf{F} = \frac{1}{r^2}\frac{\partial}{\partial r}\Bigl(r^2 F_r\Bigr) \;+\; \frac{1}{r \sin\theta}\frac{\partial}{\partial \theta}\Bigl(\sin\theta F_\theta\Bigr) \;+\; \frac{1}{r \sin\theta}\frac{\partial F_\phi}{\partial \phi}.\]This function computes the divergence of the vector field in spherical coordinates. The tensor
outputsmust have three components.- Parameters:
outputs (torch.Tensor) – Tensor of shape (…, 3) representing the vector field in spherical coordinates.
inputs (torch.Tensor) – Tensor of shape (…, 3) representing spherical coordinates \([r, \theta, \phi]\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The divergence \(\nabla \cdot \mathbf{F}\) in spherical coordinates.
- Return type:
torch.Tensor
- Raises:
ValueError: – If
outputsdoes not have three components.
- spherical_inr.diffops.spherical_gradient(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the gradient of a function defined in spherical coordinates \((r, \theta, \phi)\).
For a function \(f(r, \theta, \phi)\), the spherical gradient is defined as
\[\nabla f = \hat{r}\,\frac{\partial f}{\partial r} \;+\; \hat{\theta}\,\frac{1}{r}\frac{\partial f}{\partial \theta} \;+\; \hat{\phi}\,\frac{1}{r\,\sin\theta}\frac{\partial f}{\partial \phi}.\]This function computes the gradient of \(f(r,\theta,\phi)\) with respect to the spherical coordinates. The input tensor
inputsmust have three components representing \([r, \theta, \phi]\).- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(r, \theta, \phi)\).
inputs (torch.Tensor) – Tensor of shape (…, 3) representing spherical coordinates \([r, \theta, \phi]\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
torch.Tensor
- Return type:
The spherical gradient with components scaled as above.
- Raises:
ValueError – If
inputsdoes not have three components.:
- spherical_inr.diffops.spherical_laplacian(outputs: Tensor, inputs: Tensor, track: bool = False) Tensor[source]#
Compute the Laplacian of a function defined in spherical coordinates \((r, \theta, \phi)\).
The Laplacian is computed as the divergence of the gradient:
\[\Delta f = \nabla \cdot \left(\nabla f\right).\]In spherical coordinates, the Laplacian of \(f(r,\theta,\phi)\) is expressed as
\[\Delta f = \frac{1}{r^2}\frac{\partial}{\partial r}\Bigl(r^2 \frac{\partial f}{\partial r}\Bigr) \;+\; \frac{1}{r^2 \sin\theta}\frac{\partial}{\partial \theta}\Bigl(\sin\theta \frac{\partial f}{\partial \theta}\Bigr) \;+\; \frac{1}{r^2 \sin^2\theta}\frac{\partial^2 f}{\partial \phi^2}.\]This function computes the gradient of \(f(r,\theta,\phi)\) in spherical coordinates and then its divergence to obtain the Laplacian.
- Parameters:
outputs (torch.Tensor) – Tensor representing the function values \(f(r,\theta,\phi)\).
inputs (torch.Tensor) – Tensor representing spherical coordinates \([r, \theta, \phi]\).
track (bool, optional) – If True, enables gradient tracking for higher-order derivatives. Default =
False
- Returns:
The Laplacian \(\Delta f\) in spherical coordinates.
- Return type:
torch.Tensor