qfall_math/integer/mat_z/transpose.rs
1// Copyright © 2023 Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains the implementation of the `transpose` function.
10
11use crate::traits::MatrixDimensions;
12
13use super::MatZ;
14use flint_sys::fmpz_mat::fmpz_mat_transpose;
15
16impl MatZ {
17 /// Returns the transposed form of the given matrix, i.e. rows get transformed to columns
18 /// and vice versa.
19 ///
20 /// # Examples
21 /// ```
22 /// use qfall_math::integer::MatZ;
23 /// use std::str::FromStr;
24 ///
25 /// let mat = MatZ::from_str("[[2, 1],[2, 1],[2, 1]]").unwrap();
26 /// let cmp = MatZ::from_str("[[2, 2, 2],[1, 1, 1]]").unwrap();
27 ///
28 /// assert_eq!(mat.transpose(), cmp);
29 /// ```
30 pub fn transpose(&self) -> Self {
31 let mut out = Self::new(self.get_num_columns(), self.get_num_rows());
32 unsafe { fmpz_mat_transpose(&mut out.matrix, &self.matrix) };
33 out
34 }
35}
36
37#[cfg(test)]
38mod test_transpose {
39 use super::MatZ;
40 use std::str::FromStr;
41
42 /// Checks if a row is correctly converted to a column
43 #[test]
44 fn row_to_column() {
45 let mat = MatZ::from_str("[[1],[2],[3]]").unwrap();
46 let cmp = MatZ::from_str("[[1, 2, 3]]").unwrap();
47
48 assert_eq!(cmp, mat.transpose());
49 }
50
51 /// Checks if a column is correctly converted to a row
52 #[test]
53 fn column_to_row() {
54 let mat = MatZ::from_str("[[1, 2, 3]]").unwrap();
55 let cmp = MatZ::from_str("[[1],[2],[3]]").unwrap();
56
57 assert_eq!(cmp, mat.transpose());
58 }
59
60 /// Checks if large, negative, and zero values are transposed correctly
61 #[test]
62 fn different_entry_values() {
63 let mat = MatZ::from_str(&format!("[[{}, {}, 0]]", i64::MAX, i64::MIN)).unwrap();
64 let cmp = MatZ::from_str(&format!("[[{}],[{}],[0]]", i64::MAX, i64::MIN)).unwrap();
65
66 assert_eq!(cmp, mat.transpose());
67 }
68}