A high-speed native numerical library.
let x = Vector::arange(6);
let y = Vector::arange(6);
inspect(x + y, content="[0, 2, 4, 6, 8, 10]");
inspect(x - y, content="[0, 0, 0, 0, 0, 0]");
inspect(x * y, content="[0, 1, 4, 9, 16, 25]");
inspect(x / y, content="[NaN, 1, 1, 1, 1, 1]");let x = Vector::arange(6);
let y = Vector::arange(6);
inspect(x.add(y), content="[0, 2, 4, 6, 8, 10]");
inspect(x.add(1.F), content="[1, 3, 5, 7, 9, 11]");
inspect(x.add(1), content="[2, 4, 6, 8, 10, 12]");let x = Vector::from_array([2, 5, 7, 8, 9, 3]);
let y = x.clone();
y.sub(1).ignore(); // Changes `y`, but `x` will not change.
inspect(x, content="[2, 5, 7, 8, 9, 3]");let x = Vector::from_array([2, 5, 7, 8, 9, 3]);
let y = x;
y.mul(2).ignore(); // Now `y` and `x` will both change.
inspect(x, content="[4, 10, 14, 16, 18, 6]");let x = Matrix::from_array([[1, 2], [3, 4]]);
let y = Matrix::from_array([[5, 6], [7, 8]]);
inspect(x.rows(), content="2");
inspect(x.cols(), content="2");
inspect(x + y, content="[[6, 8], [10, 12]]");
inspect(x - y, content="[[-4, -4], [-4, -4]]");
inspect(x * y, content="[[5, 12], [21, 32]]");
// Float will lose precision at about 7th digit.
inspect(x / y, content="[[0.20000000298023224, 0.3333333432674408], [0.4285714328289032, 0.5]]");let x = Matrix::from_array([[1, 2, 3], [4, 5, 6]]);
let y = Matrix::from_array([[7, 8], [9, 10], [11, 12]]);
inspect(x.mmul(y), content="[[58, 64], [139, 154]]");
// `x` and `y` remain unchangedlet x = Matrix::from_array([[1, 2, 3], [4, 5, 6]]);
inspect(x[0], content="[1, 2, 3]");
inspect(x[1], content="[4, 5, 6]");
// As x[0] is a view of x, changing x[0] will also change x.
x[0].sub(x[1]).ignore();
inspect(x, content="[[-3, -3, -3], [4, 5, 6]]");let x = Matrix::from_array([[1, 2, 3], [4, 5, 6]]);
inspect(x.sum(), content="21");
inspect(x.sum_rows(), content="[5, 7, 9]");test "benchmark_add" {
let summary = @bench.single_bench(fn () {
let n = 1048576;
let c = Vector::zeroes(n);
let a = Vector::arange(n.to_float());
for i = 0; i < 1000; i = i + 1 {
c.add(a).ignore();
}
println(c.sum());
});
println(summary.to_json().stringify());
}test "benchmark_dot" {
let x = Vector::linspace(0.2795, 6.8704, 1048576);
let y = Vector::linspace(-1.6269, 3.5057, 1048576);
let mut sum = 0.F;
let summary = @bench.single_bench(fn () {
sum += x.dot(y);
});
println(sum);
println(summary.to_json().stringify());
}test "benchmark_mmul" {
let farr = Array::makei(500, fn (i) {
Array::makei(500, fn (j) {
return i.to_float() * 1.028F - j.to_float() * 0.906F;
})
});
let garr = Array::makei(500, fn (i) {
Array::makei(500, fn (j) {
return i.to_float() * -0.775F + j.to_float() * 1.313F;
})
});
let x = Matrix::from_array(farr);
let y = Matrix::from_array(garr);
let mut sum = 0.F;
let summary = @bench.single_bench(fn () {
sum = x.mmul(y).sum();
});
println(sum);
println(summary.to_json().stringify());
}