Skip to main content

malachite_nz/integer_polynomial/arithmetic/
floor_l2_norm.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer_polynomial::IntegerPolynomial;
10use crate::natural::Natural;
11use malachite_base::num::arithmetic::traits::FloorSqrt;
12use malachite_base::polynomial::{FloorL2Norm, L2NormSquared};
13
14impl FloorL2Norm for &IntegerPolynomial {
15    type Output = Natural;
16
17    /// Computes the floor of an [`IntegerPolynomial`]'s $L^2$ norm: the floor of the square root of
18    /// the sum of the squares of its coefficients.
19    ///
20    /// $$
21    /// f(p) = \left \lfloor \sqrt{\sum_i p_i^2} \right \rfloor.
22    /// $$
23    ///
24    /// The exact square of the norm is [`l2_norm_squared`](L2NormSquared::l2_norm_squared).
25    ///
26    /// # Worst-case complexity
27    /// $T(n) = O(n \log n \log\log n)$
28    ///
29    /// $M(n) = O(n \log n)$
30    ///
31    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
32    /// coefficients.
33    ///
34    /// # Examples
35    /// ```
36    /// use core::str::FromStr;
37    /// use malachite_base::polynomial::FloorL2Norm;
38    /// use malachite_nz::integer_polynomial::IntegerPolynomial;
39    ///
40    /// assert_eq!(
41    ///     IntegerPolynomial::from_str("x^2-3*x+2")
42    ///         .unwrap()
43    ///         .floor_l2_norm(),
44    ///     3u32
45    /// );
46    /// assert_eq!(
47    ///     IntegerPolynomial::from_str("0").unwrap().floor_l2_norm(),
48    ///     0u32
49    /// );
50    /// assert_eq!(
51    ///     IntegerPolynomial::from_str("-5").unwrap().floor_l2_norm(),
52    ///     5u32
53    /// );
54    /// ```
55    ///
56    /// This is equivalent to `fmpz_poly_2norm` from `fmpz_poly/2norm.c`, FLINT 3.6.0.
57    #[inline]
58    fn floor_l2_norm(self) -> Natural {
59        self.l2_norm_squared().floor_sqrt()
60    }
61}