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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//! Column-major matrix type for functional data analysis.
//!
//! [`FdMatrix`] provides safe, dimension-tracked access to the flat column-major
//! data layout used throughout this crate. It eliminates manual `data[i + j * n]`
//! index arithmetic and carries dimensions alongside the data.
use nalgebra::DMatrix;
/// Column-major matrix for functional data.
///
/// Stores data in a flat `Vec<f64>` with column-major (Fortran) layout:
/// element `(row, col)` is at index `row + col * nrows`.
///
/// # Conventions
///
/// For functional data, rows typically represent observations and columns
/// represent evaluation points. For 2D surfaces with `m1 x m2` grids,
/// the surface is flattened into `m1 * m2` columns.
///
/// # Examples
///
/// ```
/// use fdars_core::matrix::FdMatrix;
///
/// // 3 observations, 4 evaluation points
/// let data = vec![
/// 1.0, 2.0, 3.0, // column 0 (all obs at point 0)
/// 4.0, 5.0, 6.0, // column 1
/// 7.0, 8.0, 9.0, // column 2
/// 10.0, 11.0, 12.0, // column 3
/// ];
/// let mat = FdMatrix::from_column_major(data, 3, 4).unwrap();
///
/// assert_eq!(mat[(0, 0)], 1.0); // obs 0 at point 0
/// assert_eq!(mat[(1, 2)], 8.0); // obs 1 at point 2
/// assert_eq!(mat.column(0), &[1.0, 2.0, 3.0]);
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdMatrix {
data: Vec<f64>,
nrows: usize,
ncols: usize,
}
impl FdMatrix {
/// Create from flat column-major data with dimension validation.
///
/// Returns `Err` if `data.len() != nrows * ncols`.
pub fn from_column_major(
data: Vec<f64>,
nrows: usize,
ncols: usize,
) -> Result<Self, crate::FdarError> {
if data.len() != nrows * ncols {
return Err(crate::FdarError::InvalidDimension {
parameter: "data",
expected: format!("{}", nrows * ncols),
actual: format!("{}", data.len()),
});
}
Ok(Self { data, nrows, ncols })
}
/// Create from a borrowed slice (copies the data).
///
/// Returns `Err` if `data.len() != nrows * ncols`.
pub fn from_slice(data: &[f64], nrows: usize, ncols: usize) -> Result<Self, crate::FdarError> {
if data.len() != nrows * ncols {
return Err(crate::FdarError::InvalidDimension {
parameter: "data",
expected: format!("{}", nrows * ncols),
actual: format!("{}", data.len()),
});
}
Ok(Self {
data: data.to_vec(),
nrows,
ncols,
})
}
/// Create a zero-filled matrix.
pub fn zeros(nrows: usize, ncols: usize) -> Self {
Self {
data: vec![0.0; nrows * ncols],
nrows,
ncols,
}
}
/// Number of rows.
#[inline]
pub fn nrows(&self) -> usize {
self.nrows
}
/// Number of columns.
#[inline]
pub fn ncols(&self) -> usize {
self.ncols
}
/// Dimensions as `(nrows, ncols)`.
#[inline]
pub fn shape(&self) -> (usize, usize) {
(self.nrows, self.ncols)
}
/// Total number of elements.
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
/// Whether the matrix is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Get a contiguous column slice (zero-copy).
///
/// # Panics
/// Panics if `col >= ncols`.
#[inline]
pub fn column(&self, col: usize) -> &[f64] {
let start = col * self.nrows;
&self.data[start..start + self.nrows]
}
/// Get a mutable contiguous column slice (zero-copy).
///
/// # Panics
/// Panics if `col >= ncols`.
#[inline]
pub fn column_mut(&mut self, col: usize) -> &mut [f64] {
let start = col * self.nrows;
&mut self.data[start..start + self.nrows]
}
/// Extract a single row as a new `Vec<f64>`.
///
/// This is an O(ncols) operation because rows are not contiguous
/// in column-major layout.
pub fn row(&self, row: usize) -> Vec<f64> {
(0..self.ncols)
.map(|j| self.data[row + j * self.nrows])
.collect()
}
/// Copy a single row into a pre-allocated buffer (zero allocation).
///
/// # Panics
/// Panics if `buf.len() < ncols` or `row >= nrows`.
#[inline]
pub fn row_to_buf(&self, row: usize, buf: &mut [f64]) {
debug_assert!(
row < self.nrows,
"row {row} out of bounds for {} rows",
self.nrows
);
debug_assert!(
buf.len() >= self.ncols,
"buffer len {} < ncols {}",
buf.len(),
self.ncols
);
let n = self.nrows;
for j in 0..self.ncols {
buf[j] = self.data[row + j * n];
}
}
/// Compute the dot product of two rows without materializing either one.
///
/// The rows may come from different matrices (which must have the same `ncols`).
///
/// # Panics
/// Panics (in debug) if `row_a >= self.nrows`, `row_b >= other.nrows`,
/// or `self.ncols != other.ncols`.
#[inline]
pub fn row_dot(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64 {
debug_assert_eq!(self.ncols, other.ncols, "ncols mismatch in row_dot");
let na = self.nrows;
let nb = other.nrows;
let mut sum = 0.0;
for j in 0..self.ncols {
sum += self.data[row_a + j * na] * other.data[row_b + j * nb];
}
sum
}
/// Compute the squared L2 distance between two rows without allocation.
///
/// Equivalent to `||self.row(row_a) - other.row(row_b)||^2` but without
/// materializing either row vector.
///
/// # Panics
/// Panics (in debug) if `row_a >= self.nrows`, `row_b >= other.nrows`,
/// or `self.ncols != other.ncols`.
#[inline]
pub fn row_l2_sq(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64 {
debug_assert_eq!(self.ncols, other.ncols, "ncols mismatch in row_l2_sq");
let na = self.nrows;
let nb = other.nrows;
let mut sum = 0.0;
for j in 0..self.ncols {
let d = self.data[row_a + j * na] - other.data[row_b + j * nb];
sum += d * d;
}
sum
}
/// Iterate over rows, yielding each row as a `Vec<f64>`.
///
/// More efficient than [`to_row_major()`](Self::to_row_major) when only a
/// subset of rows are needed or when processing rows one at a time, because
/// it materializes only one row at a time instead of allocating the entire
/// transposed matrix up front.
///
/// Because `FdMatrix` uses column-major storage, row elements are not
/// contiguous and a zero-copy row slice is not possible. Each yielded
/// `Vec<f64>` is an O(ncols) allocation.
///
/// # Examples
///
/// ```
/// use fdars_core::matrix::FdMatrix;
///
/// let mat = FdMatrix::from_column_major(vec![
/// 1.0, 2.0, // col 0
/// 3.0, 4.0, // col 1
/// 5.0, 6.0, // col 2
/// ], 2, 3).unwrap();
///
/// let rows: Vec<Vec<f64>> = mat.iter_rows().collect();
/// assert_eq!(rows, vec![vec![1.0, 3.0, 5.0], vec![2.0, 4.0, 6.0]]);
/// ```
pub fn iter_rows(&self) -> impl Iterator<Item = Vec<f64>> + '_ {
(0..self.nrows).map(move |i| self.row(i))
}
/// Iterate over columns, yielding each column as a slice `&[f64]`.
///
/// Zero-copy because `FdMatrix` uses column-major storage and each column
/// is a contiguous block in memory.
///
/// # Examples
///
/// ```
/// use fdars_core::matrix::FdMatrix;
///
/// let mat = FdMatrix::from_column_major(vec![
/// 1.0, 2.0, // col 0
/// 3.0, 4.0, // col 1
/// 5.0, 6.0, // col 2
/// ], 2, 3).unwrap();
///
/// let cols: Vec<&[f64]> = mat.iter_columns().collect();
/// assert_eq!(cols, vec![&[1.0, 2.0], &[3.0, 4.0], &[5.0, 6.0]]);
/// ```
pub fn iter_columns(&self) -> impl Iterator<Item = &[f64]> {
(0..self.ncols).map(move |j| self.column(j))
}
/// Extract all rows as `Vec<Vec<f64>>`.
///
/// Equivalent to the former `extract_curves` function.
pub fn rows(&self) -> Vec<Vec<f64>> {
(0..self.nrows).map(|i| self.row(i)).collect()
}
/// Produce a single contiguous flat buffer in row-major order.
///
/// Row `i` occupies `buf[i * ncols..(i + 1) * ncols]`. This is a single
/// allocation versus `nrows` allocations from `rows()`, and gives better
/// cache locality for row-oriented iteration.
pub fn to_row_major(&self) -> Vec<f64> {
let mut buf = vec![0.0; self.nrows * self.ncols];
for i in 0..self.nrows {
for j in 0..self.ncols {
buf[i * self.ncols + j] = self.data[i + j * self.nrows];
}
}
buf
}
/// Flat slice of the underlying column-major data (zero-copy).
#[inline]
pub fn as_slice(&self) -> &[f64] {
&self.data
}
/// Mutable flat slice of the underlying column-major data.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [f64] {
&mut self.data
}
/// Consume and return the underlying `Vec<f64>`.
pub fn into_vec(self) -> Vec<f64> {
self.data
}
/// Convert to a nalgebra `DMatrix<f64>`.
///
/// This copies the data into nalgebra's storage. Both use column-major
/// layout, so the copy is a simple memcpy.
pub fn to_dmatrix(&self) -> DMatrix<f64> {
DMatrix::from_column_slice(self.nrows, self.ncols, &self.data)
}
/// Create from a nalgebra `DMatrix<f64>`.
///
/// Both use column-major layout so this is a direct copy.
pub fn from_dmatrix(mat: &DMatrix<f64>) -> Self {
let (nrows, ncols) = mat.shape();
Self {
data: mat.as_slice().to_vec(),
nrows,
ncols,
}
}
/// Get element at (row, col) with bounds checking.
#[inline]
pub fn get(&self, row: usize, col: usize) -> Option<f64> {
if row < self.nrows && col < self.ncols {
Some(self.data[row + col * self.nrows])
} else {
None
}
}
/// Extract a submatrix by row and column indices.
///
/// Returns a new `FdMatrix` containing only the specified rows and columns.
///
/// # Examples
///
/// ```
/// use fdars_core::matrix::FdMatrix;
///
/// let mat = FdMatrix::from_column_major(
/// vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
/// 3, 3,
/// ).unwrap();
///
/// // Extract rows [0, 2] and columns [1, 2]
/// let sub = mat.submatrix(&[0, 2], &[1, 2]);
/// assert_eq!(sub.shape(), (2, 2));
/// assert_eq!(sub[(0, 0)], 4.0); // row 0, col 1 of original
/// assert_eq!(sub[(1, 1)], 9.0); // row 2, col 2 of original
/// ```
pub fn submatrix(&self, rows: &[usize], cols: &[usize]) -> Self {
let nr = rows.len();
let nc = cols.len();
let mut data = vec![0.0; nr * nc];
for (jj, &col) in cols.iter().enumerate() {
for (ii, &row) in rows.iter().enumerate() {
data[ii + jj * nr] = self.data[row + col * self.nrows];
}
}
Self {
data,
nrows: nr,
ncols: nc,
}
}
/// Extract a submatrix selecting only specific rows (all columns kept).
///
/// Equivalent to `submatrix(rows, &(0..ncols).collect::<Vec<_>>())` but
/// more efficient.
pub fn select_rows(&self, rows: &[usize]) -> Self {
let nr = rows.len();
let nc = self.ncols;
let mut data = vec![0.0; nr * nc];
for j in 0..nc {
for (ii, &row) in rows.iter().enumerate() {
data[ii + j * nr] = self.data[row + j * self.nrows];
}
}
Self {
data,
nrows: nr,
ncols: nc,
}
}
/// Extract a submatrix selecting only specific columns (all rows kept).
///
/// Efficient for column-major layout — each column is a contiguous copy.
pub fn select_columns(&self, cols: &[usize]) -> Self {
let nr = self.nrows;
let nc = cols.len();
let mut data = vec![0.0; nr * nc];
for (jj, &col) in cols.iter().enumerate() {
let src = &self.data[col * nr..(col + 1) * nr];
data[jj * nr..(jj + 1) * nr].copy_from_slice(src);
}
Self {
data,
nrows: nr,
ncols: nc,
}
}
/// Set element at (row, col) with bounds checking.
#[inline]
pub fn set(&mut self, row: usize, col: usize, value: f64) -> bool {
if row < self.nrows && col < self.ncols {
self.data[row + col * self.nrows] = value;
true
} else {
false
}
}
}
impl std::ops::Index<(usize, usize)> for FdMatrix {
type Output = f64;
#[inline]
fn index(&self, (row, col): (usize, usize)) -> &f64 {
debug_assert!(
row < self.nrows && col < self.ncols,
"FdMatrix index ({}, {}) out of bounds for {}x{} matrix",
row,
col,
self.nrows,
self.ncols
);
&self.data[row + col * self.nrows]
}
}
impl std::ops::IndexMut<(usize, usize)> for FdMatrix {
#[inline]
fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut f64 {
debug_assert!(
row < self.nrows && col < self.ncols,
"FdMatrix index ({}, {}) out of bounds for {}x{} matrix",
row,
col,
self.nrows,
self.ncols
);
&mut self.data[row + col * self.nrows]
}
}
impl std::fmt::Display for FdMatrix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FdMatrix({}x{})", self.nrows, self.ncols)
}
}
/// A set of multidimensional functional curves in R^d.
///
/// Each dimension is stored as a separate [`FdMatrix`] (n curves × m points).
/// For d=1 this is equivalent to a single `FdMatrix`.
#[derive(Debug, Clone, PartialEq)]
pub struct FdCurveSet {
/// One matrix per coordinate dimension, each n × m.
pub dims: Vec<FdMatrix>,
}
impl FdCurveSet {
/// Number of coordinate dimensions (d).
pub fn ndim(&self) -> usize {
self.dims.len()
}
/// Number of curves (n).
pub fn ncurves(&self) -> usize {
if self.dims.is_empty() {
0
} else {
self.dims[0].nrows()
}
}
/// Number of evaluation points (m).
pub fn npoints(&self) -> usize {
if self.dims.is_empty() {
0
} else {
self.dims[0].ncols()
}
}
/// Wrap a single 1D `FdMatrix` as a `FdCurveSet`.
pub fn from_1d(data: FdMatrix) -> Self {
Self { dims: vec![data] }
}
/// Build from multiple dimension matrices.
///
/// Returns `Err` if `dims` is empty or if dimensions are inconsistent.
pub fn from_dims(dims: Vec<FdMatrix>) -> Result<Self, crate::FdarError> {
if dims.is_empty() {
return Err(crate::FdarError::InvalidDimension {
parameter: "dims",
expected: "non-empty".to_string(),
actual: "empty".to_string(),
});
}
let (n, m) = dims[0].shape();
if dims.iter().any(|d| d.shape() != (n, m)) {
return Err(crate::FdarError::InvalidDimension {
parameter: "dims",
expected: format!("all ({n}, {m})"),
actual: "inconsistent shapes".to_string(),
});
}
Ok(Self { dims })
}
/// Extract the R^d point for a given curve and time index.
pub fn point(&self, curve: usize, time_idx: usize) -> Vec<f64> {
self.dims.iter().map(|d| d[(curve, time_idx)]).collect()
}
}
impl std::fmt::Display for FdCurveSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"FdCurveSet(d={}, n={}, m={})",
self.ndim(),
self.ncurves(),
self.npoints()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_3x4() -> FdMatrix {
// 3 rows, 4 columns, column-major
let data = vec![
1.0, 2.0, 3.0, // col 0
4.0, 5.0, 6.0, // col 1
7.0, 8.0, 9.0, // col 2
10.0, 11.0, 12.0, // col 3
];
FdMatrix::from_column_major(data, 3, 4).unwrap()
}
#[test]
fn test_from_column_major_valid() {
let mat = sample_3x4();
assert_eq!(mat.nrows(), 3);
assert_eq!(mat.ncols(), 4);
assert_eq!(mat.shape(), (3, 4));
assert_eq!(mat.len(), 12);
assert!(!mat.is_empty());
}
#[test]
fn test_from_column_major_invalid() {
assert!(FdMatrix::from_column_major(vec![1.0, 2.0], 3, 4).is_err());
}
#[test]
fn test_from_slice() {
let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let mat = FdMatrix::from_slice(&data, 2, 3).unwrap();
assert_eq!(mat[(0, 0)], 1.0);
assert_eq!(mat[(1, 0)], 2.0);
assert_eq!(mat[(0, 1)], 3.0);
}
#[test]
fn test_from_slice_invalid() {
assert!(FdMatrix::from_slice(&[1.0, 2.0], 3, 3).is_err());
}
#[test]
fn test_zeros() {
let mat = FdMatrix::zeros(2, 3);
assert_eq!(mat.nrows(), 2);
assert_eq!(mat.ncols(), 3);
for j in 0..3 {
for i in 0..2 {
assert_eq!(mat[(i, j)], 0.0);
}
}
}
#[test]
fn test_index() {
let mat = sample_3x4();
assert_eq!(mat[(0, 0)], 1.0);
assert_eq!(mat[(1, 0)], 2.0);
assert_eq!(mat[(2, 0)], 3.0);
assert_eq!(mat[(0, 1)], 4.0);
assert_eq!(mat[(1, 1)], 5.0);
assert_eq!(mat[(2, 3)], 12.0);
}
#[test]
fn test_index_mut() {
let mut mat = sample_3x4();
mat[(1, 2)] = 99.0;
assert_eq!(mat[(1, 2)], 99.0);
}
#[test]
fn test_column() {
let mat = sample_3x4();
assert_eq!(mat.column(0), &[1.0, 2.0, 3.0]);
assert_eq!(mat.column(1), &[4.0, 5.0, 6.0]);
assert_eq!(mat.column(3), &[10.0, 11.0, 12.0]);
}
#[test]
fn test_column_mut() {
let mut mat = sample_3x4();
mat.column_mut(1)[0] = 99.0;
assert_eq!(mat[(0, 1)], 99.0);
}
#[test]
fn test_row() {
let mat = sample_3x4();
assert_eq!(mat.row(0), vec![1.0, 4.0, 7.0, 10.0]);
assert_eq!(mat.row(1), vec![2.0, 5.0, 8.0, 11.0]);
assert_eq!(mat.row(2), vec![3.0, 6.0, 9.0, 12.0]);
}
#[test]
fn test_rows() {
let mat = sample_3x4();
let rows = mat.rows();
assert_eq!(rows.len(), 3);
assert_eq!(rows[0], vec![1.0, 4.0, 7.0, 10.0]);
assert_eq!(rows[2], vec![3.0, 6.0, 9.0, 12.0]);
}
#[test]
fn test_as_slice() {
let mat = sample_3x4();
let expected = vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
];
assert_eq!(mat.as_slice(), expected.as_slice());
}
#[test]
fn test_into_vec() {
let mat = sample_3x4();
let v = mat.into_vec();
assert_eq!(v.len(), 12);
assert_eq!(v[0], 1.0);
}
#[test]
fn test_get_bounds_check() {
let mat = sample_3x4();
assert_eq!(mat.get(0, 0), Some(1.0));
assert_eq!(mat.get(2, 3), Some(12.0));
assert_eq!(mat.get(3, 0), None); // row out of bounds
assert_eq!(mat.get(0, 4), None); // col out of bounds
}
#[test]
fn test_set_bounds_check() {
let mut mat = sample_3x4();
assert!(mat.set(1, 1, 99.0));
assert_eq!(mat[(1, 1)], 99.0);
assert!(!mat.set(5, 0, 99.0)); // out of bounds
}
#[test]
fn test_nalgebra_roundtrip() {
let mat = sample_3x4();
let dmat = mat.to_dmatrix();
assert_eq!(dmat.nrows(), 3);
assert_eq!(dmat.ncols(), 4);
assert_eq!(dmat[(0, 0)], 1.0);
assert_eq!(dmat[(1, 2)], 8.0);
let back = FdMatrix::from_dmatrix(&dmat);
assert_eq!(mat, back);
}
#[test]
fn test_empty() {
let mat = FdMatrix::zeros(0, 0);
assert!(mat.is_empty());
assert_eq!(mat.len(), 0);
}
#[test]
fn test_single_element() {
let mat = FdMatrix::from_column_major(vec![42.0], 1, 1).unwrap();
assert_eq!(mat[(0, 0)], 42.0);
assert_eq!(mat.column(0), &[42.0]);
assert_eq!(mat.row(0), vec![42.0]);
}
#[test]
fn test_display() {
let mat = sample_3x4();
assert_eq!(format!("{}", mat), "FdMatrix(3x4)");
}
#[test]
fn test_clone() {
let mat = sample_3x4();
let cloned = mat.clone();
assert_eq!(mat, cloned);
}
#[test]
fn test_as_mut_slice() {
let mut mat = FdMatrix::zeros(2, 2);
let s = mat.as_mut_slice();
s[0] = 1.0;
s[1] = 2.0;
s[2] = 3.0;
s[3] = 4.0;
assert_eq!(mat[(0, 0)], 1.0);
assert_eq!(mat[(1, 0)], 2.0);
assert_eq!(mat[(0, 1)], 3.0);
assert_eq!(mat[(1, 1)], 4.0);
}
#[test]
fn test_fd_curve_set_empty() {
assert!(FdCurveSet::from_dims(vec![]).is_err());
let cs = FdCurveSet::from_dims(vec![]).unwrap_or(FdCurveSet { dims: vec![] });
assert_eq!(cs.ndim(), 0);
assert_eq!(cs.ncurves(), 0);
assert_eq!(cs.npoints(), 0);
assert_eq!(format!("{}", cs), "FdCurveSet(d=0, n=0, m=0)");
}
#[test]
fn test_fd_curve_set_from_1d() {
let mat = sample_3x4();
let cs = FdCurveSet::from_1d(mat.clone());
assert_eq!(cs.ndim(), 1);
assert_eq!(cs.ncurves(), 3);
assert_eq!(cs.npoints(), 4);
assert_eq!(cs.point(0, 0), vec![1.0]);
assert_eq!(cs.point(1, 2), vec![8.0]);
}
#[test]
fn test_fd_curve_set_from_dims_consistent() {
let m1 = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0, 4.0], 2, 2).unwrap();
let m2 = FdMatrix::from_column_major(vec![5.0, 6.0, 7.0, 8.0], 2, 2).unwrap();
let cs = FdCurveSet::from_dims(vec![m1, m2]).unwrap();
assert_eq!(cs.ndim(), 2);
assert_eq!(cs.point(0, 0), vec![1.0, 5.0]);
assert_eq!(cs.point(1, 1), vec![4.0, 8.0]);
assert_eq!(format!("{}", cs), "FdCurveSet(d=2, n=2, m=2)");
}
#[test]
fn test_fd_curve_set_from_dims_inconsistent() {
let m1 = FdMatrix::from_column_major(vec![1.0, 2.0], 2, 1).unwrap();
let m2 = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0], 3, 1).unwrap();
assert!(FdCurveSet::from_dims(vec![m1, m2]).is_err());
}
#[test]
fn test_to_row_major() {
let mat = sample_3x4();
let rm = mat.to_row_major();
// Row 0: [1,4,7,10], Row 1: [2,5,8,11], Row 2: [3,6,9,12]
assert_eq!(
rm,
vec![1.0, 4.0, 7.0, 10.0, 2.0, 5.0, 8.0, 11.0, 3.0, 6.0, 9.0, 12.0]
);
}
#[test]
fn test_row_to_buf() {
let mat = sample_3x4();
let mut buf = vec![0.0; 4];
mat.row_to_buf(0, &mut buf);
assert_eq!(buf, vec![1.0, 4.0, 7.0, 10.0]);
mat.row_to_buf(1, &mut buf);
assert_eq!(buf, vec![2.0, 5.0, 8.0, 11.0]);
mat.row_to_buf(2, &mut buf);
assert_eq!(buf, vec![3.0, 6.0, 9.0, 12.0]);
}
#[test]
fn test_row_to_buf_larger_buffer() {
let mat = sample_3x4();
let mut buf = vec![99.0; 6]; // bigger than ncols
mat.row_to_buf(0, &mut buf);
assert_eq!(&buf[..4], &[1.0, 4.0, 7.0, 10.0]);
// Remaining elements unchanged
assert_eq!(buf[4], 99.0);
}
#[test]
fn test_row_dot_same_matrix() {
let mat = sample_3x4();
// row0 = [1, 4, 7, 10], row1 = [2, 5, 8, 11]
// dot = 1*2 + 4*5 + 7*8 + 10*11 = 2 + 20 + 56 + 110 = 188
assert_eq!(mat.row_dot(0, &mat, 1), 188.0);
// self dot: row0 . row0 = 1+16+49+100 = 166
assert_eq!(mat.row_dot(0, &mat, 0), 166.0);
}
#[test]
fn test_row_dot_different_matrices() {
let mat1 = sample_3x4();
let data2 = vec![
10.0, 20.0, 30.0, // col 0
40.0, 50.0, 60.0, // col 1
70.0, 80.0, 90.0, // col 2
100.0, 110.0, 120.0, // col 3
];
let mat2 = FdMatrix::from_column_major(data2, 3, 4).unwrap();
// mat1 row0 = [1, 4, 7, 10], mat2 row0 = [10, 40, 70, 100]
// dot = 10 + 160 + 490 + 1000 = 1660
assert_eq!(mat1.row_dot(0, &mat2, 0), 1660.0);
}
#[test]
fn test_row_l2_sq_identical() {
let mat = sample_3x4();
assert_eq!(mat.row_l2_sq(0, &mat, 0), 0.0);
assert_eq!(mat.row_l2_sq(1, &mat, 1), 0.0);
}
#[test]
fn test_row_l2_sq_different() {
let mat = sample_3x4();
// row0 = [1,4,7,10], row1 = [2,5,8,11]
// diff = [-1,-1,-1,-1], sq sum = 4
assert_eq!(mat.row_l2_sq(0, &mat, 1), 4.0);
}
#[test]
fn test_row_l2_sq_cross_matrix() {
let mat1 = FdMatrix::from_column_major(vec![0.0, 0.0], 1, 2).unwrap();
let mat2 = FdMatrix::from_column_major(vec![3.0, 4.0], 1, 2).unwrap();
// row0 = [0, 0], row0 = [3, 4], sq dist = 9 + 16 = 25
assert_eq!(mat1.row_l2_sq(0, &mat2, 0), 25.0);
}
#[test]
fn test_column_major_layout_matches_manual() {
// Verify that FdMatrix[(i, j)] == data[i + j * n] for all i, j
let n = 5;
let m = 7;
let data: Vec<f64> = (0..n * m).map(|x| x as f64).collect();
let mat = FdMatrix::from_column_major(data.clone(), n, m).unwrap();
for j in 0..m {
for i in 0..n {
assert_eq!(mat[(i, j)], data[i + j * n]);
}
}
}
#[test]
fn test_iter_rows() {
let mat = sample_3x4();
let rows: Vec<Vec<f64>> = mat.iter_rows().collect();
assert_eq!(rows.len(), 3);
assert_eq!(rows[0], vec![1.0, 4.0, 7.0, 10.0]);
assert_eq!(rows[1], vec![2.0, 5.0, 8.0, 11.0]);
assert_eq!(rows[2], vec![3.0, 6.0, 9.0, 12.0]);
}
#[test]
fn test_iter_rows_matches_rows() {
let mat = sample_3x4();
let from_iter: Vec<Vec<f64>> = mat.iter_rows().collect();
let from_rows = mat.rows();
assert_eq!(from_iter, from_rows);
}
#[test]
fn test_iter_rows_partial() {
// Verify that taking only a subset avoids full materialization
let mat = sample_3x4();
let first_two: Vec<Vec<f64>> = mat.iter_rows().take(2).collect();
assert_eq!(first_two.len(), 2);
assert_eq!(first_two[0], vec![1.0, 4.0, 7.0, 10.0]);
assert_eq!(first_two[1], vec![2.0, 5.0, 8.0, 11.0]);
}
#[test]
fn test_iter_rows_empty() {
let mat = FdMatrix::zeros(0, 0);
let rows: Vec<Vec<f64>> = mat.iter_rows().collect();
assert!(rows.is_empty());
}
#[test]
fn test_iter_rows_single_row() {
let mat = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0], 1, 3).unwrap();
let rows: Vec<Vec<f64>> = mat.iter_rows().collect();
assert_eq!(rows, vec![vec![1.0, 2.0, 3.0]]);
}
#[test]
fn test_iter_rows_single_column() {
let mat = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0], 3, 1).unwrap();
let rows: Vec<Vec<f64>> = mat.iter_rows().collect();
assert_eq!(rows, vec![vec![1.0], vec![2.0], vec![3.0]]);
}
#[test]
fn test_iter_columns() {
let mat = sample_3x4();
let cols: Vec<&[f64]> = mat.iter_columns().collect();
assert_eq!(cols.len(), 4);
assert_eq!(cols[0], &[1.0, 2.0, 3.0]);
assert_eq!(cols[1], &[4.0, 5.0, 6.0]);
assert_eq!(cols[2], &[7.0, 8.0, 9.0]);
assert_eq!(cols[3], &[10.0, 11.0, 12.0]);
}
#[test]
fn test_iter_columns_partial() {
let mat = sample_3x4();
let first_two: Vec<&[f64]> = mat.iter_columns().take(2).collect();
assert_eq!(first_two.len(), 2);
assert_eq!(first_two[0], &[1.0, 2.0, 3.0]);
assert_eq!(first_two[1], &[4.0, 5.0, 6.0]);
}
#[test]
fn test_iter_columns_empty() {
let mat = FdMatrix::zeros(0, 0);
let cols: Vec<&[f64]> = mat.iter_columns().collect();
assert!(cols.is_empty());
}
#[test]
fn test_iter_columns_single_column() {
let mat = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0], 3, 1).unwrap();
let cols: Vec<&[f64]> = mat.iter_columns().collect();
assert_eq!(cols, vec![&[1.0, 2.0, 3.0]]);
}
#[test]
fn test_iter_columns_single_row() {
let mat = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0], 1, 3).unwrap();
let cols: Vec<&[f64]> = mat.iter_columns().collect();
assert_eq!(cols, vec![&[1.0_f64] as &[f64], &[2.0], &[3.0]]);
}
#[test]
fn test_iter_rows_enumerate() {
let mat = sample_3x4();
for (i, row) in mat.iter_rows().enumerate() {
assert_eq!(row, mat.row(i));
}
}
#[test]
fn test_iter_columns_enumerate() {
let mat = sample_3x4();
for (j, col) in mat.iter_columns().enumerate() {
assert_eq!(col, mat.column(j));
}
}
}