pub struct AlgebraicNumber {
pub annihilating_poly: Polynomial,
pub interval: (RnsRational, RnsRational),
pub channels: Basis,
}Expand description
An exact real algebraic number (Level 2).
Fields§
§annihilating_poly: PolynomialAn annihilating polynomial over ℚ: monic, with this number as a root.
Resultants produce an annihilating polynomial that is not necessarily
minimal. AlgebraicNumber::try_minimize reduces it toward the minimal
polynomial (square-free + rational-root factorization); without full
factorization over ℚ this is best-effort, not guaranteed.
interval: (RnsRational, RnsRational)Isolating interval (lo, hi) containing exactly one root of annihilating_poly.
channels: BasisImplementations§
Source§impl AlgebraicNumber
impl AlgebraicNumber
Sourcepub fn sqrt(n: u64, channels: Basis) -> Self
pub fn sqrt(n: u64, channels: Basis) -> Self
The positive square root √n.
Examples found in repository?
22fn main() {
23 let ch = Basis::standard();
24 println!("== adele-ring :: exact vs f64 ==\n");
25 println!(
26 "{:<16} | {:<10} | {:<22} | {:<12} | ULPs",
27 "expression", "exact", "f64 result", "abs error"
28 );
29 println!("{}", "-".repeat(78));
30
31 // 0.1 + 0.2 = 3/10
32 let exact = RnsRational::from_fraction(1, 10, ch.clone())
33 .add(&RnsRational::from_fraction(1, 5, ch.clone()));
34 row("0.1 + 0.2", &exact.display(), 0.1 + 0.2, exact.to_f64());
35
36 // 1/3 * 3 = 1
37 let one = RnsRational::from_fraction(1, 3, ch.clone()).mul(&RnsRational::from_int(3, ch.clone()));
38 row("1/3 * 3", &one.display(), (1.0 / 3.0) * 3.0, one.to_f64());
39
40 // sqrt(2) * sqrt(2) = 2 (drops to Integer through the tower)
41 let s2 = TowerValue::Algebraic(AlgebraicNumber::sqrt(2, ch.clone()));
42 let two = s2.mul(&s2);
43 let naive = 2f64.sqrt() * 2f64.sqrt();
44 row("sqrt2 * sqrt2", "2", naive, two.to_f64().unwrap());
45
46 // sin(pi) = 0 (exact identity; f64 gives ~1.2e-16)
47 let sin_pi = TowerValue::Symbolic(SymbolicExpr::Pi).sin();
48 row("sin(pi)", "0", std::f64::consts::PI.sin(), sin_pi.to_f64().unwrap());
49
50 // 1/7 * 7 = 1
51 let one7 = RnsRational::from_fraction(1, 7, ch.clone()).mul(&RnsRational::from_int(7, ch));
52 row("1/7 * 7", &one7.display(), (1.0 / 7.0) * 7.0, one7.to_f64());
53
54 println!("\nEvery `exact` column is bit-for-bit correct; the f64 column drifts.");
55}Sourcepub fn from_rational(r: RnsRational) -> Self
pub fn from_rational(r: RnsRational) -> Self
A rational as a degree-1 algebraic number.
Sourcepub fn from_poly_root(p: Polynomial, root_index: usize, channels: Basis) -> Self
pub fn from_poly_root(p: Polynomial, root_index: usize, channels: Basis) -> Self
The root_index-th real root (ascending) of p.
Sourcepub fn try_minimize(&self) -> AlgebraicNumber
pub fn try_minimize(&self) -> AlgebraicNumber
Reduce the annihilating polynomial toward the minimal polynomial by square-free + rational-root factorization over ℚ, keeping the irreducible factor whose real root matches this number. Best-effort: without full factorization, high-degree irreducible factors are returned whole.
Sourcepub fn to_rational(&self) -> Option<RnsRational>
pub fn to_rational(&self) -> Option<RnsRational>
Some(r) iff this number is actually rational (degree-1 min poly).
Sourcepub fn refine_interval(&mut self, target_width: &RnsRational)
pub fn refine_interval(&mut self, target_width: &RnsRational)
Refine the isolating interval until its width is < target_width.
Source§impl AlgebraicNumber
Bridge from Level 2 to Level 3.
impl AlgebraicNumber
Bridge from Level 2 to Level 3.
Sourcepub fn to_computable(&self) -> ComputableReal
pub fn to_computable(&self) -> ComputableReal
Produce a ComputableReal that yields digits on demand.
Trait Implementations§
Source§impl Clone for AlgebraicNumber
impl Clone for AlgebraicNumber
Source§fn clone(&self) -> AlgebraicNumber
fn clone(&self) -> AlgebraicNumber
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for AlgebraicNumber
impl RefUnwindSafe for AlgebraicNumber
impl Send for AlgebraicNumber
impl Sync for AlgebraicNumber
impl Unpin for AlgebraicNumber
impl UnsafeUnpin for AlgebraicNumber
impl UnwindSafe for AlgebraicNumber
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more