How to create a NxN matrix in nalgebra?

Hello ! I don't know how to create an NXN matrix in nalgebra, knowing the size in compilation.

For example i need this matrix: [[f32; MAX]; MAX]; but in nalgebra library. I read examples but i don't find for nxn.

Thank you !!

Reading the docs https://nalgebra.org/vectors_and_matrices/ something like:

use typenum::U1000; // just some example value.

type NxNMatrix = Matrix<f32, U1000, U1000, MatrixArray<f32, U1000, U1000>;

might work for you.

@Voultapher Perfect, thanks ! i have this because MatrixArray is deprecated:

use nalgebra::{ArrayStorage, Matrix};
use typenum::U1024;

    type NxNMatrix = Matrix<f32, U1024, U1024, ArrayStorage<f32, U1024, U1024>>;

    fn main() {
        let matrix = ;
    }

Now how can i create an NXN empty matrix (or with zeros, is the same) ? Because in the doc i see that they put the values while they instantiate it.

Thanks.

Disclaimer I've never use nalgebra myself, the convention for 'empty' constructing objects in Rust is by calling either new() or default(). Try that or from the docs https://nalgebra.org/rustdoc/nalgebra/base/struct.Matrix.html#method.identity_generic, I'll admit I find the documentation somewhat hard to grog.

You can do NxNMatrix::zeros() to get a matrix filled with zeros. You will find a bunch of construction methods there: https://www.nalgebra.org/quick_reference/#construction

Also your definition of NxNMatrix can be simpler using the MatrixN type alias:

type NxNMatrix = nalgebra::MatrixN<f32, U1024>;

Thank you ! I wan't to create a 16X16 matrix but this:
use nalgebra::*;

use typenum::U16;

type NxNMatrix = MatrixN<f32, U16>;

fn main() {
    let graph = NxNMatrix::zeros();
}

gives me:

the trait bound typenum::uint::UTerm: nalgebra::base::dimension::DimName is not satisfied

Whell, i have to use use nalgebra::base::dimension::U16; !

Yes, for all dimensions smaller or equal to 127, you need to use nalgebra::U1 up to nalgebra::U127. So in your case you need nalgebra::U16.

For dimensions greater than 127 you need to use typenum. This is done this way so we can have much better error messages for dimensions <= 127 (compiler errors involving typenum types are very difficult to read).

Sorry i have a problem. I have this:

use typenum::U1024;
type NxNMatrix = MatrixN<f32, U1024>;

fn main() {
let graph: NxNMatrix = NxNMatrix::zeros();

println!("{}", graph[(0,0)]);

}

So, this code works for me with a common matrix, but with nalgebra when i do a print this happen:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
./run.sh: line 15: 34119 Aborted (core dumped)

I have 12GB RAM.

What is happening ? Thanks

What is a "common matrix"?

You are getting a stack overflow here because MatrixN<f32, U1024> is a 1024x1024 stack-allocated matrix. The stack size is independent from the amount of RAM you have and 1024*1024 elements is to much for the stack to handle, hence the overflow as soon as you enter the main().

What you need instead is a heap-allocated matrix. You can't just do, e.g., Box::new(NxNMatrix::zeros()) because unfortunately Rust will still allocate your matrix on the stack first and then copy it to the boxed (heap-applocated) memory. So your solution for large matrices is to use the nalgebra::DMatrix type which have dimensions set at runtime and is always allocated on the heap. In your case:

type NxNMatrix = DMatrix<f32>;
let graph = DMatrix::zeros(1024, 1024);