// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2026 Rasmus Munk Larsen (rmlarsen@gmail.com) // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // Benchmarks for reciprocal condition number estimation. // // Times the rcond() call on pre-computed factorizations. // The rcond estimator is O(n^2), so this isolates its cost from the O(n^3) factorization. #include #include using namespace Eigen; // --- PartialPivLU --- template static void BM_PartialPivLU_Rcond(benchmark::State& state) { const Index n = state.range(0); using Mat = Matrix; Mat A = Mat::Random(n, n); A.diagonal().array() += Scalar(2 * n); PartialPivLU lu(A); for (auto _ : state) { auto rc = lu.rcond(); benchmark::DoNotOptimize(rc); } state.SetItemsProcessed(state.iterations()); } // --- FullPivLU --- template static void BM_FullPivLU_Rcond(benchmark::State& state) { const Index n = state.range(0); using Mat = Matrix; Mat A = Mat::Random(n, n); A.diagonal().array() += Scalar(2 * n); FullPivLU lu(A); for (auto _ : state) { auto rc = lu.rcond(); benchmark::DoNotOptimize(rc); } state.SetItemsProcessed(state.iterations()); } // --- LLT --- template static void BM_LLT_Rcond(benchmark::State& state) { const Index n = state.range(0); using Mat = Matrix; Mat A = Mat::Random(n, n); Mat SPD = A.adjoint() * A + Mat::Identity(n, n); LLT llt(SPD); for (auto _ : state) { auto rc = llt.rcond(); benchmark::DoNotOptimize(rc); } state.SetItemsProcessed(state.iterations()); } // --- Size configurations --- // clang-format off BENCHMARK(BM_PartialPivLU_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("PartialPivLU_Rcond_float"); BENCHMARK(BM_PartialPivLU_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("PartialPivLU_Rcond_double"); BENCHMARK(BM_FullPivLU_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("FullPivLU_Rcond_float"); BENCHMARK(BM_FullPivLU_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("FullPivLU_Rcond_double"); BENCHMARK(BM_LLT_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("LLT_Rcond_float"); BENCHMARK(BM_LLT_Rcond)->Arg(8)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Name("LLT_Rcond_double"); // clang-format on