qfall_math/integer_mod_q/mat_polynomial_ring_zq/to_string.rs
1// Copyright © 2024 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 all options to convert a matrix of type
10//! [`MatPolynomialRingZq`] into a [`String`].
11
12use super::MatPolynomialRingZq;
13use crate::macros::for_others::implement_for_owned;
14
15impl From<&MatPolynomialRingZq> for String {
16 /// Converts a [`MatPolynomialRingZq`] into its [`String`] representation.
17 ///
18 /// Parameters:
19 /// - `value`: specifies the matrix that will be represented as a [`String`]
20 ///
21 /// Returns a [`String`] of the form `"[[poly_1, poly_2, poly_3],[poly_4, poly_5, poly_6]] / poly_7 mod q"`.
22 ///
23 /// # Examples
24 /// ```
25 /// use qfall_math::integer_mod_q::MatPolynomialRingZq;
26 /// use std::str::FromStr;
27 /// let matrix = MatPolynomialRingZq::from_str("[[2 2 2, 1 2],[0, 1 3]] / 2 4 4 mod 3").unwrap();
28 ///
29 /// let string: String = matrix.into();
30 /// ```
31 fn from(value: &MatPolynomialRingZq) -> Self {
32 value.to_string()
33 }
34}
35
36implement_for_owned!(MatPolynomialRingZq, String, From);
37
38impl MatPolynomialRingZq {
39 /// Outputs the matrix as a [`String`], where the upper leftmost `nr_printed_rows x nr_printed_columns`
40 /// submatrix is output entirely as well as the corresponding entries in the last column and row of the matrix.
41 ///
42 /// Parameters:
43 /// - `nr_printed_rows`: defines the number of rows of the upper leftmost matrix that are printed entirely
44 /// - `nr_printed_columns`: defines the number of columns of the upper leftmost matrix that are printed entirely
45 ///
46 /// Returns a [`String`] representing the abbreviated matrix.
47 ///
48 /// # Example
49 /// ```
50 /// use qfall_math::integer::MatZ;
51 /// let matrix = MatZ::identity(10, 10);
52 ///
53 /// println!("Matrix: {}", matrix.pretty_string(2, 2));
54 /// // outputs the following:
55 /// // Matrix: [
56 /// // [1, 0, , ..., 0],
57 /// // [0, 1, , ..., 0],
58 /// // [...],
59 /// // [0, 0, , ..., 1]
60 /// // ]
61 /// ```
62 pub fn pretty_string(&self, nr_printed_rows: u64, nr_printed_columns: u64) -> String {
63 let mut result = crate::utils::parse::partial_string(
64 &self.get_representative_least_nonnegative_residue(),
65 nr_printed_rows,
66 nr_printed_columns,
67 );
68 result.push_str(&format!(" mod {}", self.modulus));
69 result
70 }
71}
72
73#[cfg(test)]
74mod test_to_string {
75 use crate::integer_mod_q::MatPolynomialRingZq;
76 use std::str::FromStr;
77
78 // Most tests are omitted, since the [`Display`](std::fmt::Display) trait
79 // is derived and therefor already tested.
80
81 /// Tests whether a roundtrip works correctly.
82 #[test]
83 fn roundtrip() {
84 let cmp =
85 MatPolynomialRingZq::from_str("[[1 2, 1 1, 0],[1 5, 1 6, 1 7]] / 2 1 1 mod 11")
86 .unwrap();
87
88 assert_eq!(
89 "[[1 2, 1 1, 0],[1 5, 1 6, 1 7]] / 2 1 1 mod 11",
90 cmp.to_string()
91 )
92 }
93
94 /// Tests whether the matrix is correctly reduced.
95 #[test]
96 fn reduced() {
97 let cmp =
98 MatPolynomialRingZq::from_str("[[2 2 2, 1 1, 0],[1 5, 1 6, 1 7]] / 2 4 4 mod 3")
99 .unwrap();
100
101 assert_eq!(
102 "[[0, 1 1, 0],[1 2, 0, 1 1]] / 2 1 1 mod 3",
103 cmp.to_string()
104 )
105 }
106}