Skip to main content

qfall_math/utils/
factorization.rs

1// Copyright 2023 Marcel Luca Schmidt
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 contains the type [`Factorization`], which is a factorized
10//! (or partly factorized) representation of integers with arbitrary length.
11
12use flint3_sys::fmpz_factor_struct;
13use std::fmt;
14
15mod default;
16mod from;
17mod ownership;
18mod refine;
19mod to_string;
20
21/// [`Factorization`] represents any integer value as its factorization or
22/// partly factorization.
23///
24/// Attributes:
25/// - `factors`: holds [FLINT](https://flintlib.org/)'s [struct](fmpz_factor_struct)
26///   for a factorization of an integer value
27///
28/// # Implicit Typecasting
29/// Most of our functions take as input values of type Integer.
30/// These capture all types that can be turned into a [`Z`](crate::integer::Z) value.
31/// The types are [`Z`](crate::integer::Z), [`Modulus`](crate::integer_mod_q::Modulus), [`i8`],
32/// [`i16`],[`i32`],[`i64`],[`u8`],[`u16`],[`u32`],[`u64`] and the
33/// references of all of these types. These types are then implicitly casted to a
34/// [`Z`](crate::integer::Z) before the desired action is performed.
35///
36/// # Examples
37/// ```
38/// use qfall_math::utils::Factorization;
39/// use qfall_math::integer::Z;
40/// use std::str::FromStr;
41/// use core::fmt;
42///
43/// // instantiations
44/// let a = Z::from(1200);
45/// let b = Z::from(20);
46///
47/// let fac_1 = Factorization::from(&a);
48/// let mut fac_2 = Factorization::from((&a, &b));
49///
50/// // refinement
51/// fac_2.refine();
52///
53/// // to_string
54/// assert_eq!("[(3, 1), (20, 3)]", &fac_2.to_string());
55/// ```
56pub struct Factorization {
57    factors: fmpz_factor_struct,
58}
59
60impl fmt::Debug for Factorization {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(
63            f,
64            "Factorization {{ factors: {}, storage: {{ fmpz_factor_struct: {}}}}}",
65            self,
66            debug_fmpz_factor_struct(&self.factors)
67        )
68    }
69}
70
71fn debug_fmpz_factor_struct(value: &fmpz_factor_struct) -> String {
72    format!(
73        "fmpz_factor_struct {{ sign: {}, p: {:#x}, exp: {:#x}, alloc: {}, num: {} }}",
74        value.sign,
75        value.p.addr(),
76        value.exp.addr(),
77        value.alloc,
78        value.num
79    )
80}