How to Do Linear Algebra in Rust (nalgebra, faer)

TITLE: How to Do Linear Algebra in Rust (nalgebra, faer)

TITLE: How to Do Linear Algebra in Rust (nalgebra, faer)

Use the nalgebra crate for general linear algebra or faer for high-performance GPU-accelerated operations. Add the crate to your Cargo.toml, import the types, and use the SMatrix or Matrix constructors to create and manipulate matrices.

[dependencies]
nalgebra = "0.33"
faer = "0.19"
use nalgebra::{Matrix2, SMatrix};
use faer::cpu::Cpu;
use faer::Matrix;

fn main() {
    let m = Matrix2::new(1.0, 2.0, 3.0, 4.0);
    let s = SMatrix::<f64, 2, 2>::new(1.0, 2.0, 3.0, 4.0);
    
    let cpu = Cpu::new();
    let faer_mat = Matrix::from_fn(&cpu, 2, 2, |i, j| i as f64 + j as f64);
    
    println!("Nalgebra: {}", m);
    println!("Faer: {}", faer_mat);
}