Add Scalar{Mul, Div, Add, Sub} for matrices.

This commit is contained in:
Sébastien Crozet 2013-08-12 17:04:53 +02:00
parent 037cf95542
commit a46b063192
2 changed files with 177 additions and 0 deletions

View File

@ -17,6 +17,7 @@ use traits::indexable::Indexable;
use traits::column::Column; use traits::column::Column;
use traits::iterable::{Iterable, IterableMut}; use traits::iterable::{Iterable, IterableMut};
use traits::outer::Outer; use traits::outer::Outer;
use traits::scalar_op::{ScalarMul, ScalarAdd, ScalarDiv, ScalarSub};
pub use traits::mat_cast::*; pub use traits::mat_cast::*;
pub use traits::column::*; pub use traits::column::*;
@ -39,6 +40,10 @@ mat_impl!(Mat1, m11)
mat_cast_impl!(Mat1, m11) mat_cast_impl!(Mat1, m11)
add_impl!(Mat1, m11) add_impl!(Mat1, m11)
sub_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) one_impl!(Mat1, _1)
iterable_impl!(Mat1, 1) iterable_impl!(Mat1, 1)
iterable_mut_impl!(Mat1, 1) iterable_mut_impl!(Mat1, 1)
@ -71,6 +76,14 @@ add_impl!(Mat2, m11, m12,
m21, m22) m21, m22)
sub_impl!(Mat2, m11, m12, sub_impl!(Mat2, m11, m12,
m21, m22) 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, one_impl!(Mat2, _1, _0,
_0, _1) _0, _1)
iterable_impl!(Mat2, 2) iterable_impl!(Mat2, 2)
@ -109,6 +122,18 @@ add_impl!(Mat3, m11, m12, m13,
sub_impl!(Mat3, m11, m12, m13, sub_impl!(Mat3, m11, m12, m13,
m21, m22, m23, m21, m22, m23,
m31, m32, m33) 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, one_impl!(Mat3, _1, _0, _0,
_0, _1, _0, _0, _1, _0,
_0, _0, _1) _0, _0, _1)
@ -161,6 +186,30 @@ sub_impl!(Mat4,
m31, m32, m33, m34, m31, m32, m33, m34,
m41, m42, m43, m44 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, one_impl!(Mat4, _1, _0, _0, _0,
_0, _1, _0, _0, _0, _1, _0, _0,
_0, _0, _1, _0, _0, _0, _1, _0,
@ -226,6 +275,34 @@ sub_impl!(Mat5,
m41, m42, m43, m44, m45, m41, m42, m43, m44, m45,
m51, m52, m53, m54, m55 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_impl!(Mat5, 5)
iterable_mut_impl!(Mat5, 5) iterable_mut_impl!(Mat5, 5)
dim_impl!(Mat5, 5) dim_impl!(Mat5, 5)
@ -285,6 +362,38 @@ sub_impl!(Mat6,
m51, m52, m53, m54, m55, m56, m51, m52, m53, m54, m55, m56,
m61, m62, m63, m64, m65, m66 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, one_impl!(Mat6,
_1, _0, _0, _0, _0, _0, _1, _0, _0, _0, _0, _0,
_0, _1, _0, _0, _0, _0, _0, _1, _0, _0, _0, _0,

View File

@ -47,6 +47,74 @@ macro_rules! sub_impl(
) )
) )
macro_rules! scalar_mul_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Mul<N, N>> ScalarMul<N> for $t<N> {
#[inline]
pub fn scalar_mul(&self, other: &N) -> $t<N> {
$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<N: Div<N, N>> ScalarDiv<N> for $t<N> {
#[inline]
pub fn scalar_div(&self, other: &N) -> $t<N> {
$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<N: Add<N, N>> ScalarAdd<N> for $t<N> {
#[inline]
pub fn scalar_add(&self, other: &N) -> $t<N> {
$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<N: Sub<N, N>> ScalarSub<N> for $t<N> {
#[inline]
pub fn scalar_sub(&self, other: &N) -> $t<N> {
$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( macro_rules! iterable_impl(
($t: ident, $dim: expr) => ( ($t: ident, $dim: expr) => (
impl<N> Iterable<N> for $t<N> { impl<N> Iterable<N> for $t<N> {