optimization_engine/constraints/zero.rs
1use super::Constraint;
2use crate::FunctionCallResult;
3use num::Float;
4
5#[derive(Clone, Copy, Default)]
6/// Set Zero, $\\{0\\}$
7pub struct Zero {}
8
9impl Zero {
10 /// Constructs new instance of `Zero`
11 ///
12 /// # Example
13 ///
14 /// ```
15 /// use optimization_engine::constraints::{Constraint, Zero};
16 ///
17 /// let zero = Zero::new();
18 /// let mut x = [1.0, -2.0, 3.0];
19 /// zero.project(&mut x).unwrap();
20 /// ```
21 #[must_use]
22 pub fn new() -> Self {
23 Zero {}
24 }
25}
26
27impl<T: Float> Constraint<T> for Zero {
28 /// Computes the projection on $\\{0\\}$, that is, $\Pi_{\\{0\\}}(x) = 0$
29 /// for all $x$
30 fn project(&self, x: &mut [T]) -> FunctionCallResult {
31 x.iter_mut().for_each(|xi| *xi = T::zero());
32 Ok(())
33 }
34
35 fn is_convex(&self) -> bool {
36 true
37 }
38}