2013-09-26 23:18:14 +08:00
|
|
|
//! Traits giving structural informations on linear algebra objects or the space they live in.
|
|
|
|
|
2014-01-16 15:17:02 +08:00
|
|
|
use std::num::{Zero, Bounded};
|
2013-09-22 16:58:21 +08:00
|
|
|
use std::vec::{VecIterator, VecMutIterator};
|
|
|
|
use traits::operations::{RMul, LMul, ScalarAdd, ScalarSub};
|
|
|
|
use traits::geometry::{Dot, Norm, UniformSphereSample};
|
|
|
|
|
2013-10-10 04:59:44 +08:00
|
|
|
/// Traits of objects which can be created from an object of type `T`.
|
|
|
|
pub trait Cast<T> {
|
|
|
|
/// Converts an element of type `T` to an element of type `Self`.
|
|
|
|
fn from(t: T) -> Self;
|
|
|
|
}
|
|
|
|
|
2013-09-22 16:58:21 +08:00
|
|
|
/// Trait of matrices.
|
|
|
|
///
|
|
|
|
/// A matrix has rows and columns and are able to multiply them.
|
|
|
|
pub trait Mat<R, C> : Row<R> + Col<C> + RMul<R> + LMul<C> { }
|
|
|
|
|
2013-09-26 23:05:11 +08:00
|
|
|
impl<M: Row<R> + Col<C> + RMul<R> + LMul<C>, R, C> Mat<R, C> for M {
|
|
|
|
}
|
2013-09-22 16:58:21 +08:00
|
|
|
|
|
|
|
// XXX: we keep ScalarAdd and ScalarSub here to avoid trait impl conflict (overriding) between the
|
|
|
|
// different Add/Sub traits. This is _so_ unfortunate…
|
|
|
|
|
|
|
|
/// Trait grouping most common operations on vectors.
|
|
|
|
pub trait Vec<N>: Dim + Sub<Self, Self> + Add<Self, Self> + Neg<Self> + Zero + Eq + Mul<N, Self>
|
|
|
|
+ Div<N, Self> + Dot<N> {
|
|
|
|
}
|
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
/// Trait of vector with components implementing the `Real` trait.
|
|
|
|
pub trait RealVec<N: Real>: Vec<N> + Norm<N> {
|
2013-09-22 16:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait grouping uncommon, low-level and borderline (from the mathematical point of view)
|
|
|
|
/// operations on vectors.
|
2014-01-16 15:15:57 +08:00
|
|
|
pub trait VecExt<N>: Vec<N> + Indexable<uint, N> + Iterable<N> +
|
|
|
|
UniformSphereSample + ScalarAdd<N> + ScalarSub<N> + Bounded + Orderable
|
2013-09-22 16:58:21 +08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
/// Trait grouping uncommon, low-level and borderline (from the mathematical point of view)
|
|
|
|
/// operations on vectors.
|
2014-01-10 03:48:30 +08:00
|
|
|
pub trait RealVecExt<N: Real>: RealVec<N> + VecExt<N> + Basis + Round { }
|
2013-09-22 16:58:21 +08:00
|
|
|
|
|
|
|
impl<N, V: Dim + Sub<V, V> + Add<V, V> + Neg<V> + Zero + Eq + Mul<N, V> + Div<N, V> + Dot<N>>
|
2013-09-26 23:05:11 +08:00
|
|
|
Vec<N> for V { }
|
2013-09-22 16:58:21 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Real, V: Vec<N> + Norm<N>> RealVec<N> for V { }
|
2013-09-22 16:58:21 +08:00
|
|
|
|
|
|
|
impl<N,
|
2014-01-16 15:15:57 +08:00
|
|
|
V: Vec<N> + Indexable<uint, N> + Iterable<N> +
|
|
|
|
UniformSphereSample + ScalarAdd<N> + ScalarSub<N> + Bounded + Orderable>
|
2013-09-26 23:05:11 +08:00
|
|
|
VecExt<N> for V { }
|
2013-09-22 16:58:21 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
impl<N: Real, V: RealVec<N> + VecExt<N> + Basis + Round> RealVecExt<N> for V { }
|
2013-09-22 16:58:21 +08:00
|
|
|
|
|
|
|
// FIXME: return an iterator instead
|
|
|
|
/// Traits of objects which can form a basis (typically vectors).
|
|
|
|
pub trait Basis {
|
|
|
|
/// Iterates through the canonical basis of the space in which this object lives.
|
2013-11-27 18:16:16 +08:00
|
|
|
fn canonical_basis(|Self| -> bool);
|
2013-09-22 16:58:21 +08:00
|
|
|
|
|
|
|
/// Iterates through a basis of the subspace orthogonal to `self`.
|
2013-11-27 18:16:16 +08:00
|
|
|
fn orthonormal_subspace_basis(&Self, |Self| -> bool);
|
2013-09-22 16:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait to access rows of a matrix or a vector.
|
|
|
|
pub trait Row<R> {
|
|
|
|
/// The number of column of `self`.
|
2013-10-06 22:54:09 +08:00
|
|
|
fn nrows(&self) -> uint;
|
2013-09-22 16:58:21 +08:00
|
|
|
/// Reads the `i`-th row of `self`.
|
|
|
|
fn row(&self, i: uint) -> R;
|
|
|
|
/// Writes the `i`-th row of `self`.
|
|
|
|
fn set_row(&mut self, i: uint, R);
|
2013-09-22 20:22:17 +08:00
|
|
|
|
|
|
|
// FIXME: add iterators on rows: this could be a very good way to generalize _and_ optimize
|
|
|
|
// a lot of operations.
|
2013-09-22 16:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait to access columns of a matrix or vector.
|
|
|
|
pub trait Col<C> {
|
|
|
|
/// The number of column of this matrix or vector.
|
2013-10-06 22:54:09 +08:00
|
|
|
fn ncols(&self) -> uint;
|
2013-09-22 20:22:17 +08:00
|
|
|
|
2013-09-22 16:58:21 +08:00
|
|
|
/// Reads the `i`-th column of `self`.
|
|
|
|
fn col(&self, i: uint) -> C;
|
2013-09-22 20:22:17 +08:00
|
|
|
|
2013-09-22 16:58:21 +08:00
|
|
|
/// Writes the `i`-th column of `self`.
|
|
|
|
fn set_col(&mut self, i: uint, C);
|
2013-09-22 20:22:17 +08:00
|
|
|
|
|
|
|
// FIXME: add iterators on columns: this could be a very good way to generalize _and_ optimize
|
|
|
|
// a lot of operations.
|
2013-09-22 16:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait of objects having a spacial dimension known at compile time.
|
|
|
|
pub trait Dim {
|
|
|
|
/// The dimension of the object.
|
|
|
|
fn dim(unused_self: Option<Self>) -> uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this trait should not be on nalgebra.
|
|
|
|
// however, it is needed because std::ops::Index is (strangely) to poor: it
|
|
|
|
// does not have a function to set values.
|
|
|
|
// Also, using Index with tuples crashes.
|
|
|
|
/// This is a workaround of current Rust limitations.
|
|
|
|
///
|
|
|
|
/// It exists because the `Index` trait cannot be used to express write access.
|
|
|
|
/// Thus, this is the same as the `Index` trait but without the syntactic sugar and with a method
|
|
|
|
/// to write to a specific index.
|
|
|
|
pub trait Indexable<Index, Res> {
|
|
|
|
/// Reads the `i`-th element of `self`.
|
|
|
|
fn at(&self, i: Index) -> Res;
|
|
|
|
/// Writes to the `i`-th element of `self`.
|
|
|
|
fn set(&mut self, i: Index, Res);
|
|
|
|
/// Swaps the `i`-th element of `self` with its `j`-th element.
|
|
|
|
fn swap(&mut self, i: Index, j: Index);
|
2013-12-02 03:17:18 +08:00
|
|
|
|
|
|
|
/// Reads the `i`-th element of `self`.
|
|
|
|
///
|
|
|
|
/// `i` is not checked.
|
|
|
|
unsafe fn unsafe_at(&self, i: Index) -> Res;
|
|
|
|
/// Writes to the `i`-th element of `self`.
|
|
|
|
///
|
|
|
|
/// `i` is not checked.
|
|
|
|
unsafe fn unsafe_set(&mut self, i: Index, Res);
|
2013-09-22 16:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a workaround of current Rust limitations.
|
|
|
|
///
|
|
|
|
/// Traits of objects which can be iterated through like a vector.
|
|
|
|
pub trait Iterable<N> {
|
|
|
|
/// Gets a vector-like read-only iterator.
|
|
|
|
fn iter<'l>(&'l self) -> VecIterator<'l, N>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a workaround of current Rust limitations.
|
|
|
|
///
|
|
|
|
/// Traits of mutable objects which can be iterated through like a vector.
|
|
|
|
pub trait IterableMut<N> {
|
|
|
|
/// Gets a vector-like read-write iterator.
|
|
|
|
fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N>;
|
|
|
|
}
|