use crate::natural_vector::NaturalVector;
use core::cmp::min;
use malachite_base::num::arithmetic::traits::{EntrywiseMin, EntrywiseMinAssign};
fn assert_same_dimension(v: &NaturalVector, w: &NaturalVector) {
assert_eq!(
v.elements.len(),
w.elements.len(),
"cannot take the entrywise minimum of vectors of different dimensions"
);
}
impl EntrywiseMin<Self> for NaturalVector {
type Output = Self;
#[inline]
fn entrywise_min(mut self, other: Self) -> Self {
self.entrywise_min_assign(other);
self
}
}
impl EntrywiseMin<&Self> for NaturalVector {
type Output = Self;
#[inline]
fn entrywise_min(mut self, other: &Self) -> Self {
self.entrywise_min_assign(other);
self
}
}
impl EntrywiseMin<NaturalVector> for &NaturalVector {
type Output = NaturalVector;
#[inline]
fn entrywise_min(self, mut other: NaturalVector) -> NaturalVector {
other.entrywise_min_assign(self);
other
}
}
impl EntrywiseMin<&NaturalVector> for &NaturalVector {
type Output = NaturalVector;
fn entrywise_min(self, other: &NaturalVector) -> NaturalVector {
assert_same_dimension(self, other);
NaturalVector {
elements: self
.elements
.iter()
.zip(&other.elements)
.map(|(x, y)| min(x, y).clone())
.collect(),
}
}
}
impl EntrywiseMinAssign<Self> for NaturalVector {
fn entrywise_min_assign(&mut self, other: Self) {
assert_same_dimension(self, &other);
for (x, y) in self.elements.iter_mut().zip(other.elements) {
if y < *x {
*x = y;
}
}
}
}
impl EntrywiseMinAssign<&Self> for NaturalVector {
fn entrywise_min_assign(&mut self, other: &Self) {
assert_same_dimension(self, other);
for (x, y) in self.elements.iter_mut().zip(&other.elements) {
if y < x {
x.clone_from(y);
}
}
}
}