Random initialization of EuclideanSpace

help

#1

I want to generate random value in generics like below.
How can i do this?

extern crate nalgebra as na;
extern crate alga;
use alga::linear::EuclideanSpace;

fn rand_add<V: EuclideanSpace>(val: V) -> V {
    // How can i generate random V?
    let rand_v = randomly_initialized_v();
    return val + rand_v;
}

fn main() {
    let v1 = na::Vector3::<f32>::new();
    println!("{:?}", rand_add(v1));
}

#2

First, EuclideanSpace is an abstraction for points, not vectors. So in your main() you should work with a Point3 instead of a Vector3. If you want an abstraction over vectors, you might use FiniteDimInnerSpace.

To obtain random values, you may add the Rand trait from the rand crate as a bound. For example, the following adds a random vector to a point:

extern crate rand;
extern crate nalgebra as na;
extern crate alga;
use rand::Rand;
use alga::linear::EuclideanSpace;

fn rand_add<P: EuclideanSpace>(val: P) -> P
   where P::Coordinates {
    // How can i generate random V?
    let rand_v: P::Coordinates = rand::random();
    return val + rand_v;
}

fn main() {
    let p1 = na::Point3::<f32>::new(1.0, 2.0, 3.0);
    println!("{:?}", rand_add(p1));
}

#3

Thank you for kind help!

I changed a little like below, that works fine.

and I succeeded to write vector version as you mentions.

extern crate rand;
extern crate nalgebra as na;
extern crate alga;
use rand::Rand;
use alga::linear::FiniteDimInnerSpace;

fn rand_add<P: FiniteDimInnerSpace + Rand>(val: P) -> P {
    let rand_v = rand::random();
    return val + rand_v;
}

fn main() {
    let p1 = na::Vector3::<f32>::new(1.0, 2.0, 3.0);
    println!("{:?}", rand_add(p1));
}