smart_big_rational/denom.rs
1// Copyright 2026 The SmartBigRational Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use num_bigint::{BigInt, BigUint};
16use num_traits::{One, Pow};
17use std::ops::{Div, DivAssign, Mul, MulAssign};
18
19/// Interface representing the positive denominator of a rational number,
20/// suitable for use in a [`SmartBigRational`](crate::SmartBigRational).
21pub trait Denom:
22 Clone
23 + From<u8>
24 + From<u16>
25 + From<u32>
26 + From<u64>
27 + From<u128>
28 + From<usize>
29 + From<BigUint>
30 + for<'a> From<&'a BigUint>
31 + Into<BigUint>
32 + One
33 + Pow<u32, Output = Self>
34 + Mul<Output = Self>
35 + for<'a> Mul<&'a Self, Output = Self>
36 + MulAssign
37 + for<'a> MulAssign<&'a Self>
38 + Div<Output = Self>
39 + for<'a> Div<&'a Self, Output = Self>
40 + DivAssign
41 + for<'a> DivAssign<&'a Self>
42 + Mul<BigInt, Output = BigInt>
43 + for<'a> Mul<&'a BigInt, Output = BigInt>
44{
45 /// Constant value of 1.
46 const ONE: Self;
47
48 /// Converts this denominator into a big integer.
49 fn into_biguint(self) -> BigUint;
50
51 /// Converts this denominator into a big integer.
52 fn to_biguint(&self) -> BigUint;
53
54 /// Returns the least common multiple of two denominators, adjusting the
55 /// numerators accordingly.
56 fn normalize(lnum: &mut BigInt, rnum: &mut BigInt, ldenom: &Self, rdenom: &Self) -> Self;
57
58 /// Reduces this denominator together with the given numerator so that their
59 /// GCD is one.
60 fn gcd_reduce(&mut self, num: &mut BigInt);
61}
62
63/// Additional trait that references to a [`Denom`] must implement.
64pub trait DenomRef<D: Denom>:
65 Into<BigUint>
66 + Pow<u32, Output = D>
67 + Mul<Self, Output = D>
68 + Mul<D, Output = D>
69 + Div<Self, Output = D>
70 + Div<D, Output = D>
71 + Mul<BigInt, Output = BigInt>
72 + for<'a> Mul<&'a BigInt, Output = BigInt>
73{
74}