Source code for mpt4py.functions.function_base

import numpy as np
from typing import Callable, Optional
from mpt4py.base import Vector

[docs] class FunctionBase: """ Abstract base class for scalar-valued functions. """ def __init__(self, lambda_expr: Optional[Callable[[Vector], float]] = None): self._lambda_expr = lambda_expr # Placeholder for symbolic expression, if needed def __call__(self, x: Vector) -> float: """ Make the function callable. """ return self._lambda_expr(x)
[docs] def evaluate(self, x: Vector) -> float: """ Evaluate the function at a given point x. """ return self._lambda_expr(x)
[docs] def gradient(self, x: Vector) -> Vector: """ Compute the gradient of the function at a given point x. """ # TODO: consider using automatic differentiation libraries raise NotImplementedError
@property def get_lambda(self) -> Callable[[Vector], float]: """ Get the symbolic expression of the function, if available. Returns: sympy expression or None: The symbolic expression of the function. """ return self._lambda_expr