numeris 0.5.5

Pure-Rust numerical algorithms library — high performance with SIMD support while also supporting no-std for embedded and WASM targets.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Conversions between numeris and nalgebra matrix/vector types.
//!
//! Enabled by the `nalgebra` cargo feature. Provides zero-copy borrows
//! via `as_slice` where possible, and cheap `memcpy`-level owned conversions
//! for fixed-size and dynamic matrices.
//!
//! # Layout compatibility
//!
//! Both numeris and nalgebra use column-major contiguous storage:
//! - `Matrix<T, M, N>` stores `[[T; M]; N]` — N columns of M rows
//! - `nalgebra::SMatrix<T, R, C>` stores `[T; R*C]` column-major
//!
//! Owned conversions go through `from_column_slice` / `as_slice`, which is a
//! single `memcpy` for fixed-size types (≤ 288 bytes for a 6×6 f64 matrix).
//!
//! # Vector convention
//!
//! numeris `Vector<T, N>` is `Matrix<T, N, 1>` — an N×1 column vector,
//! matching nalgebra's `SVector<T, N>` = `SMatrix<T, N, 1>` exactly.
//! The `From` impls for `Matrix<T, M, N>` cover vectors automatically.
//!
//! # Examples
//!
//! ```
//! # #[cfg(feature = "nalgebra")] {
//! use numeris::Matrix;
//!
//! let nm = Matrix::new([[1.0_f64, 2.0], [3.0, 4.0]]);
//! let na: nalgebra::SMatrix<f64, 2, 2> = nm.into();
//! assert_eq!(na[(0, 0)], 1.0);
//! assert_eq!(na[(1, 0)], 3.0);
//!
//! let back: Matrix<f64, 2, 2> = na.into();
//! assert_eq!(back, nm);
//! # }
//! ```

use crate::traits::{MatrixMut, MatrixRef, Scalar};
use crate::Matrix;

// ── Fixed-size Matrix ↔ SMatrix ────────────────────────────────────

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    From<Matrix<T, M, N>> for nalgebra::SMatrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn from(m: Matrix<T, M, N>) -> Self {
        nalgebra::SMatrix::from_column_slice(m.as_slice())
    }
}

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    From<&Matrix<T, M, N>> for nalgebra::SMatrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn from(m: &Matrix<T, M, N>) -> Self {
        nalgebra::SMatrix::from_column_slice(m.as_slice())
    }
}

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    From<nalgebra::SMatrix<T, M, N>> for Matrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn from(m: nalgebra::SMatrix<T, M, N>) -> Self {
        Matrix::from_slice(m.as_slice())
    }
}

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    From<&nalgebra::SMatrix<T, M, N>> for Matrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn from(m: &nalgebra::SMatrix<T, M, N>) -> Self {
        Matrix::from_slice(m.as_slice())
    }
}

// ── Vector ↔ SVector ───────────────────────────────────────────────
//
// Vector<T,N> = Matrix<T,N,1> and SVector<T,N> = SMatrix<T,N,1>:
// already covered by the Matrix<T,M,N> ↔ SMatrix<T,M,N> impls above.
// No special convenience methods needed.

// ── MatrixRef / MatrixMut for nalgebra::SMatrix ────────────────────

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    MatrixRef<T> for nalgebra::SMatrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn nrows(&self) -> usize {
        M
    }

    #[inline]
    fn ncols(&self) -> usize {
        N
    }

    #[inline]
    fn get(&self, row: usize, col: usize) -> &T {
        &self[(row, col)]
    }

    #[inline]
    fn col_as_slice(&self, col: usize, row_start: usize) -> &[T] {
        let start = col * M + row_start;
        let end = (col + 1) * M;
        &self.as_slice()[start..end]
    }
}

impl<T: Scalar + nalgebra::Scalar, const M: usize, const N: usize>
    MatrixMut<T> for nalgebra::SMatrix<T, M, N>
where
    nalgebra::Const<M>: nalgebra::DimName,
    nalgebra::Const<N>: nalgebra::DimName,
{
    #[inline]
    fn get_mut(&mut self, row: usize, col: usize) -> &mut T {
        &mut self[(row, col)]
    }

    #[inline]
    fn col_as_mut_slice(&mut self, col: usize, row_start: usize) -> &mut [T] {
        let start = col * M + row_start;
        let end = (col + 1) * M;
        &mut self.as_mut_slice()[start..end]
    }
}

// ── Dynamic: DynMatrix ↔ DMatrix, DynVector ↔ DVector ─────────────

#[cfg(feature = "alloc")]
mod dyn_interop {
    use crate::dynmatrix::{DynMatrix, DynVector};
    use crate::traits::{MatrixMut, MatrixRef, Scalar};

    impl<T: Scalar + nalgebra::Scalar> From<DynMatrix<T>> for nalgebra::DMatrix<T> {
        #[inline]
        fn from(m: DynMatrix<T>) -> Self {
            nalgebra::DMatrix::from_column_slice(m.nrows(), m.ncols(), m.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<&DynMatrix<T>> for nalgebra::DMatrix<T> {
        #[inline]
        fn from(m: &DynMatrix<T>) -> Self {
            nalgebra::DMatrix::from_column_slice(m.nrows(), m.ncols(), m.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<nalgebra::DMatrix<T>> for DynMatrix<T> {
        #[inline]
        fn from(m: nalgebra::DMatrix<T>) -> Self {
            DynMatrix::from_slice(m.nrows(), m.ncols(), m.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<&nalgebra::DMatrix<T>> for DynMatrix<T> {
        #[inline]
        fn from(m: &nalgebra::DMatrix<T>) -> Self {
            DynMatrix::from_slice(m.nrows(), m.ncols(), m.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<DynVector<T>> for nalgebra::DVector<T> {
        #[inline]
        fn from(v: DynVector<T>) -> Self {
            nalgebra::DVector::from_column_slice(v.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<&DynVector<T>> for nalgebra::DVector<T> {
        #[inline]
        fn from(v: &DynVector<T>) -> Self {
            nalgebra::DVector::from_column_slice(v.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<nalgebra::DVector<T>> for DynVector<T> {
        #[inline]
        fn from(v: nalgebra::DVector<T>) -> Self {
            DynVector::from_slice(v.as_slice())
        }
    }

    impl<T: Scalar + nalgebra::Scalar> From<&nalgebra::DVector<T>> for DynVector<T> {
        #[inline]
        fn from(v: &nalgebra::DVector<T>) -> Self {
            DynVector::from_slice(v.as_slice())
        }
    }

    // ── MatrixRef / MatrixMut for nalgebra::DMatrix ────────────────

    impl<T: Scalar + nalgebra::Scalar> MatrixRef<T> for nalgebra::DMatrix<T> {
        #[inline]
        fn nrows(&self) -> usize {
            self.nrows()
        }

        #[inline]
        fn ncols(&self) -> usize {
            self.ncols()
        }

        #[inline]
        fn get(&self, row: usize, col: usize) -> &T {
            &self[(row, col)]
        }

        #[inline]
        fn col_as_slice(&self, col: usize, row_start: usize) -> &[T] {
            let nrows = self.nrows();
            let start = col * nrows + row_start;
            let end = (col + 1) * nrows;
            &self.as_slice()[start..end]
        }
    }

    impl<T: Scalar + nalgebra::Scalar> MatrixMut<T> for nalgebra::DMatrix<T> {
        #[inline]
        fn get_mut(&mut self, row: usize, col: usize) -> &mut T {
            &mut self[(row, col)]
        }

        #[inline]
        fn col_as_mut_slice(&mut self, col: usize, row_start: usize) -> &mut [T] {
            let nrows = self.nrows();
            let start = col * nrows + row_start;
            let end = (col + 1) * nrows;
            &mut self.as_mut_slice()[start..end]
        }
    }
}

// ── Tests ──────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn matrix_to_smatrix_roundtrip() {
        let nm = Matrix::new([[1.0_f64, 2.0], [3.0, 4.0]]);
        let na: nalgebra::SMatrix<f64, 2, 2> = nm.into();
        assert_eq!(na[(0, 0)], 1.0);
        assert_eq!(na[(0, 1)], 2.0);
        assert_eq!(na[(1, 0)], 3.0);
        assert_eq!(na[(1, 1)], 4.0);
        let back: Matrix<f64, 2, 2> = na.into();
        assert_eq!(back, nm);
    }

    #[test]
    fn matrix_ref_roundtrip() {
        let nm = Matrix::new([[1.0_f64, 2.0, 3.0], [4.0, 5.0, 6.0]]);
        let na: nalgebra::SMatrix<f64, 2, 3> = (&nm).into();
        let back: Matrix<f64, 2, 3> = (&na).into();
        assert_eq!(back, nm);
    }

    #[test]
    fn vector_to_svector_roundtrip() {
        // Vector<T,N> = Matrix<T,N,1> and SVector<T,N> = SMatrix<T,N,1>:
        // From impls work directly, no transpose needed.
        use crate::Vector;
        let nv = Vector::from_array([1.0_f64, 2.0, 3.0]);
        let na: nalgebra::SVector<f64, 3> = nv.into();
        assert_eq!(na[0], 1.0);
        assert_eq!(na[1], 2.0);
        assert_eq!(na[2], 3.0);
        assert_eq!(na.nrows(), 3);
        assert_eq!(na.ncols(), 1);
        let back: Vector<f64, 3> = na.into();
        assert_eq!(back, nv);
    }

    #[test]
    fn vector_ref_roundtrip() {
        use crate::Vector;
        let nv = Vector::from_array([10.0_f32, 20.0, 30.0, 40.0]);
        let na: nalgebra::SMatrix<f32, 4, 1> = (&nv).into();
        let back: Vector<f32, 4> = (&na).into();
        assert_eq!(back, nv);
    }

    #[test]
    fn smatrix_matrix_ref_trait() {
        let na = nalgebra::SMatrix::<f64, 3, 3>::new(
            1.0, 2.0, 3.0,
            4.0, 5.0, 6.0,
            7.0, 8.0, 9.0,
        );
        assert_eq!(na.nrows(), 3);
        assert_eq!(na.ncols(), 3);
        assert_eq!(*MatrixRef::get(&na, 0, 0), 1.0);
        assert_eq!(*MatrixRef::get(&na, 1, 0), 4.0);
        assert_eq!(*MatrixRef::get(&na, 0, 1), 2.0);

        let col1 = MatrixRef::col_as_slice(&na, 1, 0);
        assert_eq!(col1, &[2.0, 5.0, 8.0]);

        let col2_from1 = MatrixRef::col_as_slice(&na, 2, 1);
        assert_eq!(col2_from1, &[6.0, 9.0]);
    }

    #[test]
    fn smatrix_matrix_mut_trait() {
        let mut na = nalgebra::SMatrix::<f64, 2, 2>::zeros();
        *MatrixMut::get_mut(&mut na, 0, 1) = 42.0;
        assert_eq!(na[(0, 1)], 42.0);

        let col = MatrixMut::col_as_mut_slice(&mut na, 0, 0);
        col[0] = 10.0;
        col[1] = 20.0;
        assert_eq!(na[(0, 0)], 10.0);
        assert_eq!(na[(1, 0)], 20.0);
    }

    #[test]
    fn lu_on_nalgebra_smatrix() {
        use crate::linalg::lu::lu_in_place;

        let mut na = nalgebra::SMatrix::<f64, 3, 3>::new(
            2.0, 1.0, -1.0,
            -3.0, -1.0, 2.0,
            -2.0, 1.0, 2.0,
        );
        let mut perm = [0usize; 3];
        let result = lu_in_place(&mut na, &mut perm);
        assert!(result.is_ok());
    }

    #[test]
    fn cholesky_on_nalgebra_smatrix() {
        use crate::linalg::cholesky::cholesky_in_place;

        // SPD matrix: [[4, 2], [2, 3]]
        let mut na = nalgebra::SMatrix::<f64, 2, 2>::new(4.0, 2.0, 2.0, 3.0);
        let result = cholesky_in_place(&mut na);
        assert!(result.is_ok());
    }

    #[test]
    fn nonsquare_matrix_roundtrip() {
        let nm = Matrix::new([[1.0_f64, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]]);
        let na: nalgebra::SMatrix<f64, 3, 4> = nm.into();
        let back: Matrix<f64, 3, 4> = na.into();
        assert_eq!(back, nm);
    }

    #[test]
    fn i32_matrix_roundtrip() {
        let nm = Matrix::new([[1_i32, 2], [3, 4]]);
        let na: nalgebra::SMatrix<i32, 2, 2> = nm.into();
        let back: Matrix<i32, 2, 2> = na.into();
        assert_eq!(back, nm);
    }

    #[cfg(feature = "alloc")]
    mod dyn_tests {
        use crate::dynmatrix::{DynMatrix, DynVector};
        use crate::traits::{MatrixMut, MatrixRef};

        #[test]
        fn dynmatrix_to_dmatrix_roundtrip() {
            let dm = DynMatrix::from_rows(2, 3, &[1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
            let na: nalgebra::DMatrix<f64> = (&dm).into();
            assert_eq!(na.nrows(), 2);
            assert_eq!(na.ncols(), 3);
            assert_eq!(na[(0, 0)], 1.0);
            assert_eq!(na[(0, 2)], 3.0);
            assert_eq!(na[(1, 0)], 4.0);
            let back: DynMatrix<f64> = na.into();
            assert_eq!(back, dm);
        }

        #[test]
        fn dynvector_to_dvector_roundtrip() {
            let dv = DynVector::from_slice(&[1.0_f64, 2.0, 3.0]);
            let na: nalgebra::DVector<f64> = (&dv).into();
            assert_eq!(na.len(), 3);
            assert_eq!(na[0], 1.0);
            assert_eq!(na[2], 3.0);
            let back: DynVector<f64> = na.into();
            assert_eq!(back, dv);
        }

        #[test]
        fn dmatrix_matrix_ref_trait() {
            let na = nalgebra::DMatrix::from_row_slice(2, 3, &[
                1.0_f64, 2.0, 3.0,
                4.0, 5.0, 6.0,
            ]);
            assert_eq!(MatrixRef::nrows(&na), 2);
            assert_eq!(MatrixRef::ncols(&na), 3);
            assert_eq!(*MatrixRef::get(&na, 0, 0), 1.0);
            assert_eq!(*MatrixRef::get(&na, 1, 0), 4.0);
            assert_eq!(*MatrixRef::get(&na, 0, 2), 3.0);

            let col1 = MatrixRef::col_as_slice(&na, 1, 0);
            assert_eq!(col1, &[2.0, 5.0]);
        }

        #[test]
        fn dmatrix_matrix_mut_trait() {
            let mut na = nalgebra::DMatrix::zeros(3, 3);
            *MatrixMut::get_mut(&mut na, 1, 2) = 99.0;
            assert_eq!(na[(1, 2)], 99.0);

            let col = MatrixMut::col_as_mut_slice(&mut na, 0, 1);
            col[0] = 10.0;
            col[1] = 20.0;
            assert_eq!(na[(1, 0)], 10.0);
            assert_eq!(na[(2, 0)], 20.0);
        }

        #[test]
        fn lu_on_nalgebra_dmatrix() {
            use crate::linalg::lu::lu_in_place;

            let mut na = nalgebra::DMatrix::from_row_slice(3, 3, &[
                2.0_f64, 1.0, -1.0,
                -3.0, -1.0, 2.0,
                -2.0, 1.0, 2.0,
            ]);
            let mut perm = [0usize; 3];
            let result = lu_in_place(&mut na, &mut perm);
            assert!(result.is_ok());
        }
    }
}