Skip to content

Instantly share code, notes, and snippets.

@rakhuba
Last active May 21, 2018 06:19
Show Gist options
  • Save rakhuba/8473f254b91888961d92030889073001 to your computer and use it in GitHub Desktop.
Save rakhuba/8473f254b91888961d92030889073001 to your computer and use it in GitHub Desktop.
def _reshape(a, size):
return a.reshape(size, order='f')
rx1, rx2, ry1, ry2, ra1, ra2, n, l, m = 40, 40, 40, 40, 100, 100, 8, 8, 8
x = np.ones((rx1, n, rx2))
y = np.ones((ry1, n, ry2))
A = np.ones((ra1, n, l, ra2))
interface_prev = np.ones((rx1, ry1*ra1**2))
# Variant 1: via reshape + transpose
Phi = _reshape(interface_prev, (rx1, ry1*ra1**2))
Phi = x.T.dot(Phi)
Phi = _reshape(Phi, (n*rx2*ry1*ra1, ra1))
Phi = Phi.T
Phi = _reshape(Phi, (ra1*n, rx2*ry1*ra1))
A_curr = _reshape(np.transpose(A, [0, 2, 1, 3]), (ra1*n, l*ra2))
Phi = A_curr.T.dot(Phi)
Phi = _reshape(Phi, (l, ra2*rx2*ry1*ra1))
Phi = Phi.T
Phi = _reshape(Phi, (ra2*rx2*ry1, ra1*l))
A_curr = _reshape(A, (ra1*l, m*ra2))
Phi = Phi.dot(A_curr)
Phi = _reshape(Phi, (ra2*rx2, ry1*m*ra2))
Phi = Phi.T
Phi = _reshape(Phi, (ry1*m, ra2**2*rx2))
Phi = Phi.T
y = _reshape(y, (ry1*m, ry2))
Phi = Phi.dot(y)
Phi = _reshape(Phi, (ra2**2, rx2*ry2))
Phi = Phi.T
Phi = _reshape(Phi, (rx2, ry2, ra2, ra2))
# Variant 2: via tensordot
interface_prev = np.ones((rx1, ry1, ra1, ra1))
x1 = _reshape(x, (rx1, n, rx2))
y1 = _reshape(y, (ry1, m, ry2))
Phi1 = np.tensordot(interface_prev, x1, axes=([0], [0])) #(ax, ay, mu1, mu2) (ax, n, bx) -> (ay, mu1, mu2, n, bx)
Phi1 = np.tensordot(Phi1, np.transpose(A, [0, 2, 1, 3]), axes=([1, 3], [0, 1])) # (ay, mu1, mu2, n, bx) (mu1, n, l, nu1) -> (ay, mu2, bx, l, nu1)
Phi1 = np.tensordot(Phi1, A, axes=([1, 3], [0, 1])) # (ay, mu2, bx, l, nu1) (mu2, l, m, nu2) -> (ay, bx, nu1, m, nu2)
Phi1 = np.tensordot(Phi1, y1, axes=([0, 3], [0, 1])) # (ay, bx, nu1, m, nu2) (ay, m, by) -> (bx, nu1, nu2, by)
Phi1 = np.transpose(Phi1, [0, 3, 2, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment