Skip to content

Commit

Permalink
Added left and right matrix pseudo-inverse calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
timbernat committed Apr 19, 2024
1 parent 70ddf1e commit 712cca6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions polymerist/maths/linearalg/decomposition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'''Tools for matric decomposition'''
'''Tools for matrix decomposition'''

import numpy as np
from ...genutils.typetools import Shape, DType, N, M


def diagonalize(matrix : np.ndarray) -> tuple[np.ndarray]:
'''Diagonalize a matrix into it's eigenbasis. Return rotation and diagonal matrices P, D, and P^-1'''
Expand All @@ -9,4 +11,12 @@ def diagonalize(matrix : np.ndarray) -> tuple[np.ndarray]:
D = np.diag(eivals)
Pinv = np.linalg.inv(eivecs) # compute inverse matrix to rotate back

return P, D, Pinv
return P, D, Pinv

def inv_left(matrix : np.ndarray[Shape[M, N], DType]) -> np.ndarray[Shape[N, M], DType]:
'''Return the left-inverse of an arbitrary (not necessarily square) matrix'''
return np.linalg.inv(matrix.T @ matrix) @ matrix.T

def inv_right(matrix : np.ndarray[Shape[M, N], DType]) -> np.ndarray[Shape[N, M], DType]:
'''Return the right-inverse of an arbitrary (not necessarily square) matrix'''
return matrix.T @ np.linalg.inv(matrix @ matrix.T)

0 comments on commit 712cca6

Please sign in to comment.