use super::Constraint;
pub struct Rectangle {
xmin: Option<Vec<f64>>,
xmax: Option<Vec<f64>>,
}
impl Rectangle {
pub fn new(xmin_: Vec<f64>, xmax_: Vec<f64>) -> Rectangle {
Rectangle {
xmin: Some(xmin_),
xmax: Some(xmax_),
}
}
pub fn new_only_xmin(xmin_: Vec<f64>) -> Rectangle {
Rectangle {
xmin: Some(xmin_),
xmax: None,
}
}
pub fn new_only_xmax(xmax_: Vec<f64>) -> Rectangle {
Rectangle {
xmin: None,
xmax: Some(xmax_),
}
}
}
impl Constraint for Rectangle {
fn project(&self, x: &mut [f64]) {
if let Some(xmin) = &self.xmin {
x.iter_mut().zip(xmin.iter()).for_each(|(x_, xmin_)| {
if *x_ < *xmin_ {
*x_ = *xmin_
};
});
}
if let Some(xmax) = &self.xmax {
x.iter_mut().zip(xmax.iter()).for_each(|(x_, xmax_)| {
if *x_ > *xmax_ {
*x_ = *xmax_
};
});
}
}
}