def check_conjugate(cell2index, index2cell): for k in range(cell2index.size): assert cell2index[tuple(index2cell[k, :])] == k for c in coords_iterate(cell2index.shape): k = cell2index[c] assert index2cell[k, 0] == c[0] assert index2cell[k, 1] == c[1]
def flat2coords(self, flat): ''' Converts a representation of the type index[i,j] = k to diffeo[i,j]= [i_k, j_k] ''' if flat.shape != self.shape: msg = 'Expected shape %s, got %s.' % (self.shape, flat.shape) raise ValueError(msg) res = np.zeros((self.shape[0], self.shape[1], 2), dtype='int32') for i, j in coords_iterate(self.shape): k = flat[i, j] res[i, j, :] = self.index2cell[k, :] return res
def by_rows(shape): M = shape[0] N = shape[1] cell2index = np.zeros((M, N), 'int32') index2cell = np.zeros((M * N, 2), 'int32') k = 0 for i, j in coords_iterate(shape): cell2index[i, j] = k index2cell[k] = [i, j] k += 1 return Flattening(cell2index, index2cell)
def apply_diffeomorphism(diffeo, y): yd = np.empty_like(y) for c in coords_iterate(y.shape): c2 = tuple(diffeo[c[0], c[1], :]) yd[c] = y[c2] return yd