diff --git a/src/dim1/vec1.rs b/src/dim1/vec1.rs index 20e8bd24..27780bdd 100644 --- a/src/dim1/vec1.rs +++ b/src/dim1/vec1.rs @@ -5,6 +5,7 @@ use traits::dot::Dot; use traits::dim::Dim; use traits::basis::Basis; use traits::norm::Norm; +use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq)] pub struct Vec1 @@ -31,6 +32,47 @@ impl> Sub, Vec1> for Vec1 { Vec1(self.x - other.x) } } +impl> +ScalarMul for Vec1 +{ + fn scalar_mul(&self, s: &T) -> Vec1 + { Vec1 { x: self.x * *s } } + + fn scalar_mul_inplace(&mut self, s: &T) + { self.x *= *s; } +} + + +impl> +ScalarDiv for Vec1 +{ + fn scalar_div(&self, s: &T) -> Vec1 + { Vec1 { x: self.x / *s } } + + fn scalar_div_inplace(&mut self, s: &T) + { self.x /= *s; } +} + +impl> +ScalarAdd for Vec1 +{ + fn scalar_add(&self, s: &T) -> Vec1 + { Vec1 { x: self.x + *s } } + + fn scalar_add_inplace(&mut self, s: &T) + { self.x += *s; } +} + +impl> +ScalarSub for Vec1 +{ + fn scalar_sub(&self, s: &T) -> Vec1 + { Vec1 { x: self.x - *s } } + + fn scalar_sub_inplace(&mut self, s: &T) + { self.x -= *s; } +} + impl + Add + Algebraic> Dot for Vec1 { fn dot(&self, other : &Vec1) -> T diff --git a/src/dim2/vec2.rs b/src/dim2/vec2.rs index 0d23228f..a8e38591 100644 --- a/src/dim2/vec2.rs +++ b/src/dim2/vec2.rs @@ -7,6 +7,7 @@ use traits::cross::Cross; use traits::basis::Basis; use traits::norm::Norm; use dim1::vec1::Vec1; +use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq)] pub struct Vec2 @@ -36,6 +37,59 @@ impl> Sub, Vec2> for Vec2 { Vec2(self.x - other.x, self.y - other.y) } } +impl> +ScalarMul for Vec2 +{ + fn scalar_mul(&self, s: &T) -> Vec2 + { Vec2 { x: self.x * *s, y: self.y * *s } } + + fn scalar_mul_inplace(&mut self, s: &T) + { + self.x *= *s; + self.y *= *s; + } +} + + +impl> +ScalarDiv for Vec2 +{ + fn scalar_div(&self, s: &T) -> Vec2 + { Vec2 { x: self.x / *s, y: self.y / *s } } + + fn scalar_div_inplace(&mut self, s: &T) + { + self.x /= *s; + self.y /= *s; + } +} + +impl> +ScalarAdd for Vec2 +{ + fn scalar_add(&self, s: &T) -> Vec2 + { Vec2 { x: self.x + *s, y: self.y + *s } } + + fn scalar_add_inplace(&mut self, s: &T) + { + self.x += *s; + self.y += *s; + } +} + +impl> +ScalarSub for Vec2 +{ + fn scalar_sub(&self, s: &T) -> Vec2 + { Vec2 { x: self.x - *s, y: self.y - *s } } + + fn scalar_sub_inplace(&mut self, s: &T) + { + self.x -= *s; + self.y -= *s; + } +} + impl + Add + Algebraic> Dot for Vec2 { fn dot(&self, other : &Vec2) -> T diff --git a/src/dim3/vec3.rs b/src/dim3/vec3.rs index ef0c7cee..c165e86b 100644 --- a/src/dim3/vec3.rs +++ b/src/dim3/vec3.rs @@ -6,6 +6,7 @@ use traits::dot::Dot; use traits::cross::Cross; use traits::basis::Basis; use traits::norm::Norm; +use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq)] pub struct Vec3 @@ -36,6 +37,64 @@ impl> Sub, Vec3> for Vec3 { Vec3(self.x - other.x, self.y - other.y, self.z - other.z) } } +impl> +ScalarMul for Vec3 +{ + fn scalar_mul(&self, s: &T) -> Vec3 + { Vec3 { x: self.x * *s, y: self.y * *s, z: self.z * *s } } + + fn scalar_mul_inplace(&mut self, s: &T) + { + self.x *= *s; + self.y *= *s; + self.z *= *s; + } +} + + +impl> +ScalarDiv for Vec3 +{ + fn scalar_div(&self, s: &T) -> Vec3 + { Vec3 { x: self.x / *s, y: self.y / *s, z: self.z / *s } } + + fn scalar_div_inplace(&mut self, s: &T) + { + self.x /= *s; + self.y /= *s; + self.z /= *s; + } +} + +impl> +ScalarAdd for Vec3 +{ + fn scalar_add(&self, s: &T) -> Vec3 + { Vec3 { x: self.x + *s, y: self.y + *s, z: self.z + *s } } + + fn scalar_add_inplace(&mut self, s: &T) + { + self.x += *s; + self.y += *s; + self.z += *s; + } +} + +impl> +ScalarSub for Vec3 +{ + fn scalar_sub(&self, s: &T) -> Vec3 + { Vec3 { x: self.x - *s, y: self.y - *s, z: self.z - *s } } + + fn scalar_sub_inplace(&mut self, s: &T) + { + self.x -= *s; + self.y -= *s; + self.z -= *s; + } +} + + impl> Neg> for Vec3 { fn neg(&self) -> Vec3 diff --git a/src/nalgebra.rc b/src/nalgebra.rc index 137dd029..50145b54 100644 --- a/src/nalgebra.rc +++ b/src/nalgebra.rc @@ -32,7 +32,7 @@ pub use traits::norm::Norm; pub use traits::workarounds::rlmul::{RMul, LMul}; pub use traits::workarounds::trigonometric::Trigonometric; -pub use traits::workarounds::scalar_op::ScalarOp; +pub use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; mod dim2 { diff --git a/src/ndim/nvec.rs b/src/ndim/nvec.rs index ec389c1c..a226abef 100644 --- a/src/ndim/nvec.rs +++ b/src/ndim/nvec.rs @@ -6,7 +6,7 @@ use traits::dim::Dim; use traits::dot::Dot; use traits::norm::Norm; use traits::basis::Basis; -use traits::workarounds::scalar_op::ScalarOp; +use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; // D is a phantom parameter, used only as a dimensional token. // Its allows use to encode the vector dimension at the type-level. @@ -65,38 +65,51 @@ Dot for NVec } } -impl + Quot + Add + Sub> -ScalarOp for NVec +impl> +ScalarMul for NVec { fn scalar_mul(&self, s: &T) -> NVec { NVec { at: map(self.at, |a| a * *s) } } - fn scalar_div(&self, s: &T) -> NVec - { NVec { at: map(self.at, |a| a / *s) } } - - fn scalar_add(&self, s: &T) -> NVec - { NVec { at: map(self.at, |a| a + *s) } } - - fn scalar_sub(&self, s: &T) -> NVec - { NVec { at: map(self.at, |a| a - *s) } } - fn scalar_mul_inplace(&mut self, s: &T) { for uint::range(0u, Dim::dim::()) |i| { self.at[i] *= *s; } } +} + + +impl> +ScalarDiv for NVec +{ + fn scalar_div(&self, s: &T) -> NVec + { NVec { at: map(self.at, |a| a / *s) } } fn scalar_div_inplace(&mut self, s: &T) { for uint::range(0u, Dim::dim::()) |i| { self.at[i] /= *s; } } +} + +impl> +ScalarAdd for NVec +{ + fn scalar_add(&self, s: &T) -> NVec + { NVec { at: map(self.at, |a| a + *s) } } fn scalar_add_inplace(&mut self, s: &T) { for uint::range(0u, Dim::dim::()) |i| { self.at[i] += *s; } } +} + +impl> +ScalarSub for NVec +{ + fn scalar_sub(&self, s: &T) -> NVec + { NVec { at: map(self.at, |a| a - *s) } } fn scalar_sub_inplace(&mut self, s: &T) { diff --git a/src/traits/workarounds/scalar_op.rs b/src/traits/workarounds/scalar_op.rs index f35c11a9..bcd0ba80 100644 --- a/src/traits/workarounds/scalar_op.rs +++ b/src/traits/workarounds/scalar_op.rs @@ -1,12 +1,23 @@ -pub trait ScalarOp +pub trait ScalarMul { fn scalar_mul(&self, &T) -> Self; - fn scalar_div(&self, &T) -> Self; - fn scalar_add(&self, &T) -> Self; - fn scalar_sub(&self, &T) -> Self; - fn scalar_mul_inplace(&mut self, &T); +} + +pub trait ScalarDiv +{ + fn scalar_div(&self, &T) -> Self; fn scalar_div_inplace(&mut self, &T); +} + +pub trait ScalarAdd +{ + fn scalar_add(&self, &T) -> Self; fn scalar_add_inplace(&mut self, &T); +} + +pub trait ScalarSub +{ + fn scalar_sub(&self, &T) -> Self; fn scalar_sub_inplace(&mut self, &T); }