From a46b063192d375660d986e4f346f540b6ef3ad84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 12 Aug 2013 17:04:53 +0200 Subject: [PATCH] Add Scalar{Mul, Div, Add, Sub} for matrices. --- src/mat.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++++ src/mat_macros.rs | 68 +++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/src/mat.rs b/src/mat.rs index eaa56619..971034eb 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -17,6 +17,7 @@ use traits::indexable::Indexable; use traits::column::Column; use traits::iterable::{Iterable, IterableMut}; use traits::outer::Outer; +use traits::scalar_op::{ScalarMul, ScalarAdd, ScalarDiv, ScalarSub}; pub use traits::mat_cast::*; pub use traits::column::*; @@ -39,6 +40,10 @@ mat_impl!(Mat1, m11) mat_cast_impl!(Mat1, m11) add_impl!(Mat1, m11) sub_impl!(Mat1, m11) +scalar_mul_impl!(Mat1, m11) +scalar_div_impl!(Mat1, m11) +scalar_add_impl!(Mat1, m11) +scalar_sub_impl!(Mat1, m11) one_impl!(Mat1, _1) iterable_impl!(Mat1, 1) iterable_mut_impl!(Mat1, 1) @@ -71,6 +76,14 @@ add_impl!(Mat2, m11, m12, m21, m22) sub_impl!(Mat2, m11, m12, m21, m22) +scalar_mul_impl!(Mat2, m11, m12, + m21, m22) +scalar_div_impl!(Mat2, m11, m12, + m21, m22) +scalar_add_impl!(Mat2, m11, m12, + m21, m22) +scalar_sub_impl!(Mat2, m11, m12, + m21, m22) one_impl!(Mat2, _1, _0, _0, _1) iterable_impl!(Mat2, 2) @@ -109,6 +122,18 @@ add_impl!(Mat3, m11, m12, m13, sub_impl!(Mat3, m11, m12, m13, m21, m22, m23, m31, m32, m33) +scalar_mul_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) +scalar_div_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) +scalar_add_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) +scalar_sub_impl!(Mat3, m11, m12, m13, + m21, m22, m23, + m31, m32, m33) one_impl!(Mat3, _1, _0, _0, _0, _1, _0, _0, _0, _1) @@ -161,6 +186,30 @@ sub_impl!(Mat4, m31, m32, m33, m34, m41, m42, m43, m44 ) +scalar_mul_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) +scalar_div_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) +scalar_add_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) +scalar_sub_impl!(Mat4, + m11, m12, m13, m14, + m21, m22, m23, m24, + m31, m32, m33, m34, + m41, m42, m43, m44 +) one_impl!(Mat4, _1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, _0, @@ -226,6 +275,34 @@ sub_impl!(Mat5, m41, m42, m43, m44, m45, m51, m52, m53, m54, m55 ) +scalar_mul_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) +scalar_div_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) +scalar_add_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) +scalar_sub_impl!(Mat5, + m11, m12, m13, m14, m15, + m21, m22, m23, m24, m25, + m31, m32, m33, m34, m35, + m41, m42, m43, m44, m45, + m51, m52, m53, m54, m55 +) iterable_impl!(Mat5, 5) iterable_mut_impl!(Mat5, 5) dim_impl!(Mat5, 5) @@ -285,6 +362,38 @@ sub_impl!(Mat6, m51, m52, m53, m54, m55, m56, m61, m62, m63, m64, m65, m66 ) +scalar_mul_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) +scalar_div_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) +scalar_add_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) +scalar_sub_impl!(Mat6, + m11, m12, m13, m14, m15, m16, + m21, m22, m23, m24, m25, m26, + m31, m32, m33, m34, m35, m36, + m41, m42, m43, m44, m45, m46, + m51, m52, m53, m54, m55, m56, + m61, m62, m63, m64, m65, m66 +) one_impl!(Mat6, _1, _0, _0, _0, _0, _0, _0, _1, _0, _0, _0, _0, diff --git a/src/mat_macros.rs b/src/mat_macros.rs index 64444327..289f070b 100644 --- a/src/mat_macros.rs +++ b/src/mat_macros.rs @@ -47,6 +47,74 @@ macro_rules! sub_impl( ) ) +macro_rules! scalar_mul_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> ScalarMul for $t { + #[inline] + pub fn scalar_mul(&self, other: &N) -> $t { + $t::new(self.$comp0 * *other $(, self.$compN * *other )*) + } + + #[inline] + pub fn scalar_mul_inplace(&mut self, other: &N) { + self.$comp0 = self.$comp0 * *other; + $(self.$compN = self.$compN * *other; )* + } + } + ) +) + +macro_rules! scalar_div_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> ScalarDiv for $t { + #[inline] + pub fn scalar_div(&self, other: &N) -> $t { + $t::new(self.$comp0 / *other $(, self.$compN / *other )*) + } + + #[inline] + pub fn scalar_div_inplace(&mut self, other: &N) { + self.$comp0 = self.$comp0 / *other; + $(self.$compN = self.$compN / *other; )* + } + } + ) +) + +macro_rules! scalar_add_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> ScalarAdd for $t { + #[inline] + pub fn scalar_add(&self, other: &N) -> $t { + $t::new(self.$comp0 + *other $(, self.$compN + *other )*) + } + + #[inline] + pub fn scalar_add_inplace(&mut self, other: &N) { + self.$comp0 = self.$comp0 + *other; + $(self.$compN = self.$compN + *other; )* + } + } + ) +) + +macro_rules! scalar_sub_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl> ScalarSub for $t { + #[inline] + pub fn scalar_sub(&self, other: &N) -> $t { + $t::new(self.$comp0 - *other $(, self.$compN - *other )*) + } + + #[inline] + pub fn scalar_sub_inplace(&mut self, other: &N) { + self.$comp0 = self.$comp0 - *other; + $(self.$compN = self.$compN - *other; )* + } + } + ) +) + macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( impl Iterable for $t {