From a468dfe1e04a2278130c709a057799ec0755b70b Mon Sep 17 00:00:00 2001 From: Marcel Schmidt Date: Wed, 19 Mar 2025 18:16:55 +0100 Subject: [PATCH] Implemented the Debug trait with meaningful output --- src/integer/mat_poly_over_z.rs | 8 +++++++- src/integer/mat_z.rs | 8 +++++++- src/integer/poly_over_z.rs | 8 +++++++- src/integer/z.rs | 8 +++++++- src/integer_mod_q/mat_polynomial_ring_zq.rs | 9 ++++++++- src/integer_mod_q/mat_zq.rs | 8 +++++++- src/integer_mod_q/modulus.rs | 9 +++++++-- src/integer_mod_q/modulus_polynomial_ring_zq.rs | 9 +++++++-- src/integer_mod_q/poly_over_zq.rs | 8 +++++++- src/integer_mod_q/polynomial_ring_zq.rs | 9 ++++++++- src/integer_mod_q/z_q.rs | 9 ++++++++- src/rational/mat_q.rs | 8 +++++++- src/rational/poly_over_q.rs | 8 +++++++- src/rational/q.rs | 8 +++++++- 14 files changed, 101 insertions(+), 16 deletions(-) diff --git a/src/integer/mat_poly_over_z.rs b/src/integer/mat_poly_over_z.rs index 30034ff7b..aa4377e3b 100644 --- a/src/integer/mat_poly_over_z.rs +++ b/src/integer/mat_poly_over_z.rs @@ -10,6 +10,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fmpz_poly_mat::fmpz_poly_mat_struct; +use std::fmt; mod arithmetic; mod cmp; @@ -76,7 +77,12 @@ mod vector; /// assert!(row_vec.is_row_vector()); /// assert!(col_vec.is_column_vector()); /// ``` -#[derive(Debug)] pub struct MatPolyOverZ { pub(crate) matrix: fmpz_poly_mat_struct, } + +impl fmt::Debug for MatPolyOverZ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer/mat_z.rs b/src/integer/mat_z.rs index 7c4c7258d..9ea7fc45f 100644 --- a/src/integer/mat_z.rs +++ b/src/integer/mat_z.rs @@ -10,6 +10,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fmpz_mat::fmpz_mat_struct; +use std::fmt; mod arithmetic; mod basis_reductions; @@ -89,7 +90,12 @@ mod vector; /// assert_eq!(col_vec.norm_eucl_sqrd().unwrap(), Z::from(2)); /// assert_eq!(row_vec.norm_infty().unwrap(), Z::ONE); /// ``` -#[derive(Debug)] pub struct MatZ { pub(crate) matrix: fmpz_mat_struct, } + +impl fmt::Debug for MatZ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer/poly_over_z.rs b/src/integer/poly_over_z.rs index c99154acb..3a711e113 100644 --- a/src/integer/poly_over_z.rs +++ b/src/integer/poly_over_z.rs @@ -11,6 +11,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fmpz_poly::fmpz_poly_struct; +use std::fmt; mod arithmetic; mod cmp; @@ -58,7 +59,12 @@ mod unsafe_functions; /// // comparison /// assert_ne!(poly_1, poly_2); /// ``` -#[derive(Debug)] pub struct PolyOverZ { pub(crate) poly: fmpz_poly_struct, } + +impl fmt::Debug for PolyOverZ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer/z.rs b/src/integer/z.rs index 3f677d9b1..761a65226 100644 --- a/src/integer/z.rs +++ b/src/integer/z.rs @@ -10,6 +10,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fmpz::fmpz; +use std::fmt; mod arithmetic; mod cmp; @@ -67,7 +68,12 @@ mod unsafe_functions; /// ); /// # Ok::<(), qfall_math::error::MathError>(()) /// ``` -#[derive(Debug)] pub struct Z { pub(crate) value: fmpz, } + +impl fmt::Debug for Z { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/mat_polynomial_ring_zq.rs b/src/integer_mod_q/mat_polynomial_ring_zq.rs index 2a23836d5..52c0ce46f 100644 --- a/src/integer_mod_q/mat_polynomial_ring_zq.rs +++ b/src/integer_mod_q/mat_polynomial_ring_zq.rs @@ -13,6 +13,7 @@ use super::ModulusPolynomialRingZq; use crate::integer::MatPolyOverZ; use derive_more::Display; use serde::{Deserialize, Serialize}; +use std::fmt; mod arithmetic; mod coefficient_embedding; @@ -87,9 +88,15 @@ mod vector; /// assert!(row_vec.is_row_vector()); /// assert!(col_vec.is_column_vector()); /// ``` -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Display, Clone)] +#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone)] #[display("{matrix} / {modulus}")] pub struct MatPolynomialRingZq { pub(crate) matrix: MatPolyOverZ, pub(crate) modulus: ModulusPolynomialRingZq, } + +impl fmt::Debug for MatPolynomialRingZq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/mat_zq.rs b/src/integer_mod_q/mat_zq.rs index 19eaaa51f..ddf591a0d 100644 --- a/src/integer_mod_q/mat_zq.rs +++ b/src/integer_mod_q/mat_zq.rs @@ -15,6 +15,7 @@ use crate::integer_mod_q::Modulus; use flint_sys::fmpz_mod_mat::fmpz_mod_mat_struct; +use std::fmt; mod arithmetic; mod cmp; @@ -82,7 +83,6 @@ mod vector; /// assert!(row_vec.is_row_vector()); /// assert!(col_vec.is_column_vector()); /// ``` -#[derive(Debug)] pub struct MatZq { pub(crate) matrix: fmpz_mod_mat_struct, @@ -94,3 +94,9 @@ pub struct MatZq { // attribute and `modulus` attribute, if they are both initalized from the same value. modulus: Modulus, } + +impl fmt::Debug for MatZq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/modulus.rs b/src/integer_mod_q/modulus.rs index 3e9b76993..1addf36ba 100644 --- a/src/integer_mod_q/modulus.rs +++ b/src/integer_mod_q/modulus.rs @@ -15,7 +15,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fmpz_mod::fmpz_mod_ctx; -use std::rc::Rc; +use std::{fmt, rc::Rc}; mod cmp; mod distance; @@ -63,7 +63,12 @@ mod unsafe_functions; /// // comparison /// assert_eq!(a, clone); /// ``` -#[derive(Debug)] pub struct Modulus { pub(crate) modulus: Rc, } + +impl fmt::Debug for Modulus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/modulus_polynomial_ring_zq.rs b/src/integer_mod_q/modulus_polynomial_ring_zq.rs index 1b592efd8..8d8919ee0 100644 --- a/src/integer_mod_q/modulus_polynomial_ring_zq.rs +++ b/src/integer_mod_q/modulus_polynomial_ring_zq.rs @@ -11,7 +11,7 @@ //! This implementation uses the [FLINT](https://flintlib.org/) library. use flint_sys::fq::fq_ctx_struct; -use std::rc::Rc; +use std::{fmt, rc::Rc}; mod cmp; mod coefficient_embedding; @@ -40,7 +40,12 @@ mod unsafe_functions; /// let poly_mod = PolyOverZq::from_str("3 1 0 1 mod 17").unwrap(); /// let modulus = ModulusPolynomialRingZq::from(poly_mod); /// ``` -#[derive(Debug)] pub struct ModulusPolynomialRingZq { modulus: Rc, } + +impl fmt::Debug for ModulusPolynomialRingZq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/poly_over_zq.rs b/src/integer_mod_q/poly_over_zq.rs index a50fe0fe0..b80b74218 100644 --- a/src/integer_mod_q/poly_over_zq.rs +++ b/src/integer_mod_q/poly_over_zq.rs @@ -16,6 +16,7 @@ use super::modulus::Modulus; use flint_sys::fmpz_mod_poly::fmpz_mod_poly_struct; +use std::fmt; mod arithmetic; mod cmp; @@ -61,8 +62,13 @@ mod unsafe_functions; /// // comparison /// assert_eq!(poly_1, poly_2); /// ``` -#[derive(Debug)] pub struct PolyOverZq { pub(crate) poly: fmpz_mod_poly_struct, pub(crate) modulus: Modulus, } + +impl fmt::Debug for PolyOverZq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/polynomial_ring_zq.rs b/src/integer_mod_q/polynomial_ring_zq.rs index 7bab7f4be..610330fe8 100644 --- a/src/integer_mod_q/polynomial_ring_zq.rs +++ b/src/integer_mod_q/polynomial_ring_zq.rs @@ -20,6 +20,7 @@ use super::ModulusPolynomialRingZq; use crate::integer::PolyOverZ; use derive_more::Display; use serde::{Deserialize, Serialize}; +use std::fmt; mod arithmetic; mod coefficient_embedding; @@ -67,9 +68,15 @@ mod unsafe_functions; /// /// # Ok::<(), MathError>(()) /// ``` -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Display, Clone)] +#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone)] #[display("{poly} / {modulus}")] pub struct PolynomialRingZq { pub(crate) poly: PolyOverZ, pub(crate) modulus: ModulusPolynomialRingZq, } + +impl fmt::Debug for PolynomialRingZq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/integer_mod_q/z_q.rs b/src/integer_mod_q/z_q.rs index f97c3731c..a23d44bf4 100644 --- a/src/integer_mod_q/z_q.rs +++ b/src/integer_mod_q/z_q.rs @@ -21,6 +21,7 @@ use super::Modulus; use crate::integer::Z; use serde::{Deserialize, Serialize}; +use std::fmt; mod arithmetic; pub(crate) mod fmpz_mod_helpers; @@ -59,8 +60,14 @@ mod unsafe_functions; /// /// # Ok::<(), MathError>(()) /// ``` -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Zq { pub(crate) value: Z, pub(crate) modulus: Modulus, } + +impl fmt::Debug for Zq { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/rational/mat_q.rs b/src/rational/mat_q.rs index 6a6110790..e40e344a9 100644 --- a/src/rational/mat_q.rs +++ b/src/rational/mat_q.rs @@ -14,6 +14,7 @@ //! values. The end-user should be unable to obtain a non-reduced value. use flint_sys::fmpq_mat::fmpq_mat_struct; +use std::fmt; mod arithmetic; mod cholesky_decomp; @@ -84,7 +85,12 @@ mod vector; /// assert!(row_vec.is_row_vector()); /// assert!(col_vec.is_column_vector()); /// ``` -#[derive(Debug)] pub struct MatQ { pub(crate) matrix: fmpq_mat_struct, } + +impl fmt::Debug for MatQ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/rational/poly_over_q.rs b/src/rational/poly_over_q.rs index b77d8a9da..bfdf27a3f 100644 --- a/src/rational/poly_over_q.rs +++ b/src/rational/poly_over_q.rs @@ -16,6 +16,7 @@ //! non-reduced value. use flint_sys::fmpq_poly::fmpq_poly_struct; +use std::fmt; mod arithmetic; mod cmp; @@ -58,7 +59,12 @@ mod unsafe_functions; /// // comparison /// assert_ne!(poly_1, poly_2); /// ``` -#[derive(Debug)] pub struct PolyOverQ { pub(crate) poly: fmpq_poly_struct, } + +impl fmt::Debug for PolyOverQ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} diff --git a/src/rational/q.rs b/src/rational/q.rs index 6490ccd69..314712b53 100644 --- a/src/rational/q.rs +++ b/src/rational/q.rs @@ -14,6 +14,7 @@ //! values. The end-user should be unable to obtain a non-reduced value. use flint_sys::fmpq::fmpq; +use std::fmt; mod arithmetic; mod cmp; @@ -71,7 +72,12 @@ mod unsafe_functions; /// assert_ne!(a, b); /// # Ok::<(), qfall_math::error::MathError>(()) /// ``` -#[derive(Debug)] pub struct Q { pub(crate) value: fmpq, } + +impl fmt::Debug for Q { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +}