Skip to main content

Crate wick_scalar

Crate wick_scalar 

Source
Expand description

Standard scalar function library for dew expressions.

This crate provides the foundation for numeric expressions: standard math functions (sin, cos, sqrt, etc.), constants (pi, e, tau), and evaluation for scalar values. All functions are generic over T: Float, supporting both f32 and f64.

§Quick Start

use wick_core::Expr;
use wick_scalar::{eval, scalar_registry};
use std::collections::HashMap;

// Parse and evaluate an expression
let expr = Expr::parse("sin(x * pi()) + 1").unwrap();
let vars: HashMap<String, f32> = [("x".into(), 0.5)].into();
let result = eval(expr.ast(), &vars, &scalar_registry()).unwrap();
assert!((result - 2.0).abs() < 0.001); // sin(0.5 * π) + 1 = 2

§Features

FeatureDescription
wgslWGSL shader code generation
lua-codegenLua code generation (pure Rust, WASM-safe)
luaLua codegen + mlua execution
craneliftCranelift JIT compilation

§Available Functions

§Constants

FunctionDescription
pi()π ≈ 3.14159
e()Euler’s number ≈ 2.71828
tau()τ = 2π ≈ 6.28318

§Trigonometric

FunctionDescription
sin(x)Sine
cos(x)Cosine
tan(x)Tangent
asin(x)Arcsine
acos(x)Arccosine
atan(x)Arctangent
atan2(y, x)Two-argument arctangent
sinh(x)Hyperbolic sine
cosh(x)Hyperbolic cosine
tanh(x)Hyperbolic tangent

§Exponential & Logarithmic

FunctionDescription
exp(x)e^x
exp2(x)2^x
log(x)Natural logarithm (alias ln)
ln(x)Natural logarithm
log2(x)Base-2 logarithm
log10(x)Base-10 logarithm
pow(x, y)x^y
sqrt(x)Square root
inversesqrt(x)1 / sqrt(x)

§Common Math

FunctionDescription
abs(x)Absolute value
sign(x)Sign (-1, 0, or 1)
floor(x)Round down
ceil(x)Round up
round(x)Round to nearest
trunc(x)Truncate toward zero
fract(x)Fractional part
min(a, b)Minimum of two values
max(a, b)Maximum of two values
clamp(x, lo, hi)Clamp to range
saturate(x)Clamp to [0, 1]

§Interpolation

FunctionDescription
lerp(a, b, t)Linear interpolation: a + (b-a)*t
mix(a, b, t)Alias for lerp (GLSL naming)
step(edge, x)0 if x < edge, else 1
smoothstep(e0, e1, x)Smooth Hermite interpolation
inverse_lerp(a, b, v)Inverse of lerp: (v-a) / (b-a)
remap(x, i0, i1, o0, o1)Remap from [i0,i1] to [o0,o1]

§Custom Functions

You can register custom functions by implementing the ScalarFn trait:

use wick_scalar::{ScalarFn, FunctionRegistry, scalar_registry};

struct Double;
impl ScalarFn<f32> for Double {
    fn name(&self) -> &str { "double" }
    fn arg_count(&self) -> usize { 1 }
    fn call(&self, args: &[f32]) -> f32 { args[0] * 2.0 }
}

let mut registry = scalar_registry();
registry.register(Double);

§Using f64

All functions work with f64 by specifying the type parameter:

use wick_core::Expr;
use wick_scalar::{eval, scalar_registry, FunctionRegistry};
use std::collections::HashMap;

let registry: FunctionRegistry<f64> = scalar_registry();
let expr = Expr::parse("sqrt(2)").unwrap();
let result: f64 = eval(expr.ast(), &HashMap::new(), &registry).unwrap();
assert!((result - std::f64::consts::SQRT_2).abs() < 1e-10);

Structs§

Abs
AbsInt
Integer abs: abs(x) for integer types
Acos
Asin
Atan
Atan2
Ceil
Clamp
ClampInt
Integer clamp: clamp(x, lo, hi) for integer types
Cos
Cosh
E
Euler’s number: e() = 2.71828…
Exp
Exp2
Floor
Fract
FunctionRegistry
Registry of scalar functions.
InverseLerp
Inverse lerp: inverse_lerp(a, b, v) = (v - a) / (b - a)
InverseSqrt
Lerp
Linear interpolation: lerp(a, b, t) = a + (b - a) * t
Ln
Log
Log2
Log10
Max
MaxInt
Integer max: max(a, b) for integer types
Min
MinInt
Integer min: min(a, b) for integer types
Mix
Alias for lerp (GLSL naming)
Pi
Pi constant: pi() = 3.14159…
Pow
Remap
Remap: remap(x, in_lo, in_hi, out_lo, out_hi)
Round
Saturate
Sign
SignInt
Integer sign: sign(x) for integer types
Sin
Sinh
Smoothstep
Smooth Hermite interpolation
Sqrt
Step
Step function: step(edge, x) = x < edge ? 0.0 : 1.0
Tan
Tanh
Tau
Tau constant: tau() = 2*pi = 6.28318…
Trunc
Xor
Bitwise XOR: xor(a, b)

Enums§

Error
Scalar evaluation error.

Traits§

Numeric
Trait for types that can be used as numeric values in expressions.
ScalarFn
A scalar function that can be called from expressions.

Functions§

eval
Evaluate an AST with scalar float values.
eval_int
Evaluate an AST with integer values.
register_scalar
Registers all standard scalar functions into the given registry.
register_scalar_int
Registers standard integer functions into the given registry.
scalar_registry
Creates a new registry with all standard scalar functions.
scalar_registry_int
Creates a new registry with standard integer functions.