Utils
pyOptSparse_utils holds a minimal set of sparse-matrix type routines for pyOptSparse. This is designed to replace the SciPy sparse matrix formats, which have no way to enforce a constant sparsity structure as required by the optimizers. We use a very simple dictionary format to represent the three most common forms of sparse matrices:
mat = {'coo':[row, col, data], 'shape':[nrow, ncols]} # A coo matrix
mat = {'csr':[rowp, colind, data], 'shape':[nrow, ncols]} # A csr matrix
mat = {'csc':[colp, rowind, data], 'shape':[nrow, ncols]} # A csc matrix
- pyoptsparse.pyOpt_utils.convertToCOO(mat)[source]
Take a pyoptsparse sparse matrix definition of a COO, CSR or CSC matrix or numpy array or scipy sparse matrix and return the same matrix in COO format.
- Parameters:
- matdict or numpy array
A sparse matrix representation or numpy array
- Returns:
- newMatdict
A coo representation of the same matrix
- pyoptsparse.pyOpt_utils.convertToCSC(mat)[source]
Take a pyoptsparse sparse matrix definition of a COO, CSR or CSC matrix or numpy array and return the same matrix in CSR format
- Parameters:
- matdict or numpy array
A sparse matrix representation or numpy array
- Returns:
- newMatdict
A coo representation of the same matrix
- pyoptsparse.pyOpt_utils.convertToCSR(mat)[source]
Take a pyoptsparse sparse matrix definition of a COO, CSR or CSC matrix or numpy array and return the same matrix in CSR format
- Parameters:
- matdict or numpy array
A sparse matrix representation or numpy array
- Returns:
- newMatdict
A coo representation of the same matrix
- pyoptsparse.pyOpt_utils.convertToDense(mat)[source]
Take a pyopsparse sparse matrix definition and convert back to a dense format. This is typically the final step for optimizers with dense constraint jacibians.
- Parameters:
- matdict
A sparse matrix representation. Should be in CSR format for best efficiency
- Returns:
- newMatarray
A dense numpy array of the same matrix
- pyoptsparse.pyOpt_utils.extractRows(mat, indices)[source]
Extract the rows defined by ‘indices’ and return a new CSR matrix.
- Parameters:
- matdict
pyoptsparse matrix CSR format
- indiceslist/array of integer
The rows the user wants to extract
- Returns:
- newMatdic
pyoptsparse CSR matrix
- pyoptsparse.pyOpt_utils.mapToCSC(mat)[source]
Given a pyoptsparse matrix definition, return a tuple containing a map of the matrix to the CSC format.
- Parameters:
- matdict
A sparse matrix representation.
- Returns:
- tuptuple of numpy arrays
- tup[0]numpy array (size=nnz)
An array that holds the row index of each element in the CSC representation of the data.
- tup[1]numpy array (size=num_cols+1)
An array that holds the indices in the CSC representation and data at which each column begins. The last index of contains the number of nonzero elements in the sparse array.
- tup[2]numpy array
An indexing array which maps the elements in the data array to elements in the CSC data array.
- pyoptsparse.pyOpt_utils.mapToCSR(mat)[source]
Given a pyoptsparse matrix definition, return a tuple containing a map of the matrix to the CSR format.
- Parameters:
- matdict
A sparse matrix representation.
- Returns:
- tuptuple of numpy arrays
- tup[0]numpy array (size=num_rows+1)
An array that holds the indices in col_idx and data at which each row begins. The last index of contains the number of nonzero elements in the sparse array.
- tup[1]numpy array (size=nnz)
An array of the column indices of each element in data.
- tup[2]numpy array (size=nnz)
An indexing array which maps the elements in the data array to elements in the CSR data array.
- pyoptsparse.pyOpt_utils.scaleColumns(mat, factor)[source]
d= Scale the columns of the matrix. Must be CSR format
- pyoptsparse.pyOpt_utils.scaleRows(mat, factor)[source]
Scale the rows of the matrix. Must be CSR format
- pyoptsparse.pyOpt_utils.try_import_compiled_module_from_path(module_name, path=None, raise_warning=False)[source]
Attempt to import a module from a given path.
- Parameters:
- module_namestr
The name of the module
- pathOptional[str]
The path to import from. If None, the default
sys.path
is used.- raise_warningbool
If true, raise an import warning. By default false.
- Returns:
- Union[types.ModuleType, str]
If importable, the imported module is returned. If not importable, the error message is instead returned.