mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
Add from_rows_vec method to construct Matrix from a flat Vec in row-major order and include corresponding tests
This commit is contained in:
parent
46cfe43983
commit
6711cad6e2
@ -63,6 +63,19 @@ impl<T: Clone> Matrix<T> {
|
||||
Matrix { rows, cols, data }
|
||||
}
|
||||
|
||||
/// Build from a flat Vec, assuming row-major order.
|
||||
pub fn from_rows_vec(data: Vec<T>, 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<i32> {
|
||||
// Column-major data:
|
||||
|
Loading…
x
Reference in New Issue
Block a user