Refined ScalaOp trait.

This commit is contained in:
Sébastien Crozet 2013-05-18 18:06:36 +00:00
parent d13924bfee
commit 01d37a1b56
6 changed files with 197 additions and 18 deletions

View File

@ -5,6 +5,7 @@ use traits::dot::Dot;
use traits::dim::Dim; use traits::dim::Dim;
use traits::basis::Basis; use traits::basis::Basis;
use traits::norm::Norm; use traits::norm::Norm;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq)] #[deriving(Eq)]
pub struct Vec1<T> pub struct Vec1<T>
@ -31,6 +32,47 @@ impl<T:Copy + Sub<T,T>> Sub<Vec1<T>, Vec1<T>> for Vec1<T>
{ Vec1(self.x - other.x) } { Vec1(self.x - other.x) }
} }
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec1<T>
{
fn scalar_mul(&self, s: &T) -> Vec1<T>
{ Vec1 { x: self.x * *s } }
fn scalar_mul_inplace(&mut self, s: &T)
{ self.x *= *s; }
}
impl<T: Copy + Quot<T, T>>
ScalarDiv<T> for Vec1<T>
{
fn scalar_div(&self, s: &T) -> Vec1<T>
{ Vec1 { x: self.x / *s } }
fn scalar_div_inplace(&mut self, s: &T)
{ self.x /= *s; }
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec1<T>
{
fn scalar_add(&self, s: &T) -> Vec1<T>
{ Vec1 { x: self.x + *s } }
fn scalar_add_inplace(&mut self, s: &T)
{ self.x += *s; }
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec1<T>
{
fn scalar_sub(&self, s: &T) -> Vec1<T>
{ Vec1 { x: self.x - *s } }
fn scalar_sub_inplace(&mut self, s: &T)
{ self.x -= *s; }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec1<T> impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec1<T>
{ {
fn dot(&self, other : &Vec1<T>) -> T fn dot(&self, other : &Vec1<T>) -> T

View File

@ -7,6 +7,7 @@ use traits::cross::Cross;
use traits::basis::Basis; use traits::basis::Basis;
use traits::norm::Norm; use traits::norm::Norm;
use dim1::vec1::Vec1; use dim1::vec1::Vec1;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq)] #[deriving(Eq)]
pub struct Vec2<T> pub struct Vec2<T>
@ -36,6 +37,59 @@ impl<T:Copy + Sub<T,T>> Sub<Vec2<T>, Vec2<T>> for Vec2<T>
{ Vec2(self.x - other.x, self.y - other.y) } { Vec2(self.x - other.x, self.y - other.y) }
} }
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec2<T>
{
fn scalar_mul(&self, s: &T) -> Vec2<T>
{ Vec2 { x: self.x * *s, y: self.y * *s } }
fn scalar_mul_inplace(&mut self, s: &T)
{
self.x *= *s;
self.y *= *s;
}
}
impl<T: Copy + Quot<T, T>>
ScalarDiv<T> for Vec2<T>
{
fn scalar_div(&self, s: &T) -> Vec2<T>
{ Vec2 { x: self.x / *s, y: self.y / *s } }
fn scalar_div_inplace(&mut self, s: &T)
{
self.x /= *s;
self.y /= *s;
}
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec2<T>
{
fn scalar_add(&self, s: &T) -> Vec2<T>
{ Vec2 { x: self.x + *s, y: self.y + *s } }
fn scalar_add_inplace(&mut self, s: &T)
{
self.x += *s;
self.y += *s;
}
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec2<T>
{
fn scalar_sub(&self, s: &T) -> Vec2<T>
{ Vec2 { x: self.x - *s, y: self.y - *s } }
fn scalar_sub_inplace(&mut self, s: &T)
{
self.x -= *s;
self.y -= *s;
}
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec2<T> impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec2<T>
{ {
fn dot(&self, other : &Vec2<T>) -> T fn dot(&self, other : &Vec2<T>) -> T

View File

@ -6,6 +6,7 @@ use traits::dot::Dot;
use traits::cross::Cross; use traits::cross::Cross;
use traits::basis::Basis; use traits::basis::Basis;
use traits::norm::Norm; use traits::norm::Norm;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq)] #[deriving(Eq)]
pub struct Vec3<T> pub struct Vec3<T>
@ -36,6 +37,64 @@ impl<T:Copy + Sub<T,T>> Sub<Vec3<T>, Vec3<T>> for Vec3<T>
{ Vec3(self.x - other.x, self.y - other.y, self.z - other.z) } { Vec3(self.x - other.x, self.y - other.y, self.z - other.z) }
} }
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec3<T>
{
fn scalar_mul(&self, s: &T) -> Vec3<T>
{ 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<T: Copy + Quot<T, T>>
ScalarDiv<T> for Vec3<T>
{
fn scalar_div(&self, s: &T) -> Vec3<T>
{ 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<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec3<T>
{
fn scalar_add(&self, s: &T) -> Vec3<T>
{ 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<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec3<T>
{
fn scalar_sub(&self, s: &T) -> Vec3<T>
{ 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<T:Copy + Neg<T>> Neg<Vec3<T>> for Vec3<T> impl<T:Copy + Neg<T>> Neg<Vec3<T>> for Vec3<T>
{ {
fn neg(&self) -> Vec3<T> fn neg(&self) -> Vec3<T>

View File

@ -32,7 +32,7 @@ pub use traits::norm::Norm;
pub use traits::workarounds::rlmul::{RMul, LMul}; pub use traits::workarounds::rlmul::{RMul, LMul};
pub use traits::workarounds::trigonometric::Trigonometric; 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 mod dim2
{ {

View File

@ -6,7 +6,7 @@ use traits::dim::Dim;
use traits::dot::Dot; use traits::dot::Dot;
use traits::norm::Norm; use traits::norm::Norm;
use traits::basis::Basis; 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. // D is a phantom parameter, used only as a dimensional token.
// Its allows use to encode the vector dimension at the type-level. // Its allows use to encode the vector dimension at the type-level.
@ -65,38 +65,51 @@ Dot<T> for NVec<D, T>
} }
} }
impl<D: Dim, T: Copy + Mul<T, T> + Quot<T, T> + Add<T, T> + Sub<T, T>> impl<D: Dim, T: Copy + Mul<T, T>>
ScalarOp<T> for NVec<D, T> ScalarMul<T> for NVec<D, T>
{ {
fn scalar_mul(&self, s: &T) -> NVec<D, T> fn scalar_mul(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a * *s) } } { NVec { at: map(self.at, |a| a * *s) } }
fn scalar_div(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a / *s) } }
fn scalar_add(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a + *s) } }
fn scalar_sub(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a - *s) } }
fn scalar_mul_inplace(&mut self, s: &T) fn scalar_mul_inplace(&mut self, s: &T)
{ {
for uint::range(0u, Dim::dim::<D>()) |i| for uint::range(0u, Dim::dim::<D>()) |i|
{ self.at[i] *= *s; } { self.at[i] *= *s; }
} }
}
impl<D: Dim, T: Copy + Quot<T, T>>
ScalarDiv<T> for NVec<D, T>
{
fn scalar_div(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a / *s) } }
fn scalar_div_inplace(&mut self, s: &T) fn scalar_div_inplace(&mut self, s: &T)
{ {
for uint::range(0u, Dim::dim::<D>()) |i| for uint::range(0u, Dim::dim::<D>()) |i|
{ self.at[i] /= *s; } { self.at[i] /= *s; }
} }
}
impl<D: Dim, T: Copy + Add<T, T>>
ScalarAdd<T> for NVec<D, T>
{
fn scalar_add(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a + *s) } }
fn scalar_add_inplace(&mut self, s: &T) fn scalar_add_inplace(&mut self, s: &T)
{ {
for uint::range(0u, Dim::dim::<D>()) |i| for uint::range(0u, Dim::dim::<D>()) |i|
{ self.at[i] += *s; } { self.at[i] += *s; }
} }
}
impl<D: Dim, T: Copy + Sub<T, T>>
ScalarSub<T> for NVec<D, T>
{
fn scalar_sub(&self, s: &T) -> NVec<D, T>
{ NVec { at: map(self.at, |a| a - *s) } }
fn scalar_sub_inplace(&mut self, s: &T) fn scalar_sub_inplace(&mut self, s: &T)
{ {

View File

@ -1,12 +1,23 @@
pub trait ScalarOp<T> pub trait ScalarMul<T>
{ {
fn scalar_mul(&self, &T) -> Self; 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); fn scalar_mul_inplace(&mut self, &T);
}
pub trait ScalarDiv<T>
{
fn scalar_div(&self, &T) -> Self;
fn scalar_div_inplace(&mut self, &T); fn scalar_div_inplace(&mut self, &T);
}
pub trait ScalarAdd<T>
{
fn scalar_add(&self, &T) -> Self;
fn scalar_add_inplace(&mut self, &T); fn scalar_add_inplace(&mut self, &T);
}
pub trait ScalarSub<T>
{
fn scalar_sub(&self, &T) -> Self;
fn scalar_sub_inplace(&mut self, &T); fn scalar_sub_inplace(&mut self, &T);
} }