--- /dev/null
+from remapyield import iterate_indices
+
+def matrix_demo():
+ X = [[1, 2, 3],
+ [3, 4, 5],
+ ]
+ Y = [[6, 7],
+ [8, 9],
+ [10, 11],
+ ]
+ # 3x3 matrix
+ X = [[12,7,3],
+ [4 ,5,6],
+ [7 ,8,9]]
+ # 3x4 matrix
+ Y = [[5,8,1,2],
+ [6,7,3,0],
+ [4,5,9,1]]
+ xdim1 = len(X[0])
+ ydim1 = len(X)
+ xdim2 = len(Y[0])
+ ydim2 = len(Y)
+ # set up result matrix of correct size
+ result = []
+ for _ in range(ydim1):
+ result.append([0]*xdim2)
+ # iterate through rows of X
+ for i in range(len(X)):
+ # iterate through columns of Y
+ for j in range(len(Y[0])):
+ # iterate through rows of Y
+ for k in range(len(Y)):
+ result[i][j] += X[i][k] * Y[k][j]
+ for r in result:
+ print (r)
+
+if __name__ == '__main__':
+ matrix_demo()
# a "yield" version of the REMAP algorithm. a little easier to read
# than the Finite State Machine version
-# set the dimension sizes here
-xdim = 3
-ydim = 2
-zdim = 1
-
-# set total (can repeat, e.g. VL=x*y*z*4)
-VL = xdim * ydim * zdim
-
-lims = [xdim, ydim, zdim]
-idxs = [0,0,0] # starting indices
-order = [1,0,2] # experiment with different permutations, here
-offset = 0 # experiment with different offset, here
-invxyz = [0,1,0] # inversion if desired
-
# python "yield" can be iterated. use this to make it clear how
# the indices are generated by using natural-looking nested loops
-def iterate_indices():
+def iterate_indices(SVSHAPE):
# get indices to iterate over, in the required order
- xd = lims[order[2]]
- yd = lims[order[1]]
- zd = lims[order[0]]
+ xd = SVSHAPE.lims[SVSHAPE.order[2]]
+ yd = SVSHAPE.lims[SVSHAPE.order[1]]
+ zd = SVSHAPE.lims[SVSHAPE.order[0]]
# create lists of indices to iterate over in each dimension
x_r = list(range(xd))
y_r = list(range(yd))
z_r = list(range(zd))
# invert the indices if needed
- if invxyz[order[2]]: x_r.reverse()
- if invxyz[order[1]]: y_r.reverse()
- if invxyz[order[0]]: z_r.reverse()
+ if SVSHAPE.invxyz[SVSHAPE.order[2]]: x_r.reverse()
+ if SVSHAPE.invxyz[SVSHAPE.order[1]]: y_r.reverse()
+ if SVSHAPE.invxyz[SVSHAPE.order[0]]: z_r.reverse()
# start an infinite (wrapping) loop
+ skip = 0
while True:
for x in x_r: # loop over 3rd order dimension
for y in y_r: # loop over 2nd order dimension
for z in z_r: # loop over 1st order dimension
+ # skip the first entries up to offset
+ if skip < SVSHAPE.offset:
+ skip += 1
+ continue
# construct the (up to) 3D remap schedule
yield (x + y * xd + z * xd * yd)
-# enumerate over the iterator function, getting new indices
-for idx, new_idx in enumerate(iterate_indices()):
- if idx < offset:
- continue
- if idx >= offset + VL:
- break
- print ("%d->%d" % (idx, new_idx))
+def demo():
+ # set the dimension sizes here
+ xdim = 3
+ ydim = 2
+ zdim = 1
+
+ # set total (can repeat, e.g. VL=x*y*z*4)
+ VL = xdim * ydim * zdim
+
+ # set up an SVSHAPE
+ class SVSHAPE:
+ pass
+ SVSHAPE0 = SVSHAPE()
+ SVSHAPE0.lims = [xdim, ydim, zdim]
+ SVSHAPE0.idxs = [0,0,0] # starting indices
+ SVSHAPE0.order = [1,0,2] # experiment with different permutations, here
+ SVSHAPE0.offset = 0 # experiment with different offset, here
+ SVSHAPE0.invxyz = [0,1,0] # inversion if desired
+
+ # enumerate over the iterator function, getting new indices
+ for idx, new_idx in enumerate(iterate_indices(SVSHAPE0)):
+ if idx >= VL:
+ break
+ print ("%d->%d" % (idx, new_idx))
+# run the demo
+if __name__ == '__main__':
+ demo()