Skip to content

Instantly share code, notes, and snippets.

@onriv
Created March 21, 2014 14:26
Show Gist options
  • Save onriv/9687345 to your computer and use it in GitHub Desktop.
Save onriv/9687345 to your computer and use it in GitHub Desktop.
math:KroneckerProduct
# OLS_4 = [[(0, 0), (2, 2), (3, 3), (1, 1)],
# [(2, 3), (0, 1), (1, 0), (3, 2)],
# [(3, 1), (1, 3), (0, 2), (2, 0)],
# [(1, 2), (3, 0), (2, 1), (0, 3)]]
# OLS_5 = [[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)],
# [(1, 2), (2, 3), (3, 4), (4, 0), (0, 1)],
# [(2, 4), (3, 0), (4, 1), (0, 2), (1, 3)],
# [(3, 1), (4, 2), (0, 3), (1, 4), (2, 0)],
# [(4, 3), (0, 4), (1, 0), (2, 1), (3, 2)]]
def KroneckerProduct(A,B):
m = len(A)
n = len(A[1])
p = len(B)
q = len(B[1])
AotimesB = [[0 for j in xrange(n * q)] for i in xrange(m * p)]
for i in xrange(m * p):
for j in xrange(n * q):
AotimesB[i][j] = (A[i // p][j // q], B[i % p][j % q])
return AotimesB
if __name__ == '__main__':
# A = [[1,2,3],
# [4,5,6]]
# B = [[7,8],
# [9,10],
# [11,12]]
OLS_4 = [[(0, 0), (2, 2), (3, 3), (1, 1)],
[(2, 3), (0, 1), (1, 0), (3, 2)],
[(3, 1), (1, 3), (0, 2), (2, 0)],
[(1, 2), (3, 0), (2, 1), (0, 3)]]
OLS_5 = [[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)],
[(1, 2), (2, 3), (3, 4), (4, 0), (0, 1)],
[(2, 4), (3, 0), (4, 1), (0, 2), (1, 3)],
[(3, 1), (4, 2), (0, 3), (1, 4), (2, 0)],
[(4, 3), (0, 4), (1, 0), (2, 1), (3, 2)]]
OLS_4a = [[0, 2, 3, 1],
[2, 0, 1, 3],
[3, 1, 0, 2],
[1, 3, 2, 0]]
OLS_4b = [[0, 2, 3, 1],
[3, 1, 0, 2],
[1, 3, 2, 0],
[2, 0, 1, 3]]
OLS_5a = [[0, 1, 2, 3, 4],
[1, 2, 3, 4, 0],
[2, 3, 4, 0, 1],
[3, 4, 0, 1, 2],
[4, 0, 1, 2, 3]]
OLS_5b = [[0, 1, 2, 3, 4],
[2, 3, 4, 0, 1],
[4, 0, 1, 2, 3],
[1, 2, 3, 4, 0],
[3, 4, 0, 1, 2]]
OLS_20a = KroneckerProduct(OLS_4a,OLS_5a)
OLS_20b = KroneckerProduct(OLS_4b,OLS_5b)
OLS_20 = [[0 for j in xrange(20)] for i in xrange(20)]
for i in xrange(20):
for j in xrange(20):
OLS_20a[i][j] = 5 * OLS_20a[i][j][0] + OLS_20a[i][j][1]
OLS_20b[i][j] = 5 * OLS_20b[i][j][0] + OLS_20b[i][j][1]
OLS_20[i][j] = (OLS_20a[i][j],OLS_20b[i][j])
# for a in OLS_20a:
# print a
# for b in OLS_20b:
# print b
for a in OLS_20:
print a
OLS_20_test = set({})
for a in OLS_20:
OLS_20_test |= set(a)
assert(len(OLS_20_test) == 400)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment