Skip to main content

Da

Struct Da 

Source
pub struct Da { /* private fields */ }
Expand description

A truncated multivariate Taylor polynomial (“differential algebra” value).

Stored as a sparse list of (monomial index, coefficient) terms, sorted by ascending monomial index; the index packs the exponent vector into the canonical position determined by the active context.

Values created under one context keep working after init is called again (they hold on to their original context); mixing values from different contexts in one operation panics. See the crate-level Multithreading section for cross-thread use.

Implementations§

Source§

impl Da

Source

pub fn new() -> Da

The zero polynomial (also Da::default).

§Panics

Panics with crate::DaceError if DACE has not been initialized.

Source

pub fn constant(c: f64) -> Da

The constant polynomial c (daceCreateConstant); |c| <= eps gives the zero polynomial.

Source

pub fn variable(var: u32) -> Da

The independent DA variable number var (1-based), i.e. the identity in that variable (daceCreateVariable).

Divergence from C: an out-of-range var logs a warning and returns the zero polynomial instead of raising C error 624.

Source

pub fn identity(var: u32) -> Da

Alias of Da::variable (DA::identity in the C++ interface).

Source

pub fn monomial(jj: &[u32], c: f64) -> Da

The polynomial consisting of the single monomial c * jj[0]^jj[1] * ... (daceCreateMonomial).

jj is padded with zeros or truncated to the number of DA variables (with a warning). Terms whose total order exceeds the maximum computation order, or with an exponent above it, are dropped with a warning (divergence from C, which encodes them as the constant term after raising error 622). |c| <= eps gives the zero polynomial.

Source

pub fn random(cmu: f64) -> Da

A DA with randomly filled coefficients (daceCreateRandom).

cmu is the filling factor: |cmu| is the fraction of non-zero coefficients; cmu < 0 draws coefficients in [-1, 1], cmu > 0 weights them to decay exponentially with order from 1.0 towards the machine epsilon.

Divergence from C: the C library uses libc rand() (platform- and seed-dependent); this implementation uses a deterministic 64-bit LCG per thread, so results are reproducible everywhere.

Source

pub fn cons(&self) -> f64

The constant part of the polynomial (daceGetConstant).

Source

pub fn linear(&self) -> Vec<f64>

The linear coefficients, one per DA variable (daceGetLinear).

Source

pub fn gradient(&self) -> Vec<Da>

The gradient: derivatives with respect to all DA variables (DA::gradient in the C++ interface).

Source

pub fn size(&self) -> usize

The number of stored (non-zero) monomials (daceGetLength).

Source

pub fn get_coefficient(&self, jj: &[u32]) -> f64

The coefficient of the monomial with exponents jj (daceGetCoefficient). jj is padded/truncated to the number of DA variables; invalid exponents return 0.0 with a warning.

Source

pub fn set_coefficient(&mut self, jj: &[u32], c: f64)

Set the coefficient of the monomial with exponents jj (daceSetCoefficient): sets it, replaces it, or removes the monomial when |c| <= eps, keeping the term list sorted.

Source

pub fn get_monomial(&self, pos: usize) -> Option<Monomial>

The monomial at 1-based position pos in the stored term list (DA::getMonomial in the C++ interface); None when out of range. The ordering is implementation-dependent.

Source

pub fn iter_monomials(&self) -> impl Iterator<Item = Monomial> + '_

Iterate over all stored monomials, in stored order.

Source

pub fn is_nan(&self) -> bool

Whether any coefficient is NaN (daceIsNan).

Source

pub fn is_inf(&self) -> bool

Whether any coefficient is infinite (daceIsInf).

Source

pub fn deriv(&self, var: u32) -> Da

Derivative with respect to independent variable var (1-based) (daceDifferentiate). Out-of-range variables warn and return zero.

Source

pub fn deriv_vars(&self, vars: &[u32]) -> Da

Repeated derivative with respect to vars[0], then vars[1], …

Source

pub fn integ(&self, var: u32) -> Da

Integral with respect to independent variable var (1-based) (daceIntegrate); the integration constant is zero. Out-of-range variables warn and return zero.

Source

pub fn integ_vars(&self, vars: &[u32]) -> Da

Repeated integral with respect to vars[0], then vars[1], …

Source

pub fn trim(&self, min_order: u32, max_order: u32) -> Da

Keep only terms of order between min_order and max_order inclusive (daceTrim).

Source

pub fn minv(&self) -> Da

The multiplicative inverse 1/self (daceMultiplicativeInverse): direct alternating series below truncation order 5, Newton iteration above.

§Panics

Panics with crate::DaceError code 641 (“Dividing by zero”) when the constant part of self is zero.

Source

pub fn sqr(&self) -> Da

The square self * self (daceSquare).

Source

pub fn divide_variable(&self, var: u32, p: u32) -> Da

Divide by var^p, when every monomial’s exponent in var is at least p (daceDivideByVariable): exact polynomial division on the exponents, coefficients unchanged. p == 0 returns a copy.

Out-of-range variables warn and return zero. Division is impossible when some exponent is too small (p > nomax, or the DA is non-zero with insufficient exponents).

§Panics

Panics with crate::DaceError code 642 (“Inverse does not exists”) when the division is impossible on a non-zero DA.

Source

pub fn multiply_monomials(&self, other: &Da) -> Da

Multiply with other monomial-by-monomial: the coefficient-wise product over matching monomial indices (daceMultiplyMonomials).

Source§

impl Da

Source

pub fn exp(&self) -> Da

Method form of exp.

Source

pub fn log(&self) -> Da

Method form of log.

Source

pub fn log10(&self) -> Da

Method form of log10.

Source

pub fn log2(&self) -> Da

Method form of log2.

Source

pub fn sin(&self) -> Da

Method form of sin.

Source

pub fn cos(&self) -> Da

Method form of cos.

Source

pub fn tan(&self) -> Da

Method form of tan.

Source

pub fn asin(&self) -> Da

Method form of asin.

Source

pub fn acos(&self) -> Da

Method form of acos.

Source

pub fn atan(&self) -> Da

Method form of atan.

Source

pub fn sinh(&self) -> Da

Method form of sinh.

Source

pub fn cosh(&self) -> Da

Method form of cosh.

Source

pub fn tanh(&self) -> Da

Method form of tanh.

Source

pub fn asinh(&self) -> Da

Method form of asinh.

Source

pub fn acosh(&self) -> Da

Method form of acosh.

Source

pub fn atanh(&self) -> Da

Method form of atanh.

Source

pub fn erf(&self) -> Da

Method form of erf.

Source

pub fn erfc(&self) -> Da

Method form of erfc.

Source

pub fn sqrt(&self) -> Da

Method form of sqrt.

Source

pub fn isrt(&self) -> Da

Method form of isrt.

Source

pub fn cbrt(&self) -> Da

Method form of cbrt.

Source

pub fn icrt(&self) -> Da

Method form of icrt.

Source

pub fn powf(&self, p: f64) -> Da

Method form of powf.

Source

pub fn powi(&self, np: i32) -> Da

Method form of powi.

Source

pub fn root(&self, np: i32) -> Da

Method form of root.

Source

pub fn log_base(&self, b: f64) -> Da

Method form of log_base.

Source

pub fn hypot(&self, b: &Da) -> Da

Method form of hypot.

Source

pub fn atan2(&self, x: &Da) -> Da

Method form of atan2.

Source

pub fn modulo(&self, p: f64) -> Da

Method form of modulo.

Source

pub fn trunc(&self) -> Da

Method form of trunc.

Source

pub fn round(&self) -> Da

Method form of round.

Source§

impl Da

Source

pub fn plug(&self, var: u32, val: f64) -> Da

Partial evaluation: replace independent variable var (1-based) by the value val (daceEvalVariable). Out-of-range variables warn and return zero.

Source

pub fn replace_variable(&self, from: u32, to: u32, val: f64) -> Da

Replace independent variable from by val times independent variable to (daceReplaceVariable); from == to scales the variable by val. Out-of-range variables warn and return zero.

Divergence from C: the C implementation indexes its 0-based exponent array with the 1-based variable numbers, so it actually replaces variable from + 1 by val · (variable to + 1) and silently does nothing when from == nvmax. This implementation follows the documented (1-based) semantics instead.

Source

pub fn scale_variable(&self, var: u32, val: f64) -> Da

Scale independent variable var by val: x_var -> val * x_var (daceScaleVariable). Out-of-range variables warn and return zero.

Source

pub fn translate_variable(&self, var: u32, a: f64, c: f64) -> Da

Translate independent variable var to a*x + c (daceTranslateVariable). Out-of-range variables warn and return zero.

Source

pub fn eval_monomials(&self, values: &Da) -> f64

Evaluate by providing the value of each monomial in values: the monomial-wise dot product of the two DAs (daceEvalMonomials).

Source

pub fn eval(&self, args: &[f64]) -> f64

Evaluate at a point: compile and evaluate the tree, returning the first component (C++ DA::eval).

Source

pub fn eval_da(&self, args: &[Da]) -> Da

Evaluate with DA arguments (contraction): substitute each argument DA for the corresponding variable.

Source

pub fn compile(&self) -> CompiledDa

Compile into a reusable evaluation tree (C++ compiledDA).

Source§

impl Da

Source

pub fn to_blob(&self) -> Vec<u8> ⓘ

Export in the C binary blob format (daceExportBlob): magic, no, nv1, nv2, len as little-endian u32s, then len packed {i1, i2, cc} records. As in C, the buffer always includes one record slot (zeroed when the DA is empty).

All supported targets are little-endian, so blobs interoperate with the C library on the reference platforms.

Source

pub fn from_blob(blob: &[u8]) -> Result<Da, DaceError>

Import from the C binary blob format (daceImportBlob): silently truncates orders above the current maximum computation order and any extra variables present, as in C.

§Errors

Returns crate::DaceError code 631 (“Invalid data”) when the magic is wrong or the buffer is too short.

Source§

impl Da

Source

pub fn abs(&self) -> f64

The absolute value (maximum coefficient norm) of the DA (daceAbsoluteValue).

Source

pub fn norm(&self, ityp: NormType) -> f64

A norm of all coefficients (daceNorm).

Source

pub fn order_norm(&self, var: u32, ityp: NormType) -> Vec<f64>

Order-sorted norms (daceOrderedNorm): one norm per order (var == 0, length nomax + 1) or per exponent of variable var (1-based, length nomax + 1).

Out-of-range variables log a warning and return all zeros.

Source

pub fn estim_norm(&self, var: u32, ityp: NormType, nc: u32) -> Vec<f64>

Estimate order-sorted norms up to order nc by an exponential least-squares fit (daceEstimate): returns the estimates c[0..=nc].

If fewer than two orders have non-negligible norms, the fit is impossible; a warning is logged and zeros are returned (as in C, where this is informational).

§Panics

Panics with crate::DaceError code 651 when nomax < 2.

Source

pub fn estim_norm_err( &self, var: u32, ityp: NormType, nc: u32, ) -> (Vec<f64>, Vec<f64>)

Like Da::estim_norm, also returning the fit residuals per order (length min(nc, nomax) + 1).

Source

pub fn bound(&self) -> Interval

An upper and lower bound of the DA over [-1, 1]^nv (daceGetBounds): monomials with all-even exponents contribute their signed coefficient to one side, others their absolute value to both.

Source

pub fn conv_radius(&self, eps: f64, ityp: NormType) -> f64

Estimate the convergence radius: the radius at which the estimated norm of the next order falls below eps (C++ DA::convRadius).

Source§

impl Da

Source

pub fn bessel_j(&self, n: i32) -> Da

Method form of bessel_j.

Source

pub fn bessel_y(&self, n: i32) -> Da

Method form of bessel_y.

Source

pub fn bessel_i(&self, n: i32, scaled: bool) -> Da

Method form of bessel_i.

Source

pub fn bessel_k(&self, n: i32, scaled: bool) -> Da

Method form of bessel_k.

Source

pub fn log_gamma(&self) -> Da

Method form of log_gamma.

Source

pub fn gamma(&self) -> Da

Method form of gamma.

Source

pub fn psi(&self, n: u32) -> Da

Method form of psi.

Trait Implementations§

Source§

impl Add for Da

Source§

type Output = Da

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Da) -> Da

Performs the + operation. Read more
Source§

impl Add<Da> for f64

Source§

type Output = Da

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Da) -> Da

Performs the + operation. Read more
Source§

impl Add<f64> for Da

Source§

type Output = Da

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f64) -> Da

Performs the + operation. Read more
Source§

impl AddAssign for Da

Source§

fn add_assign(&mut self, rhs: Da)

Performs the += operation. Read more
Source§

impl AddAssign<f64> for Da

Source§

fn add_assign(&mut self, rhs: f64)

Performs the += operation. Read more
Source§

impl Clone for Da

Source§

fn clone(&self) -> Self

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 Da

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Da

Source§

fn default() -> Da

Returns the “default value” for a type. Read more
Source§

impl Display for Da

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Div for Da

Source§

fn div(self, rhs: Da) -> Da

§Panics

Panics with crate::DaceError code 641 when rhs has a zero constant part (division by zero), via the multiplicative inverse.

Source§

type Output = Da

The resulting type after applying the / operator.
Source§

impl Div<Da> for f64

Source§

type Output = Da

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Da) -> Da

Performs the / operation. Read more
Source§

impl Div<f64> for Da

Source§

fn div(self, rhs: f64) -> Da

§Panics

Panics with crate::DaceError code 641 when rhs == 0.0.

Source§

type Output = Da

The resulting type after applying the / operator.
Source§

impl DivAssign for Da

Source§

fn div_assign(&mut self, rhs: Da)

Performs the /= operation. Read more
Source§

impl DivAssign<f64> for Da

Source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
Source§

impl FromStr for Da

Source§

fn from_str(s: &str) -> Result<Da, DaceError>

Parse the text format emitted by Display (the daceRead parser, including its tolerance: line-number and order mismatches warn, order/truncation-excess rows are skipped, duplicate monomials accumulate).

§Panics

Panics with crate::DaceError if DACE has not been initialized.

Source§

type Err = DaceError

The associated error which can be returned from parsing.
Source§

impl Mul for Da

Source§

type Output = Da

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Da) -> Da

Performs the * operation. Read more
Source§

impl Mul<Da> for f64

Source§

type Output = Da

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Da) -> Da

Performs the * operation. Read more
Source§

impl Mul<f64> for Da

Source§

type Output = Da

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Da

Performs the * operation. Read more
Source§

impl MulAssign for Da

Source§

fn mul_assign(&mut self, rhs: Da)

Performs the *= operation. Read more
Source§

impl MulAssign<f64> for Da

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl Neg for Da

Source§

type Output = Da

The resulting type after applying the - operator.
Source§

fn neg(self) -> Da

Performs the unary - operation. Read more
Source§

impl Sub for Da

Source§

type Output = Da

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Da) -> Da

Performs the - operation. Read more
Source§

impl Sub<Da> for f64

Source§

type Output = Da

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Da) -> Da

Performs the - operation. Read more
Source§

impl Sub<f64> for Da

Source§

type Output = Da

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f64) -> Da

Performs the - operation. Read more
Source§

impl SubAssign for Da

Source§

fn sub_assign(&mut self, rhs: Da)

Performs the -= operation. Read more
Source§

impl SubAssign<f64> for Da

Source§

fn sub_assign(&mut self, rhs: f64)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Da

§

impl RefUnwindSafe for Da

§

impl Send for Da

§

impl Sync for Da

§

impl Unpin for Da

§

impl UnsafeUnpin for Da

§

impl UnwindSafe for Da

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> 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.