Math

Mathematical functions, constants, and linear algebra operations.

Constants

use std::math;

let pi = math::PI;           // 3.141592653589793
let e = math::E;             // 2.718281828459045
let tau = math::TAU;         // 6.283185307179586
let sqrt2 = math::SQRT_2;   // 1.4142135623730951

Basic Functions

use std::math;

let a = math::abs(-42.0);       // 42.0
let s = math::sqrt(144.0);      // 12.0
let p = math::pow(2.0, 10.0);   // 1024.0
let l = math::log(math::E);     // 1.0
let l2 = math::log2(1024.0);    // 10.0
let l10 = math::log10(1000.0);  // 3.0

Trigonometry

use std::math;

let s = math::sin(math::PI / 2.0);    // 1.0
let c = math::cos(0.0);                // 1.0
let t = math::tan(math::PI / 4.0);    // 1.0

let as = math::asin(1.0);             // PI/2
let ac = math::acos(0.0);             // PI/2
let at = math::atan(1.0);             // PI/4
let at2 = math::atan2(1.0, 1.0);     // PI/4

Rounding

use std::math;

let f = math::floor(3.7);    // 3.0
let c = math::ceil(3.2);     // 4.0
let r = math::round(3.5);    // 4.0
let t = math::trunc(3.9);    // 3.0

Min/Max

use std::math;

let mn = math::min(3.0, 7.0);    // 3.0
let mx = math::max(3.0, 7.0);    // 7.0
let cl = math::clamp(15.0, 0.0, 10.0);  // 10.0

Linear Algebra

use std::math::linear;

// Vector operations
let v1 = linear::Vector::new([1.0, 2.0, 3.0]);
let v2 = linear::Vector::new([4.0, 5.0, 6.0]);

let sum = v1.add(v2);
let dot = v1.dot(v2);           // 32.0
let norm = v1.norm();            // sqrt(14)
let scaled = v1.scale(2.0);

// Matrix operations
let m = linear::Matrix::identity(3);
let det = m.determinant();
let inv = m.inverse();
let product = m.multiply(m);

Complex Numbers

use std::math::complex::Complex;

let z1 = Complex::new(3.0, 4.0);   // 3 + 4i
let z2 = Complex::new(1.0, 2.0);   // 1 + 2i

let sum = z1.add(z2);               // 4 + 6i
let product = z1.mul(z2);           // -5 + 10i
let magnitude = z1.abs();           // 5.0
let conjugate = z1.conj();          // 3 - 4i

Statistics

use std::statistics;

let data = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0];

let mean = statistics::mean(data);         // 5.0
let median = statistics::median(data);     // 4.5
let stddev = statistics::std_dev(data);    // ~2.0
let variance = statistics::variance(data);

Random Numbers

use std::math::random;

let n = random::int(0, 100);       // random integer in [0, 100)
let f = random::float();            // random f64 in [0.0, 1.0)
let b = random::bool();             // random boolean