rustconstantsbuildernalgebra

How to allocate a static `SMatrix`/`SVector` using nalgebra?


I would like to allocate an SVector<f64, 7> as a static variable. It seems that nalgebra's library-provided builders cannot do this, as none of them are const. Is there a workaround?

Here's what I tried (this doesn't compile):

const C5: SVector<f64, 7> = SVector::<f64, 7>::from_row_slice(&[
  5179.0 / 57600.0,
  0.0,
  7571.0 / 16695.0,
  393.0 / 640.0,
  -92097.0 / 339200.0,
  187.0 / 2100.0,
  1.0 / 40.0,
]);

Edit: As @kmdreko pointed out, the following questions were more appropriate for a separate post: Why can't Rust use non-const-qualified functions to allocate const variables? Why does it matter whether or not a function is const if the data it's operating on is const?


Solution

  • I found a const constructor: from_array_storage.

    use nalgebra::{ArrayStorage, SVector};
    
    const C5: SVector<f64, 7> = SVector::<f64, 7>::from_array_storage(ArrayStorage([[
        5179.0 / 57600.0,
        0.0,
        7571.0 / 16695.0,
        393.0 / 640.0,
        -92097.0 / 339200.0,
        187.0 / 2100.0,
        1.0 / 40.0,
    ]]));
    

    For your other questions: