bugfix: refactor swap_columns method

This commit is contained in:
Palash Tyagi 2025-04-19 00:43:00 +01:00
parent 5bc37d58e3
commit 9e05ad836a

View File

@ -85,14 +85,20 @@ impl<T> Matrix<T> {
"column index out of bounds" "column index out of bounds"
); );
if c1 == c2 { if c1 == c2 {
return; return; // No-op if indices are the same
} }
// Loop through each row
for r in 0..self.rows { for r in 0..self.rows {
self.data.swap(c1 * self.rows + r, c2 * self.rows + r); // Calculate the flat index for the element in row r, column c1
} let idx1 = c1 * self.rows + r;
} // Calculate the flat index for the element in row r, column c2
let idx2 = c2 * self.rows + r;
// Swap the elements directly in the data vector
self.data.swap(idx1, idx2);
}
}
/// Deletes a column from the matrix. /// Deletes a column from the matrix.
pub fn delete_column(&mut self, col: usize) { pub fn delete_column(&mut self, col: usize) {
assert!(col < self.cols, "column index out of bounds"); assert!(col < self.cols, "column index out of bounds");