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
// Copyright 2025 Jan Niklas Siemer
//
// 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 to compute several norms
//! defined on matrices.
use super::MatQ;
use crate::{
rational::Q,
traits::{MatrixDimensions, MatrixGetEntry, MatrixGetSubmatrix},
};
impl MatQ {
/// Outputs the squared l_{2, ∞}-norm, i.e. it computes the squared Euclidean
/// norm of each column of the matrix and returns the largest one.
///
/// # Examples
/// ```
/// use qfall_math::rational::{MatQ, Q};
/// use std::str::FromStr;
///
/// let mat = MatQ::from_str("[[2, 3],[-2/1, 0]]").unwrap();
///
/// let eucl_norm = mat.norm_l_2_infty_sqrd();
///
/// // 3^2 + 0^2 = 9
/// assert_eq!(Q::from(9), eucl_norm);
/// ```
pub fn norm_l_2_infty_sqrd(&self) -> Q {
let mut max_sqrd_norm = Q::ZERO;
for i in 0..self.get_num_columns() {
let column = unsafe { self.get_column_unchecked(i) };
let sqrd_norm = column.norm_eucl_sqrd().unwrap();
if sqrd_norm > max_sqrd_norm {
max_sqrd_norm = sqrd_norm;
}
}
max_sqrd_norm
}
/// Outputs the l_{2, ∞}-norm, i.e. it computes the Euclidean
/// norm of each column of the matrix and returns the largest one.
///
/// # Examples
/// ```
/// use qfall_math::rational::{Q, MatQ};
/// use std::str::FromStr;
///
/// let mat = MatQ::from_str("[[4/2, 3],[2, 0]]").unwrap();
///
/// let eucl_norm = mat.norm_l_2_infty();
///
/// // sqrt(3^2 + 0^2) = 3
/// assert_eq!(Q::from(3), eucl_norm);
/// ```
pub fn norm_l_2_infty(&self) -> Q {
self.norm_l_2_infty_sqrd().sqrt()
}
/// Outputs the l_{∞, ∞}-norm, i.e. it computes the ∞-norm
/// of each column of the matrix and returns the largest one.
///
/// # Examples
/// ```
/// use qfall_math::rational::{MatQ, Q};
/// use std::str::FromStr;
///
/// let mat = MatQ::from_str("[[2, 6/2],[2, 0]]").unwrap();
///
/// let eucl_norm = mat.norm_l_infty_infty();
///
/// // max{2, 3} = 3
/// assert_eq!(Q::from(3), eucl_norm);
/// ```
pub fn norm_l_infty_infty(&self) -> Q {
let mut max_norm = Q::ZERO;
for i in 0..self.get_num_columns() {
let column = unsafe { self.get_column_unchecked(i) };
let norm = column.norm_infty().unwrap();
if norm > max_norm {
max_norm = norm;
}
}
max_norm
}
/// Outputs the hamming weight of `self`, i.e. it returns the number of
/// non-zero entries in the matrix.
///
/// # Examples
/// ```
/// use qfall_math::rational::MatQ;
/// use std::str::FromStr;
///
/// let mat = MatQ::from_str("[[2, 3/2],[2/3, 0]]").unwrap();
///
/// let hamming_weight = mat.hamming_weight();
///
/// assert_eq!(3, hamming_weight);
/// ```
pub fn hamming_weight(&self) -> i64 {
let mut hamming_weight = 0;
for row in 0..self.get_num_rows() {
for col in 0..self.get_num_columns() {
let entry = unsafe { self.get_entry_unchecked(row, col) };
if !entry.is_zero() {
hamming_weight += 1;
}
}
}
hamming_weight
}
}
#[cfg(test)]
mod test_matrix_norms {
use super::{MatQ, Q};
use std::str::FromStr;
/// Ensures that the squared l_{2, ∞}-norm is correctly computed.
#[test]
fn norm_sqrd_l_2_infty() {
let mat = MatQ::from_str("[[3, -2/1, 5],[-10/2, -6, 2],[4/-1, 0/1, 0],[2, 0, 1]]").unwrap();
let sqrd_norm = mat.norm_l_2_infty_sqrd();
assert_eq!(Q::from(54), sqrd_norm);
}
/// Ensures that the l_{2, ∞}-norm is correctly computed.
#[test]
fn norm_l_2_infty() {
let mat = MatQ::from_str("[[-2, -2/1],[-2, 3/-1],[-2, 0],[2, 0]]").unwrap();
let norm = mat.norm_l_2_infty();
assert_eq!(Q::from(4), norm);
}
/// Ensures that the l_{∞, ∞}-norm is correctly computed.
#[test]
fn norm_l_infty_infty() {
let mat = MatQ::from_str("[[-4/2, 3/1],[2, -5],[-2, 0]]").unwrap();
let infty_norm = mat.norm_l_infty_infty();
assert_eq!(Q::from(5), infty_norm);
}
}
#[cfg(test)]
mod test_hamming_weight {
use super::MatQ;
use std::str::FromStr;
/// Ensures that the hamming weight is computed correctly.
#[test]
fn hamming_weight() {
let mat0 = MatQ::new(10, 8);
let mat1 = MatQ::from_str("[[-2/5, 3/4],[2, -5/2],[-2, 0]]").unwrap();
let hw0 = mat0.hamming_weight();
let hw1 = mat1.hamming_weight();
assert_eq!(0, hw0);
assert_eq!(5, hw1);
}
}