Skip to main content

AlgebraicNumber

Struct AlgebraicNumber 

Source
pub struct AlgebraicNumber {
    pub min_poly: Polynomial,
    pub interval: (RnsRational, RnsRational),
    pub channels: Channels,
}
Expand description

An exact real algebraic number (Level 2).

Fields§

§min_poly: Polynomial

Minimal polynomial over ℚ (monic, irreducible for the cases we build).

§interval: (RnsRational, RnsRational)

Isolating interval (lo, hi) containing exactly one root of min_poly.

§channels: Channels

Implementations§

Source§

impl AlgebraicNumber

Source

pub fn sqrt(n: u64, channels: Channels) -> Self

The positive square root √n.

Examples found in repository?
examples/float_comparison.rs (line 41)
22fn main() {
23    let ch = Channels::standard(32);
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}
Source

pub fn cbrt(n: u64, channels: Channels) -> Self

The real cube root ∛n.

Source

pub fn from_rational(r: RnsRational) -> Self

A rational as a degree-1 algebraic number.

Source

pub fn from_poly_root( p: Polynomial, root_index: usize, channels: Channels, ) -> Self

The root_index-th real root (ascending) of p.

Source

pub fn degree(&self) -> usize

Degree of the minimal polynomial.

Source

pub fn to_rational(&self) -> Option<RnsRational>

Some(r) iff this number is actually rational (degree-1 min poly).

Source

pub fn refine_interval(&mut self, target_width: &RnsRational)

Refine the isolating interval until its width is < target_width.

Source

pub fn to_f64(&self) -> f64

An f64 approximation (refines internally first).

Source

pub fn sign(&self) -> i32

Exact sign of the number: -1, 0, or +1.

Source

pub fn neg(&self) -> Self

Additive inverse: negate the root (x -> -x in the min poly).

Source

pub fn recip(&self) -> Self

Multiplicative inverse: reciprocal of the root (reverse the coefficients).

Source

pub fn add(&self, other: &Self) -> Self

α + β.

Source

pub fn mul(&self, other: &Self) -> Self

α × β.

Source§

impl AlgebraicNumber

Bridge from Level 2 to Level 3.

Source

pub fn to_computable(&self) -> ComputableReal

Produce a ComputableReal that yields digits on demand.

Trait Implementations§

Source§

impl Clone for AlgebraicNumber

Source§

fn clone(&self) -> AlgebraicNumber

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlgebraicNumber

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,