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