Skip to main content

Scalar

Trait Scalar 

Source
pub trait Scalar:
    Copy
    + Clone
    + Debug
    + Display
    + PartialOrd
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Neg<Output = Self>
    + AddAssign
    + SubAssign
    + MulAssign
    + DivAssign
    + Sum
    + Send
    + Sync
    + 'static {
    const ZERO: Self;
    const ONE: Self;
    const TWO: Self;
    const HALF: Self;
    const EPSILON: Self;
    const INFINITY: Self;
    const NEG_INFINITY: Self;
    const NAN: Self;
    const PI: Self;
    const E: Self;
    const SQRT_2: Self;
    const LN_2: Self;
Show 55 methods // Required methods fn from_f64(x: f64) -> Self; fn from_f32(x: f32) -> Self; fn from_i32(x: i32) -> Self; fn from_usize(n: usize) -> Self; fn to_f64(self) -> f64; fn to_f32(self) -> f32; fn abs(self) -> Self; fn sqrt(self) -> Self; fn cbrt(self) -> Self; fn powi(self, n: i32) -> Self; fn powf(self, n: Self) -> Self; fn hypot(self, other: Self) -> Self; fn sin(self) -> Self; fn cos(self) -> Self; fn tan(self) -> Self; fn asin(self) -> Self; fn acos(self) -> Self; fn atan(self) -> Self; fn atan2(self, other: Self) -> Self; fn exp(self) -> Self; fn exp2(self) -> Self; fn exp_m1(self) -> Self; fn ln(self) -> Self; fn log2(self) -> Self; fn log10(self) -> Self; fn ln_1p(self) -> Self; fn sinh(self) -> Self; fn cosh(self) -> Self; fn tanh(self) -> Self; fn asinh(self) -> Self; fn acosh(self) -> Self; fn atanh(self) -> Self; fn max(self, other: Self) -> Self; fn min(self, other: Self) -> Self; fn clamp(self, min: Self, max: Self) -> Self; fn copysign(self, sign: Self) -> Self; fn is_finite(self) -> bool; fn is_nan(self) -> bool; fn is_infinite(self) -> bool; fn is_sign_positive(self) -> bool; fn is_sign_negative(self) -> bool; fn floor(self) -> Self; fn ceil(self) -> Self; fn round(self) -> Self; fn trunc(self) -> Self; fn fract(self) -> Self; fn gamma_fn(self) -> Self; fn ln_gamma(self) -> Self; fn erf_fn(self) -> Self; fn erfc_fn(self) -> Self; fn mul_add(self, a: Self, b: Self) -> Self; // Provided methods fn sq(self) -> Self { ... } fn recip(self) -> Self { ... } fn sincos(self) -> (Self, Self) { ... } fn signum(self) -> Self { ... }
}
Expand description

A real scalar type suitable for numerical computation.

This trait provides all mathematical operations needed by numerical methods, including basic arithmetic, trigonometric functions, and special functions.

§Example

use numra_core::Scalar;

fn quadratic_formula<S: Scalar>(a: S, b: S, c: S) -> Option<(S, S)> {
    let discriminant = b * b - S::from_f64(4.0) * a * c;
    if discriminant < S::ZERO {
        return None;
    }
    let sqrt_d = discriminant.sqrt();
    let two_a = S::TWO * a;
    Some(((-b + sqrt_d) / two_a, (-b - sqrt_d) / two_a))
}

Required Associated Constants§

Source

const ZERO: Self

Additive identity: 0

Source

const ONE: Self

Multiplicative identity: 1

Source

const TWO: Self

Two (commonly needed constant)

Source

const HALF: Self

One half

Source

const EPSILON: Self

Machine epsilon (smallest x such that 1 + x ≠ 1)

Source

const INFINITY: Self

Positive infinity

Source

const NEG_INFINITY: Self

Negative infinity

Source

const NAN: Self

Not a number

Source

const PI: Self

Pi (π)

Source

const E: Self

Euler’s number (e)

Source

const SQRT_2: Self

Square root of 2

Source

const LN_2: Self

Natural log of 2

Required Methods§

Source

fn from_f64(x: f64) -> Self

Create from f64

Source

fn from_f32(x: f32) -> Self

Create from f32

Source

fn from_i32(x: i32) -> Self

Create from i32

Source

fn from_usize(n: usize) -> Self

Create from usize

Source

fn to_f64(self) -> f64

Convert to f64

Source

fn to_f32(self) -> f32

Convert to f32

Source

fn abs(self) -> Self

Absolute value

Source

fn sqrt(self) -> Self

Square root

Source

fn cbrt(self) -> Self

Cube root

Source

fn powi(self, n: i32) -> Self

Integer power

Source

fn powf(self, n: Self) -> Self

Floating point power

Source

fn hypot(self, other: Self) -> Self

Hypotenuse: sqrt(x² + y²) computed without overflow

Source

fn sin(self) -> Self

Sine

Source

fn cos(self) -> Self

Cosine

Source

fn tan(self) -> Self

Tangent

Source

fn asin(self) -> Self

Arcsine

Source

fn acos(self) -> Self

Arccosine

Source

fn atan(self) -> Self

Arctangent

Source

fn atan2(self, other: Self) -> Self

Two-argument arctangent (atan2)

Source

fn exp(self) -> Self

Natural exponential (e^x)

Source

fn exp2(self) -> Self

Base-2 exponential (2^x)

Source

fn exp_m1(self) -> Self

exp(x) - 1, accurate for small x

Source

fn ln(self) -> Self

Natural logarithm

Source

fn log2(self) -> Self

Base-2 logarithm

Source

fn log10(self) -> Self

Base-10 logarithm

Source

fn ln_1p(self) -> Self

ln(1 + x), accurate for small x

Source

fn sinh(self) -> Self

Hyperbolic sine

Source

fn cosh(self) -> Self

Hyperbolic cosine

Source

fn tanh(self) -> Self

Hyperbolic tangent

Source

fn asinh(self) -> Self

Inverse hyperbolic sine

Source

fn acosh(self) -> Self

Inverse hyperbolic cosine

Source

fn atanh(self) -> Self

Inverse hyperbolic tangent

Source

fn max(self, other: Self) -> Self

Maximum of two values

Source

fn min(self, other: Self) -> Self

Minimum of two values

Source

fn clamp(self, min: Self, max: Self) -> Self

Clamp value to range [min, max]

Source

fn copysign(self, sign: Self) -> Self

Copy sign from another value

Source

fn is_finite(self) -> bool

Is the value finite (not NaN or infinity)?

Source

fn is_nan(self) -> bool

Is the value NaN?

Source

fn is_infinite(self) -> bool

Is the value infinite?

Source

fn is_sign_positive(self) -> bool

Is the sign positive (including +0)?

Source

fn is_sign_negative(self) -> bool

Is the sign negative (including -0)?

Source

fn floor(self) -> Self

Round toward negative infinity

Source

fn ceil(self) -> Self

Round toward positive infinity

Source

fn round(self) -> Self

Round to nearest integer

Source

fn trunc(self) -> Self

Round toward zero

Source

fn fract(self) -> Self

Fractional part

Source

fn gamma_fn(self) -> Self

Gamma function Γ(x)

Source

fn ln_gamma(self) -> Self

Natural log of gamma function ln(Γ(x))

Source

fn erf_fn(self) -> Self

Error function erf(x)

Source

fn erfc_fn(self) -> Self

Complementary error function erfc(x) = 1 - erf(x)

Source

fn mul_add(self, a: Self, b: Self) -> Self

Fused multiply-add: (self * a) + b with single rounding

Provided Methods§

Source

fn sq(self) -> Self

Square (x²) - more efficient than x * x for some types

Source

fn recip(self) -> Self

Reciprocal (1/x)

Source

fn sincos(self) -> (Self, Self)

Simultaneous sine and cosine (more efficient than separate calls)

Source

fn signum(self) -> Self

Sign function: -1, 0, or 1

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 Scalar for f32

Source§

const ZERO: f32 = 0.0

Source§

const ONE: f32 = 1.0

Source§

const TWO: f32 = 2.0

Source§

const HALF: f32 = 0.5

Source§

const EPSILON: f32 = f32::EPSILON

Source§

const INFINITY: f32 = f32::INFINITY

Source§

const NEG_INFINITY: f32 = f32::NEG_INFINITY

Source§

const NAN: f32 = f32::NAN

Source§

const PI: f32 = core::f32::consts::PI

Source§

const E: f32 = core::f32::consts::E

Source§

const SQRT_2: f32 = core::f32::consts::SQRT_2

Source§

const LN_2: f32 = core::f32::consts::LN_2

Source§

fn from_f64(x: f64) -> f32

Source§

fn from_f32(x: f32) -> f32

Source§

fn from_i32(x: i32) -> f32

Source§

fn from_usize(n: usize) -> f32

Source§

fn to_f64(self) -> f64

Source§

fn to_f32(self) -> f32

Source§

fn abs(self) -> f32

Source§

fn sqrt(self) -> f32

Source§

fn cbrt(self) -> f32

Source§

fn powi(self, n: i32) -> f32

Source§

fn powf(self, n: f32) -> f32

Source§

fn hypot(self, other: f32) -> f32

Source§

fn sin(self) -> f32

Source§

fn cos(self) -> f32

Source§

fn tan(self) -> f32

Source§

fn asin(self) -> f32

Source§

fn acos(self) -> f32

Source§

fn atan(self) -> f32

Source§

fn atan2(self, other: f32) -> f32

Source§

fn sincos(self) -> (f32, f32)

Source§

fn exp(self) -> f32

Source§

fn exp2(self) -> f32

Source§

fn exp_m1(self) -> f32

Source§

fn ln(self) -> f32

Source§

fn log2(self) -> f32

Source§

fn log10(self) -> f32

Source§

fn ln_1p(self) -> f32

Source§

fn sinh(self) -> f32

Source§

fn cosh(self) -> f32

Source§

fn tanh(self) -> f32

Source§

fn asinh(self) -> f32

Source§

fn acosh(self) -> f32

Source§

fn atanh(self) -> f32

Source§

fn max(self, other: f32) -> f32

Source§

fn min(self, other: f32) -> f32

Source§

fn clamp(self, min: f32, max: f32) -> f32

Source§

fn copysign(self, sign: f32) -> f32

Source§

fn is_finite(self) -> bool

Source§

fn is_nan(self) -> bool

Source§

fn is_infinite(self) -> bool

Source§

fn is_sign_positive(self) -> bool

Source§

fn is_sign_negative(self) -> bool

Source§

fn floor(self) -> f32

Source§

fn ceil(self) -> f32

Source§

fn round(self) -> f32

Source§

fn trunc(self) -> f32

Source§

fn fract(self) -> f32

Source§

fn gamma_fn(self) -> f32

Source§

fn ln_gamma(self) -> f32

Source§

fn erf_fn(self) -> f32

Source§

fn erfc_fn(self) -> f32

Source§

fn mul_add(self, a: f32, b: f32) -> f32

Source§

impl Scalar for f64

Source§

const ZERO: f64 = 0.0

Source§

const ONE: f64 = 1.0

Source§

const TWO: f64 = 2.0

Source§

const HALF: f64 = 0.5

Source§

const EPSILON: f64 = f64::EPSILON

Source§

const INFINITY: f64 = f64::INFINITY

Source§

const NEG_INFINITY: f64 = f64::NEG_INFINITY

Source§

const NAN: f64 = f64::NAN

Source§

const PI: f64 = core::f64::consts::PI

Source§

const E: f64 = core::f64::consts::E

Source§

const SQRT_2: f64 = core::f64::consts::SQRT_2

Source§

const LN_2: f64 = core::f64::consts::LN_2

Source§

fn from_f64(x: f64) -> f64

Source§

fn from_f32(x: f32) -> f64

Source§

fn from_i32(x: i32) -> f64

Source§

fn from_usize(n: usize) -> f64

Source§

fn to_f64(self) -> f64

Source§

fn to_f32(self) -> f32

Source§

fn abs(self) -> f64

Source§

fn sqrt(self) -> f64

Source§

fn cbrt(self) -> f64

Source§

fn powi(self, n: i32) -> f64

Source§

fn powf(self, n: f64) -> f64

Source§

fn hypot(self, other: f64) -> f64

Source§

fn sin(self) -> f64

Source§

fn cos(self) -> f64

Source§

fn tan(self) -> f64

Source§

fn asin(self) -> f64

Source§

fn acos(self) -> f64

Source§

fn atan(self) -> f64

Source§

fn atan2(self, other: f64) -> f64

Source§

fn sincos(self) -> (f64, f64)

Source§

fn exp(self) -> f64

Source§

fn exp2(self) -> f64

Source§

fn exp_m1(self) -> f64

Source§

fn ln(self) -> f64

Source§

fn log2(self) -> f64

Source§

fn log10(self) -> f64

Source§

fn ln_1p(self) -> f64

Source§

fn sinh(self) -> f64

Source§

fn cosh(self) -> f64

Source§

fn tanh(self) -> f64

Source§

fn asinh(self) -> f64

Source§

fn acosh(self) -> f64

Source§

fn atanh(self) -> f64

Source§

fn max(self, other: f64) -> f64

Source§

fn min(self, other: f64) -> f64

Source§

fn clamp(self, min: f64, max: f64) -> f64

Source§

fn copysign(self, sign: f64) -> f64

Source§

fn is_finite(self) -> bool

Source§

fn is_nan(self) -> bool

Source§

fn is_infinite(self) -> bool

Source§

fn is_sign_positive(self) -> bool

Source§

fn is_sign_negative(self) -> bool

Source§

fn floor(self) -> f64

Source§

fn ceil(self) -> f64

Source§

fn round(self) -> f64

Source§

fn trunc(self) -> f64

Source§

fn fract(self) -> f64

Source§

fn gamma_fn(self) -> f64

Source§

fn ln_gamma(self) -> f64

Source§

fn erf_fn(self) -> f64

Source§

fn erfc_fn(self) -> f64

Source§

fn mul_add(self, a: f64, b: f64) -> f64

Implementors§