changes Norm to use associated type N

This commit is contained in:
John P Mayer Jr 2015-04-05 01:05:26 -04:00
parent 0e7116e3bb
commit 2071fdd14f
8 changed files with 28 additions and 17 deletions

View File

@ -329,13 +329,13 @@ pub fn center<N: BaseFloat, P: FloatPnt<N, V>, V: Copy>(a: &P, b: &P) -> P {
*/ */
/// Returns the distance between two points. /// Returns the distance between two points.
#[inline(always)] #[inline(always)]
pub fn dist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N { pub fn dist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N = N>>(a: &P, b: &P) -> N {
a.dist(b) a.dist(b)
} }
/// Returns the squared distance between two points. /// Returns the squared distance between two points.
#[inline(always)] #[inline(always)]
pub fn sqdist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N { pub fn sqdist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N = N>>(a: &P, b: &P) -> N {
a.sqdist(b) a.sqdist(b)
} }
@ -678,19 +678,19 @@ pub fn dot<V: Dot<N>, N>(a: &V, b: &V) -> N {
/// Computes the L2 norm of a vector. /// Computes the L2 norm of a vector.
#[inline(always)] #[inline(always)]
pub fn norm<V: Norm<N>, N: BaseFloat>(v: &V) -> N { pub fn norm<V: Norm<N = N>, N: BaseFloat>(v: &V) -> N {
Norm::norm(v) Norm::norm(v)
} }
/// Computes the squared L2 norm of a vector. /// Computes the squared L2 norm of a vector.
#[inline(always)] #[inline(always)]
pub fn sqnorm<V: Norm<N>, N: BaseFloat>(v: &V) -> N { pub fn sqnorm<V: Norm<N = N>, N: BaseFloat>(v: &V) -> N {
Norm::sqnorm(v) Norm::sqnorm(v)
} }
/// Gets the normalized version of a vector. /// Gets the normalized version of a vector.
#[inline(always)] #[inline(always)]
pub fn normalize<V: Norm<N>, N: BaseFloat>(v: &V) -> V { pub fn normalize<V: Norm<N = N>, N: BaseFloat>(v: &V) -> V {
Norm::normalize(v) Norm::normalize(v)
} }

View File

@ -40,7 +40,7 @@ pub fn householder_matrix<N, V, M>(dim: usize, start: usize, vec: V) -> M
/// * `m` - matrix to decompose /// * `m` - matrix to decompose
pub fn qr<N, V, M>(m: &M) -> (M, M) pub fn qr<N, V, M>(m: &M) -> (M, M)
where N: BaseFloat, where N: BaseFloat,
V: Indexable<usize, N> + Norm<N>, V: Indexable<usize, N> + Norm<N = N>,
M: Copy + Eye + ColSlice<V> + Transpose + Indexable<(usize, usize), N> + M: Copy + Eye + ColSlice<V> + Transpose + Indexable<(usize, usize), N> +
Mul<M, Output = M> { Mul<M, Output = M> {
let (rows, cols) = m.shape(); let (rows, cols) = m.shape();
@ -74,7 +74,7 @@ pub fn qr<N, V, M>(m: &M) -> (M, M)
/// Eigendecomposition of a square matrix using the qr algorithm. /// Eigendecomposition of a square matrix using the qr algorithm.
pub fn eigen_qr<N, V, VS, M>(m: &M, eps: &N, niter: usize) -> (M, V) pub fn eigen_qr<N, V, VS, M>(m: &M, eps: &N, niter: usize) -> (M, V)
where N: BaseFloat, where N: BaseFloat,
VS: Indexable<usize, N> + Norm<N>, VS: Indexable<usize, N> + Norm<N = N>,
M: Indexable<(usize, usize), N> + SquareMat<N, V> + Add<M, Output = M> + M: Indexable<(usize, usize), N> + SquareMat<N, V> + Add<M, Output = M> +
Sub<M, Output = M> + ColSlice<VS> + Sub<M, Output = M> + ColSlice<VS> +
ApproxEq<N> + Copy { ApproxEq<N> + Copy {

View File

@ -281,7 +281,9 @@ macro_rules! dvec_impl(
} }
} }
impl<N: BaseFloat> Norm<N> for $dvec<N> { impl<N: BaseFloat> Norm for $dvec<N> {
type N = N;
#[inline] #[inline]
fn sqnorm(&self) -> N { fn sqnorm(&self) -> N {
Dot::dot(self, self) Dot::dot(self, self)

View File

@ -108,7 +108,9 @@ impl<N: BaseFloat + ApproxEq<N>> Inv for Quat<N> {
} }
} }
impl<N: BaseFloat> Norm<N> for Quat<N> { impl<N: BaseFloat> Norm for Quat<N> {
type N = N;
#[inline] #[inline]
fn sqnorm(&self) -> N { fn sqnorm(&self) -> N {
self.w * self.w + self.i * self.i + self.j * self.j + self.k * self.k self.w * self.w + self.i * self.i + self.j * self.j + self.k * self.k

View File

@ -191,7 +191,9 @@ impl<N: Copy + Add<N, Output = N> + Neg<Output = N>> Translation<vec::Vec0<N>> f
} }
} }
impl<N: BaseFloat> Norm<N> for vec::Vec0<N> { impl<N: BaseFloat> Norm for vec::Vec0<N> {
type N = N;
#[inline] #[inline]
fn sqnorm(&self) -> N { fn sqnorm(&self) -> N {
::zero() ::zero()

View File

@ -593,7 +593,9 @@ macro_rules! translation_impl(
macro_rules! norm_impl( macro_rules! norm_impl(
($t: ident, $($compN: ident),+) => ( ($t: ident, $($compN: ident),+) => (
impl<N: Copy + BaseFloat> Norm<N> for $t<N> { impl<N: Copy + BaseFloat> Norm for $t<N> {
type N = N;
#[inline] #[inline]
fn sqnorm(&self) -> N { fn sqnorm(&self) -> N {
Dot::dot(self, self) Dot::dot(self, self)

View File

@ -1,5 +1,6 @@
//! Traits of operations having a well-known or explicit geometric meaning. //! Traits of operations having a well-known or explicit geometric meaning.
use std::num::Float;
use std::ops::Neg; use std::ops::Neg;
use traits::structure::{BaseFloat, Mat}; use traits::structure::{BaseFloat, Mat};
@ -208,23 +209,25 @@ pub trait Dot<N> {
} }
/// Traits of objects having an euclidian norm. /// Traits of objects having an euclidian norm.
pub trait Norm<N: BaseFloat> { pub trait Norm {
type N: BaseFloat;
/// Computes the norm of `self`. /// Computes the norm of `self`.
#[inline] #[inline]
fn norm(&self) -> N { fn norm(&self) -> <Self as Norm>::N {
self.sqnorm().sqrt() self.sqnorm().sqrt()
} }
/// Computes the squared norm of `self`. /// Computes the squared norm of `self`.
/// ///
/// This is usually faster than computing the norm itself. /// This is usually faster than computing the norm itself.
fn sqnorm(&self) -> N; fn sqnorm(&self) -> <Self as Norm>::N;
/// Gets the normalized version of a copy of `v`. /// Gets the normalized version of a copy of `v`.
fn normalize(&self) -> Self; fn normalize(&self) -> Self;
/// Normalizes `self`. /// Normalizes `self`.
fn normalize_mut(&mut self) -> N; fn normalize_mut(&mut self) -> <Self as Norm>::N;
} }
/** /**

View File

@ -244,7 +244,7 @@ pub trait NumVec<N>: Dim +
} }
/// Trait of vector with components implementing the `BaseFloat` trait. /// Trait of vector with components implementing the `BaseFloat` trait.
pub trait FloatVec<N: BaseFloat>: NumVec<N> + Norm<N> + Basis { pub trait FloatVec<N: BaseFloat>: NumVec<N> + Norm<N = N> + Basis {
} }
/* /*
@ -282,7 +282,7 @@ pub trait NumPnt<N, V>:
} }
/// Trait of points with components implementing the `BaseFloat` trait. /// Trait of points with components implementing the `BaseFloat` trait.
pub trait FloatPnt<N: BaseFloat, V: Norm<N>>: NumPnt<N, V> + Sized { pub trait FloatPnt<N: BaseFloat, V: Norm<N = N>>: NumPnt<N, V> + Sized {
/// Computes the square distance between two points. /// Computes the square distance between two points.
#[inline] #[inline]
fn sqdist(&self, other: &Self) -> N { fn sqdist(&self, other: &Self) -> N {