qfall_math/integer/mat_z/
norm.rs

1// Copyright © 2025 Niklas Siemer
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 includes functionality to compute several norms
10//! defined on matrices.
11
12use super::MatZ;
13use crate::{
14    integer::Z,
15    rational::Q,
16    traits::{MatrixDimensions, MatrixGetSubmatrix},
17};
18
19impl MatZ {
20    /// Outputs the squared l_{2, ∞}-norm, i.e. it computes the squared Euclidean
21    /// norm of each column of the matrix and returns the largest one.
22    ///
23    /// # Examples
24    /// ```
25    /// use qfall_math::integer::{MatZ, Z};
26    /// use std::str::FromStr;
27    ///
28    /// let mat = MatZ::from_str("[[2, 3],[2, 0]]").unwrap();
29    ///
30    /// let eucl_norm = mat.norm_l_2_infty_sqrd();
31    ///
32    /// // 3^2 + 0^2 = 9
33    /// assert_eq!(Z::from(9), eucl_norm);
34    /// ```
35    pub fn norm_l_2_infty_sqrd(&self) -> Z {
36        let mut max_sqrd_norm = Z::ZERO;
37        for i in 0..self.get_num_columns() {
38            let column = unsafe { self.get_column_unchecked(i) };
39            let sqrd_norm = column.norm_eucl_sqrd().unwrap();
40            if sqrd_norm > max_sqrd_norm {
41                max_sqrd_norm = sqrd_norm;
42            }
43        }
44        max_sqrd_norm
45    }
46
47    /// Outputs the l_{2, ∞}-norm, i.e. it computes the Euclidean
48    /// norm of each column of the matrix and returns the largest one.
49    ///
50    /// # Examples
51    /// ```
52    /// use qfall_math::{integer::MatZ, rational::Q};
53    /// use std::str::FromStr;
54    ///
55    /// let mat = MatZ::from_str("[[2, 3],[2, 0]]").unwrap();
56    ///
57    /// let eucl_norm = mat.norm_l_2_infty();
58    ///
59    /// // sqrt(3^2 + 0^2) = 3
60    /// assert_eq!(Q::from(3), eucl_norm);
61    /// ```
62    pub fn norm_l_2_infty(&self) -> Q {
63        self.norm_l_2_infty_sqrd().sqrt()
64    }
65
66    /// Outputs the l_{∞, ∞}-norm, i.e. it computes the ∞-norm
67    /// of each column of the matrix and returns the largest one.
68    ///
69    /// # Examples
70    /// ```
71    /// use qfall_math::integer::{MatZ, Z};
72    /// use std::str::FromStr;
73    ///
74    /// let mat = MatZ::from_str("[[2, 3],[2, 0]]").unwrap();
75    ///
76    /// let eucl_norm = mat.norm_l_infty_infty();
77    ///
78    /// // max{2, 3} = 3
79    /// assert_eq!(Z::from(3), eucl_norm);
80    /// ```
81    pub fn norm_l_infty_infty(&self) -> Z {
82        let mut max_norm = Z::ZERO;
83        for i in 0..self.get_num_columns() {
84            let column = unsafe { self.get_column_unchecked(i) };
85            let norm = column.norm_infty().unwrap();
86            if norm > max_norm {
87                max_norm = norm;
88            }
89        }
90        max_norm
91    }
92}
93
94#[cfg(test)]
95mod test_matrix_norms {
96    use super::{MatZ, Q, Z};
97    use std::str::FromStr;
98
99    /// Ensures that the squared l_{2, ∞}-norm is correctly computed.
100    #[test]
101    fn norm_sqrd_l_2_infty() {
102        let mat = MatZ::from_str("[[3, -2, 5],[-5, -6, 2],[-4, 0, 0],[2, 0, 1]]").unwrap();
103
104        let sqrd_norm = mat.norm_l_2_infty_sqrd();
105
106        assert_eq!(Z::from(54), sqrd_norm);
107    }
108
109    /// Ensures that the l_{2, ∞}-norm is correctly computed.
110    #[test]
111    fn norm_l_2_infty() {
112        let mat = MatZ::from_str("[[-2, -2],[-2, -3],[-2, 0],[2, 0]]").unwrap();
113
114        let norm = mat.norm_l_2_infty();
115
116        assert_eq!(Q::from(4), norm);
117    }
118
119    /// Ensures that the l_{∞, ∞}-norm is correctly computed.
120    #[test]
121    fn norm_l_infty_infty() {
122        let mat = MatZ::from_str("[[-2, 3],[2, -5],[-2, 0]]").unwrap();
123
124        let infty_norm = mat.norm_l_infty_infty();
125
126        assert_eq!(Z::from(5), infty_norm);
127    }
128}