nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

How to compute determinant of an SMatrix?

Open MelvinSam2000 opened this issue 2 years ago • 2 comments

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<const R: usize>; | -------------------------------- | | | 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?

MelvinSam2000 avatar Sep 19 '23 01:09 MelvinSam2000

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?

JulianKnodt avatar Sep 26 '23 05:09 JulianKnodt

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.

MelvinSam2000 avatar Sep 26 '23 13:09 MelvinSam2000