optimization_solvers/
func_eval.rs

1use super::*;
2
3// Function evaluation structure. Builder pattern
4#[derive(derive_getters::Getters, Debug)]
5pub struct FuncEval<T, H> {
6    f: Floating,
7    g: T,
8    hessian: Option<H>,
9}
10
11impl<T, H> FuncEval<T, H> {
12    pub fn new(f: Floating, g: T) -> Self {
13        FuncEval {
14            f,
15            g,
16            hessian: None,
17        }
18    }
19}
20
21pub type FuncEvalUnivariate = FuncEval<Floating, Floating>;
22pub type FuncEvalMultivariate = FuncEval<DVector<Floating>, DMatrix<Floating>>;
23
24// We define a macro that will write pub type FuncEvalMultivariatek = FuncEval<Vectork<Floating>, Matrixk<Floating>>; for arbitrary number k
25
26impl FuncEvalMultivariate {
27    pub fn with_hessian(mut self, hessian: DMatrix<Floating>) -> Self {
28        self.hessian = Some(hessian);
29        self
30    }
31    pub fn take_hessian(&mut self) -> DMatrix<Floating> {
32        self.hessian.take().unwrap()
33    }
34}
35
36impl From<(Floating, DVector<Floating>)> for FuncEvalMultivariate {
37    fn from(value: (Floating, DVector<Floating>)) -> Self {
38        let (f, g) = value;
39        FuncEvalMultivariate::new(f, g)
40    }
41}