Пример #1
0
    def fromsp(cls, geometry, P, S=None):
        """ Read and return the object with possible overlap """
        # Calculate maximum number of connections per row
        nc = 0

        # Ensure list of csr format (to get dimensions)
        if isspmatrix(P):
            P = [P]

        # Number of dimensions
        dim = len(P)
        # Sort all indices for the passed sparse matrices
        for i in range(dim):
            P[i] = P[i].tocsr()
            P[i].sort_indices()
            P[i].sum_duplicates()

        # Figure out the maximum connections per
        # row to reduce number of re-allocations to 0
        for i in range(P[0].shape[0]):
            nc = max(nc, P[0][i, :].getnnz())

        # Create the sparse object
        p = cls(geometry, dim, P[0].dtype, nc, orthogonal=S is None)

        if p._size != P[0].shape[0]:
            raise ValueError(cls.__name__ + '.fromsp cannot create a new class, the geometry ' + \
                             'and sparse matrices does not have coinciding dimensions size != sp.shape[0]')

        for i in range(dim):
            ptr = P[i].indptr
            col = P[i].indices
            D = P[i].data

            # loop and add elements
            for r in range(p.shape[0]):
                sl = slice(ptr[r], ptr[r+1], None)
                p[r, col[sl], i] = D[sl]

        if not S is None:
            S = S.tocsr()
            S.sort_indices()
            S.sum_duplicates()
            ptr = S.indptr
            col = S.indices
            D = S.data

            # loop and add elements
            for r in range(p.shape[0]):
                sl = slice(ptr[r], ptr[r+1], None)
                p.S[r, col[sl]] = D[sl]

        return p
Пример #2
0
    def fromsp(cls, geometry, P, S=None, **kwargs):
        r""" Create a sparse model from a preset `Geometry` and a list of sparse matrices

        The passed sparse matrices are in one of `scipy.sparse` formats.

        Parameters
        ----------
        geometry : Geometry
           geometry to describe the new sparse geometry
        P : list of scipy.sparse or scipy.sparse
           the new sparse matrices that are to be populated in the sparse
           matrix
        S : scipy.sparse, optional
           if provided this refers to the overlap matrix and will force the
           returned sparse matrix to be non-orthogonal
        **kwargs : optional
           any arguments that are directly passed to the ``__init__`` method
           of the class.

        Returns
        -------
        SparseGeometry
             a new sparse matrix that holds the passed geometry and the elements of `P` and optionally being non-orthogonal if `S` is not none
        """
        # Ensure list of csr format (to get dimensions)
        if isspmatrix(P):
            P = [P]
        if isinstance(P, tuple):
            P = list(P)

        # Number of dimensions, before S!
        dim = len(P)
        if not S is None:
            P.append(S)

        p = cls(geometry, dim, P[0].dtype, 1, orthogonal=S is None, **kwargs)
        p._csr = p._csr.fromsp(*P, dtype=kwargs.get("dtype"))

        if p._size != P[0].shape[0]:
            raise ValueError(
                f"{cls.__name__}.fromsp cannot create a new class, the geometry "
                "and sparse matrices does not have coinciding dimensions size != P[0].shape[0]"
            )

        return p
Пример #3
0
    def fromsp(cls, geometry, P, S=None, **kwargs):
        r""" Create a sparse model from a preset `Geometry` and a list of sparse matrices

        The passed sparse matrices are in one of `scipy.sparse` formats.

        Parameters
        ----------
        geometry : Geometry
           geometry to describe the new sparse geometry
        P : list of scipy.sparse or scipy.sparse
           the new sparse matrices that are to be populated in the sparse
           matrix
        S : scipy.sparse, optional
           if provided this refers to the overlap matrix and will force the
           returned sparse matrix to be non-orthogonal
        **kwargs : optional
           any arguments that are directly passed to the ``__init__`` method
           of the class.

        Returns
        -------
        SparseGeometry
             a new sparse matrix that holds the passed geometry and the elements of `P` and optionally being non-orthogonal if `S` is not none
        """
        # Ensure list of csr format (to get dimensions)
        if isspmatrix(P):
            P = [P]
        if isinstance(P, tuple):
            P = list(P)

        # Number of dimensions
        dim = len(P)
        nnzpr = 1
        # Sort all indices for the passed sparse matrices
        for i in range(dim):
            P[i] = P[i].tocsr()
            P[i].sort_indices()
            P[i].sum_duplicates()

            nnzpr = max(nnzpr, P[i].nnz // P[i].shape[0])

        # Create the sparse object
        p = cls(geometry,
                dim,
                P[0].dtype,
                nnzpr,
                orthogonal=S is None,
                **kwargs)

        if p._size != P[0].shape[0]:
            raise ValueError(cls.__name__ + '.fromsp cannot create a new class, the geometry ' + \
                             'and sparse matrices does not have coinciding dimensions size != P[0].shape[0]')

        for i in range(dim):
            ptr = P[i].indptr
            col = P[i].indices
            D = P[i].data

            # loop and add elements
            for r in range(p.shape[0]):
                sl = slice(ptr[r], ptr[r + 1], None)
                p[r, col[sl], i] = D[sl]

        if not S is None:
            S = S.tocsr()
            S.sort_indices()
            S.sum_duplicates()
            ptr = S.indptr
            col = S.indices
            D = S.data

            # loop and add elements
            for r in range(p.shape[0]):
                sl = slice(ptr[r], ptr[r + 1], None)
                p.S[r, col[sl]] = D[sl]

        return p