From 2071fdd14fc9688b912548c750acf00ddf319425 Mon Sep 17 00:00:00 2001 From: John P Mayer Jr Date: Sun, 5 Apr 2015 01:05:26 -0400 Subject: [PATCH] changes Norm to use associated type N --- src/lib.rs | 10 +++++----- src/linalg/decompositions.rs | 4 ++-- src/structs/dvec_macros.rs | 4 +++- src/structs/quat.rs | 4 +++- src/structs/spec/vec0.rs | 4 +++- src/structs/vec_macros.rs | 4 +++- src/traits/geometry.rs | 11 +++++++---- src/traits/structure.rs | 4 ++-- 8 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9436c358..40b8ba7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -329,13 +329,13 @@ pub fn center, V: Copy>(a: &P, b: &P) -> P { */ /// Returns the distance between two points. #[inline(always)] -pub fn dist, V: Norm>(a: &P, b: &P) -> N { +pub fn dist, V: Norm>(a: &P, b: &P) -> N { a.dist(b) } /// Returns the squared distance between two points. #[inline(always)] -pub fn sqdist, V: Norm>(a: &P, b: &P) -> N { +pub fn sqdist, V: Norm>(a: &P, b: &P) -> N { a.sqdist(b) } @@ -678,19 +678,19 @@ pub fn dot, N>(a: &V, b: &V) -> N { /// Computes the L2 norm of a vector. #[inline(always)] -pub fn norm, N: BaseFloat>(v: &V) -> N { +pub fn norm, N: BaseFloat>(v: &V) -> N { Norm::norm(v) } /// Computes the squared L2 norm of a vector. #[inline(always)] -pub fn sqnorm, N: BaseFloat>(v: &V) -> N { +pub fn sqnorm, N: BaseFloat>(v: &V) -> N { Norm::sqnorm(v) } /// Gets the normalized version of a vector. #[inline(always)] -pub fn normalize, N: BaseFloat>(v: &V) -> V { +pub fn normalize, N: BaseFloat>(v: &V) -> V { Norm::normalize(v) } diff --git a/src/linalg/decompositions.rs b/src/linalg/decompositions.rs index 78de85ed..1a8ec2a9 100644 --- a/src/linalg/decompositions.rs +++ b/src/linalg/decompositions.rs @@ -40,7 +40,7 @@ pub fn householder_matrix(dim: usize, start: usize, vec: V) -> M /// * `m` - matrix to decompose pub fn qr(m: &M) -> (M, M) where N: BaseFloat, - V: Indexable + Norm, + V: Indexable + Norm, M: Copy + Eye + ColSlice + Transpose + Indexable<(usize, usize), N> + Mul { let (rows, cols) = m.shape(); @@ -74,7 +74,7 @@ pub fn qr(m: &M) -> (M, M) /// Eigendecomposition of a square matrix using the qr algorithm. pub fn eigen_qr(m: &M, eps: &N, niter: usize) -> (M, V) where N: BaseFloat, - VS: Indexable + Norm, + VS: Indexable + Norm, M: Indexable<(usize, usize), N> + SquareMat + Add + Sub + ColSlice + ApproxEq + Copy { diff --git a/src/structs/dvec_macros.rs b/src/structs/dvec_macros.rs index 60d8c86d..6cedb17a 100644 --- a/src/structs/dvec_macros.rs +++ b/src/structs/dvec_macros.rs @@ -281,7 +281,9 @@ macro_rules! dvec_impl( } } - impl Norm for $dvec { + impl Norm for $dvec { + type N = N; + #[inline] fn sqnorm(&self) -> N { Dot::dot(self, self) diff --git a/src/structs/quat.rs b/src/structs/quat.rs index 89bf2c26..f8a63a67 100644 --- a/src/structs/quat.rs +++ b/src/structs/quat.rs @@ -108,7 +108,9 @@ impl> Inv for Quat { } } -impl Norm for Quat { +impl Norm for Quat { + type N = N; + #[inline] fn sqnorm(&self) -> N { self.w * self.w + self.i * self.i + self.j * self.j + self.k * self.k diff --git a/src/structs/spec/vec0.rs b/src/structs/spec/vec0.rs index 86380f83..537497f8 100644 --- a/src/structs/spec/vec0.rs +++ b/src/structs/spec/vec0.rs @@ -191,7 +191,9 @@ impl + Neg> Translation> f } } -impl Norm for vec::Vec0 { +impl Norm for vec::Vec0 { + type N = N; + #[inline] fn sqnorm(&self) -> N { ::zero() diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index 735d1fa1..c09dbc67 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -593,7 +593,9 @@ macro_rules! translation_impl( macro_rules! norm_impl( ($t: ident, $($compN: ident),+) => ( - impl Norm for $t { + impl Norm for $t { + type N = N; + #[inline] fn sqnorm(&self) -> N { Dot::dot(self, self) diff --git a/src/traits/geometry.rs b/src/traits/geometry.rs index e9c22678..f9fb96b7 100644 --- a/src/traits/geometry.rs +++ b/src/traits/geometry.rs @@ -1,5 +1,6 @@ //! Traits of operations having a well-known or explicit geometric meaning. +use std::num::Float; use std::ops::Neg; use traits::structure::{BaseFloat, Mat}; @@ -208,23 +209,25 @@ pub trait Dot { } /// Traits of objects having an euclidian norm. -pub trait Norm { +pub trait Norm { + type N: BaseFloat; + /// Computes the norm of `self`. #[inline] - fn norm(&self) -> N { + fn norm(&self) -> ::N { self.sqnorm().sqrt() } /// Computes the squared norm of `self`. /// /// This is usually faster than computing the norm itself. - fn sqnorm(&self) -> N; + fn sqnorm(&self) -> ::N; /// Gets the normalized version of a copy of `v`. fn normalize(&self) -> Self; /// Normalizes `self`. - fn normalize_mut(&mut self) -> N; + fn normalize_mut(&mut self) -> ::N; } /** diff --git a/src/traits/structure.rs b/src/traits/structure.rs index 57630bc4..665ea071 100644 --- a/src/traits/structure.rs +++ b/src/traits/structure.rs @@ -244,7 +244,7 @@ pub trait NumVec: Dim + } /// Trait of vector with components implementing the `BaseFloat` trait. -pub trait FloatVec: NumVec + Norm + Basis { +pub trait FloatVec: NumVec + Norm + Basis { } /* @@ -282,7 +282,7 @@ pub trait NumPnt: } /// Trait of points with components implementing the `BaseFloat` trait. -pub trait FloatPnt>: NumPnt + Sized { +pub trait FloatPnt>: NumPnt + Sized { /// Computes the square distance between two points. #[inline] fn sqdist(&self, other: &Self) -> N {