2 Quadratic form optimization: x.dot(Q @ x) -> QuadraticForm

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 | Sequence[float] | NDArray | None Lower bound applied to all elements (None for unbounded). None
ub float | Sequence[float] | NDArray | 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 Attributes

Name Description
T Transpose is not supported for vectors.

1.4 Methods

Name Description
between Create element-wise range constraints: lb <= self <= ub.
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.4.1 between

core.vectors.VectorVariable.between(lb, ub)

Create element-wise range constraints: lb <= self <= ub.

1.4.1.1 Returns

Name Type Description
list[Constraint] List of constraints covering both bounds.

1.4.2 dot

core.vectors.VectorVariable.dot(other)

Compute dot product with another vector.

Special case: if other is a MatrixVectorProduct of A @ self, this returns a QuadraticForm for optimized gradient computation. That is, x.dot(A @ x) returns QuadraticForm(x, A) which has an O(1) gradient rule instead of the general O(n) DotProduct gradient.

1.4.2.1 Parameters

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

1.4.2.2 Returns

Name Type Description
DotProduct | Expression DotProduct expression (scalar), or QuadraticForm if other is A @ self.

1.4.2.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

import numpy as np Q = np.eye(3) qf = x.dot(Q @ x) # Returns QuadraticForm, not DotProduct

2.0.1 eq

core.vectors.VectorVariable.eq(other)

Element-wise == constraint.

2.0.1.1 Example

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

2.0.2 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.

2.0.2.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'

2.0.2.2 Returns

Name Type Description
VectorVariable VectorVariable with size matching the array.

2.0.2.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

2.0.3 get_variables

core.vectors.VectorVariable.get_variables()

Return all variables in this vector.

2.0.3.1 Returns

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

2.0.4 norm

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

Compute the norm of this vector.

2.0.4.1 Parameters

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

2.0.4.2 Returns

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

2.0.4.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

2.0.5 sum

core.vectors.VectorVariable.sum()

Compute sum of all elements in the vector.

2.0.5.1 Returns

Name Type Description
VectorSum VectorSum expression (scalar).

2.0.5.2 Example

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

2.0.6 to_numpy

core.vectors.VectorVariable.to_numpy(solution)

Extract solution values as a NumPy array.

2.0.6.1 Parameters

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

2.0.6.2 Returns

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

2.0.6.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.])