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]:
((5, 8), (5, 8))

In [8]:
yy[0]


Out[8]:
array([-0.33691821, -0.87299196,  0.43176834,  0.42599932, -0.45451356,
       -2.00948747, -0.47509179, -1.33467787])

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')


(2, 3, 4) (2, 2)
(3, 4, 2) (3, 3)
(4, 2, 3) (4, 4)
[[10.99962294 15.19661121  5.5063145   9.01771564 12.61538645  4.60233337
  24.87344961 35.0344141  12.82801743 15.39708439 21.49565226  7.8333187 ]
 [ 7.64995715  9.93058687  3.47091734  6.34768314  8.40817922  2.9744955
  17.62394488 23.59626105  8.39978261 10.81676928 14.28117798  5.04240786]]
Yt= Y[:, :, 1] A.T.dot(Yt).dot(B)

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')


[[10.99962294 15.19661121  5.5063145   9.01771564 12.61538645  4.60233337
  24.87344961 35.0344141  12.82801743 15.39708439 21.49565226  7.8333187 ]
 [ 7.64995715  9.93058687  3.47091734  6.34768314  8.40817922  2.9744955
  17.62394488 23.59626105  8.39978261 10.81676928 14.28117798  5.04240786]]
[[10.99962294 15.19661121  5.5063145   9.01771564 12.61538645  4.60233337
  24.87344961 35.0344141  12.82801743 15.39708439 21.49565226  7.8333187 ]
 [ 7.64995715  9.93058687  3.47091734  6.34768314  8.40817922  2.9744955
  17.62394488 23.59626105  8.39978261 10.81676928 14.28117798  5.04240786]]

In [65]:
np.tensordot(B.T, Y, axes = [[0], [1]])


Out[65]:
array([[[ 6.56048368,  8.07175543,  9.58302718, 11.09429894],
        [24.69574469, 26.20701644, 27.71828819, 29.22955994]],

       [[ 1.99985765,  2.52151716,  3.04317666,  3.56483616],
        [ 8.2597717 ,  8.7814312 ,  9.3030907 ,  9.82475021]],

       [[ 3.01868856,  4.16381732,  5.30894608,  6.45407484],
        [16.76023367, 17.90536243, 19.05049118, 20.19561994]]])

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)]


[2, 0]
[0, 1]

In [82]:
Sa = np.random.rand(shape[0])
Sb = np.random.rand(shape[1])
Sc = np.random.rand(shape[2])
Sa = np.ones(shape[0]) Sb = np.ones(shape[1]) Sc = np.ones(shape[2])
Sa = np.zeros(shape[0]) Sb = np.zeros(shape[1]) Sc = np.ones(shape[2]) Sb[1] = 1 #Sc[2] = 1

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]:
array([[1795.39979674, 1481.66099104],
       [1481.66099104, 1224.3365629 ]])

In [87]:
tmp = np.tensordot(A, Y_, axes = 1)
print 0.5*np.tensordot(tmp*Sb, tmp.T, axes = 1)


[[1795.39979674 1481.66099104]
 [1481.66099104 1224.3365629 ]]

In [88]:
tmp = A.dot(Y.reshape((shape[0], -1), order = 'F'))
.5*(tmp*np.kron(Sb, Sc)).dot(tmp.T)


Out[88]:
array([[72406.11014877, 58899.66892788],
       [58899.66892788, 47987.84306244]])

In [89]:
tmp


Out[89]:
array([[ 7.82123566, 13.84427622, 19.86731677,  9.3269958 , 15.35003636,
        21.37307691, 10.83275594, 16.85579649, 22.87883705, 12.33851608,
        18.36155663, 24.38459719],
       [ 7.73576313, 11.92925834, 16.12275356,  8.78413694, 12.97763215,
        17.17112736,  9.83251074, 14.02600595, 18.21950116, 10.88088454,
        15.07437975, 19.26787497]])
print Sb print Sc print np.outer(Sb, Sc).flatten() print np.kron(Sb,Sc)

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]]))


[[86830.02339022 69902.64862187]
 [69902.64862187 56317.28258906]]

In [91]:
j,k,l = 1,0,0
tmp[j,k,l]


Out[91]:
7.7357631325050455

In [92]:
np.sum(A[j,:]*Y[:, k,l])


Out[92]:
7.7357631325050455

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]


71.57555006981994
71.57555006981994

In [95]:
AYtSt.shape


Out[95]:
(2, 12)

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]


52.70482161372769
52.70482161372769

In [97]:
AYtStYtA = AYtSt.dot(A.dot(Y.reshape((shape[0], -1), order = 'F')).T)

In [98]:
Sb, Sc


Out[98]:
(array([1, 4, 7]), array([ 1,  6, 11, 16]))

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


95975.68612487777

1 0 0 0 	0.0 1 1
2 0 0 1 	2.6079262388206184 1 6
3 0 0 2 	10.431704955282473 4 1
4 0 0 3 	0.16299538992628865 4 6
5 0 0 4 	4.074884748157216 7 1
6 0 0 5 	13.20262658402938 7 6
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-99-e4e88d90e0fe> in <module>()
      9         for l in xrange(Yt.shape[1]):
     10 
---> 11             x = A[m, i]*A[n,j]*Yt[i,l]*Yt[j,l]*Sb[l/2]*Sc[l%2]
     12             counter+=1
     13             print counter,i,j,l,'\t',A[m, i]*A[n,j]*Yt[i,l]*Yt[j,l], Sb[l/2],Sc[l%2]

IndexError: index 3 is out of bounds for axis 0 with size 3

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]:
((2, 2), (3, 3), (4, 4), (4, 3, 2))

In [139]:
print np.tensordot(A, Y, axes = 1).shape
np.tensordot(B, np.tensordot(A, Y, axes = 1), axes =[[-1],[1]]).shape


(2, 3, 4)
Out[139]:
(3, 2, 4)

In [130]:
np.tensordot(A, np.tensordot(B, np.tensordot(Y, C, axes = 1), axes = [[-1], [1]]), axes=[[-1],[1]] ).flatten(order='F')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-130-e0e7312c393e> in <module>()
----> 1 np.tensordot(A, np.tensordot(B, np.tensordot(Y, C, axes = 1), axes =1), axes=[[-1],[1]] ).flatten(order='F')

/u/ki/swmclau2/.conda/envs/hodemulator/lib/python2.7/site-packages/numpy/core/numeric.pyc in tensordot(a, b, axes)
   1369                 axes_b[k] += ndb
   1370     if not equal:
-> 1371         raise ValueError("shape-mismatch for sum")
   1372 
   1373     # Move the axes to sum over to the end of "a"

ValueError: shape-mismatch for sum

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)


(2, 3, 4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-100-859115352d1e> in <module>()
      3 # TODO reversed order?
      4 for u in [B,A]:
----> 5     Y_ = np.tensordot(u, Y_.T, axes=1)

/u/ki/swmclau2/.conda/envs/hodemulator/lib/python2.7/site-packages/numpy/core/numeric.pyc in tensordot(a, b, axes)
   1369                 axes_b[k] += ndb
   1370     if not equal:
-> 1371         raise ValueError("shape-mismatch for sum")
   1372 
   1373     # Move the axes to sum over to the end of "a"

ValueError: shape-mismatch for sum

In [145]:
reduce( np.kron, reversed([Sa, Sb, Sc])  )


Out[145]:
array([  1,   2,   4,   8,   7,  14,   6,  12,  24,  48,  42,  84,  11,
        22,  44,  88,  77, 154,  16,  32,  64, 128, 112, 224])

In [ ]: