dimforge / nalgebra

Linear algebra library for Rust.
https://nalgebra.org
Apache License 2.0
3.95k stars 471 forks source link

How to compute determinant of an SMatrix? #1297

Open MelvinSam2000 opened 1 year ago

MelvinSam2000 commented 1 year ago

I am unable to compute the determinant of an SMatrix (even if the dimensions are equal). Here is the error I get:

error[E0599]: the method determinant exists for reference &Matrix<f64, Const<N>, Const<N>, ArrayStorage<f64, N, N>>, but its trait bounds were not satisfied --> src/gmm.rs:73:25 73 * covar.determinant()) ^^^^^^^^^^^ method cannot be called on &Matrix<f64, Const<N>, Const<N>, ArrayStorage<f64, N, N>> due to unsatisfied trait bounds
::: /home/msam/.cargo/registry/src/github.com-1ecc6299db9ec823/nalgebra-0.32.3/src/base/dimension.rs:220:1 220 pub struct Const;
doesn't satisfy <Const<N> as DimMin<Const<N>>>::Output = Const<N>
doesn't satisfy Const<N>: DimMin<Const<N>>
= note: the following trait bounds were not satisfied:
        `<Const<N> as DimMin<Const<N>>>::Output = Const<N>`
        `Const<N>: DimMin<Const<N>>`

Is there something Im missing or is it just not ready for SMatrices yet?

JulianKnodt commented 1 year ago

Static matrices should support determinants:

i.e. this code works:

use nalgebra; // 0.32.3

fn main() {
    let m = nalgebra::base::Matrix::<f32, nalgebra::base::Const<4>, nalgebra::base::Const<4>, _>::new(1., 1., 1., 1.,
    1., 1., 1., 1.,
    1., 1., 1., 1.,
    1., 1., 1., 1.,);

    let det = m.determinant();
}

Can you provide the source of your code to help understand why it's not working?

MelvinSam2000 commented 1 year ago

Hi, here is the sample code which does not compile: fn demonstration<const N: usize>(mat: SMatrix<f64, N, N>) -> f64 { mat.determinant() }

This does not compile and gives the error I posted. After asking in the discord server I got a quick solution as such:

fn demonstration<const N: usize>(mat: &SMatrix<f64, N, N>) -> f64 { mat.view((0, 0), (N, N)).determinant() }

This works so it was enough for me, but not sure if its ideal. Letting you know just in case.