From 9e05ad836a936d4de65ce4e61ac2aca3e4ad09c9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 19 Apr 2025 00:43:00 +0100 Subject: [PATCH] bugfix: refactor swap_columns method --- src/matrix/mat.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 717ad06..6ae1194 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -85,14 +85,20 @@ impl Matrix { "column index out of bounds" ); if c1 == c2 { - return; + return; // No-op if indices are the same } + // Loop through each row 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. pub fn delete_column(&mut self, col: usize) { assert!(col < self.cols, "column index out of bounds");