differential_geometry.symbolic.compute_gradient#
- differential_geometry.symbolic.compute_gradient(scalar_field: Basic, coordinate_axes: Sequence[Symbol], basis: Literal['unit', 'covariant', 'contravariant'] = 'covariant', inverse_metric: ImmutableDenseMatrix | MutableDenseMatrix | ImmutableDenseNDimArray | MutableDenseNDimArray | None = None) MutableDenseNDimArray | ImmutableDenseNDimArray | ImmutableDenseMatrix | MutableDenseMatrix [source]#
Compute the symbolic gradient of a scalar field \(\phi\) in either covariant or contravariant basis.
- Parameters:
scalar_field (
Basic
) – The scalar field \(\phi\) to differentiate. This should be any valid sympy expression dependent on thecoordinate_axes
and any other relevant symbols.coordinate_axes (
list
ofSymbol
) – The coordinate axes (variables) with respect to which to compute the gradient. This should be the full list of the coordinate axes for the relevant coordinate system.basis (
'covariant'
or'contravariant'
, optional) –The basis in which to return the gradient. Defaults to ‘covariant’.
Note
if
basis != 'covariant'
, the index must be raised and theinverse_metric
will be used for contraction. Ifinverse_metric
is not specified, an error results.inverse_metric (
MutableDenseMatrix
orMutableDenseNDimArray
) – Either a full inverse metric \(g^{\mu\nu}\) (shape(n, n)
) or a 1D diagonal array of shape(n,)
for orthogonal coordinates.
- Returns:
The components of the gradient of \(\phi\), in the chosen basis.
- Return type:
See also
Examples
Compute the gradient of the scalar field \(\phi(r,\theta) = r^2 \sin(\theta)\).
>>> # Import the necessary functions. >>> import sympy as sp >>> from pymetric.differential_geometry.symbolic import compute_gradient >>> >>> # Create the symbols. >>> r, theta, phi = sp.symbols('r theta phi') >>> Phi = (r**2)*sp.sin(theta) >>> inv_metric = sp.Matrix([[1,0,0],[0,r**2,0],[0,0,r**2*sp.sin(theta)]]).inv() >>> >>> # Compute the covariant gradient. >>> compute_gradient(Phi, [r,theta, phi]) [2*r*sin(theta), r**2*cos(theta), 0] >>> >>> # Compute the contravariant gradient. >>> compute_gradient(Phi, [r, theta, phi],basis='contravariant',inverse_metric=inv_metric) [2*r*sin(theta), cos(theta), 0]