def find(self, app) -> np.ndarray: rows, cols, vals = [], [], [] block_shape: np.ndarray = np.array(self.grid.block_shape, dtype=np.int) arrays: list = self.system.get([ self.blocks[grid_entry].oid for grid_entry in self.grid.get_entry_iterator() ]) for block_index, grid_entry in enumerate( self.grid.get_entry_iterator()): start = block_shape * grid_entry (r, c, v) = scipy.sparse.find(arrays[block_index]) r = r + start[0] c = c + start[1] rows.append(r) cols.append(c) vals.append(v) rows = np.concatenate(rows) cols = np.concatenate(cols) vals = np.concatenate(vals) block_shape = app.get_block_shape(rows.shape, rows.dtype) val_block_shape = app.get_block_shape(vals.shape, vals.dtype) return ( BlockArray.from_np(rows, block_shape, False, self.system), BlockArray.from_np(cols, block_shape, False, self.system), BlockArray.from_np(vals, val_block_shape, False, self.system), )
def array(self, array: Union[np.ndarray, List[float]], block_shape: tuple = None): if not isinstance(array, np.ndarray): if array_utils.is_array_like(array): array = np.array(array) else: raise ValueError( "Unable to instantiate array from type %s" % type(array) ) assert len(array.shape) == len(block_shape) return BlockArray.from_np( array, block_shape=block_shape, copy=False, cm=self.cm )
def array(self, array: np.ndarray, block_shape: tuple = None): assert len(array.shape) == len(block_shape) return BlockArray.from_np(array, block_shape=block_shape, copy=False, cm=self.cm)
arr = np.zeros((w, h)) ind = random.sample(range(w * h), sparsity) ind = [(i % w, i // w) for i in ind] for i in ind: arr[i] = np.random.randint(0, 100) dtype = np.__getattribute__(str(arr.dtype)) shape = arr.shape app = _instance() block_shape = app.compute_block_shape(shape, dtype) sparse_result = SparseBlockArray.from_np(arr, block_shape=block_shape, copy=False, system=app.system) dense_result = BlockArray.from_np(arr, block_shape=block_shape, copy=False, system=app.system) funcs = [ lambda x: x @ x, lambda x: x + x, lambda x: x - x, # lambda x: x ** x, ] for f in funcs: assert (f(sparse_result).get() == f(dense_result).get()).all() destroy()