1 core.parameters.MatrixParameter
core.parameters.MatrixParameter(name, values, symmetric=False)A matrix of parameters for array-valued constants.
MatrixParameter stores a 2D array of constant values that can be updated between solves without rebuilding the problem structure. This is useful for: - Covariance matrices in portfolio optimization - Constraint coefficient matrices that change over time - Distance/cost matrices in routing problems
Unlike VectorParameter (which creates individual Parameter objects), MatrixParameter stores values directly for efficiency with large matrices.
1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| name | str | Name identifier for this parameter matrix. | required |
| values | ArrayLike | Initial 2D array of values. | required |
| symmetric | bool | If True, enforce and exploit symmetry. Only the upper triangle is stored, and updates must maintain symmetry. | False |
1.2 Example
import numpy as np from optyx import VectorVariable, MatrixParameter
2 Portfolio with updatable covariance
cov = np.array([[0.04, 0.01], [0.01, 0.09]]) Sigma = MatrixParameter(“Sigma”, cov, symmetric=True) weights = VectorVariable(“w”, 2, lb=0, ub=1)
3 Access elements
Sigma[0, 1] # 0.01 Sigma.values # Full 2D array
4 Update for re-solve
new_cov = np.array([[0.05, 0.02], [0.02, 0.10]]) Sigma.set(new_cov)
4.1 Attributes
| Name | Description |
|---|---|
| cols | Get the number of columns. |
| rows | Get the number of rows. |
| shape | Get the shape of the matrix. |
| symmetric | Check if this matrix is constrained to be symmetric. |
| values | Get the current matrix values. |
4.2 Methods
| Name | Description |
|---|---|
| col | Get a column as a 1D array. |
| row | Get a row as a 1D array. |
| set | Update the matrix values. |
| to_numpy | Get the matrix values as a numpy array. |
4.2.1 col
core.parameters.MatrixParameter.col(j)Get a column as a 1D array.
4.2.1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| j | int | Column index. | required |
4.2.1.2 Returns
| Name | Type | Description |
|---|---|---|
| NDArray[np.floating] | 1D array of column values. |
4.2.2 row
core.parameters.MatrixParameter.row(i)Get a row as a 1D array.
4.2.2.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| i | int | Row index. | required |
4.2.2.2 Returns
| Name | Type | Description |
|---|---|---|
| NDArray[np.floating] | 1D array of row values. |
4.2.3 set
core.parameters.MatrixParameter.set(values)Update the matrix values.
4.2.3.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| values | ArrayLike | New 2D array (must match original shape). | required |
4.2.3.2 Raises
| Name | Type | Description |
|---|---|---|
| ValueError | If shape doesn’t match or symmetric constraint violated. |
4.2.3.3 Example
Sigma.set(new_covariance_matrix)
4.2.4 to_numpy
core.parameters.MatrixParameter.to_numpy()Get the matrix values as a numpy array.
4.2.4.1 Returns
| Name | Type | Description |
|---|---|---|
| NDArray[np.floating] | Copy of the internal 2D array. |