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