argmin/core/
nooperator.rs

1// Copyright 2018-2020 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::core::{ArgminFloat, ArgminOp, Error};
9use serde::de::DeserializeOwned;
10use serde::{Deserialize, Serialize};
11use std::fmt::{Debug, Display};
12
13/// Fake Operators for testing
14
15/// No-op operator with free choice of the types
16#[derive(
17    Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash, Copy,
18)]
19pub struct NoOperator<T, U, H, J, F> {
20    /// Fake parameter
21    param: std::marker::PhantomData<T>,
22    /// Fake output
23    output: std::marker::PhantomData<U>,
24    /// Fake Hessian
25    hessian: std::marker::PhantomData<H>,
26    /// Fake Jacobian
27    jacobian: std::marker::PhantomData<J>,
28    /// Fake Float
29    float: std::marker::PhantomData<F>,
30}
31
32impl<T, U, H, J, F> NoOperator<T, U, H, J, F> {
33    /// Constructor
34    #[allow(dead_code)]
35    pub fn new() -> Self {
36        NoOperator {
37            param: std::marker::PhantomData,
38            output: std::marker::PhantomData,
39            hessian: std::marker::PhantomData,
40            jacobian: std::marker::PhantomData,
41            float: std::marker::PhantomData,
42        }
43    }
44}
45
46impl<T, U, H, J, F> Display for NoOperator<T, U, H, J, F> {
47    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
48        write!(f, "NoOperator")
49    }
50}
51
52impl<T, U, H, J, F> ArgminOp for NoOperator<T, U, H, J, F>
53where
54    T: Clone + Default + Debug + Send + Sync + Serialize + DeserializeOwned,
55    U: Clone + Default + Debug + Send + Sync + Serialize + DeserializeOwned,
56    H: Clone + Default + Debug + Send + Sync + Serialize + DeserializeOwned,
57    J: Clone + Default + Debug + Send + Sync + Serialize + DeserializeOwned,
58    F: ArgminFloat,
59{
60    type Param = T;
61    type Output = U;
62    type Hessian = H;
63    type Jacobian = J;
64    type Float = F;
65
66    /// Do nothing, really.
67    fn apply(&self, _p: &Self::Param) -> Result<Self::Output, Error> {
68        Ok(Self::Output::default())
69    }
70
71    /// Do nothing, really.
72    fn gradient(&self, _p: &Self::Param) -> Result<Self::Param, Error> {
73        Ok(Self::Param::default())
74    }
75
76    /// Do nothing, really.
77    fn hessian(&self, _p: &Self::Param) -> Result<Self::Hessian, Error> {
78        Ok(Self::Hessian::default())
79    }
80
81    /// Do nothing, really.
82    fn modify(&self, _p: &Self::Param, _t: Self::Float) -> Result<Self::Param, Error> {
83        Ok(Self::Param::default())
84    }
85}
86
87/// Minimal No-op operator which does nothing, really.
88#[derive(
89    Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash, Copy,
90)]
91pub struct MinimalNoOperator {}
92
93/// No-op operator with fixed types (See `ArgminOp` impl on `MinimalNoOperator`)
94impl MinimalNoOperator {
95    /// Constructor
96    #[allow(dead_code)]
97    pub fn new() -> Self {
98        MinimalNoOperator {}
99    }
100}
101
102impl Display for MinimalNoOperator {
103    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
104        write!(f, "MinimalNoOperator")
105    }
106}
107
108impl ArgminOp for MinimalNoOperator {
109    type Param = Vec<f64>;
110    type Output = f64;
111    type Hessian = Vec<Vec<f64>>;
112    type Jacobian = Vec<f64>;
113    type Float = f64;
114
115    /// Do nothing, really.
116    fn apply(&self, _p: &Self::Param) -> Result<Self::Output, Error> {
117        unimplemented!()
118    }
119
120    /// Do nothing, really.
121    fn gradient(&self, _p: &Self::Param) -> Result<Self::Param, Error> {
122        unimplemented!()
123    }
124
125    /// Do nothing, really.
126    fn hessian(&self, _p: &Self::Param) -> Result<Self::Hessian, Error> {
127        unimplemented!()
128    }
129
130    /// Do nothing, really.
131    fn modify(&self, _p: &Self::Param, _t: f64) -> Result<Self::Param, Error> {
132        unimplemented!()
133    }
134}