RMath

RMath Quickstart

Learn the fundamentals of RMath in under 5 minutes. All code examples on this page have been run against the live library — outputs are exact.

1. The RMath Array

rmath.array.Array is the N-dimensional compute engine. Chained operations like .sin() and .exp() run in parallel Rust threads outside the GIL.

quickstart_array.py
import rmath.array as ra

# Create a 2×2 array and inspect it
a = ra.Array([[1.0, 2.0], [3.0, 4.0]])
print(a)
print("Mean:", a.mean())
Array([ [ 1.0000, 2.0000], [ 3.0000, 4.0000]]) Mean: 2.5

2. Working with Vectors

rmath.vector.Vector is the 1-D parallel engine. All operations are immutable — methods like .sort() return a new Vector and never modify the original.

quickstart_vector.py
import rmath.vector as rv

v = rv.Vector([5.0, 2.0, 9.0, 1.0, 5.0, 6.0])

# .sort() returns a NEW vector — v is unchanged
sorted_v = v.sort()

print(f"Original : {v}")
print(f"Sorted   : {sorted_v}")
print(f"Mean     : {v.mean():.4f}")
print(f"Std Dev  : {v.std_dev():.4f}")
print(f"Median   : {v.median()}")
Original : Vector([5.0000, 2.0000, 9.0000, 1.0000, 5.0000, 6.0000]) Sorted : Vector([1.0000, 2.0000, 5.0000, 5.0000, 6.0000, 9.0000]) Mean : 4.6667 Std Dev : 2.8752 Median : 5.0

3. Element-wise Math & Operators

quickstart_math.py
import rmath.vector as rv

a = rv.Vector([1.0, 2.0, 3.0])
b = rv.Vector([4.0, 5.0, 6.0])

# Python operators work element-wise
print("a + b :", (a + b).to_list())   # [5.0, 7.0, 9.0]
print("a * 2 :", (a * 2.0).to_list()) # [2.0, 4.0, 6.0]

# Dot product — use .dot(), not @ (@ operator not supported at runtime)
print("a · b :", a.dot(b))             # 32.0

# Trigonometry on linspace
angles = rv.Vector.linspace(0, 3.14159, 5)
print("sin   :", angles.sin())
a + b : [5.0, 7.0, 9.0] a * 2 : [2.0, 4.0, 6.0] a · b : 32.0 sin : Vector([0.0000, 0.7071, 1.0000, 0.7071, 0.0000])

4. Filtering

quickstart_filter.py
import rmath.vector as rv

v = rv.Vector([10.0, 20.0, 30.0, 40.0, 50.0])

print(v.filter_gt(25.0))  # elements > 25
print(v.filter_lt(35.0))  # elements < 35

# Zero-allocation fused multi-condition filter (no mask vectors created)
income = rv.Vector([100.0, 200.0, 50.0, 300.0, 80.0])
age    = rv.Vector([25.0,  35.0,  22.0, 45.0,  30.0])

result = age.filter_where([(income, "lt", 150.0), (age, "lt", 32.0)])
print(result)
Vector([30.0000, 40.0000, 50.0000]) Vector([10.0000, 20.0000, 30.0000]) Vector([25.0000, 22.0000, 30.0000])

5. Scaling and Clamping

quickstart_scale.py
import rmath.vector as rv

data = rv.Vector([-10.0, 5.0, 15.0, 25.0])

# Clamp elements to a specific range
print("clamp  :", data.clamp(0.0, 10.0))

# Standardize to zero-mean, unit-variance
print("z-score:", data.standardize())
clamp : Vector([0.0000, 5.0000, 10.0000, 10.0000]) z-score: Vector([-1.2558, -0.2512, 0.4186, 1.0884])
Next Steps: Explore the Vector module reference (70+ methods), Array reference, or jump to Performance Benchmarks to see rmath vs NumPy on real-world 5M-row pipelines.