In [1]:
import numpy as np
In [2]:
N1, N2 = 5, 4
In [3]:
X1 = np.random.randn(N1, 1)
X2 = np.random.randn(N2, 2)
In [5]:
yy, xx = np.meshgrid(X2,X1)
In [7]:
yy.shape, xx.shape
Out[7]:
In [8]:
yy[0]
Out[8]:
In [25]:
shape = (2,3,4)
Y = np.arange(np.prod(shape)).reshape(shape)
#Y = np.ones(shape)
#Y[0,1] = -1
A = np.ones((shape[0], shape[0])) B = np.ones((shape[1], shape[1])) C = np.ones((shape[2], shape[2]))
In [26]:
A = np.random.rand(shape[0], shape[0])
B = np.random.rand(shape[1], shape[1])
C = np.random.rand(shape[2], shape[2])
In [ ]:
In [63]:
Y_list = [Y]
Y_list.extend([A,B,C])
def foo(x,y):
print x.shape, y.shape
return np.tensordot(x, y.T, axes =[[0],[1]])
Y_ = reduce(foo , Y_list)
print Y_.reshape((shape[0], -1), order = 'F')
In [61]:
Yt= Y.reshape((shape[0], -1), order = 'F')
print A.T.dot(Yt).dot(np.kron(C,B)).reshape((shape[0], -1), order ='F')
Yt= Y.reshape((shape[0]*shape[1], -1), order = 'F')
print np.kron(B,A).T.dot(Yt).dot(C).reshape((shape[0], -1), order = 'F')
In [65]:
np.tensordot(B.T, Y, axes = [[0], [1]])
Out[65]:
In [73]:
dims = 3
i = 1
print [k for k in xrange(dims-1, -1, -1) if k!=i]
print [j for j in xrange(dims - 1)]
In [82]:
Sa = np.random.rand(shape[0])
Sb = np.random.rand(shape[1])
Sc = np.random.rand(shape[2])
In [83]:
Sa = np.arange(shape[0]) +1
Sb = np.arange(shape[1])*3 +1
Sc = np.arange(shape[2])*5 +1
In [84]:
Y_ = Y[:,:,0]
In [85]:
tmp = A.dot(Y_)
dL_dK1 = .5*(tmp*Sb).dot(tmp.T)
In [86]:
dL_dK1
Out[86]:
In [87]:
tmp = np.tensordot(A, Y_, axes = 1)
print 0.5*np.tensordot(tmp*Sb, tmp.T, axes = 1)
In [88]:
tmp = A.dot(Y.reshape((shape[0], -1), order = 'F'))
.5*(tmp*np.kron(Sb, Sc)).dot(tmp.T)
Out[88]:
In [89]:
tmp
Out[89]:
In [90]:
tmp = np.tensordot(A, Y, axes = 1)
tmps = tmp*np.outer(Sb, Sc)
print (0.5*np.tensordot(tmps, tmp.T, axes = [[2,1],[0,1]]))
In [91]:
j,k,l = 1,0,0
tmp[j,k,l]
Out[91]:
In [92]:
np.sum(A[j,:]*Y[:, k,l])
Out[92]:
In [93]:
AYtSt = A.dot(Y.reshape((shape[0], -1), order = 'F'))*np.kron(Sb, Sc)
AYS = np.tensordot(A, Y, axes = 1)*np.outer(Sb, Sc)
In [94]:
j,k = 1,1
print AYtSt[j,k]#,l]
print np.sum(A[j,:]* (Y.reshape((shape[0], -1), order = 'F'))[:, k])*Sb[k/2]*Sc[k%2]
In [95]:
AYtSt.shape
Out[95]:
In [96]:
j,k_,l = 1, k/2, k%2
print AYS[j,k_, l]
print (np.sum(A[j,:]* Y[:, k_, l]))*Sb[k_]*Sc[l]
In [97]:
AYtStYtA = AYtSt.dot(A.dot(Y.reshape((shape[0], -1), order = 'F')).T)
In [98]:
Sb, Sc
Out[98]:
In [99]:
m,n = 1, 1
print AYtStYtA[m,n]
print
Yt= Y.reshape((shape[0], -1), order = 'F')
o=0.0
counter = 0
for i in xrange(A.shape[0]):
for j in xrange(A.shape[0]):
for l in xrange(Yt.shape[1]):
x = A[m, i]*A[n,j]*Yt[i,l]*Yt[j,l]*Sb[l/2]*Sc[l%2]
counter+=1
print counter,i,j,l,'\t',A[m, i]*A[n,j]*Yt[i,l]*Yt[j,l], Sb[l/2],Sc[l%2]
o+= x
print
print o
In [ ]:
tmp = np.tensordot(A, Y, axes = 1)
tmps = tmp*np.outer(Sb, Sc)
AYSYA = (np.tensordot(tmps, tmp.T, axes = [[2,1],[0,1]]))
In [ ]:
tmp = np.tensordot(B, Y, axes = [[1], [1]])
tmps = tmp*np.outer(Sa, Sc)
AYSYA = (np.tensordot(tmps, tmp.T, axes = 2))
In [ ]:
AYSYA.shape
In [ ]:
reduce(np.multiply.outer, [Sa, Sb, Sc]).shape
In [ ]:
m,n = 1,1
print AYtStYtA[m,n]
print AYSYA[m,n]
print
out=0.0
counter = 0
for i in xrange(A.shape[0]):
for j in xrange(A.shape[0]):
for l in xrange(Y.shape[1]):
for o in xrange(Y.shape[2]):
x = A[m, i]*A[n,j]*Y[i,l,o]*Y[j,l,o]*Sb[l]*Sc[o]
counter+=1
print counter,i,j,l,o,'\t',A[m, i]*A[n,j]*Y[i,l,o]*Y[j,l,o], Sb[l],Sc[o]
out+= x
print
print out
In [ ]:
Yt = Y.reshape((shape[0], -1), order = 'F')
In [ ]:
for l in xrange(Yt.shape[1]):
print Yt[0,l], Sb[l/2], Sc[l%2]
print
for l in xrange(Y.shape[1]):
for o in xrange(Y.shape[2]):
print Y[0,l,o], Sb[l], Sc[o]
In [ ]:
AYSYA
In [ ]:
AYtStYtA
In [ ]:
Yt.shape, Sb.shape
In [ ]:
Yt = Y[:,:,0]
tmp = np.dot(Yt, Sb)
print tmp.shape
-0.5*(A*tmp).dot(A.T)
In [ ]:
S = reduce(np.multiply.outer, [Sb, Sc])
In [ ]:
Y.shape, S.shape
In [ ]:
tmp = np.tensordot(Y, S.T, axes = [[2,1],[0,1]])
np.dot(A*tmp, A.T)
In [ ]:
dims = 3
axes = [[i for i in xrange(dims-1, 0, -1)], [j for j in xrange(dims-1)]]
In [ ]:
axes
In [135]:
A.shape, B.shape, C.shape, Y.T.shape
Out[135]:
In [139]:
print np.tensordot(A, Y, axes = 1).shape
np.tensordot(B, np.tensordot(A, Y, axes = 1), axes =[[-1],[1]]).shape
Out[139]:
In [130]:
np.tensordot(A, np.tensordot(B, np.tensordot(Y, C, axes = 1), axes = [[-1], [1]]), axes=[[-1],[1]] ).flatten(order='F')
In [100]:
Y_ = np.tensordot(A, Y, axes=1)
print Y_.shape
# TODO reversed order?
for u in [B,A]:
Y_ = np.tensordot(u, Y_.T, axes=1)
In [145]:
reduce( np.kron, reversed([Sa, Sb, Sc]) )
Out[145]:
In [ ]: