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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright © 2023 Phil Milewski, Marvin Beckmann
//
// 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 includes functionality about properties of [`MatQ`] instances.
use super::MatQ;
use crate::traits::{MatrixDimensions, MatrixGetEntry};
use flint_sys::fmpq_mat::{fmpq_mat_is_one, fmpq_mat_is_square, fmpq_mat_is_zero};
impl MatQ {
/// Checks if a [`MatQ`] is the identity matrix.
///
/// Returns `true` if every diagonal entry of the upper square matrix is `1`
/// and all other entries are `0`.
///
/// # Examples
/// ```
/// use qfall_math::rational::MatQ;
///
/// let value = MatQ::identity(2, 2);
/// assert!(value.is_identity());
/// ```
///
/// ```
/// use qfall_math::rational::MatQ;
/// use std::str::FromStr;
///
/// let value = MatQ::from_str("[[1, 0],[0, 1],[0, 0]]").unwrap();
/// assert!(value.is_identity());
/// ```
pub fn is_identity(&self) -> bool {
1 == unsafe { fmpq_mat_is_one(&self.matrix) }
}
/// Checks if a [`MatQ`] is a square matrix.
///
/// Returns `true` if the number of rows and columns is identical.
///
/// # Examples
/// ```
/// use qfall_math::rational::MatQ;
/// use std::str::FromStr;
///
/// let value = MatQ::from_str("[[4/7, 0],[5/8, 1/9]]").unwrap();
/// assert!(value.is_square());
/// ```
pub fn is_square(&self) -> bool {
1 == unsafe { fmpq_mat_is_square(&self.matrix) }
}
/// Checks if every entry of a [`MatQ`] is `0`.
///
/// Returns `true` if every entry is `0`.
///
/// # Examples
/// ```
/// use qfall_math::rational::MatQ;
/// use std::str::FromStr;
///
/// let value = MatQ::from_str("[[0, 0],[0, 0]]").unwrap();
/// assert!(value.is_zero());
/// ```
pub fn is_zero(&self) -> bool {
1 == unsafe { fmpq_mat_is_zero(&self.matrix) }
}
/// Checks if a [`MatQ`] is symmetric.
///
/// Returns `true` if we have `a_ij == a_ji` for all i,j.
///
/// # Examples
/// ```
/// use qfall_math::rational::MatQ;
///
/// let value = MatQ::identity(2,2);
/// assert!(value.is_symmetric());
/// ```
pub fn is_symmetric(&self) -> bool {
if !self.is_square() {
return false;
}
for row in 0..self.get_num_rows() {
for column in 0..row {
if unsafe {
self.get_entry_unchecked(row, column) != self.get_entry_unchecked(column, row)
} {
return false;
}
}
}
true
}
}
#[cfg(test)]
mod test_is_identity {
use super::MatQ;
use std::str::FromStr;
/// Ensure that is_identity returns `true` for identity matrices.
#[test]
fn identity_detection() {
let ident = MatQ::identity(2, 2);
assert!(ident.is_identity());
}
/// Ensure that is_identity returns `false` for non-identity matrices.
#[test]
fn identity_rejection() {
let small = MatQ::from_str("[[0, 0],[2/81, 0]]").unwrap();
let large = MatQ::from_str(&format!("[[1, 0],[0, {}]]", (u128::MAX - 1) / 2 + 2)).unwrap();
assert!(!small.is_identity());
assert!(!large.is_identity());
}
}
#[cfg(test)]
mod test_is_zero {
use super::MatQ;
use std::str::FromStr;
/// Ensure that is_zero returns `true` for all zero matrices.
#[test]
fn zero_detection() {
let zero = MatQ::from_str("[[0, 0],[0, 0]]").unwrap();
assert!(zero.is_zero());
}
/// Ensure that is_zero returns `false` for non-zero matrices.
#[test]
fn zero_rejection() {
let small = MatQ::from_str("[[0, 7/8],[2, 0]]").unwrap();
let large = MatQ::from_str(&format!("[[0, 0],[{}, 0]]", (u128::MAX - 1) / 2 + 1)).unwrap();
assert!(!small.is_zero());
assert!(!large.is_zero());
}
}
#[cfg(test)]
mod test_is_square {
use super::MatQ;
use std::str::FromStr;
/// Ensure that is_square returns `true` for square matrices.
#[test]
fn square_detection() {
let mat_2x2 = MatQ::from_str("[[0, 4/9],[0, 0]]").unwrap();
let mat_3x3 = MatQ::from_str("[[0, 6/123, 4/7],[0, 0, 1/213],[4/341, 6/83, 1]]").unwrap();
assert!(mat_2x2.is_square());
assert!(mat_3x3.is_square());
}
/// Ensure that is_square returns `false` for non-square matrices.
#[test]
fn sqaure_rejection() {
let mat_2x3 = MatQ::from_str("[[0, 5/6, 4],[2/7, 0, 1]]").unwrap();
let mat_3x2 = MatQ::from_str("[[9, 0],[127/71, 0],[0, 0]]").unwrap();
assert!(!mat_2x3.is_square());
assert!(!mat_3x2.is_square());
}
}
#[cfg(test)]
mod test_is_symmetric {
use super::MatQ;
use std::str::FromStr;
/// Ensure that is_symmetric returns `false` for non-symmetric matrices.
#[test]
fn symmetric_rejection() {
let mat_2x3 = MatQ::from_str("[[0, 5/6, 4],[2/7, 0, 1]]").unwrap();
let mat_2x2 = MatQ::from_str("[[9, 0],[127/71, 0]]").unwrap();
assert!(!mat_2x3.is_symmetric());
assert!(!mat_2x2.is_symmetric());
}
/// Ensure that is_symmetric returns `true` for symmetric matrices.
#[test]
fn symmetric_detection() {
let mat_2x2 = MatQ::from_str(&format!(
"[[{}, 1/{}],[1/{}, {}]]",
u64::MIN,
u64::MAX,
u64::MAX,
i64::MAX
))
.unwrap();
assert!(mat_2x2.is_symmetric());
}
}