2015-01-08 04:11:09 +08:00
|
|
|
#![macro_use]
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! mat_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2015-01-10 04:55:15 +08:00
|
|
|
pub fn new($($compN: N ),+) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
$t {
|
2015-01-10 04:55:15 +08:00
|
|
|
$($compN: $compN ),+
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2014-11-21 18:21:02 +08:00
|
|
|
macro_rules! as_array_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
|
|
|
impl<N> $t<N> {
|
|
|
|
/// View this matrix as a column-major array of arrays.
|
|
|
|
#[inline]
|
2015-01-03 22:19:52 +08:00
|
|
|
pub fn as_array(&self) -> &[[N; $dim]; $dim] {
|
2014-11-21 18:21:02 +08:00
|
|
|
unsafe {
|
|
|
|
mem::transmute(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// View this matrix as a column-major mutable array of arrays.
|
|
|
|
#[inline]
|
2015-01-03 22:19:52 +08:00
|
|
|
pub fn as_array_mut<'a>(&'a mut self) -> &'a mut [[N; $dim]; $dim] {
|
2014-11-21 18:21:02 +08:00
|
|
|
unsafe {
|
|
|
|
mem::transmute(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: because of https://github.com/rust-lang/rust/issues/16418 we cannot do the
|
|
|
|
// array-to-mat conversion by-value:
|
|
|
|
//
|
2015-01-03 22:19:52 +08:00
|
|
|
// pub fn from_array(array: [N; $dim]) -> $t<N>
|
2014-11-21 18:21:02 +08:00
|
|
|
|
|
|
|
/// View a column-major array of array as a vector.
|
|
|
|
#[inline]
|
2015-01-03 22:19:52 +08:00
|
|
|
pub fn from_array_ref(array: &[[N; $dim]; $dim]) -> &$t<N> {
|
2014-11-21 18:21:02 +08:00
|
|
|
unsafe {
|
|
|
|
mem::transmute(array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// View a column-major array of array as a mutable vector.
|
|
|
|
#[inline]
|
2015-01-03 22:19:52 +08:00
|
|
|
pub fn from_array_mut(array: &mut [[N; $dim]; $dim]) -> &mut $t<N> {
|
2014-11-21 18:21:02 +08:00
|
|
|
unsafe {
|
|
|
|
mem::transmute(array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-11-21 18:21:02 +08:00
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
macro_rules! at_fast_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy> $t<N> {
|
2013-09-13 19:21:42 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub unsafe fn at_fast(&self, (i, j): (usize, usize)) -> N {
|
2015-01-03 22:19:52 +08:00
|
|
|
(*mem::transmute::<&$t<N>, &[N; $dim * $dim]>(self)
|
2015-01-05 22:12:06 +08:00
|
|
|
.get_unchecked(i + j * $dim))
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub unsafe fn set_fast(&mut self, (i, j): (usize, usize), val: N) {
|
2015-01-03 22:19:52 +08:00
|
|
|
(*mem::transmute::<&mut $t<N>, &mut [N; $dim * $dim]>(self)
|
2015-01-02 06:23:35 +08:00
|
|
|
.get_unchecked_mut(i + j * $dim)) = val
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-09-13 19:21:42 +08:00
|
|
|
|
2013-07-23 05:42:35 +08:00
|
|
|
macro_rules! mat_cast_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<Nin: Copy, Nout: Copy + Cast<Nin>> Cast<$t<Nin>> for $t<Nout> {
|
2013-10-10 04:59:44 +08:00
|
|
|
#[inline]
|
2014-11-26 21:17:34 +08:00
|
|
|
fn from(v: $t<Nin>) -> $t<Nout> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(Cast::from(v.$compN)),+)
|
2013-10-10 04:59:44 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-10-10 04:59:44 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-23 05:42:35 +08:00
|
|
|
|
2013-08-12 22:50:50 +08:00
|
|
|
macro_rules! add_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Add<N, Output = N>> Add<$t<N>> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn add(self, right: $t<N>) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN + right.$compN),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 22:50:50 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 22:50:50 +08:00
|
|
|
|
|
|
|
macro_rules! sub_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Sub<N, Output = N>> Sub<$t<N>> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn sub(self, right: $t<N>) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN - right.$compN),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 22:50:50 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 22:50:50 +08:00
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_mul_scalar_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Mul<N, Output = N>> Mul<N> for N {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn mul(self, right: N) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN * *right),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 23:04:53 +08:00
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_div_scalar_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Div<N, Output = N>> Div<N> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn div(self, right: N) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN / *right),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 23:04:53 +08:00
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_add_scalar_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Add<N, Output = N>> Add<N> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn add(self, right: N) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN + *right),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 23:04:53 +08:00
|
|
|
|
2014-05-12 01:46:04 +08:00
|
|
|
|
|
|
|
macro_rules! eye_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $dim: expr, $($comp_diagN: ident),+) => (
|
2014-05-12 01:46:04 +08:00
|
|
|
impl<N: Zero + One> Eye for $t<N> {
|
2015-01-10 05:26:05 +08:00
|
|
|
fn new_identity(dim: usize) -> $t<N> {
|
2014-11-26 21:17:34 +08:00
|
|
|
assert!(dim == $dim);
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut eye: $t<N> = ::zero();
|
|
|
|
$(eye.$comp_diagN = ::one();)+
|
2014-05-12 01:46:04 +08:00
|
|
|
eye
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-05-12 01:46:04 +08:00
|
|
|
|
2014-05-23 01:54:54 +08:00
|
|
|
macro_rules! mat_sub_scalar_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Sub<N, Output = N> Sub<N> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
#[inline]
|
2014-11-26 21:17:34 +08:00
|
|
|
fn sub(self, right: &N) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(self.$compN - *right),+)
|
2013-09-15 03:11:43 +08:00
|
|
|
}
|
2013-08-12 23:04:53 +08:00
|
|
|
}
|
2013-09-15 03:11:43 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 23:04:53 +08:00
|
|
|
|
2013-09-13 16:26:19 +08:00
|
|
|
macro_rules! absolute_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+) => (
|
2014-11-15 22:47:59 +08:00
|
|
|
impl<N: Absolute<N>> Absolute<$t<N>> for $t<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
#[inline]
|
2013-10-17 03:44:33 +08:00
|
|
|
fn abs(m: &$t<N>) -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($(::abs(&m.$compN) ),+)
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-09-13 16:26:19 +08:00
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! iterable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Iterable<N> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-12-24 02:01:49 +08:00
|
|
|
fn iter<'l>(&'l self) -> Iter<'l, N> {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
mem::transmute::<&'l $t<N>, &'l [N; $dim * $dim]>(self).iter()
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! iterable_mut_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> IterableMut<N> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-12-24 02:01:49 +08:00
|
|
|
fn iter_mut<'l>(&'l mut self) -> IterMut<'l, N> {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
mem::transmute::<&'l mut $t<N>, &'l mut [N; $dim * $dim]>(self).iter_mut()
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! one_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($valueN: expr),+ ) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy + BaseNum> One for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn one() -> $t<N> {
|
2015-01-10 04:55:15 +08:00
|
|
|
$t::new($($valueN() ),+)
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2014-11-16 21:04:15 +08:00
|
|
|
macro_rules! zero_impl(
|
2015-01-10 04:55:15 +08:00
|
|
|
($t: ident, $($compN: ident),+ ) => (
|
2014-11-16 21:04:15 +08:00
|
|
|
impl<N: Zero> Zero for $t<N> {
|
|
|
|
#[inline]
|
|
|
|
fn zero() -> $t<N> {
|
|
|
|
$t {
|
2015-01-10 04:55:15 +08:00
|
|
|
$($compN: ::zero() ),+
|
2014-11-16 21:04:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_zero(&self) -> bool {
|
2015-01-10 04:55:15 +08:00
|
|
|
$(::is_zero(&self.$compN) )&&+
|
2014-11-16 21:04:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-11-16 21:04:15 +08:00
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! dim_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Dim for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn dim(_: Option<$t<N>>) -> usize {
|
2013-08-05 16:13:44 +08:00
|
|
|
$dim
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! indexable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2015-01-10 05:26:05 +08:00
|
|
|
impl<N> Shape<(usize, usize)> for $t<N> {
|
2014-10-26 17:46:51 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn shape(&self) -> (usize, usize) {
|
2014-10-26 17:46:51 +08:00
|
|
|
($dim, $dim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 05:26:05 +08:00
|
|
|
impl<N: Copy> Indexable<(usize, usize), N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn at(&self, (i, j): (usize, usize)) -> N {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
mem::transmute::<&$t<N>, &[N; $dim * $dim]>(self)[i + j * $dim]
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn set(&mut self, (i, j): (usize, usize), val: N) {
|
2013-08-05 16:13:44 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
mem::transmute::<&mut $t<N>, &mut [N; $dim * $dim]>(self)[i + j * $dim] = val
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn swap(&mut self, (i1, j1): (usize, usize), (i2, j2): (usize, usize)) {
|
2013-08-05 15:44:56 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
mem::transmute::<&mut $t<N>, &mut [N; $dim * $dim]>(self)
|
2013-10-18 04:40:44 +08:00
|
|
|
.swap(i1 + j1 * $dim, i2 + j2 * $dim)
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
2013-12-02 03:17:18 +08:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
unsafe fn unsafe_at(&self, (i, j): (usize, usize)) -> N {
|
2015-01-05 22:12:06 +08:00
|
|
|
(*mem::transmute::<&$t<N>, &[N; $dim * $dim]>(self).get_unchecked(i + j * $dim))
|
2013-12-02 03:17:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
unsafe fn unsafe_set(&mut self, (i, j): (usize, usize), val: N) {
|
2015-01-03 22:19:52 +08:00
|
|
|
(*mem::transmute::<&mut $t<N>, &mut [N; $dim * $dim]>(self).get_unchecked_mut(i + j * $dim)) = val
|
2013-12-02 03:17:18 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2014-09-18 10:20:36 +08:00
|
|
|
macro_rules! index_impl(
|
2014-10-26 05:02:16 +08:00
|
|
|
($t: ident, $dim: expr) => (
|
2015-01-10 05:26:05 +08:00
|
|
|
impl<N> Index<(usize, usize)> for $t<N> {
|
2015-01-05 02:03:28 +08:00
|
|
|
type Output = N;
|
|
|
|
|
2015-01-10 05:26:05 +08:00
|
|
|
fn index(&self, &(i, j): &(usize, usize)) -> &N {
|
2014-09-18 10:20:36 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
&mem::transmute::<&$t<N>, &mut [N; $dim * $dim]>(self)[i + j * $dim]
|
2014-09-18 10:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 05:26:05 +08:00
|
|
|
impl<N> IndexMut<(usize, usize)> for $t<N> {
|
|
|
|
fn index_mut(&mut self, &(i, j): &(usize, usize)) -> &mut N {
|
2014-09-18 10:20:36 +08:00
|
|
|
unsafe {
|
2015-01-03 22:19:52 +08:00
|
|
|
&mut mem::transmute::<&mut $t<N>, &mut [N; $dim * $dim]>(self)[i + j * $dim]
|
2014-09-18 10:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-09-18 10:20:36 +08:00
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
macro_rules! col_slice_impl(
|
2014-08-16 18:16:26 +08:00
|
|
|
($t: ident, $tv: ident, $slice: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Clone + Copy + Zero> ColSlice<$slice<N>> for $t<N> {
|
2015-01-10 05:26:05 +08:00
|
|
|
fn col_slice(&self, cid: usize, rstart: usize, rend: usize) -> $slice<N> {
|
2014-05-12 20:06:25 +08:00
|
|
|
let col = self.col(cid);
|
2014-08-16 18:16:26 +08:00
|
|
|
|
2015-03-23 21:30:31 +08:00
|
|
|
$slice::from_slice(rend - rstart, &col.as_array()[rstart .. rend])
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-05-12 20:06:25 +08:00
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
macro_rules! row_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy + Zero> Row<$tv<N>> for $t<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn nrows(&self) -> usize {
|
2013-09-13 16:26:19 +08:00
|
|
|
Dim::dim(None::<$t<N>>)
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn set_row(&mut self, row: usize, v: $tv<N>) {
|
2014-09-20 05:51:27 +08:00
|
|
|
for (i, e) in v.iter().enumerate() {
|
2014-12-18 06:28:32 +08:00
|
|
|
self.set((row, i), *e);
|
2014-09-20 05:51:27 +08:00
|
|
|
}
|
2013-08-26 05:01:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn row(&self, row: usize) -> $tv<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $tv<N> = ::zero();
|
2014-09-20 05:51:27 +08:00
|
|
|
|
2014-10-22 19:35:17 +08:00
|
|
|
for (i, e) in res.iter_mut().enumerate() {
|
2014-09-20 05:51:27 +08:00
|
|
|
*e = self.at((row, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
2013-08-26 05:01:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-26 05:01:44 +08:00
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
macro_rules! row_slice_impl(
|
2014-08-16 18:16:26 +08:00
|
|
|
($t: ident, $tv: ident, $slice: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Clone + Copy + Zero> RowSlice<$slice<N>> for $t<N> {
|
2015-01-10 05:26:05 +08:00
|
|
|
fn row_slice(&self, rid: usize, cstart: usize, cend: usize) -> $slice<N> {
|
2014-05-12 20:06:25 +08:00
|
|
|
let row = self.row(rid);
|
2014-08-16 18:16:26 +08:00
|
|
|
|
2015-03-23 21:30:31 +08:00
|
|
|
$slice::from_slice(cend - cstart, &row.as_array()[cstart .. cend])
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-05-12 20:06:25 +08:00
|
|
|
|
2013-09-13 16:26:19 +08:00
|
|
|
macro_rules! col_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy + Zero> Col<$tv<N>> for $t<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn ncols(&self) -> usize {
|
2013-09-13 16:26:19 +08:00
|
|
|
Dim::dim(None::<$t<N>>)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn set_col(&mut self, col: usize, v: $tv<N>) {
|
2014-10-26 05:02:16 +08:00
|
|
|
for (i, e) in v.iter().enumerate() {
|
2014-12-18 06:28:32 +08:00
|
|
|
self.set((i, col), *e);
|
2014-10-26 05:02:16 +08:00
|
|
|
}
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
fn col(&self, col: usize) -> $tv<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $tv<N> = ::zero();
|
2014-10-26 05:02:16 +08:00
|
|
|
|
|
|
|
for (i, e) in res.iter_mut().enumerate() {
|
|
|
|
*e = self.at((i, col));
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
2013-09-13 16:26:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-09-13 16:26:19 +08:00
|
|
|
|
2014-08-16 19:12:08 +08:00
|
|
|
macro_rules! diag_impl(
|
|
|
|
($t: ident, $tv: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy + Zero> Diag<$tv<N>> for $t<N> {
|
2014-08-16 19:12:08 +08:00
|
|
|
#[inline]
|
|
|
|
fn from_diag(diag: &$tv<N>) -> $t<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $t<N> = ::zero();
|
2014-08-16 19:12:08 +08:00
|
|
|
|
|
|
|
res.set_diag(diag);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_diag(&mut self, diag: &$tv<N>) {
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
2014-08-16 19:12:08 +08:00
|
|
|
unsafe { self.unsafe_set((i, i), diag.unsafe_at(i)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn diag(&self) -> $tv<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut diag: $tv<N> = ::zero();
|
2014-08-16 19:12:08 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
2014-08-16 19:12:08 +08:00
|
|
|
unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) }
|
|
|
|
}
|
|
|
|
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-08-16 19:12:08 +08:00
|
|
|
|
2013-09-14 23:08:48 +08:00
|
|
|
macro_rules! mat_mul_mat_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $dim: expr) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + BaseNum> Mul<$t<N>> for $t<N> {
|
|
|
|
type Output = $t<N>;
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn mul(self, right: $t<N>) -> $t<N> {
|
2013-09-14 23:08:48 +08:00
|
|
|
// careful! we need to comute other * self here (self is the rhs).
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $t<N> = ::zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
|
|
|
for j in 0..$dim {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut acc: N = ::zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
2015-02-21 22:07:50 +08:00
|
|
|
for k in 0..$dim {
|
2014-11-26 21:17:34 +08:00
|
|
|
acc = acc + self.at_fast((i, k)) * right.at_fast((k, j));
|
2013-09-13 19:21:42 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast((i, j), acc);
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
macro_rules! vec_mul_mat_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $v: ident, $dim: expr, $zero: expr) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + BaseNum> Mul<$t<N>> for $v<N> {
|
|
|
|
type Output = $v<N>;
|
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn mul(self, right: $t<N>) -> $v<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
let mut res : $v<N> = $zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
|
|
|
for j in 0..$dim {
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
2014-11-26 21:17:34 +08:00
|
|
|
let val = res.at_fast(i) + self.at_fast(j) * right.at_fast((j, i));
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast(i, val)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-09-15 16:48:18 +08:00
|
|
|
macro_rules! mat_mul_vec_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $v: ident, $dim: expr, $zero: expr) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + BaseNum> Mul<$v<N>> for $t<N> {
|
|
|
|
type Output = $v<N>;
|
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-12-18 06:28:32 +08:00
|
|
|
fn mul(self, right: $v<N>) -> $v<N> {
|
2014-10-10 17:23:52 +08:00
|
|
|
let mut res : $v<N> = $zero();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
|
|
|
for j in 0..$dim {
|
2013-09-13 19:21:42 +08:00
|
|
|
unsafe {
|
2014-11-26 21:17:34 +08:00
|
|
|
let val = res.at_fast(i) + self.at_fast((i, j)) * right.at_fast(j);
|
2013-09-13 19:21:42 +08:00
|
|
|
res.set_fast(i, val)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
macro_rules! pnt_mul_mat_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $v: ident, $dim: expr, $zero: expr) => (
|
2014-12-19 22:33:01 +08:00
|
|
|
vec_mul_mat_impl!($t, $v, $dim, $zero);
|
2014-10-10 17:23:52 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-10-10 17:23:52 +08:00
|
|
|
|
|
|
|
macro_rules! mat_mul_pnt_impl(
|
2014-11-26 21:17:34 +08:00
|
|
|
($t: ident, $v: ident, $dim: expr, $zero: expr) => (
|
2014-12-19 22:33:01 +08:00
|
|
|
mat_mul_vec_impl!($t, $v, $dim, $zero);
|
2014-10-10 17:23:52 +08:00
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-10-10 17:23:52 +08:00
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! inv_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy + BaseNum>
|
2013-08-05 16:13:44 +08:00
|
|
|
Inv for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2015-02-02 06:23:57 +08:00
|
|
|
fn inv(&self) -> Option<$t<N>> {
|
2014-12-18 06:28:32 +08:00
|
|
|
let mut res : $t<N> = *self;
|
2015-02-02 06:23:57 +08:00
|
|
|
if res.inv_mut() {
|
2013-08-05 16:13:44 +08:00
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2015-02-02 06:23:57 +08:00
|
|
|
fn inv_mut(&mut self) -> bool {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $t<N> = ::one();
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// inversion using Gauss-Jordan elimination
|
2015-02-21 22:07:50 +08:00
|
|
|
for k in 0..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
// search a non-zero value on the k-th column
|
|
|
|
// FIXME: would it be worth it to spend some more time searching for the
|
|
|
|
// max instead?
|
|
|
|
|
|
|
|
let mut n0 = k; // index of a non-zero entry
|
|
|
|
|
2014-01-23 08:22:23 +08:00
|
|
|
while n0 != $dim {
|
2014-11-16 21:04:15 +08:00
|
|
|
if self.at((n0, k)) != ::zero() {
|
2013-08-05 16:13:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
n0 = n0 + 1;
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 == $dim {
|
|
|
|
return false
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
// swap pivot line
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 != k {
|
2015-02-21 22:07:50 +08:00
|
|
|
for j in 0..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
self.swap((n0, j), (k, j));
|
|
|
|
res.swap((n0, j), (k, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pivot = self.at((k, k));
|
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for j in k..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((k, j)) / pivot;
|
|
|
|
self.set((k, j), selfval);
|
|
|
|
}
|
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for j in 0..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((k, j)) / pivot;
|
|
|
|
res.set((k, j), resval);
|
|
|
|
}
|
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for l in 0..$dim {
|
2013-08-05 16:13:44 +08:00
|
|
|
if l != k {
|
2013-08-05 15:44:56 +08:00
|
|
|
let normalizer = self.at((l, k));
|
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for j in k..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((l, j)) - self.at((k, j)) * normalizer;
|
|
|
|
self.set((l, j), selfval);
|
|
|
|
}
|
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for j in 0..$dim {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((l, j)) - res.at((k, j)) * normalizer;
|
|
|
|
res.set((l, j), resval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
*self = res;
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
true
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! transpose_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: Copy> Transpose for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2015-02-02 06:23:57 +08:00
|
|
|
fn transpose(&self) -> $t<N> {
|
2014-12-18 06:28:32 +08:00
|
|
|
let mut res = *self;
|
|
|
|
|
2015-02-02 06:23:57 +08:00
|
|
|
res.transpose_mut();
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
|
|
|
#[inline]
|
2015-02-02 06:23:57 +08:00
|
|
|
fn transpose_mut(&mut self) {
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 1..$dim {
|
|
|
|
for j in 0..i {
|
2013-08-05 16:13:44 +08:00
|
|
|
self.swap((i, j), (j, i))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! approx_eq_impl(
|
|
|
|
($t: ident) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-01-10 03:48:30 +08:00
|
|
|
fn approx_epsilon(_: Option<$t<N>>) -> N {
|
|
|
|
ApproxEq::approx_epsilon(None::<N>)
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-08-08 02:53:51 +08:00
|
|
|
|
2015-01-01 05:08:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn approx_ulps(_: Option<$t<N>>) -> u32 {
|
|
|
|
ApproxEq::approx_ulps(None::<N>)
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
|
2015-03-01 08:48:58 +08:00
|
|
|
let mut zip = self.iter().zip(other.iter());
|
2014-01-10 03:48:30 +08:00
|
|
|
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2015-01-01 05:08:42 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn approx_eq_ulps(&self, other: &$t<N>, ulps: u32) -> bool {
|
2015-03-01 08:48:58 +08:00
|
|
|
let mut zip = self.iter().zip(other.iter());
|
2015-01-01 05:08:42 +08:00
|
|
|
zip.all(|(a, b)| ApproxEq::approx_eq_ulps(a, b, ulps))
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! to_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: BaseNum + Copy> ToHomogeneous<$t2<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2014-11-22 00:00:39 +08:00
|
|
|
fn to_homogeneous(&self) -> $t2<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $t2<N> = ::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim {
|
|
|
|
for j in 0..$dim {
|
2014-11-22 00:00:39 +08:00
|
|
|
res.set((i, j), self.at((i, j)))
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
macro_rules! from_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2014-12-18 06:28:32 +08:00
|
|
|
impl<N: BaseNum + Copy> FromHomogeneous<$t2<N>> for $t<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn from(m: &$t2<N>) -> $t<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $t<N> = ::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..$dim2 {
|
|
|
|
for j in 0..$dim2 {
|
2013-08-05 16:13:44 +08:00
|
|
|
res.set((i, j), m.at((i, j)))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// FIXME: do we have to deal the lost components
|
|
|
|
// (like if the 1 is not a 1… do we have to divide?)
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-08-12 22:45:31 +08:00
|
|
|
|
|
|
|
macro_rules! outer_impl(
|
|
|
|
($t: ident, $m: ident) => (
|
2015-01-05 02:03:28 +08:00
|
|
|
impl<N: Copy + Mul<N, Output = N> + Zero> Outer<$m<N>> for $t<N> {
|
2013-08-12 22:45:31 +08:00
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
fn outer(&self, other: &$t<N>) -> $m<N> {
|
2014-11-16 21:04:15 +08:00
|
|
|
let mut res: $m<N> = ::zero();
|
2015-02-21 22:07:50 +08:00
|
|
|
for i in 0..Dim::dim(None::<$t<N>>) {
|
|
|
|
for j in 0..Dim::dim(None::<$t<N>>) {
|
2014-12-02 01:50:11 +08:00
|
|
|
res.set((i, j), self.at(i) * other.at(j))
|
2013-08-12 22:45:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-10-27 00:24:33 +08:00
|
|
|
|
|
|
|
macro_rules! eigen_qr_impl(
|
|
|
|
($t: ident, $v: ident) => (
|
|
|
|
impl<N> EigenQR<N, $v<N>> for $t<N>
|
2014-12-18 06:28:32 +08:00
|
|
|
where N: BaseFloat + ApproxEq<N> + Clone {
|
2015-01-10 05:26:05 +08:00
|
|
|
fn eigen_qr(&self, eps: &N, niter: usize) -> ($t<N>, $v<N>) {
|
2014-12-02 01:50:11 +08:00
|
|
|
linalg::eigen_qr(self, eps, niter)
|
2014-10-27 00:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|