summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/utils.py b/utils.py
index 7440919..f0ba2f4 100644
--- a/utils.py
+++ b/utils.py
@@ -19,6 +19,24 @@ def matrix_invert(mat):
inv = np.linalg.inv(mat)
return inv.transpose((1, 2, 0)).reshape(oldshape)
+def matrix_det(mat):
+ """
+ Pointwise matrix determinant.
+
+ Given an array that stores many square matrices (e.g. the values of the
+ metric tensor at spacetime points), compute the array of corresponding
+ determinants.
+
+ :param array_like mat: N-D array with N>2 and each mat[i0, ..., :, :] a
+ square matrix.
+ :return: Array of determinants.
+ """
+ oldshape = mat.shape
+ newshape = oldshape[:2] + (np.product(mat.shape[2:]),)
+
+ mat = mat.reshape(newshape).transpose((2, 0, 1))
+ return np.linalg.det(mat).reshape(oldshape[2:])
+
def array_reflect(data, parity = 1.0, axis = -1):
"""
Reflect an N-D array with respect to the specified axis. E.g. input