Exemple #1
0
def sparse_permute(A, rperm=(), cperm=(), safe=True):
    """
    Permutes the rows and columns of a sparse CSR/CSC matrix or Qobj 
    according to the permutation arrays rperm and cperm, respectively.  
    Here, the permutation arrays specify the new order of the rows and 
    columns. i.e. [0,1,2,3,4] -> [3,0,4,1,2].
    
    Parameters
    ----------
    A : qobj, csr_matrix, csc_matrix
        Input matrix.
    rperm : array_like of integers
        Array of row permutations.
    cperm : array_like of integers
        Array of column permutations.
    safe : bool
        Check structure of permutation arrays.
    
    Returns
    -------
    perm_csr : csr_matrix, csc_matrix
        CSR or CSC matrix with permuted rows/columns.
    
    """
    rperm = np.asarray(rperm, dtype=np.int32)
    cperm = np.asarray(cperm, dtype=np.int32)
    nrows = A.shape[0]
    ncols = A.shape[1]
    if len(rperm)==0:
        rperm = np.arange(nrows, dtype=np.int32)
    if len(cperm)==0:
        cperm = np.arange(ncols, dtype=np.int32)
    if safe:
        if len(np.setdiff1d(rperm, np.arange(nrows)))!=0:
            raise Exception('Invalid row permutation array.')
        if len(np.setdiff1d(cperm, np.arange(ncols)))!=0:
            raise Exception('Invalid column permutation array.')
    shp = A.shape
    if A.__class__.__name__=='Qobj':
        kind = 'csr'
        dt = complex
        data, ind, ptr = _sparse_permute(
                A.data.data, A.data.indices, A.data.indptr,
                nrows, ncols, rperm, cperm, 0)
    else:
        kind=A.getformat()
        if kind=='csr':
            flag = 0
        elif kind=='csc':
            flag = 1
        else:
            raise Exception('Input must be Qobj, CSR, or CSC matrix.')
        data, ind, ptr = _sparse_permute(A.data, A.indices, A.indptr,
                nrows, ncols, rperm, cperm, flag)
    if kind=='csr':
        return sp.csr_matrix((data, ind, ptr), shape=shp, dtype=data.dtype)
    elif kind=='csc':
        return sp.csc_matrix((data, ind, ptr), shape=shp, dtype=data.dtype)
Exemple #2
0
def sparse_permute(A, rperm=(), cperm=(), safe=True):
    """
    Permutes the rows and columns of a sparse CSR/CSC matrix or Qobj 
    according to the permutation arrays rperm and cperm, respectively.  
    Here, the permutation arrays specify the new order of the rows and 
    columns. i.e. [0,1,2,3,4] -> [3,0,4,1,2].
    
    Parameters
    ----------
    A : qobj, csr_matrix, csc_matrix
        Input matrix.
    rperm : array_like of integers
        Array of row permutations.
    cperm : array_like of integers
        Array of column permutations.
    safe : bool
        Check structure of permutation arrays.
    
    Returns
    -------
    perm_csr : csr_matrix, csc_matrix
        CSR or CSC matrix with permuted rows/columns.
    
    """
    rperm = np.asarray(rperm, dtype=np.int32)
    cperm = np.asarray(cperm, dtype=np.int32)
    nrows = A.shape[0]
    ncols = A.shape[1]
    if len(rperm) == 0:
        rperm = np.arange(nrows, dtype=np.int32)
    if len(cperm) == 0:
        cperm = np.arange(ncols, dtype=np.int32)
    if safe:
        if len(np.setdiff1d(rperm, np.arange(nrows))) != 0:
            raise Exception('Invalid row permutation array.')
        if len(np.setdiff1d(cperm, np.arange(ncols))) != 0:
            raise Exception('Invalid column permutation array.')
    shp = A.shape
    if A.__class__.__name__ == 'Qobj':
        kind = 'csr'
        dt = complex
        data, ind, ptr = _sparse_permute(A.data.data, A.data.indices,
                                         A.data.indptr, nrows, ncols, rperm,
                                         cperm, 0)
    else:
        kind = A.getformat()
        if kind == 'csr':
            flag = 0
        elif kind == 'csc':
            flag = 1
        else:
            raise Exception('Input must be Qobj, CSR, or CSC matrix.')
        data, ind, ptr = _sparse_permute(A.data, A.indices, A.indptr, nrows,
                                         ncols, rperm, cperm, flag)
    if kind == 'csr':
        return sp.csr_matrix((data, ind, ptr), shape=shp, dtype=data.dtype)
    elif kind == 'csc':
        return sp.csc_matrix((data, ind, ptr), shape=shp, dtype=data.dtype)