nalgebra/src/ndim/dvec.rs

269 lines
5.5 KiB
Rust
Raw Normal View History

use std::uint::iterate;
use std::num::{Zero, One, Algebraic};
2013-06-09 20:09:22 +08:00
use std::vec::{map_zip, map, from_elem};
use std::cmp::ApproxEq;
2013-06-09 20:09:22 +08:00
use std::iterator::IteratorUtil;
2013-05-31 17:01:07 +08:00
use traits::ring::Ring;
use traits::division_ring::DivisionRing;
use traits::dot::Dot;
use traits::sub_dot::SubDot;
use traits::norm::Norm;
use traits::translation::Translation;
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
2013-05-31 17:01:07 +08:00
2013-06-16 02:06:13 +08:00
#[deriving(Eq, Ord, ToStr, Clone)]
2013-06-10 07:36:47 +08:00
pub struct DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
at: ~[N]
2013-05-31 17:01:07 +08:00
}
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
pub fn zero_vec_with_dim<N: Zero + Copy>(dim: uint) -> DVec<N>
{ DVec { at: from_elem(dim, Zero::zero::<N>()) } }
2013-05-31 17:01:07 +08:00
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
pub fn is_zero_vec<N: Zero>(vec: &DVec<N>) -> bool
2013-06-09 20:09:22 +08:00
{ vec.at.all(|e| e.is_zero()) }
2013-05-31 17:01:07 +08:00
// FIXME: is Clone needed?
2013-06-10 07:36:47 +08:00
impl<N: Copy + DivisionRing + Algebraic + Clone + ApproxEq<N>> DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<N>]
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
let mut res : ~[DVec<N>] = ~[];
2013-05-31 17:01:07 +08:00
for iterate(0u, dim) |i|
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
let mut basis_element : DVec<N> = zero_vec_with_dim(dim);
2013-05-31 17:01:07 +08:00
basis_element.at[i] = One::one();
res.push(basis_element);
}
res
}
2013-06-10 07:36:47 +08:00
pub fn orthogonal_subspace_basis(&self) -> ~[DVec<N>]
2013-05-31 17:01:07 +08:00
{
// compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm
2013-06-09 20:09:22 +08:00
let dim = self.at.len();
2013-06-10 07:36:47 +08:00
let mut res : ~[DVec<N>] = ~[];
2013-05-31 17:01:07 +08:00
for iterate(0u, dim) |i|
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
let mut basis_element : DVec<N> = zero_vec_with_dim(self.at.len());
2013-05-31 17:01:07 +08:00
basis_element.at[i] = One::one();
if (res.len() == dim - 1)
{ break; }
let mut elt = basis_element.clone();
elt = elt - self.scalar_mul(&basis_element.dot(self));
2013-05-31 17:01:07 +08:00
for res.each |v|
{ elt = elt - v.scalar_mul(&elt.dot(v)) };
2013-05-31 17:01:07 +08:00
if (!elt.sqnorm().approx_eq(&Zero::zero()))
{ res.push(elt.normalized()); }
}
assert!(res.len() == dim - 1);
res
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn add(&self, other: &DVec<N>) -> DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
assert!(self.at.len() == other.at.len());
2013-05-31 17:01:07 +08:00
DVec { at: map_zip(self.at, other.at, | a, b | { *a + *b }) }
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn sub(&self, other: &DVec<N>) -> DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
assert!(self.at.len() == other.at.len());
2013-05-31 17:01:07 +08:00
DVec { at: map_zip(self.at, other.at, | a, b | *a - *b) }
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Neg<N>> Neg<DVec<N>> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn neg(&self) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ DVec { at: map(self.at, |a| -a) } }
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Ring>
Dot<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn dot(&self, other: &DVec<N>) -> N
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
assert!(self.at.len() == other.at.len());
2013-05-31 17:01:07 +08:00
2013-06-10 07:36:47 +08:00
let mut res = Zero::zero::<N>();
2013-05-31 17:01:07 +08:00
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ res = res + self.at[i] * other.at[i]; }
2013-05-31 17:01:07 +08:00
res
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Ring> SubDot<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn sub_dot(&self, a: &DVec<N>, b: &DVec<N>) -> N
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
let mut res = Zero::zero::<N>();
2013-05-31 17:01:07 +08:00
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ res = res + (self.at[i] - a.at[i]) * b.at[i]; }
2013-05-31 17:01:07 +08:00
res
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_mul(&self, s: &N) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ DVec { at: map(self.at, |a| a * *s) } }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_mul_inplace(&mut self, s: &N)
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ self.at[i] = self.at[i] * copy *s; }
2013-05-31 17:01:07 +08:00
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_div(&self, s: &N) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ DVec { at: map(self.at, |a| a / *s) } }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_div_inplace(&mut self, s: &N)
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ self.at[i] = self.at[i] / copy *s; }
2013-05-31 17:01:07 +08:00
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_add(&self, s: &N) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ DVec { at: map(self.at, |a| a + *s) } }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_add_inplace(&mut self, s: &N)
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ self.at[i] = self.at[i] + copy *s; }
2013-05-31 17:01:07 +08:00
}
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_sub(&self, s: &N) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ DVec { at: map(self.at, |a| a - *s) } }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn scalar_sub_inplace(&mut self, s: &N)
2013-05-31 17:01:07 +08:00
{
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ self.at[i] = self.at[i] - copy *s; }
2013-05-31 17:01:07 +08:00
}
}
2013-06-10 07:36:47 +08:00
impl<N: Clone + Copy + Add<N, N>> Translation<DVec<N>> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn translation(&self) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ self.clone() }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn translated(&self, t: &DVec<N>) -> DVec<N>
2013-05-31 17:01:07 +08:00
{ self + *t }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn translate(&mut self, t: &DVec<N>)
2013-05-31 17:01:07 +08:00
{ *self = *self + *t; }
}
2013-06-10 07:36:47 +08:00
impl<N: Copy + DivisionRing + Algebraic + Clone>
Norm<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn sqnorm(&self) -> N
2013-05-31 17:01:07 +08:00
{ self.dot(self) }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn norm(&self) -> N
2013-05-31 17:01:07 +08:00
{ self.sqnorm().sqrt() }
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn normalized(&self) -> DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-10 07:36:47 +08:00
let mut res : DVec<N> = self.clone();
2013-05-31 17:01:07 +08:00
res.normalize();
res
}
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn normalize(&mut self) -> N
2013-05-31 17:01:07 +08:00
{
let l = self.norm();
2013-06-09 20:09:22 +08:00
for iterate(0u, self.at.len()) |i|
{ self.at[i] = self.at[i] / copy l; }
2013-05-31 17:01:07 +08:00
l
}
}
2013-06-10 07:36:47 +08:00
impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N>
2013-05-31 17:01:07 +08:00
{
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
2013-05-31 17:01:07 +08:00
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn approx_eq(&self, other: &DVec<N>) -> bool
2013-06-09 20:09:22 +08:00
{
let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
2013-05-31 17:01:07 +08:00
2013-06-14 00:48:28 +08:00
#[inline(always)]
2013-06-10 07:36:47 +08:00
fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool
2013-06-09 20:09:22 +08:00
{
let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
}
2013-05-31 17:01:07 +08:00
}