nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Implement Default for all matrix types in `nalgebra` and `nalgebra-sparse`

Open Andlon opened this issue 2 years ago • 4 comments

Currently, VecStorage has no Default impl, and CsrMatrix, CscMatrix etc. have no default impls.

Andlon avatar Feb 09 '23 07:02 Andlon

I wouldn't mind giving this one a try. Would it be preferred that they generate identity matrices or 0 matrices as defaults?

AnbyKatz avatar Jun 09 '23 03:06 AnbyKatz

I just picked up this library and came across this issue as well. Just running the following line:

let x = nalgebra::SVector::<f64, 37>::default();

fails with the following:

error[E0599]: the function or associated item `default` exists for struct `Matrix<f64, Const<37>, Const<1>, ArrayStorage<f64, 37, 1>>`, but its trait bounds were not satisfied
   --> src\bin\main.rs:136:51
    |
136 |     let x = nalgebra::SVector::<f64, 37>::default();
    |                                                   ^^^^^^^ function or associated item cannot be called due to unsatisfied trait bounds
    |
   ::: C:\Users\acxz\.cargo\registry\src\index.crates.io-6f17d22bba15001f\nalgebra-0.32.3\src\base\array_storage.rs:46:1
    |
46  | pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);
    | ---------------------------------------------------------- doesn't satisfy `ArrayStorage<f64, 37, 1>: Default`
    |
   ::: C:\Users\acxz\.cargo\registry\src\index.crates.io-6f17d22bba15001f\nalgebra-0.32.3\src\base\matrix.rs:175:1
    |
175 | pub struct Matrix<T, R, C, S> {
    | ----------------------------- doesn't satisfy `_: Default`
    |
    = note: the following trait bounds were not satisfied:
            `ArrayStorage<f64, 37, 1>: Default`
            which is required by `Matrix<f64, Const<37>, Const<1>, ArrayStorage<f64, 37, 1>>: Default`   

Values up to 32 do work however, looks like for static vectors, default isn't implemented for sizes up to 64.

Would implementing this be feasible and part of the roadplan?

Edit: going through the code I see that values up to 127 are included: https://github.com/dimforge/nalgebra/blob/f404bcbd6d9b2acd699b0b9ec8ff387d8a9c1742/src/base/dimension.rs#L325-L334

Is there a reason or has it been simply overlooked that sizes above 32 do not implement the default trait?

acxz avatar Sep 25 '23 21:09 acxz