def _ttv_compute(self, v, dims, vidx, remdims): nvals = self.vals nsubs = self.subs for i in range(len(dims)): idx = nsubs[dims[i]] w = v[vidx[i]] nvals = nvals * w[idx] # Case 1: all dimensions used -> return sum if len(remdims) == 0: return nvals.sum() nsubs = tuple(self.subs[i] for i in remdims) nshp = tuple(self.shape[i] for i in remdims) # Case 2: result is a vector if len(remdims) == 1: c, nsubs = accum(nsubs, nvals, shape=nshp, with_subs=True) nshp = nshp[0] if len(np.nonzero(c)[0]) <= 0.5 * nshp: return sptensor(nsubs, c, (nshp,)) else: return c # Case 3: result is an array return sptensor(nsubs, nvals, shape=nshp, accumfun=np.sum)
def __init__(self, subs, vals, shape=None, dtype=None, accumfun=None, issorted=False): if not isinstance(subs, tuple): raise ValueError('Subscripts must be a tuple of array-likes') if len(subs[0]) != len(vals): raise ValueError('Subscripts and values must be of equal length') if dtype is None: dtype = np.array(vals).dtype for i in range(len(subs)): if np.array(subs[i]).dtype.kind != 'i': raise ValueError('Subscripts must be integers') vals = np.array(vals, dtype=dtype) if accumfun is not None: vals, subs = accum(subs, vals, issorted=False, with_subs=True, func=accumfun) self.subs = subs self.vals = vals self.dtype = dtype self.issorted = issorted self.accumfun = accumfun if shape is None: self.shape = tuple(np.array(subs).max(axis=1).flatten() + 1) else: self.shape = tuple(int(d) for d in shape) self.ndim = len(subs)
def __init__(self, subs, vals, shape=None, dtype=None, accumfun=None): if not isinstance(subs, tuple): raise ValueError('Subscripts must be a tuple of array-likes') if len(subs[0]) != len(vals): raise ValueError('Subscripts and values must be of equal length') if dtype is None: dtype = array(vals).dtype for i in range(len(subs)): if array(subs[i]).dtype.kind != 'i': raise ValueError('Subscripts must be integers') vals = array(vals) if accumfun is not None: vals, subs = accum( subs, vals, sorted=False, with_subs=True, fun=accumfun ) self.subs = subs self.vals = vals self.dtype = dtype if shape is None: self.shape = tuple(array(subs).max(axis=1).flatten()) else: self.shape = tuple(int(d) for d in shape) self.ndim = len(subs)