diff options
author | Anton Khirnov <anton@khirnov.net> | 2018-03-22 10:30:09 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2018-03-22 10:30:09 +0100 |
commit | f4d6e7a434db5967f9df67caf318326bd023f6c7 (patch) | |
tree | 05b195a5735a5b9adf7038b30025011d0d2a9c9f | |
parent | f55cb13c91ea247c595eb9e654a6997713787a0e (diff) |
Add pointwise matrix inversion.
-rw-r--r-- | utils.py | 21 |
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) + |