mlua_mathlib/lib.rs
1//! Math library for mlua — RNG, distributions, and descriptive statistics.
2//!
3//! Provides math functions that are impractical or numerically unstable
4//! to implement in pure Lua: distribution sampling with proper algorithms,
5//! independent seeded RNG instances, and numerically stable statistics.
6//!
7//! # Quick start
8//!
9//! ```rust,no_run
10//! use mlua::prelude::*;
11//!
12//! let lua = Lua::new();
13//! let math = mlua_mathlib::module(&lua).unwrap();
14//! lua.globals().set("math", math).unwrap();
15//!
16//! lua.load(r#"
17//! local rng = math.rng_create(42)
18//! print(math.normal_sample(rng, 0.0, 1.0))
19//! print(math.mean({1, 2, 3, 4, 5}))
20//! "#).exec().unwrap();
21//! ```
22
23mod distribution;
24mod rng;
25mod stats;
26
27use mlua::prelude::*;
28
29/// Create the math module table with all functions registered.
30pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
31 let t = lua.create_table()?;
32
33 rng::register(lua, &t)?;
34 distribution::register(lua, &t)?;
35 stats::register(lua, &t)?;
36
37 Ok(t)
38}