Skip to content

Instantly share code, notes, and snippets.

@tkw1536
Last active August 29, 2015 14:00
Show Gist options
  • Save tkw1536/11084098 to your computer and use it in GitHub Desktop.
Save tkw1536/11084098 to your computer and use it in GitHub Desktop.
Matrix Aware divison
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