differential_geometry.dense_utils.dense_compute_tensor_trace#
- differential_geometry.dense_utils.dense_compute_tensor_trace(tensor_field: ndarray, indices: Tuple[int, int], tensor_signature: ndarray, metric_field: ndarray | None = None, inverse_metric_field: ndarray | None = None, **kwargs) ndarray [source]#
Compute the trace over a pair of tensor indices, adjusting their variances if needed.
This function traces over two tensor slots in a tensor field by summing over their diagonal components. If the indices are both covariant or both contravariant, the appropriate metric (or inverse metric) is used to first raise or lower one of them to enable contraction.
- Parameters:
tensor_field (
numpy.ndarray
) – Tensor field of shape(..., I₁, ..., I_r)
, where the last r axes represent tensor indices.indices (
Tuple[int
,int]
) – The pair of indices to trace over. Indices must be distinct and in the range [0, rank).tensor_signature (
numpy.ndarray
) – Array of shape (rank,) with +1 for contravariant indices and -1 for covariant ones.metric_field (
numpy.ndarray
, optional) – Metric tensor to lower contravariant indices. Required if both traced indices are contravariant.inverse_metric_field (
numpy.ndarray
, optional) – Inverse metric tensor to raise covariant indices. Required if both traced indices are covariant.**kwargs – Additional keyword arguments passed to the low-level numpy.trace call.
- Returns:
Tensor field with the two specified indices traced over. Output rank is rank - 2.
- Return type:
- Raises:
ValueError – If input validation fails or required metrics are not provided.
Examples
>>> import numpy as np >>> from pymetric.differential_geometry.dense_utils import compute_tensor_trace >>> >>> T = np.eye(3)[None, None, :, :] # Rank-2 (1,1)-tensor over a 1x1 grid >>> sig = np.array([+1, -1]) >>> compute_tensor_trace(T, indices=(0, 1), tensor_signature=sig) array([[3.]])