Source code for mpt4py.functions.quadratic_function

import numpy as np
from typing import Optional
from mpt4py.base import Matrix, Vector
from .function_base import FunctionBase

[docs] class QuadraticFunction(FunctionBase): """ Class representing a (scalar) quadratic function of the form f(x) = 1/2*x'*Q*x + F'*x + g. """ def __init__(self, Q: Matrix, F: Optional[Vector], g: float = 0.0): super().__init__() self._Q = np.array(Q) self._F = np.array(F).reshape((-1,)) if F is not None else np.zeros((self._Q.shape[0],)) self._g = g self._lambda_expr = lambda x: float(0.5 * x.T @ self._Q @ x + self._F.T @ x + self._g)
[docs] def gradient(self, x: Vector) -> Vector: """ Compute the gradient of the quadratic function at a given point x. """ return self._Q @ x + self._F