Skip to main content

qfall_math/rational/
poly_over_q.rs

1// Copyright 2023 Marvin Beckmann
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//! [`PolyOverQ`] is a type of polynomial with arbitrarily many coefficients of type
10//! [`Q`](crate::rational::Q).
11//! This implementation uses the [FLINT](https://flintlib.org/) library.
12
13// For **DEVELOPERS**: Many functions assume that the [`PolyOverQ`] instances
14// are reduced. To avoid unnecessary checks and reductions, always return
15// canonical/reduced values. The end-user should be unable to obtain a
16// non-reduced value.
17
18use crate::integer::debug_fmpz;
19use flint3_sys::fmpq_poly_struct;
20use std::fmt;
21
22mod arithmetic;
23mod cmp;
24mod coefficient_embedding;
25mod default;
26mod dot_product;
27mod evaluate;
28mod from;
29mod get;
30mod norm;
31mod ownership;
32mod properties;
33mod rounding;
34mod sample;
35mod serialize;
36mod set;
37mod to_string;
38mod unsafe_functions;
39
40/// [`PolyOverQ`] is a type of polynomial with arbitrarily many coefficients of type
41/// [`Q`](crate::rational::Q).
42///
43// Attributes:
44// - `poly`: holds the content of the polynomial
45//
46/// # Examples
47/// ```
48/// use qfall_math::rational::{PolyOverQ, Q};
49/// use qfall_math::traits::*;
50/// use std::str::FromStr;
51///
52/// // instantiations
53/// let poly_1 = PolyOverQ::from_str("4  0 1/2 2 3/4").unwrap();
54/// let poly_2 = PolyOverQ::default();
55///
56/// // evaluate function
57/// let value = Q::default();
58/// let res = poly_1.evaluate(&value);
59///
60/// // comparison
61/// assert_ne!(poly_1, poly_2);
62/// ```
63pub struct PolyOverQ {
64    pub(crate) poly: fmpq_poly_struct,
65}
66
67impl fmt::Debug for PolyOverQ {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        write!(
70            f,
71            "PolyOverQ {{poly_f64(may be rounded; 5 decimals): {}, poly: {}, \
72            storage: {{ poly: {}}}}}",
73            self.to_string_decimal(5),
74            self,
75            debug_fmpq_poly_struct(&self.poly)
76        )
77    }
78}
79
80fn debug_fmpq_poly_struct(value: &fmpq_poly_struct) -> String {
81    format!(
82        "fmpq_poly_struct {{ coeffs: {:#x}, den: [{}], alloc: {}, length: {} }}",
83        value.coeffs.addr(),
84        debug_fmpz(&value.den[0]),
85        value.alloc,
86        value.length
87    )
88}