Created
June 3, 2014 11:55
-
-
Save DomNomNom/98dc0bf282bc6af52f61 to your computer and use it in GitHub Desktop.
Lots of documentation for a one-liner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
''' | |
Solves matrix M in the following equation given matricies A and B: | |
MA = B | |
A.shape == B.shape == (m, n) | |
M.shape = (n, n) | |
How it works when n=3: | |
M A = B | |
| m11 m12 m13 | | a1.x a2.x a3.x a4.x ... | | b1.x b2.x b3.x b4.x ... | | |
| m21 m22 m23 | | a1.y a2.y a3.y a4.y ... | = | b1.y b2.y b3.y b4.y ... | | |
| m31 m32 m33 | | a1.z a2.z a3.z a4.z ... | | b1.z b2.z b3.z b4.z ... | | |
m11*a1.x + m12*a1.y + m13*a1.z = b1.x | |
m21*a1.x + m22*a1.y + m23*a1.z = b1.y | |
m31*a1.x + m32*a1.y + m33*a1.z = b1.z | |
m11*a2.x + m12*a2.y + m13*a2.z = b2.x | |
m21*a2.x + m22*a2.y + m23*a2.z = b2.y | |
m31*a2.x + m32*a2.y + m33*a2.z = b2.z | |
| a1.x a1.y a1.z 0 0 0 0 0 0 | | m11 | | b1.x | | |
| 0 0 0 a1.x a1.y a1.z 0 0 0 | | m12 | | b1.y | | |
| 0 0 0 0 0 0 a1.x a1.y a1.z | | m13 | | b1.z | | |
| a2.x a2.y a2.z 0 0 0 0 0 0 | | m21 | | b2.x | | |
| 0 0 0 a2.x a2.y a2.z 0 0 0 | | m22 | = | b2.y | | |
| 0 0 0 0 0 0 a2.x a2.y a2.z | | m23 | | b2.z | | |
| a3.x a3.y a3.z 0 0 0 0 0 0 | | m31 | | b3.x | | |
| 0 0 0 a3.x a3.y a3.z 0 0 0 | | m32 | | b3.y | | |
| 0 0 0 0 0 0 a3.x a3.y a3.z | | m33 | | b3.z | | |
| a4.x a4.y a4.z 0 0 0 0 0 0 | | b4.x | | |
| 0 0 0 a4.x a4.y a4.z 0 0 0 | | b4.y | | |
| 0 0 0 0 0 0 a4.x a4.y a4.z | | b4.z | | |
| . | | . | | |
| . | | . | | |
| . | | . | | |
| a1.x a1.y a1.z 0 0 0 0 0 0 | | m11 | | b1.x | | |
| a2.x a2.y a2.z 0 0 0 0 0 0 | | m12 | | b2.x | | |
| a3.x a3.y a3.z 0 0 0 0 0 0 | | m13 | | b3.x | | |
| a4.x a4.y a4.z 0 0 0 0 0 0 | | m21 | | b4.x | | |
| . | | m23 | | . | | |
| . | | m31 | = | . | | |
| . | | m32 | | . | | |
| 0 0 0 a1.x a1.y a1.z 0 0 0 | | m33 | | b1.y | | |
| 0 0 0 a2.x a2.y a2.z 0 0 0 | | b2.y | | |
| 0 0 0 a3.x a3.y a3.z 0 0 0 | | b3.y | | |
| 0 0 0 a4.x a4.y a4.z 0 0 0 | | b4.y | | |
| . | | . | | |
| . | | . | | |
| . | | . | | |
| 0 0 0 0 0 0 a1.x a1.y a1.z | | b1.z | | |
| 0 0 0 0 0 0 a2.x a2.y a2.z | | b2.z | | |
| 0 0 0 0 0 0 a3.x a3.y a3.z | | b3.z | | |
| 0 0 0 0 0 0 a4.x a4.y a4.z | | b4.z | | |
| . | | . | | |
| . | | . | | |
| . | | . | | |
| a1.x a1.y a1.z | | |
| a2.x a2.y a2.z | | |
AT = A transposed = | a3.x a3.y a3.z | | |
| a4.x a4.y a4.z | | |
| . | | |
| . | | |
| . | | |
| m11 | | |
AT | m12 | = B.x | |
| m13 | | |
| m21 | | |
AT | m22 | = B.y | |
| m23 | | |
| m31 | | |
AT | m32 | = B.z | |
| m33 | | |
We can solve these as they are just Ax=b linear equations | |
''' | |
def solveMatrixMult(A, B): | |
return np.array([ np.linalg.lstsq(A.T, b)[0] for b in B ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment