diff --git a/src/vector.rs b/src/vector.rs index 3ce76c2..5f4b4ca 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -375,6 +375,25 @@ macro_rules! impl_vector_default { pub const fn new($($field: S),+) -> $VectorN { $VectorN { $($field: $field),+ } } + + /// Perform the given operation on each field in the vector, returning a new point + /// constructed from the operations. + #[inline] + pub fn map(self, mut f: F) -> $VectorN + where F: FnMut(S) -> U + { + $VectorN { $($field: f(self.$field)),+ } + } + + /// Construct a new vector where each component is the result of + /// applying the given operation to each pair of components of the + /// given vectors. + #[inline] + pub fn zip(self, v2: $VectorN, mut f: F) -> $VectorN + where F: FnMut(S, S2) -> S3 + { + $VectorN { $($field: f(self.$field, v2.$field)),+ } + } } /// The short constructor. @@ -383,13 +402,6 @@ macro_rules! impl_vector_default { $VectorN::new($($field),+) } - impl $VectorN { - /// True if all entries in the vector are finite - pub fn is_finite(&self) -> bool { - $(self.$field.is_finite())&&+ - } - } - impl $VectorN { /// Component-wise casting to another type. #[inline] @@ -453,17 +465,17 @@ macro_rules! impl_vector_default { } } - impl iter::Sum for $VectorN { + impl iter::Sum<$VectorN> for $VectorN { #[inline] - fn sum>(iter: I) -> Self { - iter.fold(Self::zero(), Add::add) + fn sum>>(iter: I) -> $VectorN { + iter.fold($VectorN::zero(), Add::add) } } - impl<'a, S: 'a + BaseNum> iter::Sum<&'a Self> for $VectorN { + impl<'a, S: 'a + BaseNum> iter::Sum<&'a $VectorN> for $VectorN { #[inline] - fn sum>(iter: I) -> Self { - iter.fold(Self::zero(), Add::add) + fn sum>>(iter: I) -> $VectorN { + iter.fold($VectorN::zero(), Add::add) } } @@ -518,18 +530,29 @@ macro_rules! impl_vector_default { #[cfg(feature = "rand")] impl Distribution<$VectorN> for Standard - where S: BaseFloat, - Standard: Distribution { + where Standard: Distribution, + S: BaseFloat { #[inline] fn sample(&self, rng: &mut R) -> $VectorN { $VectorN { $($field: rng.gen()),+ } } } + impl Bounded for $VectorN { + #[inline] + fn min_value() -> $VectorN { + $VectorN { $($field: S::min_value()),+ } + } + + #[inline] + fn max_value() -> $VectorN { + $VectorN { $($field: S::max_value()),+ } + } + } + impl_operator!( Add<$VectorN > for $VectorN { fn add(lhs, rhs) -> $VectorN { $VectorN::new($(lhs.$field + rhs.$field),+) } }); - impl_assignment_operator!( AddAssign<$VectorN > for $VectorN { fn add_assign(&mut self, other) { $(self.$field += other.$field);+ } }); @@ -537,7 +560,6 @@ macro_rules! impl_vector_default { impl_operator!( Sub<$VectorN > for $VectorN { fn sub(lhs, rhs) -> $VectorN { $VectorN::new($(lhs.$field - rhs.$field),+) } }); - impl_assignment_operator!( SubAssign<$VectorN > for $VectorN { fn sub_assign(&mut self, other) { $(self.$field -= other.$field);+ } }); @@ -545,7 +567,6 @@ macro_rules! impl_vector_default { impl_operator!( Mul for $VectorN { fn mul(vector, scalar) -> $VectorN { $VectorN::new($(vector.$field * scalar),+) } }); - impl_assignment_operator!( MulAssign for $VectorN { fn mul_assign(&mut self, scalar) { $(self.$field *= scalar);+ } }); @@ -553,7 +574,6 @@ macro_rules! impl_vector_default { impl_operator!( Div for $VectorN { fn div(vector, scalar) -> $VectorN { $VectorN::new($(vector.$field / scalar),+) } }); - impl_assignment_operator!( DivAssign for $VectorN { fn div_assign(&mut self, scalar) { $(self.$field /= scalar);+ } });