summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2018-03-22 10:30:09 +0100
committerAnton Khirnov <anton@khirnov.net>2018-03-22 10:30:09 +0100
commitf4d6e7a434db5967f9df67caf318326bd023f6c7 (patch)
tree05b195a5735a5b9adf7038b30025011d0d2a9c9f /utils.py
parentf55cb13c91ea247c595eb9e654a6997713787a0e (diff)
Add pointwise matrix inversion.
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/utils.py b/utils.py
new file mode 100644
index 0000000..b225bbe
--- /dev/null
+++ b/utils.py
@@ -0,0 +1,21 @@
+import numpy as np
+
+def matrix_invert(mat):
+ """
+ Pointwise matrix inversion.
+
+ Given an array that stores many square matrices (e.g. the values of the
+ metric tensor at spacetime points), compute the array of corresponding
+ inverses.
+
+ :param array_like mat: N-D array with N>2 and each mat[i0, ..., :, :] a
+ square matrix to be inverted.
+ :return: Array (same shape as mat) of inverses.
+ """
+ oldshape = mat.shape
+ newshape = oldshape[:2] + (np.product(mat.shape[2:]),)
+
+ mat = mat.reshape(newshape).transpose((2, 0, 1))
+ inv = np.linalg.inv(mat)
+ return inv.transpose((1, 2, 0)).reshape(oldshape)
+