Skip to content

Commit

Permalink
transposition
Browse files Browse the repository at this point in the history
  • Loading branch information
A-UNDERSCORE-D committed Dec 25, 2024
1 parent 83f9a01 commit ac0f63c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/math/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ impl Matrix {
count: self.width,
}
}

pub fn transpose(&self) -> Matrix {
Self::new_with_data(
self.width,
self.height,
(0..self.height)
.flat_map(|i| self.col(i).iter().copied().collect::<Vec<_>>())
.collect(),
)
}
}

pub static IDENTITY_4X4: LazyLock<Matrix> = LazyLock::new(|| Matrix {
Expand Down Expand Up @@ -385,4 +395,29 @@ mod test {
}
)
}

#[test]
fn transpose_ident() {
assert_eq!(IDENTITY_4X4.transpose(), *IDENTITY_4X4)
}

#[test]
fn transpose() {
let a: Matrix = "\
| 0 | 9 | 3 | 0 |
| 9 | 8 | 0 | 8 |
| 1 | 8 | 5 | 3 |
| 0 | 0 | 5 | 8 |"
.parse()
.unwrap();
let expected: Matrix = "\
| 0 | 9 | 1 | 0 |
| 9 | 8 | 8 | 0 |
| 3 | 0 | 5 | 5 |
| 0 | 8 | 3 | 8 |"
.parse()
.unwrap();

assert_eq!(a.transpose(), expected)
}
}

0 comments on commit ac0f63c

Please sign in to comment.