Source code for mpt4py.functions.affine_function

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

[docs] class AffineFunction(FunctionBase): """ Class representing a (vector-valued) affine function of the form f(x) = Fx + g. """ def __init__(self, F: Matrix, g: Optional[Vector]): super().__init__() F = np.array(F) if np.ndim(F) != 2: raise ValueError("AffineFunction: F must be a matrix.") if g is None: g = np.zeros(F.shape[0]) else: g = np.array(g).reshape((-1,)) if np.ndim(g) != 1: raise ValueError("AffineFunction: g must be a vector.") self._F = np.array(F) self._g = np.array(g) self._lambda_expr = lambda x: self._F @ x + self._g
[docs] def gradient(self, x: Vector) -> Vector: """ Compute the gradient of the affine function at a given point x. """ return self._F.T
@property def F(self) -> Matrix: return self._F @property def g(self) -> Vector: return self._g