Last active
August 29, 2015 14:00
-
-
Save tkw1536/11084098 to your computer and use it in GitHub Desktop.
Matrix Aware divison
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 | |
# matrix-aware division | |
def mdiv(x, y): | |
# everything is float | |
x = 1.0*x | |
y = 1.0*y | |
# if y is a matrix, do matrix division | |
if isinstance(y, np.ndarray): | |
# turn x into an array of the right size if it isnt already | |
if not isinstance(x, np.ndarray): | |
# x time sidentity | |
x = x * np.identity(y.shape[0]) | |
# invert y | |
y = np.linalg.inv(y) | |
#dot with x | |
return y.dot(x) | |
# else do normal division | |
else: | |
return x / y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment