I have an ndarray array that is used throughout my program:
Note: ndarray and nalgebra are both used here, imported as nd and na respectively.
let test: nd::ArrayBase<nd::OwnedRepr<na::Vector2<f64>>, nd::Dim<[usize; 1]>> = nd::arr1(
&[
// row 1
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
// row 2
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
// row 3
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
]
);
I'd therefore, like to make this array into a const, however, I can't find a const way to initialize the array, for example the code shown above cannot be used to declare a constant as arr1 is a non-const fn. nalgebra has from_array_storage which can be used to solve this problem for it's vectors/matricies e.g.
const DIRECTION_VELOCITIES: na::SVector<na::Vector2<f64>, 9> =
na::SVector::from_array_storage(na::ArrayStorage([[
// row 1
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
// row 2
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
// row 3
na::Vector2::new(1.0, -1.0),
na::Vector2::new(1.0, 0.0),
na::Vector2::new(1.0, 1.0),
]]));
Is there an equivalent for ndarray? and if so what is it?
OwnedRepris aVecwith slightly altered ownership semantics, it stores it's data on the heap, you can't do that at compile time.