array
Array manipulation operations for symbolic expressions.
This module provides operations for indexing, slicing, concatenating, and stacking symbolic expressions. These are structural operations that manipulate array shapes and combine or extract array elements, as opposed to mathematical transformations.
Key Operations:
-
Indexing and Slicing:
Index- NumPy-style indexing and slicing to extract subarrays
-
Concatenation:
Concat- Concatenate expressions along the first dimension (axis 0)
-
Stacking:
Stack- Stack expressions along a new first dimensionHstack- Horizontal stacking (along columns for 2D arrays)Vstack- Vertical stacking (along rows for 2D arrays)
-
Block Matrix Construction:
Block- Assemble block matrices from nested arrays (like numpy.block)
All operations follow NumPy conventions for shapes and indexing behavior, enabling familiar array manipulation patterns in symbolic optimization problems.
Example
Indexing and slicing arrays::
import openscvx as ox
x = ox.State("x", shape=(10,))
first_half = x[0:5] # Slice: Index(x, slice(0, 5))
element = x[3] # Single element: Index(x, 3)
A = ox.State("A", shape=(5, 4))
row = A[2, :] # Extract row
col = A[:, 1] # Extract column
Concatenating expressions::
from openscvx.symbolic.expr.array import Concat
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(4,))
combined = Concat(x, y) # Result shape (7,)
Stacking to build matrices::
from openscvx.symbolic.expr.array import Stack, Hstack, Vstack
# Stack vectors into a matrix
v1 = ox.State("v1", shape=(3,))
v2 = ox.State("v2", shape=(3,))
v3 = ox.State("v3", shape=(3,))
matrix = Stack([v1, v2, v3]) # Result shape (3, 3)
# Horizontal stacking (concatenate along columns)
A = ox.State("A", shape=(3, 4))
B = ox.State("B", shape=(3, 2))
wide = Hstack([A, B]) # Result shape (3, 6)
# Vertical stacking (concatenate along rows)
C = ox.State("C", shape=(2, 4))
tall = Vstack([A, C]) # Result shape (5, 4)
Building rotation matrices with Block (recommended)::
import openscvx as ox
from openscvx.symbolic.expr.array import Block
theta = ox.Variable("theta", shape=(1,))
R = Block([
[ox.Cos(theta), -ox.Sin(theta)],
[ox.Sin(theta), ox.Cos(theta)]
]) # 2D rotation matrix, shape (2, 2)
Building rotation matrices with stacking (alternative)::
import openscvx as ox
from openscvx.symbolic.expr.array import Stack, Hstack
theta = ox.Variable("theta", shape=(1,))
R = Stack([
Hstack([ox.Cos(theta), -ox.Sin(theta)]),
Hstack([ox.Sin(theta), ox.Cos(theta)])
]) # 2D rotation matrix, shape (2, 2)
Block
¶
Bases: Expr
Block matrix/tensor construction from nested arrays of expressions.
Assembles a block matrix (or N-D tensor) from a nested list of expressions, analogous to numpy.block(). Each inner list represents a row of blocks, and blocks within the same row are concatenated horizontally, while rows are stacked vertically.
This provides a convenient way to construct matrices from sub-expressions without manually nesting Stack/Hstack/Vstack operations.
Attributes:
| Name | Type | Description |
|---|---|---|
blocks |
Nested list of expressions forming the block structure (each expression can be a scalar, 1D, 2D, or N-D tensor) |
Example
Build a 2D rotation matrix::
import openscvx as ox
from openscvx.symbolic.expr.array import Block
theta = ox.Variable("theta", shape=(1,))
R = Block([
[ox.Cos(theta), -ox.Sin(theta)],
[ox.Sin(theta), ox.Cos(theta)]
]) # Result shape (2, 2)
Build a block diagonal matrix::
A = ox.State("A", shape=(2, 2))
B = ox.State("B", shape=(3, 3))
zeros_23 = ox.Constant(np.zeros((2, 3)))
zeros_32 = ox.Constant(np.zeros((3, 2)))
block_diag = Block([
[A, zeros_23],
[zeros_32, B]
]) # Result shape (5, 5)
Build from scalars and expressions::
x = ox.State("x", shape=(1,))
y = ox.State("y", shape=(1,))
# Scalars are automatically promoted to 1D arrays
M = Block([
[x, 0],
[0, y]
]) # Result shape (2, 2)
Note
- All blocks in the same row must have the same height (first dimension)
- All blocks in the same column must have the same width (second dimension)
- For N-D tensors (3D+), all trailing dimensions must match across all blocks
- Scalar values and raw Python lists are automatically wrapped via to_expr()
- 1D arrays are treated as row vectors when determining block dimensions
- N-D tensors are supported for JAX lowering; CVXPy only supports 2D blocks
Source code in openscvx/symbolic/expr/array.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
__init__(blocks: List[Union[Expr, float, int, np.ndarray, List]])
¶
Initialize a block matrix construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks
|
List[Union[Expr, float, int, ndarray, List]]
|
A nested list of expressions. Can be either: - 2D: [[row1_blocks], [row2_blocks], ...] for multiple rows - 1D: [block1, block2, ...] for a single row (auto-promoted to [[...]]) Raw values (numbers, lists, numpy arrays) are automatically converted to Constant expressions. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If blocks is empty |
Source code in openscvx/symbolic/expr/array.py
canonicalize() -> Expr
¶
Canonicalize by recursively canonicalizing all blocks.
If the block contains only a single element ([[a]]), returns the canonicalized element directly to simplify the expression tree.
Source code in openscvx/symbolic/expr/array.py
check_shape() -> Tuple[int, ...]
¶
Validate block dimensions and compute output shape.
For 2D blocks, returns (total_rows, total_cols). For N-D blocks, returns the shape after assembling blocks along the first two axes, with trailing dimensions preserved.
Returns:
| Type | Description |
|---|---|
Tuple[int, ...]
|
Tuple representing the assembled block array shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If block dimensions are incompatible |
Source code in openscvx/symbolic/expr/array.py
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
Concat
¶
Bases: Expr
Concatenation operation for symbolic expressions.
Concatenates a sequence of expressions along their first dimension. All inputs must have the same rank and matching dimensions except for the first dimension.
Attributes:
| Name | Type | Description |
|---|---|---|
exprs |
Tuple of expressions to concatenate |
Example
Define a Concat expression:
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(4,))
z = Concat(x, y) # Creates Concat(x, y), result shape (7,)
Source code in openscvx/symbolic/expr/array.py
__init__(*exprs: Expr)
¶
Initialize a concatenation operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exprs
|
Expr
|
Expressions to concatenate along the first dimension |
()
|
Source code in openscvx/symbolic/expr/array.py
canonicalize() -> Expr
¶
Canonicalize concatenation by canonicalizing all operands.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the concatenation expression |
Source code in openscvx/symbolic/expr/array.py
check_shape() -> Tuple[int, ...]
¶
Check concatenation shape compatibility and return result shape.
Source code in openscvx/symbolic/expr/array.py
Hstack
¶
Bases: Expr
Horizontal stacking operation for symbolic expressions.
Concatenates expressions horizontally (along columns for 2D arrays). This is analogous to numpy.hstack() or jax.numpy.hstack().
Behavior depends on input dimensionality: - 1D arrays: Concatenates along axis 0 (making a longer vector) - 2D arrays: Concatenates along axis 1 (columns), rows must match - Higher-D: Concatenates along axis 1, all other dimensions must match
Attributes:
| Name | Type | Description |
|---|---|---|
arrays |
List of expressions to stack horizontally |
Example
1D case: concatenate vectors:
x = Variable("x", shape=(3,))
y = Variable("y", shape=(2,))
h = Hstack([x, y]) # Result shape (5,)
2D case: concatenate matrices horizontally:
A = Variable("A", shape=(3, 4))
B = Variable("B", shape=(3, 2))
C = Hstack([A, B]) # Result shape (3, 6)
Source code in openscvx/symbolic/expr/array.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
__init__(arrays: List[Union[Expr, float, int, np.ndarray]])
¶
Initialize a horizontal stack operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arrays
|
List[Union[Expr, float, int, ndarray]]
|
List of expressions to concatenate horizontally |
required |
Source code in openscvx/symbolic/expr/array.py
check_shape() -> Tuple[int, ...]
¶
Horizontal stack concatenates arrays along the second axis (columns).
Source code in openscvx/symbolic/expr/array.py
Index
¶
Bases: Expr
Indexing and slicing operation for symbolic expressions.
Represents indexing or slicing of an expression using NumPy-style indexing. Can be created using square bracket notation on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
base |
Expression to index into |
|
index |
Index specification (int, slice, or tuple of indices/slices) |
Example
Define an Index expression:
x = ox.State("x", shape=(10,))
y = x[0:5] # Creates Index(x, slice(0, 5))
z = x[3] # Creates Index(x, 3)
Source code in openscvx/symbolic/expr/array.py
__init__(base: Expr, index: Union[int, slice, tuple])
¶
Initialize an indexing operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Expr
|
Expression to index into |
required |
index
|
Union[int, slice, tuple]
|
NumPy-style index (int, slice, or tuple of indices/slices) |
required |
Source code in openscvx/symbolic/expr/array.py
canonicalize() -> Expr
¶
Canonicalize index by canonicalizing the base expression.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the indexing expression |
check_shape() -> Tuple[int, ...]
¶
Compute the shape after indexing.
Source code in openscvx/symbolic/expr/array.py
Stack
¶
Bases: Expr
Stack expressions vertically to create a higher-dimensional array.
Stacks a list of expressions along a new first dimension. All input expressions must have the same shape. The result has shape (num_rows, *row_shape).
This is similar to numpy.array([row1, row2, ...]) or jax.numpy.stack(rows, axis=0).
Attributes:
| Name | Type | Description |
|---|---|---|
rows |
List of expressions to stack, each representing a "row" |
Example
Leverage stack to combine expressions:
x = Variable("x", shape=(3,))
y = Variable("y", shape=(3,))
z = Variable("z", shape=(3,))
stacked = Stack([x, y, z]) # Creates shape (3, 3)
# Equivalent to: [[x[0], x[1], x[2]],
# [y[0], y[1], y[2]],
# [z[0], z[1], z[2]]]
Source code in openscvx/symbolic/expr/array.py
__init__(rows: List[Union[Expr, float, int, np.ndarray]])
¶
Initialize a stack operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
List[Union[Expr, float, int, ndarray]]
|
List of expressions to stack along a new first dimension. All expressions must have the same shape. |
required |
Source code in openscvx/symbolic/expr/array.py
check_shape() -> Tuple[int, ...]
¶
Stack creates a 2D matrix from 1D rows.
Source code in openscvx/symbolic/expr/array.py
Vstack
¶
Bases: Expr
Vertical stacking operation for symbolic expressions.
Concatenates expressions vertically (along rows for 2D arrays). This is analogous to numpy.vstack() or jax.numpy.vstack().
All input expressions must have the same number of dimensions, and all dimensions except the first must match. The result concatenates along axis 0 (rows).
Attributes:
| Name | Type | Description |
|---|---|---|
arrays |
List of expressions to stack vertically |
Example
Stack vectors to create a matrix:
x = Variable("x", shape=(3,))
y = Variable("y", shape=(3,))
v = Vstack([x, y]) # Result shape (2, 3)
Stack matrices vertically:
A = Variable("A", shape=(3, 4))
B = Variable("B", shape=(2, 4))
C = Vstack([A, B]) # Result shape (5, 4)
Source code in openscvx/symbolic/expr/array.py
__init__(arrays: List[Union[Expr, float, int, np.ndarray]])
¶
Initialize a vertical stack operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arrays
|
List[Union[Expr, float, int, ndarray]]
|
List of expressions to concatenate vertically. All must have matching dimensions except the first. |
required |
Source code in openscvx/symbolic/expr/array.py
check_shape() -> Tuple[int, ...]
¶
Vertical stack concatenates arrays along the first axis (rows).