RawScalarTrait

Trait RawScalarTrait 

Source
pub trait RawScalarTrait:
    ScalarCore
    + Arithmetic
    + NeumaierAddable
    + FpChecks
    + RawScalarTrigonometric
    + RawScalarHyperbolic
    + RawScalarPow {
    type ValidationErrors: Error;

    // Required methods
    fn is_zero(&self) -> bool;
    fn raw_zero(precision: u32) -> Self;
    fn raw_one(precision: u32) -> Self;
    fn unchecked_reciprocal(self) -> Self;
    fn unchecked_exp(self) -> Self;
    fn unchecked_sqrt(self) -> Self;
    fn unchecked_ln(self) -> Self;
    fn unchecked_log2(self) -> Self;
    fn unchecked_log10(self) -> Self;
    fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self;
    fn compute_hash<H: Hasher>(&self, state: &mut H);

    // Provided method
    fn raw_negative_one(precision: u32) -> Self { ... }
}
Expand description

A baseline trait for raw scalar types, defining core operations and properties.

This trait must be implemented by the underlying number types used in a kernel, such as like f64 or rug::Float. It provides a standard interface for arithmetic and a suite of unchecked_* methods for mathematical functions.

§Hashing Support

All raw scalar types (both real and complex) must implement compute_hash(), which provides a consistent hashing mechanism. The hash implementation must ensure:

  • Mathematical Equality: Values that are mathematically equal produce identical hashes
  • Signed Zero Handling: Both +0.0 and -0.0 hash to the same value
  • Finite Values Only: The hash is only well-defined for finite values (not NaN or infinity)
  • Consistency: The same value always produces the same hash across multiple calls

For complex numbers, the hash is computed by sequentially hashing the real and imaginary parts, ensuring that different complex numbers produce different hashes while maintaining the equality invariant.

This enables validated wrapper types to implement Hash when their validation policies guarantee finite values (via crate::core::traits::validation::GuaranteesFiniteRealValues), allowing them to be used as keys in HashMap and HashSet.

§Safety and Contracts

The unchecked_* methods are designed for performance and assume that the caller has already validated the inputs. Calling them with invalid data (e.g., unchecked_sqrt on a negative real number) may lead to panics, incorrect results, or undefined behavior, depending on the underlying type’s implementation.

Required Associated Types§

Source

type ValidationErrors: Error

The associated error type for validation failures of this raw type.

Required Methods§

Source

fn is_zero(&self) -> bool

Returns true if self is zero.

Source

fn raw_zero(precision: u32) -> Self

Returns zero with the given precision.

Source

fn raw_one(precision: u32) -> Self

Returns one with the given precision.

Source

fn unchecked_reciprocal(self) -> Self

Computes the reciprocal (1/x) without validation.

Contract: The caller must ensure self is not zero.

Source

fn unchecked_exp(self) -> Self

Computes the exponential (e^x) without validation.

Contract: The caller must ensure the input is valid.

Source

fn unchecked_sqrt(self) -> Self

Computes the square root without validation.

Contract: For real numbers, the caller must ensure self >= 0.

Source

fn unchecked_ln(self) -> Self

Computes the natural logarithm without validation.

Source

fn unchecked_log2(self) -> Self

Computes the base-2 logarithm without validation.

Source

fn unchecked_log10(self) -> Self

Computes the base-10 logarithm without validation.

Source

fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self

Multiplies and adds in one fused operation, rounding to the nearest with only one rounding error.

a.mul_add(b, c) produces a result like a * &b + &c.

Contract: The caller must ensure all inputs are valid.

Source

fn compute_hash<H: Hasher>(&self, state: &mut H)

Computes a hash value for this scalar (real or complex).

This method must ensure that mathematically equal values produce the same hash, even across different representations (e.g., +0.0 and -0.0).

For complex numbers, implementations should hash the real and imaginary parts sequentially to ensure distinct complex numbers have different hashes.

§Implementation Notes
  • For real numbers: Hash the value directly, normalizing signed zeros
  • For complex numbers: Call compute_hash() on real part, then imaginary part
§Debug Assertions

Implementations should include a debug assertion that the value is finite, as hashing non-finite values may lead to inconsistent results.

Provided Methods§

Source

fn raw_negative_one(precision: u32) -> Self

Returns negative one with the given precision.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl RawScalarTrait for f64

Source§

fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self

Multiplies and adds in one fused operation, rounding to the nearest with only one rounding error.

a.mul_add(b, c) produces a result like a * &b + &c.

Source§

type ValidationErrors = ErrorsValidationRawReal<f64>

Source§

fn raw_zero(_precision: u32) -> f64

Source§

fn is_zero(&self) -> bool

Source§

fn raw_one(_precision: u32) -> f64

Source§

fn unchecked_reciprocal(self) -> f64

Source§

fn unchecked_exp(self) -> f64

Source§

fn unchecked_sqrt(self) -> f64

Source§

fn unchecked_ln(self) -> f64

Source§

fn unchecked_log2(self) -> f64

Source§

fn unchecked_log10(self) -> f64

Source§

fn compute_hash<H: Hasher>(&self, state: &mut H)

Source§

impl RawScalarTrait for Complex<f64>

Source§

fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self

Multiplies and adds in one fused operation, rounding to the nearest with only one rounding error.

a.mul_add(b, c) produces a result like a * &b + &c.

Source§

type ValidationErrors = ErrorsValidationRawComplex<ErrorsValidationRawReal<f64>>

Source§

fn raw_zero(_precision: u32) -> Self

Source§

fn is_zero(&self) -> bool

Source§

fn raw_one(_precision: u32) -> Self

Source§

fn unchecked_exp(self) -> Self

Source§

fn unchecked_sqrt(self) -> Self

Source§

fn unchecked_ln(self) -> Self

Source§

fn unchecked_log10(self) -> Self

Source§

fn unchecked_reciprocal(self) -> Self

Source§

fn unchecked_log2(self) -> Self

Source§

fn compute_hash<H: Hasher>(&self, state: &mut H)

Source§

impl RawScalarTrait for Complex

Source§

fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self

Multiplies and adds in one fused operation, rounding to the nearest with only one rounding error.

a.mul_add(b, c) produces a result like a * &b + &c.

Source§

type ValidationErrors = ErrorsValidationRawComplex<ErrorsValidationRawReal<Float>>

Source§

fn raw_zero(precision: u32) -> Self

Source§

fn is_zero(&self) -> bool

Source§

fn raw_one(precision: u32) -> Self

Source§

fn unchecked_reciprocal(self) -> Self

Source§

fn unchecked_exp(self) -> Self

Source§

fn unchecked_sqrt(self) -> Self

Source§

fn unchecked_ln(self) -> Self

Source§

fn unchecked_log10(self) -> Self

Source§

fn unchecked_log2(self) -> Self

Source§

fn compute_hash<H: Hasher>(&self, state: &mut H)

Source§

impl RawScalarTrait for Float

Source§

fn unchecked_mul_add(self, b: &Self, c: &Self) -> Self

Multiplies and adds in one fused operation, rounding to the nearest with only one rounding error.

a.mul_add(b, c) produces a result like a * &b + &c.

Source§

type ValidationErrors = ErrorsValidationRawReal<Float>

Source§

fn raw_zero(precision: u32) -> Self

Source§

fn is_zero(&self) -> bool

Source§

fn raw_one(precision: u32) -> Self

Source§

fn unchecked_reciprocal(self) -> Self

Source§

fn unchecked_exp(self) -> Self

Source§

fn unchecked_sqrt(self) -> Self

Source§

fn unchecked_ln(self) -> Self

Source§

fn unchecked_log2(self) -> Self

Source§

fn unchecked_log10(self) -> Self

Source§

fn compute_hash<H: Hasher>(&self, state: &mut H)

Implementors§