rustmultidimensional-arraynalgebra

best alternative to ndarray's .windows() method in nalgebra


I'm working on a project using central differences meaning I need to work with the previous, current and next value on each iteration. Using nalgebra this would be simple, just call .windows(3) and map the values like so:

y.windows(3).map(|window_vals| window_vals[0] - 2.0 * window_vals[1] + window_vals[2])

However, unfortunately I need to use this central difference method as part of an impl block for ode_solver's (for those of you familiar with the crate this is the system method) unfortunately the trait requirements for this library mean I need to use nalgebra matrix's instead, which don't appear to have a similar method. Please let me know what the best option is for replicating the above code snippet with when working with nalgebra matricies rather than ndarray arrays


Solution

  • You can use as_slice or as_mut_slice to get a slice view of your Matrix, then use the standard windows method on that slice:

    y.as_slice().windows(3).map(|window_vals| window_vals[0] - 2.0 * window_vals[1] + window_vals[2])