1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright © 2023 Marcel Luca Schmidt
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! This module contains all options to convert a matrix of type
//! [`MatPolyOverZ`] into a [`String`].
//!
//! This includes the [`Display`](std::fmt::Display) trait.
use super::MatPolyOverZ;
use crate::{macros::for_others::implement_for_owned, utils::parse::matrix_to_string};
use core::fmt;
impl From<&MatPolyOverZ> for String {
/// Converts a [`MatPolyOverZ`] into its [`String`] representation.
///
/// Parameters:
/// - `value`: specifies the matrix that will be represented as a [`String`]
///
/// Returns a [`String`] of the form `"[[row_0],[row_1],...[row_n]]"`.
///
/// # Examples
/// ```
/// use qfall_math::integer::MatPolyOverZ;
/// use std::str::FromStr;
/// let matrix = MatPolyOverZ::from_str("[[1 17, 1 5],[2 1 7, 1 2]]").unwrap();
///
/// let string: String = matrix.into();
/// ```
fn from(value: &MatPolyOverZ) -> Self {
value.to_string()
}
}
implement_for_owned!(MatPolyOverZ, String, From);
impl fmt::Display for MatPolyOverZ {
/// Allows to convert a matrix of type [`MatPolyOverZ`] into a [`String`].
///
/// Returns the Matrix in form of a [`String`]. For matrix
/// `[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]` the String looks
/// like this `[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]`.
///
/// # Examples
/// ```
/// use qfall_math::integer::MatPolyOverZ;
/// use core::fmt;
/// use std::str::FromStr;
///
/// let matrix = MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
/// println!("{matrix}");
/// ```
///
/// ```
/// use qfall_math::integer::MatPolyOverZ;
/// use core::fmt;
/// use std::str::FromStr;
///
/// let matrix = MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
/// let matrix_string = matrix.to_string();
/// ```
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", matrix_to_string(self))
}
}
crate::macros::serialize::matrix_pretty_string!(MatPolyOverZ, PolyOverZ);
#[cfg(test)]
mod test_to_string {
use crate::integer::MatPolyOverZ;
use std::str::FromStr;
/// Tests whether a matrix with a large entry works in a roundtrip
#[test]
fn working_large_positive() {
let cmp = MatPolyOverZ::from_str(&format!(
"[[0, 1 {}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
u64::MAX
))
.unwrap();
assert_eq!(
format!(
"[[0, 1 {}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
u64::MAX
),
cmp.to_string()
)
}
/// Tests whether a matrix with a large negative entry works in a roundtrip
#[test]
fn working_large_negative() {
let cmp = MatPolyOverZ::from_str(&format!(
"[[0, 1 -{}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
u64::MAX
))
.unwrap();
assert_eq!(
format!(
"[[0, 1 -{}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
u64::MAX
),
cmp.to_string()
)
}
/// Tests whether a matrix with positive entries works in a roundtrip
#[test]
fn working_positive() {
let cmp =
MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
assert_eq!(
"[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
cmp.to_string()
)
}
/// Tests whether a matrix with negative entries works in a roundtrip
#[test]
fn working_negative() {
let cmp =
MatPolyOverZ::from_str("[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]").unwrap();
assert_eq!(
"[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]",
cmp.to_string()
)
}
/// Tests whether a large matrix works in a roundtrip
#[test]
fn working_large_dimensions() {
let cmp_1 = MatPolyOverZ::from_str(&format!(
"[{}[3 17 24 42, 1 -17, 1 42]]",
"[0, 1 42, 2 42 24],".repeat(99)
))
.unwrap();
let cmp_2 = MatPolyOverZ::from_str(&format!("[[{}1 42]]", "1 42, ".repeat(99))).unwrap();
assert_eq!(
format!(
"[{}[3 17 24 42, 1 -17, 1 42]]",
"[0, 1 42, 2 42 24],".repeat(99)
),
cmp_1.to_string()
);
assert_eq!(
format!("[[{}1 42]]", "1 42, ".repeat(99)),
cmp_2.to_string()
);
}
/// Tests whether a matrix that is created using a string, returns a
/// string that can be used to create a [`MatZ`]
#[test]
fn working_use_result_of_to_string_as_input() {
let cmp =
MatPolyOverZ::from_str("[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]").unwrap();
let cmp_str_2 = cmp.to_string();
assert!(MatPolyOverZ::from_str(&cmp_str_2).is_ok());
}
/// Ensures that the `Into<String>` trait works properly
#[test]
fn into_works_properly() {
let cmp = "[[1 17, 1 5],[2 1 7, 1 2]]";
let matrix = MatPolyOverZ::from_str(cmp).unwrap();
let string: String = matrix.clone().into();
let borrowed_string: String = (&matrix).into();
assert_eq!(cmp, string);
assert_eq!(cmp, borrowed_string);
}
}