Example #1
0
    def tobsr(self, blocksize=None, copy=True):
        from scipy.sparse.bsr import bsr_matrix

        if blocksize is None:
            from scipy.sparse.spfuncs import estimate_blocksize
            return self.tobsr(blocksize=estimate_blocksize(self))

        elif blocksize == (1, 1):
            arg1 = (self.data.reshape(-1, 1, 1), self.indices, self.indptr)
            return bsr_matrix(arg1, shape=self.shape, copy=copy)

        else:
            R, C = blocksize
            M, N = self.shape

            if R < 1 or C < 1 or M % R != 0 or N % C != 0:
                raise ValueError('invalid blocksize %s' % blocksize)

            blks = csr_count_blocks(M, N, R, C, self.indptr, self.indices)

            idx_dtype = get_index_dtype((self.indptr, self.indices),
                                        maxval=max(N // C, blks))
            indptr = np.empty(M // R + 1, dtype=idx_dtype)
            indices = np.empty(blks, dtype=idx_dtype)
            data = np.zeros((blks, R, C), dtype=self.dtype)

            csr_tobsr(M, N, R, C, self.indptr.astype(idx_dtype),
                      self.indices.astype(idx_dtype), self.data, indptr,
                      indices, data.ravel())

            return bsr_matrix((data, indices, indptr), shape=self.shape)
Example #2
0
    def test_estimate_blocksize(self):
        mats = []
        mats.append( [[0,1],[1,0]] )
        mats.append( [[1,1,0],[0,0,1],[1,0,1]] )
        mats.append( [[0],[0],[1]] )
        mats = [array(x) for x in mats]

        blks = []
        blks.append( [[1]] )
        blks.append( [[1,1],[1,1]] )
        blks.append( [[1,1],[0,1]] )
        blks.append( [[1,1,0],[1,0,1],[1,1,1]] )
        blks = [array(x) for x in blks]

        for A in mats:
            for B in blks:
                X = kron(A,B)
                r,c = spfuncs.estimate_blocksize(X)
                assert_(r >= B.shape[0])
                assert_(c >= B.shape[1])