1 core.vectors.VectorVariable

core.vectors.VectorVariable(name, size, lb=None, ub=None, domain='continuous')

A vector of optimization variables.

VectorVariable creates and manages a collection of scalar Variable instances, providing natural indexing, slicing, and iteration.

1.1 Parameters

Name Type Description Default
name str Base name for the vector. Elements are named “{name}[0]”, “{name}[1]”, etc. required
size int Number of elements in the vector. 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'

1.2 Example

x = VectorVariable(“x”, 5, lb=0) x[0] # Variable named “x[0]” with lb=0 x[1:3] # VectorVariable with elements x[1], x[2] len(x) # 5 for v in x: print(v.name) # x[0], x[1], …, x[4]

1.3 Methods

Name Description
dot Compute dot product with another vector.
eq Element-wise == constraint.
from_numpy Create a VectorVariable with size inferred from a NumPy array.
get_variables Return all variables in this vector.
norm Compute the norm of this vector.
sum Compute sum of all elements in the vector.
to_numpy Extract solution values as a NumPy array.

1.3.1 dot

core.vectors.VectorVariable.dot(other)

Compute dot product with another vector.

1.3.1.1 Parameters

Name Type Description Default
other VectorVariable | VectorExpression Vector to compute dot product with. required

1.3.1.2 Returns

Name Type Description
DotProduct DotProduct expression (scalar).

1.3.1.3 Example

x = VectorVariable(“x”, 3) y = VectorVariable(“y”, 3) d = x.dot(y) d.evaluate({“x[0]”: 1, “x[1]”: 2, “x[2]”: 3, “y[0]”: 4, “y[1]”: 5, “y[2]”: 6}) 32.0

1.3.2 eq

core.vectors.VectorVariable.eq(other)

Element-wise == constraint.

1.3.2.1 Example

x = VectorVariable(“x”, 3) y = VectorVariable(“y”, 3) constraints = x.eq(y) # 3 constraints: x[i] == y[i]

1.3.3 from_numpy

core.vectors.VectorVariable.from_numpy(
    name,
    array,
    lb=None,
    ub=None,
    domain='continuous',
)

Create a VectorVariable with size inferred from a NumPy array.

The array values are not stored - this is just a convenience method to create a vector matching the array’s shape.

1.3.3.1 Parameters

Name Type Description Default
name str Base name for the vector variables. required
array np.ndarray NumPy array to match size (1D array expected). required
lb float | None Lower bound for all variables. None
ub float | None Upper bound for all variables. None
domain DomainType Variable domain type. 'continuous'

1.3.3.2 Returns

Name Type Description
VectorVariable VectorVariable with size matching the array.

1.3.3.3 Example

import numpy as np data = np.array([1.0, 2.0, 3.0, 4.0]) x = VectorVariable.from_numpy(“x”, data, lb=0) len(x) 4

1.3.4 get_variables

core.vectors.VectorVariable.get_variables()

Return all variables in this vector.

1.3.4.1 Returns

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

1.3.5 norm

core.vectors.VectorVariable.norm(ord=2)

Compute the norm of this vector.

1.3.5.1 Parameters

Name Type Description Default
ord int Order of the norm. 2 for L2 (Euclidean), 1 for L1 (Manhattan). 2

1.3.5.2 Returns

Name Type Description
L2Norm | L1Norm L2Norm or L1Norm expression (scalar).

1.3.5.3 Example

x = VectorVariable(“x”, 2) n = x.norm() # L2 norm n.evaluate({“x[0]”: 3, “x[1]”: 4}) 5.0 n1 = x.norm(1) # L1 norm n1.evaluate({“x[0]”: 3, “x[1]”: -4}) 7.0

1.3.6 sum

core.vectors.VectorVariable.sum()

Compute sum of all elements in the vector.

1.3.6.1 Returns

Name Type Description
VectorSum VectorSum expression (scalar).

1.3.6.2 Example

x = VectorVariable(“x”, 3) s = x.sum() s.evaluate({“x[0]”: 1, “x[1]”: 2, “x[2]”: 3}) 6.0

1.3.7 to_numpy

core.vectors.VectorVariable.to_numpy(solution)

Extract solution values as a NumPy array.

1.3.7.1 Parameters

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

1.3.7.2 Returns

Name Type Description
np.ndarray NumPy array of solution values in order.

1.3.7.3 Example

x = VectorVariable(“x”, 3) solution = {“x[0]”: 1.0, “x[1]”: 2.0, “x[2]”: 3.0} x.to_numpy(solution) array([1., 2., 3.])