Add outer product.

This commit is contained in:
Sébastien Crozet 2013-08-12 16:45:31 +02:00
parent 3858c63291
commit b08a8384ae
6 changed files with 47 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use traits::homogeneous::{FromHomogeneous, ToHomogeneous};
use traits::indexable::Indexable; 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;
pub use traits::mat_cast::*; pub use traits::mat_cast::*;
pub use traits::column::*; pub use traits::column::*;
@ -51,6 +52,7 @@ approx_eq_impl!(Mat1)
column_impl!(Mat1, 1) column_impl!(Mat1, 1)
to_homogeneous_impl!(Mat1, Mat2, 1, 2) to_homogeneous_impl!(Mat1, Mat2, 1, 2)
from_homogeneous_impl!(Mat1, Mat2, 1, 2) from_homogeneous_impl!(Mat1, Mat2, 1, 2)
outer_impl!(Vec1, Mat1)
/// Square matrix of dimension 2. /// Square matrix of dimension 2.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -79,6 +81,7 @@ approx_eq_impl!(Mat2)
column_impl!(Mat2, 2) column_impl!(Mat2, 2)
to_homogeneous_impl!(Mat2, Mat3, 2, 3) to_homogeneous_impl!(Mat2, Mat3, 2, 3)
from_homogeneous_impl!(Mat2, Mat3, 2, 3) from_homogeneous_impl!(Mat2, Mat3, 2, 3)
outer_impl!(Vec2, Mat2)
/// Square matrix of dimension 3. /// Square matrix of dimension 3.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -111,6 +114,7 @@ approx_eq_impl!(Mat3)
column_impl!(Mat3, 3) column_impl!(Mat3, 3)
to_homogeneous_impl!(Mat3, Mat4, 3, 4) to_homogeneous_impl!(Mat3, Mat4, 3, 4)
from_homogeneous_impl!(Mat3, Mat4, 3, 4) from_homogeneous_impl!(Mat3, Mat4, 3, 4)
outer_impl!(Vec3, Mat3)
/// Square matrix of dimension 4. /// Square matrix of dimension 4.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -151,6 +155,7 @@ approx_eq_impl!(Mat4)
column_impl!(Mat4, 4) column_impl!(Mat4, 4)
to_homogeneous_impl!(Mat4, Mat5, 4, 5) to_homogeneous_impl!(Mat4, Mat5, 4, 5)
from_homogeneous_impl!(Mat4, Mat5, 4, 5) from_homogeneous_impl!(Mat4, Mat5, 4, 5)
outer_impl!(Vec4, Mat4)
/// Square matrix of dimension 5. /// Square matrix of dimension 5.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -197,6 +202,7 @@ approx_eq_impl!(Mat5)
column_impl!(Mat5, 5) column_impl!(Mat5, 5)
to_homogeneous_impl!(Mat5, Mat6, 5, 6) to_homogeneous_impl!(Mat5, Mat6, 5, 6)
from_homogeneous_impl!(Mat5, Mat6, 5, 6) from_homogeneous_impl!(Mat5, Mat6, 5, 6)
outer_impl!(Vec5, Mat5)
/// Square matrix of dimension 6. /// Square matrix of dimension 6.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -245,3 +251,4 @@ inv_impl!(Mat6, 6)
transpose_impl!(Mat6, 6) transpose_impl!(Mat6, 6)
approx_eq_impl!(Mat6) approx_eq_impl!(Mat6)
column_impl!(Mat6, 6) column_impl!(Mat6, 6)
outer_impl!(Vec6, Mat6)

View File

@ -397,3 +397,22 @@ macro_rules! from_homogeneous_impl(
} }
) )
) )
macro_rules! outer_impl(
($t: ident, $m: ident) => (
impl<N: Mul<N, N> + Zero + Clone> Outer<$t<N>, $m<N>> for $t<N> {
#[inline]
fn outer(&self, other: &$t<N>) -> $m<N> {
let mut res = Zero::zero::<$m<N>>();
for i in range(0u, Dim::dim::<$t<N>>()) {
for j in range(0u, Dim::dim::<$t<N>>()) {
res.set((i, j), self.at(i) * other.at(j))
}
}
res
}
}
)
)

View File

@ -52,6 +52,7 @@ pub mod traits
pub mod column; pub mod column;
pub mod iterable; pub mod iterable;
pub mod dot; pub mod dot;
pub mod outer;
pub mod cross; pub mod cross;
pub mod inv; pub mod inv;
pub mod transpose; pub mod transpose;

View File

@ -7,6 +7,8 @@ use std::cmp::ApproxEq;
#[test] #[test]
use vec::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; use vec::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
#[test] #[test]
use mat::Mat3;
#[test]
use traits::basis::Basis; use traits::basis::Basis;
#[test] #[test]
use traits::cross::Cross; use traits::cross::Cross;
@ -18,6 +20,8 @@ use traits::norm::Norm;
use traits::iterable::{Iterable, IterableMut}; use traits::iterable::{Iterable, IterableMut};
#[test] #[test]
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[test]
use traits::outer::Outer;
macro_rules! test_iterator_impl( macro_rules! test_iterator_impl(
($t: ty, $n: ty) => ( ($t: ty, $n: ty) => (
@ -322,3 +326,13 @@ fn test_min_max_vec3() {
), Vec3::new(1, 2, 3) ), Vec3::new(1, 2, 3)
); );
} }
#[test]
fn test_outer_vec3() {
assert_eq!(
Vec3::new(1, 2, 3).outer(&Vec3::new(4, 5, 6)),
Mat3::new(
4, 5, 6,
8, 10, 12,
12, 15, 18));
}

5
src/traits/outer.rs Normal file
View File

@ -0,0 +1,5 @@
/// Traits of objects having an outer product.
pub trait Outer<V, M> {
/// Computes the outer product `self * other`
fn outer(&self, other: &V) -> M;
}

View File

@ -15,6 +15,7 @@ use traits::indexable::Indexable;
pub use traits::vec_cast::*; pub use traits::vec_cast::*;
pub use traits::basis::*; pub use traits::basis::*;
pub use traits::cross::*; pub use traits::cross::*;
pub use traits::outer::*;
pub use traits::dot::*; pub use traits::dot::*;
pub use traits::indexable::*; pub use traits::indexable::*;
pub use traits::iterable::*; pub use traits::iterable::*;