Skip to content

dew-linalg

Linear algebra types and operations for dew expressions.

Installation

toml
[dependencies]
rhizome-dew-core = "0.1"
rhizome-dew-linalg = "0.1"

# Features
rhizome-dew-linalg = { version = "0.1", features = ["3d", "4d", "wgsl"] }

Features

FeatureDescriptionDefault
3dVec3, Mat3 typesYes
4dVec4, Mat4 typesNo
wgslWGSL backendNo
luaLua backendNo
craneliftCranelift JIT backendNo

Basic Usage

rust
use rhizome_dew_core::Expr;
use rhizome_dew_linalg::{Value, eval, linalg_registry};
use std::collections::HashMap;

// Parse an expression
let expr = Expr::parse("a + b").unwrap();

// Set up vector variables
let mut vars: HashMap<String, Value<f32>> = HashMap::new();
vars.insert("a".to_string(), Value::Vec2([1.0, 2.0]));
vars.insert("b".to_string(), Value::Vec2([3.0, 4.0]));

// Evaluate
let registry = linalg_registry();
let result = eval(expr.ast(), &vars, &registry).unwrap();
assert_eq!(result, Value::Vec2([4.0, 6.0]));

Types

Value Enum

rust
pub enum Value<T> {
    Scalar(T),
    Vec2([T; 2]),
    Vec3([T; 3]),    // feature = "3d"
    Vec4([T; 4]),    // feature = "4d"
    Mat2([T; 4]),    // column-major
    Mat3([T; 9]),    // feature = "3d", column-major
    Mat4([T; 16]),   // feature = "4d", column-major
}

Type Enum

rust
pub enum Type {
    Scalar,
    Vec2,
    Vec3,  // feature = "3d"
    Vec4,  // feature = "4d"
    Mat2,
    Mat3,  // feature = "3d"
    Mat4,  // feature = "4d"
}

Operators

Arithmetic

ExpressionResult
vec + vecComponent-wise addition
vec - vecComponent-wise subtraction
vec * scalarScale vector
scalar * vecScale vector
vec / scalarDivide components
-vecNegate components

Matrix Operations

ExpressionResult
mat * vecMatrix-vector multiply (column vector)
vec * matVector-matrix multiply (row vector)
mat * matMatrix multiplication
mat * scalarScale matrix

Functions

Vector Functions

FunctionDescriptionResult
dot(a, b)Dot productScalar
cross(a, b)Cross product (3D only)Vec3
length(v)Vector lengthScalar
normalize(v)Unit vectorSame as input
distance(a, b)Distance between pointsScalar
reflect(i, n)Reflect incident vectorSame as input
hadamard(a, b)Element-wise multiplySame as input
lerp(a, b, t)Linear interpolationSame as a, b
mix(a, b, t)Linear interpolation (GLSL naming)Same as a, b

Examples

Vector Math

rust
// Dot product
let expr = Expr::parse("dot(a, b)").unwrap();
vars.insert("a".to_string(), Value::Vec2([1.0, 0.0]));
vars.insert("b".to_string(), Value::Vec2([0.0, 1.0]));
let result = eval(expr.ast(), &vars, &registry).unwrap();
// result = Scalar(0.0) - perpendicular vectors

Matrix Transform

rust
// Transform a vector by a matrix
let expr = Expr::parse("m * v").unwrap();
vars.insert("m".to_string(), Value::Mat2([
    1.0, 0.0,  // column 0
    0.0, 1.0,  // column 1 (identity)
]));
vars.insert("v".to_string(), Value::Vec2([3.0, 4.0]));
let result = eval(expr.ast(), &vars, &registry).unwrap();
// result = Vec2([3.0, 4.0])

Normalization

rust
let expr = Expr::parse("normalize(v)").unwrap();
vars.insert("v".to_string(), Value::Vec2([3.0, 4.0]));
let result = eval(expr.ast(), &vars, &registry).unwrap();
// result = Vec2([0.6, 0.8]) - unit vector

Generic Numeric Types

Works with floating-point (f32, f64) or integer (i32, i64) types:

rust
use rhizome_dew_linalg::{eval, linalg_registry, linalg_registry_int, Value};

// f64 precision (full function set)
let mut vars: HashMap<String, Value<f64>> = HashMap::new();
vars.insert("v".to_string(), Value::Vec2([3.0, 4.0]));
let result: Value<f64> = eval(expr.ast(), &vars, &linalg_registry()).unwrap();

// Integer vectors (Numeric-only functions)
let mut int_vars: HashMap<String, Value<i32>> = HashMap::new();
int_vars.insert("a".to_string(), Value::Vec2([1, 2]));
int_vars.insert("b".to_string(), Value::Vec2([3, 4]));
let result: Value<i32> = eval(expr.ast(), &int_vars, &linalg_registry_int()).unwrap();

Integer-specific Notes

  • linalg_registry_int() provides functions that work with integers:
    • dot, cross, hadamard, lerp, mix
    • Constructors: vec2, vec3, vec4, mat2, mat3, mat4
    • Extractors: x, y, z, w
  • Float-only functions (length, normalize, distance, reflect) are not available
  • Use case: integer grid coordinates, euclidean rhythms, pixel operations

Backends

See WGSL, Lua, Cranelift for backend-specific usage.