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