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.
import rmath.array as ra
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.
import rmath.vector as rv
v = rv.Vector([5.0, 2.0, 9.0, 1.0, 5.0, 6.0])
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
import rmath.vector as rv
a = rv.Vector([1.0, 2.0, 3.0])
b = rv.Vector([4.0, 5.0, 6.0])
print("a + b :", (a + b).to_list())
print("a * 2 :", (a * 2.0).to_list())
print("a · b :", a.dot(b))
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
import rmath.vector as rv
v = rv.Vector([10.0, 20.0, 30.0, 40.0, 50.0])
print(v.filter_gt(25.0))
print(v.filter_lt(35.0))
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
import rmath.vector as rv
data = rv.Vector([-10.0, 5.0, 15.0, 25.0])
print("clamp :", data.clamp(0.0, 10.0))
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])