Skip to content

Commit 812adc5

Browse files
committed
fix: Add reproducible_smatrix()
Some algorithms may not converge when used on completely random values with the default value of epsilon and unlimited iterations. `reproducible_dmatrix()` already exist to circumvent this for `DMatrix`, so I implemented the same for `SMatrix`. In my tests this problem manifested itself only on `schur_decompose_4x4`, but I decided to apply similar fix for all benchmarks that also use `reproducible_dmatrix()` for `DMatrix`.
1 parent 8bec499 commit 812adc5

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

benches/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@ extern crate rand_package as rand;
66
#[macro_use]
77
extern crate criterion;
88

9-
use na::DMatrix;
9+
use na::{DMatrix, SMatrix, Scalar};
1010
use rand::Rng;
11+
use rand::distr::{Distribution, StandardUniform};
1112
use rand_isaac::IsaacRng;
1213

1314
pub mod core;
1415
pub mod geometry;
1516
pub mod linalg;
1617

18+
fn reproducible_smatrix<T, const R: usize, const C: usize>() -> SMatrix<T, R, C>
19+
where
20+
T: Scalar,
21+
StandardUniform: Distribution<T>,
22+
{
23+
use rand::SeedableRng;
24+
let mut rng = IsaacRng::seed_from_u64(0);
25+
SMatrix::from_fn(|_, _| rng.random())
26+
}
27+
1728
fn reproducible_dmatrix(nrows: usize, ncols: usize) -> DMatrix<f64> {
1829
use rand::SeedableRng;
1930
let mut rng = IsaacRng::seed_from_u64(0);

benches/linalg/schur.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use na::{Matrix4, Schur};
1+
use na::Schur;
22

33
fn schur_decompose_4x4(bh: &mut criterion::Criterion) {
44
bh.bench_function("schur_decompose_4x4", |bh| {
55
bh.iter_batched(
6-
|| Matrix4::<f64>::new_random(),
6+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
77
|m| Schur::new(m),
88
criterion::BatchSize::SmallInput,
99
)
@@ -43,7 +43,7 @@ fn schur_decompose_200x200(bh: &mut criterion::Criterion) {
4343
fn eigenvalues_4x4(bh: &mut criterion::Criterion) {
4444
bh.bench_function("eigenvalues_4x4", |bh| {
4545
bh.iter_batched_ref(
46-
|| Matrix4::<f64>::new_random(),
46+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
4747
|m| m.complex_eigenvalues(),
4848
criterion::BatchSize::SmallInput,
4949
)

benches/linalg/svd.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use na::{Matrix2, Matrix3, Matrix4, SVD};
1+
use na::SVD;
22

33
fn svd_decompose_2x2_f32(bh: &mut criterion::Criterion) {
44
bh.bench_function("svd_decompose_2x2", |bh| {
55
bh.iter_batched(
6-
|| Matrix2::<f32>::new_random(),
6+
|| crate::reproducible_smatrix::<f32, 2, 2>(),
77
|m| SVD::new_unordered(m, true, true),
88
criterion::BatchSize::SmallInput,
99
)
@@ -13,7 +13,7 @@ fn svd_decompose_2x2_f32(bh: &mut criterion::Criterion) {
1313
fn svd_decompose_3x3_f32(bh: &mut criterion::Criterion) {
1414
bh.bench_function("svd_decompose_3x3", |bh| {
1515
bh.iter_batched(
16-
|| Matrix3::<f32>::new_random(),
16+
|| crate::reproducible_smatrix::<f32, 3, 3>(),
1717
|m| SVD::new_unordered(m, true, true),
1818
criterion::BatchSize::SmallInput,
1919
)
@@ -23,7 +23,7 @@ fn svd_decompose_3x3_f32(bh: &mut criterion::Criterion) {
2323
fn svd_decompose_4x4(bh: &mut criterion::Criterion) {
2424
bh.bench_function("svd_decompose_4x4", |bh| {
2525
bh.iter_batched(
26-
|| Matrix4::<f64>::new_random(),
26+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
2727
|m| SVD::new(m, true, true),
2828
criterion::BatchSize::SmallInput,
2929
)
@@ -63,7 +63,7 @@ fn svd_decompose_200x200(bh: &mut criterion::Criterion) {
6363
fn rank_4x4(bh: &mut criterion::Criterion) {
6464
bh.bench_function("rank_4x4", |bh| {
6565
bh.iter_batched_ref(
66-
|| Matrix4::<f64>::new_random(),
66+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
6767
|m| m.rank(1.0e-10),
6868
criterion::BatchSize::SmallInput,
6969
)
@@ -103,7 +103,7 @@ fn rank_200x200(bh: &mut criterion::Criterion) {
103103
fn singular_values_4x4(bh: &mut criterion::Criterion) {
104104
bh.bench_function("singular_values_4x4", |bh| {
105105
bh.iter_batched_ref(
106-
|| Matrix4::<f64>::new_random(),
106+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
107107
|m| m.singular_values(),
108108
criterion::BatchSize::SmallInput,
109109
)
@@ -143,7 +143,7 @@ fn singular_values_200x200(bh: &mut criterion::Criterion) {
143143
fn pseudo_inverse_4x4(bh: &mut criterion::Criterion) {
144144
bh.bench_function("pseudo_inverse_4x4", |bh| {
145145
bh.iter_batched(
146-
|| Matrix4::<f64>::new_random(),
146+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
147147
|m| m.pseudo_inverse(1.0e-10),
148148
criterion::BatchSize::SmallInput,
149149
)

benches/linalg/symmetric_eigen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use na::{Matrix4, SymmetricEigen};
1+
use na::SymmetricEigen;
22

33
fn symmetric_eigen_decompose_4x4(bh: &mut criterion::Criterion) {
44
bh.bench_function("symmetric_eigen_decompose_4x4", |bh| {
55
bh.iter_batched(
6-
|| Matrix4::<f64>::new_random(),
6+
|| crate::reproducible_smatrix::<f64, 4, 4>(),
77
|m| SymmetricEigen::new(m),
88
criterion::BatchSize::SmallInput,
99
)

0 commit comments

Comments
 (0)