1 core.matrices.QuadraticForm
core.matrices.QuadraticForm(vector, matrix)Quadratic form: x’ @ Q @ x where Q is a constant matrix.
This represents the scalar expression xᵀQx, commonly used for: - Portfolio variance: w’ @ Σ @ w - Regularization terms: x’ @ I @ x = ||x||² - Quadratic objectives in optimization
1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| vector | VectorVariable | VectorExpression | VectorVariable or VectorExpression (the x). | required |
| matrix | np.ndarray | 2D NumPy array (the Q matrix, should be square). | required |
1.2 Example
import numpy as np Q = np.array([[1, 0.5], [0.5, 2]]) x = VectorVariable(“x”, 2) qf = QuadraticForm(x, Q) qf.evaluate({“x[0]”: 1, “x[1]”: 1}) 4.0 # 11 + 20.511 + 2*1 = 1 + 1 + 2 = 4
1.3 Methods
| Name | Description |
|---|---|
| evaluate | Evaluate the quadratic form xᵀQx. |
| get_variables | Return all variables this expression depends on. |
| jacobian_row | Return Jacobian row using O(1) gradient rule. |
1.3.1 evaluate
core.matrices.QuadraticForm.evaluate(values)Evaluate the quadratic form xᵀQx.
1.3.2 get_variables
core.matrices.QuadraticForm.get_variables()Return all variables this expression depends on.
1.3.3 jacobian_row
core.matrices.QuadraticForm.jacobian_row(variables)Return Jacobian row using O(1) gradient rule.
For QuadraticForm(x, Q), gradient is (Q + Q.T) @ x. This computes the gradient vector in O(n²) but avoids n separate gradient() calls which each do O(n²) work.
1.3.3.1 Returns
| Name | Type | Description |
|---|---|---|
| list[Expression] | None | List of expressions, or None if optimization not applicable. |