linalg
Linear algebra operations for symbolic expressions.
This module provides essential linear algebra operations for matrix and vector manipulation in optimization problems. Operations follow NumPy/JAX conventions for shapes and broadcasting behavior.
Key Operations
- Matrix Operations:
Transpose- Matrix/tensor transposition (swaps last two dimensions)Diag- Construct diagonal matrix from vectorInv- Matrix inverse (square matrices only, JAX lowering only)
- Reductions:
Sum- Sum all elements of an array (reduces to scalar)Norm- Euclidean (L2) norm and other norms of vectors/matrices
Note
For array manipulation operations like stacking and concatenation, see the
array module.
Example
Matrix transposition and diagonal matrices::
import openscvx as ox
import numpy as np
# Transpose a matrix
A = ox.State("A", shape=(3, 4))
A_T = A.T # Result shape (4, 3)
# Create a diagonal matrix
v = ox.State("v", shape=(5,))
D = ox.Diag(v) # Result shape (5, 5)
Reduction operations::
x = ox.State("x", shape=(3, 4))
# Sum all elements
total = ox.Sum(x) # Result is scalar
# Compute norm
magnitude = ox.Norm(x) # Result is scalar
Computing kinetic energy with norms::
v = ox.State("v", shape=(3,)) # Velocity vector
m = 10.0 # Mass
kinetic_energy = 0.5 * m * ox.Norm(v)**2
Diag
¶
Bases: Expr
Diagonal matrix construction from a vector.
Creates a square diagonal matrix from a 1D vector. The vector elements become the diagonal entries, with all off-diagonal entries set to zero. This is analogous to numpy.diag() or jax.numpy.diag().
Note
Currently only supports creating diagonal matrices from vectors. Extracting diagonals from matrices is not yet implemented.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
1D vector expression to place on the diagonal |
Example
Define a Diag:
v = Variable("v", shape=(3,))
D = Diag(v) # Creates a (3, 3) diagonal matrix
Source code in openscvx/symbolic/expr/linalg.py
__init__(operand: Union[Expr, float, int, np.ndarray])
¶
Initialize a diagonal matrix operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
Union[Expr, float, int, ndarray]
|
1D vector expression to place on the diagonal |
required |
check_shape() -> Tuple[int, ...]
¶
Diag converts a vector (n,) to a diagonal matrix (n,n).
Source code in openscvx/symbolic/expr/linalg.py
Inv
¶
Bases: Expr
Matrix inverse operation for symbolic expressions.
Computes the inverse of a square matrix. For batched inputs with shape (..., M, M), inverts the last two dimensions following jax.numpy.linalg.inv conventions.
The canonicalization includes an optimization that eliminates double inverses: Inv(Inv(A)) simplifies to A.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
Square matrix expression to invert |
Example
Define matrix inverse expressions::
M = Variable("M", shape=(3, 3))
M_inv = Inv(M) # Result shape (3, 3)
# Batched case
M_batch = Variable("M_batch", shape=(5, 3, 3))
M_batch_inv = Inv(M_batch) # Result shape (5, 3, 3)
Note
Matrix inverse is non-convex and only supported in JAX lowering. CVXPy lowering will raise NotImplementedError since inv(X) is neither convex nor concave for variable matrices.
Warning
Solving a matrix inverse inside an optimization loop can be somewhat of an oxymoron and performance may be severly impacted. Consider whether your problem can be reformulated to avoid the inverse.
Source code in openscvx/symbolic/expr/linalg.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
__init__(operand: Union[Expr, float, int, np.ndarray])
¶
Initialize a matrix inverse operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
Union[Expr, float, int, ndarray]
|
Square matrix expression to invert. Must have shape (..., M, M) where the last two dimensions are equal. |
required |
Source code in openscvx/symbolic/expr/linalg.py
canonicalize() -> Expr
¶
Canonicalize the operand with double inverse optimization and constant folding.
Source code in openscvx/symbolic/expr/linalg.py
check_shape() -> Tuple[int, ...]
¶
Matrix inverse preserves shape; validates square matrix.
Source code in openscvx/symbolic/expr/linalg.py
Norm
¶
Bases: Expr
Norm operation for symbolic expressions (reduction to scalar).
Computes the norm of an expression according to the specified order parameter. This is a reduction operation that always produces a scalar result regardless of the input shape. Supports various norm types following NumPy/SciPy conventions.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
Expression to compute norm of |
|
ord |
Norm order specification (default: "fro" for Frobenius norm) - "fro": Frobenius norm (default) - "inf": Infinity norm - 1: L1 norm (sum of absolute values) - 2: L2 norm (Euclidean norm) - Other values as supported by the backend |
Example
Define Norms:
x = Variable("x", shape=(3,))
euclidean_norm = Norm(x, ord=2) # L2 norm, result is scalar
A = Variable("A", shape=(3, 4))
frobenius_norm = Norm(A) # Frobenius norm, result is scalar
Source code in openscvx/symbolic/expr/linalg.py
__init__(operand: Union[Expr, float, int, np.ndarray], ord: Union[str, int, float] = 'fro')
¶
Initialize a norm operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
Union[Expr, float, int, ndarray]
|
Expression to compute norm of |
required |
ord
|
Union[str, int, float]
|
Norm order specification (default: "fro") |
'fro'
|
Source code in openscvx/symbolic/expr/linalg.py
canonicalize() -> Expr
¶
Canonicalize the operand but preserve the ord parameter.
check_shape() -> Tuple[int, ...]
¶
Norm reduces any shape to a scalar.
Sum
¶
Bases: Expr
Sum reduction operation for symbolic expressions.
Sums all elements of an expression, reducing it to a scalar. This is a reduction operation that collapses all dimensions.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
Expression whose elements will be summed |
Example
Define a Sum expression::
x = ox.State("x", shape=(3, 4))
total = Sum(x) # Creates Sum(x), result shape ()
Source code in openscvx/symbolic/expr/linalg.py
__init__(operand: Union[Expr, float, int, np.ndarray])
¶
Initialize a sum reduction operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
Union[Expr, float, int, ndarray]
|
Expression to sum over all elements |
required |
canonicalize() -> Expr
¶
Canonicalize sum: canonicalize the operand.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the sum expression |
check_shape() -> Tuple[int, ...]
¶
Sum reduces any shape to a scalar.
Transpose
¶
Bases: Expr
Matrix transpose operation for symbolic expressions.
Transposes the last two dimensions of an expression. For matrices, this swaps rows and columns. For higher-dimensional arrays, it swaps the last two axes. Scalars and vectors are unchanged by transposition.
The canonicalization includes an optimization that eliminates double transposes: (A.T).T simplifies to A.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
Expression to transpose |
Example
Define Tranpose expressions:
A = Variable("A", shape=(3, 4))
A_T = Transpose(A) # or A.T, result shape (4, 3)
v = Variable("v", shape=(5,))
v_T = Transpose(v) # result shape (5,) - vectors unchanged
Source code in openscvx/symbolic/expr/linalg.py
__init__(operand: Union[Expr, float, int, np.ndarray])
¶
Initialize a transpose operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
Union[Expr, float, int, ndarray]
|
Expression to transpose |
required |
canonicalize() -> Expr
¶
Canonicalize the operand with double transpose optimization.
Source code in openscvx/symbolic/expr/linalg.py
check_shape() -> Tuple[int, ...]
¶
Matrix transpose operation swaps the last two dimensions.