pub struct RationalVector<const D: usize> { /* private fields */ }exact only.Expand description
Exact rational vector with compile-time dimension D.
Construction validates that every denominator is non-zero and canonicalizes
every entry to lowest terms with a positive denominator. Solutions returned
by RationalMatrix::solve and crate::Matrix::solve_exact also use this
type, making any later conversion to Vector explicit through
ExactF64Conversion.
Use try_to_f64 when every component
must remain exact, or to_rounded_f64
to opt into round-to-nearest, ties-to-even. Both conversions reject results
that cannot be rounded to finite binary64 values.
Implementations§
Source§impl<const D: usize> RationalVector<D>
impl<const D: usize> RationalVector<D>
Sourcepub fn try_new(data: [BigRational; D]) -> Result<Self, LaError>
pub fn try_new(data: [BigRational; D]) -> Result<Self, LaError>
Try to create an exact vector from rational storage.
Raw, non-reduced values and negative denominators are accepted and stored canonically. A raw zero denominator is rejected.
§Examples
use la_stack::prelude::*;
let rhs = RationalVector::<2>::try_new([
BigRational::new(1.into(), 2.into()),
BigRational::from_integer(3.into()),
])?;
assert_eq!(rhs.try_to_f64()?.into_array(), [0.5, 3.0]);§Errors
Returns LaError::NonFinite at the first vector entry whose raw
rational denominator is zero.
Sourcepub fn try_from_fn(
make_entry: impl FnMut(usize) -> BigRational,
) -> Result<Self, LaError>
pub fn try_from_fn( make_entry: impl FnMut(usize) -> BigRational, ) -> Result<Self, LaError>
Try to create an exact vector by evaluating a function at every index.
The function is evaluated once per entry in increasing index order.
All entries are generated before validation and canonicalization, as
in try_new.
§Examples
use la_stack::prelude::*;
let numerators = [1, 2, 3];
let rhs = RationalVector::<3>::try_from_fn(|index| {
BigRational::new(numerators[index].into(), 2.into())
})?;
assert_eq!(rhs.try_to_f64()?.into_array(), [0.5, 1.0, 1.5]);§Errors
Returns LaError::NonFinite at the first generated entry whose raw
rational denominator is zero.
Sourcepub const fn as_array(&self) -> &[BigRational; D]
pub const fn as_array(&self) -> &[BigRational; D]
Borrow the exact backing array.
Sourcepub fn into_array(self) -> [BigRational; D]
pub fn into_array(self) -> [BigRational; D]
Consume the vector and return its exact backing array.
Sourcepub fn get(&self, index: usize) -> Option<&BigRational>
pub fn get(&self, index: usize) -> Option<&BigRational>
Borrow one entry, returning None for an out-of-bounds index.
Trait Implementations§
Source§impl<const D: usize> Clone for RationalVector<D>
impl<const D: usize> Clone for RationalVector<D>
Source§fn clone(&self) -> RationalVector<D>
fn clone(&self) -> RationalVector<D>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more