differential_geometry.symbolic.invert_metric#

differential_geometry.symbolic.invert_metric(metric: ImmutableDenseMatrix | MutableDenseMatrix | ImmutableDenseNDimArray | MutableDenseNDimArray) MutableDenseMatrix[source]#

Compute the inverse of the metric \(g_{\mu \nu}\).

Parameters:

metric (MutableDenseMatrix or MutableDenseNDimArray) – The metric to invert. If the metric is provided as a full matrix, it will be returned as a full matrix. If it is provided as a single array (1D), it is assumed to be a diagonal metric and the returned inverse is also diagnonal and returned as an array.

Returns:

The inverted metric.

Return type:

MutableDenseMatrix or MutableDenseNDimArray

Examples

To invert the metric for spherical coordinates, one need only do the following:

>>> from pymetric.differential_geometry.symbolic import invert_metric
>>> import sympy as sp
>>>
>>> # Construct the symbols and the metric to
>>> # pass into the function.
>>> r,theta,phi = sp.symbols('r,theta,phi')
>>> metric = sp.Matrix([
...     [1,0,0],
...     [0,r**2,0],
...     [0,0,(r*sp.sin(theta))**2]
...     ])
>>>
>>> # Now compute the inverse metric.
>>> print(invert_metric(metric))
Matrix([[1, 0, 0], [0, r**(-2), 0], [0, 0, 1/(r**2*sin(theta)**2)]])

We can also invert a metric which is diagonal by simply pushing through the diagonal values as an array:

>>> from pymetric.differential_geometry.symbolic import invert_metric
>>> import sympy as sp
>>>
>>> # Construct the symbols and the metric to
>>> # pass into the function.
>>> r,theta,phi = sp.symbols('r,theta,phi')
>>> metric = sp.Array([1,r**2,(r*sp.sin(theta))**2])
>>>
>>> # Now compute the inverse metric.
>>> print(invert_metric(metric))
[1, r**(-2), 1/(r**2*sin(theta)**2)]