qfall_math/integer/poly_over_z/
properties.rs

1// Copyright © 2023 Phil Milewski
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 about properties of [`PolyOverZ`] instances.
10
11use super::PolyOverZ;
12use flint_sys::fmpz_poly::{fmpz_poly_degree, fmpz_poly_is_one};
13
14impl PolyOverZ {
15    /// Checks if a [`PolyOverZ`] is the constant polynomial with coefficient `1`.
16    ///
17    /// Returns `true` if there is only one coefficient, which is `1`.
18    ///
19    /// # Examples
20    /// ```
21    /// use qfall_math::integer::PolyOverZ;
22    /// use std::str::FromStr;
23    ///
24    /// let value = PolyOverZ::from(1);
25    /// assert!(value.is_one());
26    /// ```
27    pub fn is_one(&self) -> bool {
28        1 == unsafe { fmpz_poly_is_one(&self.poly) }
29    }
30
31    /// Checks if every entry of a [`PolyOverZ`] is `0`.
32    ///
33    /// Returns `true` if [`PolyOverZ`] has no coefficients.
34    ///
35    /// # Examples
36    /// ```
37    /// use qfall_math::integer::PolyOverZ;
38    /// use std::str::FromStr;
39    ///
40    /// let value = PolyOverZ::from(0);
41    /// assert!(value.is_zero());
42    /// ```
43    pub fn is_zero(&self) -> bool {
44        -1 == unsafe { fmpz_poly_degree(&self.poly) }
45    }
46}
47
48#[cfg(test)]
49mod test_is_one {
50    use super::PolyOverZ;
51    use std::str::FromStr;
52
53    /// Ensure that is_one returns `true` for the one polynomial.
54    #[test]
55    fn one_detection() {
56        let constant_1 = PolyOverZ::from(1);
57        let constant_2 = PolyOverZ::from_str("3  1 0 0").unwrap();
58
59        assert!(constant_1.is_one());
60        assert!(constant_2.is_one());
61    }
62
63    /// Ensure that is_one returns `false` for other polynomials.
64    #[test]
65    fn one_rejection() {
66        let small = PolyOverZ::from_str("4  1 0 0 1").unwrap();
67        let large = PolyOverZ::from_str(&format!("1  {}", (u128::MAX - 1) / 2 + 2)).unwrap();
68
69        assert!(!small.is_one());
70        assert!(!large.is_one());
71    }
72}
73
74#[cfg(test)]
75mod test_is_zero {
76    use super::PolyOverZ;
77    use std::str::FromStr;
78
79    /// Ensure that is_zero returns `true` for the zero polynomial.
80    #[test]
81    fn zero_detection() {
82        let zero_1 = PolyOverZ::from(0);
83        let zero_2 = PolyOverZ::from_str("3  0 0 0").unwrap();
84
85        assert!(zero_1.is_zero());
86        assert!(zero_2.is_zero());
87    }
88
89    /// Ensure that is_zero returns `false` for non-zero polynomials.
90    #[test]
91    fn zero_rejection() {
92        let small = PolyOverZ::from_str("4  0 0 0 1").unwrap();
93        let large = PolyOverZ::from(u64::MAX);
94
95        assert!(!small.is_zero());
96        assert!(!large.is_zero());
97    }
98}