qfall_math/integer/mat_poly_over_z/
transpose.rs

1// Copyright © 2023 Marcel Luca Schmidt
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 super::MatPolyOverZ;
12use crate::traits::MatrixDimensions;
13use flint_sys::fmpz_poly_mat::fmpz_poly_mat_transpose;
14
15impl MatPolyOverZ {
16    /// Returns the transposed form of the given matrix, i.e. rows get transformed to columns
17    /// and vice versa.
18    ///
19    /// # Examples
20    /// ```
21    /// use qfall_math::integer::MatPolyOverZ;
22    /// use std::str::FromStr;
23    ///
24    /// let mat = MatPolyOverZ::from_str("[[0, 1  42],[3  17 24 42, 1  17]]").unwrap();
25    /// let cmp = MatPolyOverZ::from_str("[[0, 3  17 24 42],[1  42, 1  17]]").unwrap();
26    ///
27    /// assert_eq!(mat.transpose(), cmp);
28    /// ```
29    pub fn transpose(&self) -> Self {
30        let mut out = Self::new(self.get_num_columns(), self.get_num_rows());
31        unsafe { fmpz_poly_mat_transpose(&mut out.matrix, &self.matrix) };
32        out
33    }
34}
35
36#[cfg(test)]
37mod test_transpose {
38    use super::MatPolyOverZ;
39    use std::str::FromStr;
40
41    /// Checks if a row is correctly converted to a column
42    #[test]
43    fn row_to_column() {
44        let mat = MatPolyOverZ::from_str("[[1  42],[2  1 2],[1  17]]").unwrap();
45        let cmp = MatPolyOverZ::from_str("[[1  42, 2  1 2, 1  17]]").unwrap();
46
47        assert_eq!(cmp, mat.transpose());
48    }
49
50    /// Checks if a column is correctly converted to a row
51    #[test]
52    fn column_to_row() {
53        let mat = MatPolyOverZ::from_str("[[1  42, 2  1 2, 1  17]]").unwrap();
54        let cmp = MatPolyOverZ::from_str("[[1  42],[2  1 2],[1  17]]").unwrap();
55
56        assert_eq!(cmp, mat.transpose());
57    }
58
59    /// Checks if large, negative, and zero values are transposed correctly
60    #[test]
61    fn different_entry_values() {
62        let mat =
63            MatPolyOverZ::from_str(&format!("[[1  {}, 1  {}, 1  0]]", i64::MAX, i64::MIN)).unwrap();
64        let cmp = MatPolyOverZ::from_str(&format!("[[1  {}],[1  {}],[1  0]]", i64::MAX, i64::MIN))
65            .unwrap();
66
67        assert_eq!(cmp, mat.transpose());
68    }
69}