pg_utils.pg_model.expansion.SystemMatrix

class pg_utils.pg_model.expansion.SystemMatrix(*args, **kwargs)[source]

Bases: object

System matrix

This is a wrapper for a matrix of symbolic expression (inner products) whose numerical values will be evaluated numerically. This is one of the final outcomes of the symbolic computation.

Variables:
  • _row_names (List[str]) – list of names corresponding to each row (block)

  • _row_idx (dict) – dict that maps row name to their index

  • _col_names (List[str]) – list of names corresponding to each col (block)

  • _col_idx (dict) – dict that maps col names to their index

  • _matrix (array-like) – a nested list representing the matrix

__init__(*args, **kwargs) None[source]

Initialization

There are currently two modes of initialization.

  • If the first argument is a base.LabeledCollection instance, it will be interpreted as a collection of expressions; the second argument is then interpreted as the collection of coefficients. The signaure is SystemMatrix(expressions, coefficients). The SystemMatrix will be constructed by invoking the SystemMatrix.build_matrix method

  • Otherwise, the arguments will be interpreted as the row names, column names and matrix of expressions, respectively. The signature is SystemMatrix(row_names, col_names, matrix)

Methods

__init__(*args, **kwargs)

Initialization

apply(fun[, inplace, metadata])

Apply function to elements iteratively.

block_sparsity()

Give the sparsity pattern of the symbolic matrix

build_matrix(exprs, coeffs)

Build system matrix from a collection of expressions

deserialize(matrix_obj)

Construct system matrix from object

load_json(fp)

Load from json file

load_serialized_element(element)

save_json(fp)

Save to json file

serialize()

Serialize object

serialize_element(element)

Serialize an element

apply(fun: Callable, inplace: bool = False, metadata: bool = False) SystemMatrix[source]

Apply function to elements iteratively.

Parameters:
  • fun (Callable) – function that processes the elements

  • inplace (bool) – whether the change is applied in situ

  • metadata (bool) – whether the name of the element is passed to fun.

block_sparsity()[source]

Give the sparsity pattern of the symbolic matrix

Returns:

sparsity matrix. If an element of the SystemMatrix is zero or None, then it will be marked as False in the output; otherwise it will be marked as True.

Return type:

numpy.ndarray

static build_matrix(exprs: LabeledCollection, coeffs: LabeledCollection) List[source]

Build system matrix from a collection of expressions

Parameters:

Warning

Whatever term in exprs that does not contain a coefficient included in the coeffs collection will be discarded in the process!

static deserialize(matrix_obj: List[List]) SystemMatrix[source]

Construct system matrix from object

static load_json(fp: TextIO) SystemMatrix[source]

Load from json file

save_json(fp: TextIO) None[source]

Save to json file

serialize() List[List][source]

Serialize object

static serialize_element(element: Union[InnerProduct1D, Expr]) Any[source]

Serialize an element