0

I have a function that takes in a 2-D DMatrix, x as a parameter, operates on slices of x and takes the output of the operation and inserts it into a new output matrix. What I have so far, with code and pseuod-code:

use nalgebra::DMatrix;  // 0.31.1

fn rolling_function(x: DMatrix<f64>) -> DMatrix<f64> {
    let nrows = x.shape().0;
    let ncols = x.shape().1;
    let mut out = DMatrix::from_element(nrows, ncols, 0.);  // initialize x with zeros
    let mut y: DMatrix<f64>;
    let mut tmp_arr: DMatrix<f64>;
    for i in 0..nrows {
        //pseudo-code part
        tmp_arr = x[0..i; all_cols];  // how do I take slices of x here?
        y = my_func(&tmp_arr);  // Some function that operates on &DMatrix<f64> and returns a DMatrix<f64>
        out[i; all_cols] = y;  // how do I overwrite parts of out here?
    }
    return out;
}

The issue is, I want to take a slice of x using something like x[0..i; all_cols], which would only take elements of x up to i, and all columns of x. Then I want to write y, which is a ncols by 1 DMatrix, into out at the i'th element.

What is the best way to do this using Rust and nalgebra syntax?

PyRsquared
  • 6,970
  • 11
  • 50
  • 86

0 Answers0