wick-scalar 0.1.0

Standard scalar function library for wick expressions
Documentation

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

Feature Description
wgsl WGSL shader code generation
lua-codegen Lua code generation (pure Rust, WASM-safe)
lua Lua codegen + mlua execution
cranelift Cranelift JIT compilation

Available Functions

Constants

Function Description
pi() π ≈ 3.14159
e() Euler's number ≈ 2.71828
tau() τ = 2π ≈ 6.28318

Trigonometric

Function Description
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

Function Description
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

Function Description
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

Function Description
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);