4 ms·
It might be helpful to have some actual example code of what this looks like. In Python it would be def invert_2_by_2_matrix(matrix): # matrix is of for
by cochne 7y ago
It might be helpful to have some actual example code of what this looks like. In Python it would be
def invert_2_by_2_matrix(matrix):
# matrix is of form, for example ((1,2), (3,4))
((a, b), (c, d)) = matrix
det = (a*d - b*c)
return ((d/det, -b/det), (-c/det, a/det))
(This doesn't do any input checking including if the determinant is 0)
- SilasX 7y ago>This doesn't do any input checking including if the determinant is 0 Which seems to me like it assumes away a big part of the difficulty -- you'd need to follow up with: for this problem domain, what am I doing with the answer? How do I need to handle a singular matrix? Should do I throw an exception, and how should the caller handle it?
- PureParadigm 7y agoCompared to the library version: import numpy as np np.linalg.inv([[1,2],[3,4]]) and this does handle the case where the determinant is 0 (by throwing an error about it being a singular matrix).
- xaedes 7y agoAvoid np.linalg.inv if you can do it in another way.. It slows the already slow python even more. Recently replaced np.linalg.inv by an affine transformation inverse (what was really needed) in one of our codebases to get a speedup of 100x.