Example #1
0
def sparse_reverse_permute(A, rperm=(), cperm=(), safe=True):
    """
    Performs a reverse permutations of 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 order of the rows and columns used to permute the original array/Qobj.
    
    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_reverse_permute(
                A.data, A.indices, A.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_reverse_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)
Example #2
0
File: sparse.py Project: trxw/qutip
def sparse_reverse_permute(A, rperm=(), cperm=(), safe=True):
    """
    Performs a reverse permutations of 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 order of the rows and columns used to permute the original array/Qobj.
    
    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_reverse_permute(A.data, A.indices, A.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_reverse_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)