def merge_adjpair(self, i0, i1): assert i0 + 1 == i1 p0 = self.ptys[i0] p1 = self.ptys[i1] d0 = self.dims[i0] d1 = self.dims[i1] p01 = [] d01 = [] ns0 = len(p0) ns1 = len(p1) for j0 in range(ns0): for j1 in range(ns1): p01.append(p0[j0] ^ p1[j1]) d01.append(d0[j0] * d1[j1]) ptys = copy.deepcopy(self.ptys) ptys.pop(i0) ptys.pop(i0) ptys.insert(i0, p01) dims = copy.deepcopy(self.dims) dims.pop(i0) dims.pop(i0) dims.insert(i0, d01) new = PArray(ptys, dims) for ix in np.ndindex(self.shape): if not self.ifpc(ix): continue nix = np.ravel_multi_index([ix[i0], ix[i1]], (ns0, ns1)) nix = ix[:i0] + tuple([nix]) + ix[i1 + 1:] shape = list(self[ix].shape) nshape = shape[i0] * shape[i1] shape.pop(i0) shape.pop(i0) shape.insert(i0, nshape) new[nix] = self[ix].reshape(shape) new = new.merge_axis(i0) return new
def merge_axis(self, i): ptys = copy.deepcopy(self.ptys) dims = copy.deepcopy(self.dims) dic = {} for idx, ip in enumerate(ptys[i]): if ip not in dic: dic[ip] = [idx] else: dic[ip].append(idx) qsec = sorted(dic.keys()) nq = len(qsec) dsec = [ sum([dims[i][iq] for iq in dic[qsec[idx]]]) for idx in range(nq) ] ptys.pop(i) ptys.insert(i, qsec) dims.pop(i) dims.insert(i, dsec) new = PArray(ptys, dims) for ix in np.ndindex(new.shape): if not new.ifpc(ix): continue tensors = [] for j in dic[qsec[ix[i]]]: px = list(ix) px[i] = j tensors.append(self[tuple(px)]) new[ix] = np.concatenate(tensors, axis=i) return new
def fill(self, vec): ptr = 0 for ix in np.ndindex(self.shape): if self.ifpc(ix): shape = self[ix].shape nelem = np.product(shape) self[ix] = vec[ptr:ptr + nelem].reshape(shape) ptr += nelem return vec
def transpose(self, axes): ptys_new = [self.ptys[i] for i in axes] dims_new = [self.dims[i] for i in axes] new = PArray(ptys_new, dims_new) # OLD_INDEX for ix in np.ndindex(self.shape): if self.ifpc(ix): ix_new = tuple([ix[i] for i in axes]) new[ix_new] = self[ix].transpose(axes) return new
def prt(self): print 'PArray.prt' print ' ptys=', self.ptys print ' dims=', self.dims nrm2 = 0.0 for ix in np.ndindex(self.shape): line = None if self.ifpc(ix): nrm2i = np.linalg.norm(self[ix])**2 nrm2 += nrm2i line = ' norm2=' + str(nrm2i) print ' iblk=', ix, self.ifpc(ix), self[ix].shape, line print ' norm2=', nrm2, '\n' return 0
def __new__(cls, ptys, dims): shape = map(lambda x: len(x), ptys) shape1 = map(lambda x: len(x), dims) assert shape == shape1 obj = np.ndarray.__new__(cls, shape, dtype=np.object) obj[:] = null.Null() obj.ptys = copy.deepcopy(ptys) obj.dims = copy.deepcopy(dims) # Initialize for ix in np.ndindex(obj.shape): # np.ndindex(3, 2, 1) if not obj.ifpc(ix): continue shp = obj.get_shp(ix) obj[ix] = np.zeros(shp, dtype=dataType) return obj
def ravel(self): vec = np.empty((0)) for ix in np.ndindex(self.shape): if self.ifpc(ix): vec = np.append(vec, np.ravel(self[ix])) return vec
def size(self): size = 0 for ix in np.ndindex(self.shape): if self.ifpc(ix): size += np.product(self[ix].shape) return size
def random(self, fac=1.0): for ix in np.ndindex(self.shape): if self.ifpc(ix): self[ix] = fac * np.random.uniform(-1, 1, self[ix].shape) return 0
def scale(self, fac=1.0): for ix in np.ndindex(self.shape): if self.ifpc(ix): self[ix] = fac * self[ix] return 0