diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 407675a..509cc8b 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -63,6 +63,19 @@ impl Matrix { Matrix { rows, cols, data } } + /// Build from a flat Vec, assuming row-major order. + pub fn from_rows_vec(data: Vec, rows: usize, cols: usize) -> Self { + let mut new_vec = Vec::with_capacity(rows * cols); + + for c in 0..cols { + for r in 0..rows { + new_vec.push(data[r * cols + c].clone()); + } + } + + Matrix::from_vec(new_vec, rows, cols) + } + pub fn data(&self) -> &[T] { &self.data } @@ -978,6 +991,20 @@ mod tests { assert_eq!(m.to_vec(), vec![1.0, 3.0, 2.0, 4.0]); } + #[test] + fn test_from_rows_vec() { + // Representing: + // 1 2 3 + // 4 5 6 + let rows_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; + let matrix = Matrix::from_rows_vec(rows_data, 2, 3); + + let data = vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0]; // Column-major + let expected = Matrix::from_vec(data, 2, 3); + + assert_eq!(matrix, expected); + } + // Helper function to create a basic Matrix for testing fn static_test_matrix() -> Matrix { // Column-major data: