1

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?

2
  • 1
    OwnedRepr is a Vec with slightly altered ownership semantics, it stores it's data on the heap, you can't do that at compile time. Commented Jan 26 at 22:48
  • 1
    you can use LazyLock for static, immutable, heap allocated data Commented Jan 27 at 5:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.