diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 3f016ee..5264958 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -97,6 +97,29 @@ macro_rules! impl_vector { } } + impl Vector + { + #[inline] + pub fn dot(self,rhs:Vector)->V + where + T:core::ops::Mul, + V:core::iter::Sum, + { + self.array.into_iter().zip(rhs.array).map(|(a,b)|a*b).sum() + } + } + + impl Vector + where + T:core::ops::Mul+Copy, + V:core::iter::Sum, + { + #[inline] + pub fn length_squared(self)->V{ + self.array.into_iter().map(|t|t*t).sum() + } + } + // Impl arithmetic operators $crate::impl_vector_assign_operator!(AddAssign, add_assign ); $crate::impl_vector_operator!(Add, add ); @@ -197,6 +220,22 @@ macro_rules! impl_vector_named_fields { #[macro_export(local_inner_macros)] macro_rules! impl_vector_3 { ()=>{ + impl Vector<3,T> + { + #[inline] + pub fn cross(self,rhs:Vector<3,U>)->Vector<3,::Output> + where + T:core::ops::Mul+Copy, + U:Copy, + V:core::ops::Sub, + { + Vector::new([ + self.y*rhs.z-self.z*rhs.y, + self.z*rhs.x-self.x*rhs.z, + self.x*rhs.y-self.y*rhs.x, + ]) + } + } #[cfg(feature="fixed_wide")] $crate::impl_vector_wide_3!(); } diff --git a/fixed_wide_vectors/src/tests/tests.rs b/fixed_wide_vectors/src/tests/tests.rs index d81a7e0..8883fa2 100644 --- a/fixed_wide_vectors/src/tests/tests.rs +++ b/fixed_wide_vectors/src/tests/tests.rs @@ -8,6 +8,11 @@ fn test_bool(){ assert_eq!(Vector3::new([true,true,true]).all(),true); } +#[test] +fn test_length_squared(){ + assert_eq!(Vector3::new([1,2,3]).length_squared(),14); +} + #[test] fn test_arithmetic(){ let a=Vector3::new([1,2,3]);