Fixed linalg for general matricies

This commit is contained in:
Alan Saul 2015-04-30 14:22:46 +01:00
parent 2dcfabf300
commit 16e6d39317
2 changed files with 8 additions and 7 deletions

View file

@ -53,7 +53,8 @@ class LinalgTests(np.testing.TestCase):
def test_einsum_ijk_ljk_to_ilk(self): def test_einsum_ijk_ljk_to_ilk(self):
A = np.random.randn(150, 20, 5) A = np.random.randn(150, 20, 5)
B = np.random.randn(20, 30, 5) B = np.random.randn(150, 20, 5)
pure = np.einsum('ijk,jlk->il', A, B) #B = A.copy()
quick = GPy.util.linalg.ijk_jlk_to_il(A,B) pure = np.einsum('ijk,ljk->ilk', A, B)
quick = GPy.util.linalg.ijk_ljk_to_ilk(A,B)
np.testing.assert_allclose(pure, quick) np.testing.assert_allclose(pure, quick)

View file

@ -463,7 +463,7 @@ def ijk_jlk_to_il(A, B):
Faster version of einsum einsum('ijk,jlk->il', A,B) Faster version of einsum einsum('ijk,jlk->il', A,B)
""" """
res = np.zeros((A.shape[0], B.shape[1])) res = np.zeros((A.shape[0], B.shape[1]))
[np.add(np.dot(A[:,:,k], B[:,:,k]), res, res) for k in range(B.shape[-1])] [np.add(np.dot(A[:,:,k], B[:,:,k]), res, out=res) for k in range(B.shape[-1])]
return res return res
def ijk_ljk_to_ilk(A, B): def ijk_ljk_to_ilk(A, B):
@ -472,7 +472,7 @@ def ijk_ljk_to_ilk(A, B):
I.e A.dot(B.T) for every dimension I.e A.dot(B.T) for every dimension
""" """
res = np.empty((A.shape[0], B.shape[0], A.shape[-1])) res = np.zeros((A.shape[-1], A.shape[0], B.shape[0]))
[np.dot(A[:,:,i], B[:,:,i].T, res[i,:,:]) for i in range(A.shape[0])] [np.dot(A[:,:,i], B[:,:,i].T, out=res[i,:,:]) for i in range(A.shape[-1])]
res = res.swapaxes(0,2) res = res.swapaxes(0, 2).swapaxes(0,1)
return res return res