def reshape(self, shape):
if self.shape == shape:
return self
if any(d == -1 for d in shape):
extra = int(np.prod(self.shape) /
np.prod([d for d in shape if d != -1]))
shape = tuple([d if d != -1 else extra for d in shape])
if self.shape == shape:
return self
if self._cache is not None:
for sh, value in self._cache['reshape']:
if sh == shape:
return value
# TODO: this np.prod(self.shape) enforces a 2**64 limit to array size
linear_loc = self.linear_loc()
coords = np.empty((len(shape), self.nnz), dtype=np.min_scalar_type(max(shape)))
strides = 1
for i, d in enumerate(shape[::-1]):
coords[-(i + 1), :] = (linear_loc // strides) % d
strides *= d
result = COO(coords, self.data, shape,
has_duplicates=self.has_duplicates,
sorted=self.sorted, cache=self._cache is not None)
if self._cache is not None:
self._cache['reshape'].append((shape, result))
return result
评论列表
文章目录