Add more functions to initialize DMat and DVec.
This commit is contained in:
parent
a00860df41
commit
8db01c6409
40
src/dmat.rs
40
src/dmat.rs
@ -1,5 +1,5 @@
|
||||
use std::num::{One, Zero};
|
||||
use std::vec::from_elem;
|
||||
use std::vec;
|
||||
use std::cmp::ApproxEq;
|
||||
use std::util;
|
||||
use traits::inv::Inv;
|
||||
@ -23,11 +23,7 @@ impl<N: Zero + Clone> DMat<N> {
|
||||
/// components.
|
||||
#[inline]
|
||||
pub fn new_zeros(nrows: uint, ncols: uint) -> DMat<N> {
|
||||
DMat {
|
||||
nrows: nrows,
|
||||
ncols: ncols,
|
||||
mij: from_elem(nrows * ncols, Zero::zero())
|
||||
}
|
||||
DMat::from_elem(nrows, ncols, Zero::zero())
|
||||
}
|
||||
|
||||
/// Tests if all components of the matrix are zeroes.
|
||||
@ -37,6 +33,38 @@ impl<N: Zero + Clone> DMat<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: One + Clone> DMat<N> {
|
||||
/// Builds a matrix filled with a given constant.
|
||||
#[inline]
|
||||
pub fn new_ones(nrows: uint, ncols: uint) -> DMat<N> {
|
||||
DMat::from_elem(nrows, ncols, One::one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Clone> DMat<N> {
|
||||
/// Builds a matrix filled with a given constant.
|
||||
#[inline]
|
||||
pub fn from_elem(nrows: uint, ncols: uint, val: N) -> DMat<N> {
|
||||
DMat {
|
||||
nrows: nrows,
|
||||
ncols: ncols,
|
||||
mij: vec::from_elem(nrows * ncols, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> DMat<N> {
|
||||
/// Builds a matrix filled with a given constant.
|
||||
#[inline(always)]
|
||||
pub fn from_fn(nrows: uint, ncols: uint, f: &fn(uint, uint) -> N) -> DMat<N> {
|
||||
DMat {
|
||||
nrows: nrows,
|
||||
ncols: ncols,
|
||||
mij: vec::from_fn(nrows * ncols, |i| { let m = i % ncols; f(m, m - i * ncols) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: add a function to modify the dimension (to avoid useless allocations)?
|
||||
|
||||
impl<N: One + Zero + Clone> DMat<N> {
|
||||
|
31
src/dvec.rs
31
src/dvec.rs
@ -1,6 +1,6 @@
|
||||
use std::num::{Zero, One, Algebraic};
|
||||
use std::vec;
|
||||
use std::vec::{VecIterator, VecMutIterator};
|
||||
use std::vec::from_elem;
|
||||
use std::cmp::ApproxEq;
|
||||
use std::iterator::FromIterator;
|
||||
use traits::vector::{Vec, AlgebraicVec};
|
||||
@ -22,7 +22,7 @@ impl<N: Zero + Clone> DVec<N> {
|
||||
/// * `dim` - The dimension of the vector.
|
||||
#[inline]
|
||||
pub fn new_zeros(dim: uint) -> DVec<N> {
|
||||
DVec { at: from_elem(dim, Zero::zero()) }
|
||||
DVec::from_elem(dim, Zero::zero())
|
||||
}
|
||||
|
||||
/// Tests if all components of the vector are zeroes.
|
||||
@ -32,6 +32,33 @@ impl<N: Zero + Clone> DVec<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: One + Clone> DVec<N> {
|
||||
/// Builds a vector filled with ones.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `dim` - The dimension of the vector.
|
||||
#[inline]
|
||||
pub fn new_ones(dim: uint) -> DVec<N> {
|
||||
DVec::from_elem(dim, One::one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Clone> DVec<N> {
|
||||
/// Builds a vector filled with a constant.
|
||||
#[inline]
|
||||
pub fn from_elem(dim: uint, elem: N) -> DVec<N> {
|
||||
DVec { at: vec::from_elem(dim, elem) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Clone> DVec<N> {
|
||||
/// Builds a vector filled with the result of a function.
|
||||
#[inline(always)]
|
||||
pub fn from_fn(dim: uint, f: &fn(uint) -> N) -> DVec<N> {
|
||||
DVec { at: vec::from_fn(dim, |i| f(i)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> Iterable<N> for DVec<N> {
|
||||
fn iter<'l>(&'l self) -> VecIterator<'l, N> {
|
||||
self.at.iter()
|
||||
|
Loading…
Reference in New Issue
Block a user