From e879610232a399f35b72e14c83b51873c7047db2 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Wed, 7 Sep 2022 22:55:41 +0200 Subject: [PATCH] Add extra vector operators Add Add/Sub/Mul/Div by scalar Add Neg --- narcissus-maths/src/lib.rs | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/narcissus-maths/src/lib.rs b/narcissus-maths/src/lib.rs index 23050f8..174f69f 100644 --- a/narcissus-maths/src/lib.rs +++ b/narcissus-maths/src/lib.rs @@ -141,5 +141,81 @@ macro_rules! impl_vector { Self::dot(self, self) } } + + impl std::ops::Neg for $name { + type Output = $name; + #[inline(always)] + fn neg(self) -> Self::Output { + self.map(|x| -x) + } + } + + impl std::ops::Add<$t> for $name { + type Output = $name; + #[inline(always)] + fn add(self, rhs: $t) -> Self::Output { + self.map(|x| x + rhs) + } + } + + impl std::ops::Sub<$t> for $name { + type Output = $name; + #[inline(always)] + fn sub(self, rhs: $t) -> Self::Output { + self.map(|x| x - rhs) + } + } + + impl std::ops::Mul<$t> for $name { + type Output = $name; + #[inline(always)] + fn mul(self, rhs: $t) -> Self::Output { + self.map(|x| x * rhs) + } + } + + impl std::ops::Mul<$name> for $t { + type Output = $name; + #[inline(always)] + fn mul(self, rhs: $name) -> Self::Output { + rhs.map(|x| self * x) + } + } + + impl std::ops::Div<$t> for $name { + type Output = $name; + #[inline(always)] + fn div(self, rhs: $t) -> Self::Output { + self.map(|x| x / rhs) + } + } + + impl std::ops::AddAssign<$t> for $name { + #[inline(always)] + fn add_assign(&mut self, rhs: $t) { + *self = *self + rhs; + } + } + + impl std::ops::SubAssign<$t> for $name { + #[inline(always)] + fn sub_assign(&mut self, rhs: $t) { + *self = *self - rhs; + } + } + + impl std::ops::MulAssign<$t> for $name { + #[inline(always)] + fn mul_assign(&mut self, rhs: $t) { + *self = *self * rhs; + } + } + + impl std::ops::DivAssign<$t> for $name { + #[inline(always)] + fn div_assign(&mut self, rhs: $t) { + *self = *self / rhs; + } + } }; } -- 2.49.0