Get a unique element of a constant matrix



Hi there,

How can I get a unique element of a matrix? Currently, I use a DMatrix as a way to store fixed coefficients, cf this line.

I’m currently trying the following:

for j in 0..k.len() {
    let a_ij = self.a_coeffs.slice((i, j), (1, 1)).0;
    wi += a_ij * &k[j];

This fails because a_ij is not an f64. How can I get a specific value from my DMatrix<f64> as an f64?

Follow up question: would it be more memory efficient to store these values as an array of array? My reason for opting for a DMatrix is because different integrators will have a different number of values (e.g. an RK 54 can store all its coefficients in a 5x5, but an RK87 stores it in an 8x8 matrix – and I’ve defined any RK as a trait).

Thanks for your help


Since my matrix values are static, I ended up changing the signature of that function to fn a_coeffs() -> &[f64]; where the array is &'static [f64]. I haven’t checked yet that I can handle differently sized arrays for different implementations, but my initial hunch is that it should work.


I don’t think using an array of array for the coefficients would make any difference performancewise.

If I understand correctly your issue you need to access a single element of the matrix, which you can do simply by indexing it, i.e., let a_ij = self.a_coeffs[(i, j)];.