1 core.matrices.MatrixVariable

core.matrices.MatrixVariable(
    name,
    rows,
    cols,
    lb=None,
    ub=None,
    domain='continuous',
    symmetric=False,
)

A 2D matrix of optimization variables.

MatrixVariable creates and manages a 2D collection of scalar Variable instances, providing natural 2D indexing, row/column slicing, and transpose views.

1.1 Parameters

Name Type Description Default
name str Base name for the matrix. Elements are named “{name}[i,j]”. required
rows int Number of rows. required
cols int Number of columns. required
lb float | None Lower bound applied to all elements (None for unbounded). None
ub float | None Upper bound applied to all elements (None for unbounded). None
domain DomainType Variable type for all elements - ‘continuous’, ‘integer’, or ‘binary’. 'continuous'
symmetric bool If True, enforces A[i,j] == A[j,i] (must be square). False

1.2 Example

A = MatrixVariable(“A”, 3, 4) A[0, 0] # Variable named “A[0,0]” A[1, :] # VectorVariable with 4 elements (row 1) A[:, 2] # VectorVariable with 3 elements (column 2) A.shape # (3, 4)

1.3 Attributes

Name Description
T Return a transpose view of the matrix.
shape Return the shape of the matrix as (rows, cols).

1.4 Methods

Name Description
cols_iter Iterate over columns of the matrix.
diagonal Extract the main diagonal of a square matrix.
get_variables Return all variables in this matrix (row-major order).
rows_iter Iterate over rows of the matrix.
to_numpy Extract matrix values from solution as numpy array.
trace Compute the trace (sum of diagonal elements) of a square matrix.

1.4.1 cols_iter

core.matrices.MatrixVariable.cols_iter()

Iterate over columns of the matrix.

1.4.1.1 Returns

Name Type Description
Iterator[VectorVariable] Iterator of VectorVariable, one for each column.

1.4.1.2 Example

A = MatrixVariable(“A”, 3, 4) for j, col in enumerate(A.cols_iter()): … print(f”Col {j}: {len(col)} elements”)

1.4.2 diagonal

core.matrices.MatrixVariable.diagonal()

Extract the main diagonal of a square matrix.

1.4.2.1 Returns

Name Type Description
VectorVariable VectorVariable containing the diagonal elements.

1.4.2.2 Raises

Name Type Description
ValueError If the matrix is not square.

1.4.2.3 Example

A = MatrixVariable(“A”, 3, 3) d = A.diagonal() len(d) # 3 d[0].name # ‘A[0,0]’

1.4.3 get_variables

core.matrices.MatrixVariable.get_variables()

Return all variables in this matrix (row-major order).

For symmetric matrices, each unique variable appears only once.

1.4.3.1 Returns

Name Type Description
list[Variable] List of Variable instances.

1.4.4 rows_iter

core.matrices.MatrixVariable.rows_iter()

Iterate over rows of the matrix.

1.4.4.1 Returns

Name Type Description
Iterator[VectorVariable] Iterator of VectorVariable, one for each row.

1.4.4.2 Example

A = MatrixVariable(“A”, 3, 4) for i, row in enumerate(A.rows_iter()): … print(f”Row {i}: {len(row)} elements”)

1.4.5 to_numpy

core.matrices.MatrixVariable.to_numpy(solution)

Extract matrix values from solution as numpy array.

1.4.5.1 Parameters

Name Type Description Default
solution dict[str, float] Dictionary mapping variable names to values. required

1.4.5.2 Returns

Name Type Description
NDArray[np.floating] 2D numpy array with the solution values.

1.4.5.3 Example

A = MatrixVariable(“A”, 2, 2) solution = {“A[0,0]”: 1, “A[0,1]”: 2, “A[1,0]”: 3, “A[1,1]”: 4} A.to_numpy(solution) array([[1., 2.], [3., 4.]])

1.4.6 trace

core.matrices.MatrixVariable.trace()

Compute the trace (sum of diagonal elements) of a square matrix.

1.4.6.1 Returns

Name Type Description
Expression Expression representing the sum of diagonal elements.

1.4.6.2 Raises

Name Type Description
ValueError If the matrix is not square.

1.4.6.3 Example

A = MatrixVariable(“A”, 3, 3) tr = A.trace() # tr = A[0,0] + A[1,1] + A[2,2]