Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:31

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Utilities/AlgebraHelpers.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 
0015 #include <Eigen/Dense>
0016 
0017 Acts::Logging::Level logLevel = Acts::Logging::VERBOSE;
0018 
0019 namespace Acts {
0020 namespace Test {
0021 
0022 BOOST_AUTO_TEST_SUITE(AlgebraHelpers)
0023 
0024 ACTS_LOCAL_LOGGER(Acts::getDefaultLogger("SafeInverse", logLevel))
0025 
0026 BOOST_AUTO_TEST_CASE(SafeInverseSmallMatrix) {
0027   Eigen::Matrix<double, 2, 2> m;
0028   m << 1, 2, 3, 4;
0029 
0030   Eigen::Matrix<double, 2, 2> mInvRef;
0031   mInvRef << -2, 1, 1.5, -0.5;
0032 
0033   auto mInv = Acts::safeInverse(m);
0034 
0035   BOOST_CHECK(mInv);
0036   BOOST_CHECK_EQUAL(*mInv, mInvRef);
0037 
0038   Eigen::Matrix<int, 2, 2> identityInt;
0039   identityInt << 1, 0, 0, 1;
0040   auto identityIntInv = Acts::safeInverse(identityInt);
0041 
0042   BOOST_CHECK(identityIntInv);
0043   BOOST_CHECK_EQUAL(*identityIntInv, identityInt);
0044 }
0045 
0046 BOOST_AUTO_TEST_CASE(safeInverseLargeMatrix) {
0047   const Eigen::Matrix<double, 5, 5> identity{Eigen::MatrixXd::Identity(5, 5)};
0048   auto identityInv = Acts::safeInverse(identity);
0049 
0050   BOOST_CHECK(identityInv);
0051   BOOST_CHECK_EQUAL(*identityInv, identity);
0052 }
0053 
0054 BOOST_AUTO_TEST_CASE(SafeInverseBadSmallMatrix) {
0055   Eigen::Matrix<double, 2, 2> m;
0056   m << 1, 1, 2, 2;
0057 
0058   auto mInv = Acts::safeInverse(m);
0059 
0060   BOOST_CHECK(!mInv);
0061 }
0062 
0063 BOOST_AUTO_TEST_CASE(safeInverseBadLargeMatrix) {
0064   const Eigen::Matrix<double, 5, 5> m{Eigen::MatrixXd::Zero(5, 5)};
0065   auto mInv = Acts::safeInverse(m);
0066 
0067   BOOST_CHECK(!mInv);
0068 }
0069 
0070 BOOST_AUTO_TEST_CASE(SafeInverseFPESmallMatrix) {
0071   Eigen::Matrix<double, 4, 4> m = Eigen::MatrixXd::Identity(4, 4) * SIZE_MAX;
0072   m(1, 1) = 1;
0073 
0074   auto mInv = Acts::safeInverse(m);
0075   auto mInvInv = Acts::safeInverse(*mInv);
0076 
0077   BOOST_CHECK(mInv);
0078   BOOST_CHECK(!mInvInv);
0079 
0080   ACTS_VERBOSE("Test: SafeInverseFPESmallMatrix"
0081                << "\n"
0082                << "m:\n"
0083                << m << "\n"
0084                << "mInv:\n"
0085                << *mInv << "\n"
0086                << "mInvInv [garbage]:\n"
0087                << *mInvInv);
0088 }
0089 
0090 BOOST_AUTO_TEST_CASE(SafeInverseFPELargeMatrix) {
0091   Eigen::Matrix<double, 5, 5> m = Eigen::MatrixXd::Identity(5, 5) * SIZE_MAX;
0092   m(1, 1) = 1;
0093 
0094   auto mInv = Acts::safeInverse(m);
0095 
0096   BOOST_CHECK(!mInv);
0097 
0098   ACTS_VERBOSE("Test: SafeInverseFPELargeMatrix"
0099                << "\n"
0100                << "m:\n"
0101                << m << "\n"
0102                << "mInv [garbage]:\n"
0103                << *mInv);
0104 }
0105 
0106 /// This test should not compile
0107 // BOOST_AUTO_TEST_CASE(SafeInverseNonsquareMatrix) {
0108 //   Eigen::Matrix<double, 2, 3> m;
0109 //   m << 1, 2, 3, 4, 5, 6;
0110 //
0111 //   auto mInv = Acts::safeInverse(m);
0112 // }
0113 
0114 /// This test should not compile
0115 // BOOST_AUTO_TEST_CASE(SafeInverseDynamicMatrix) {
0116 //   Eigen::MatrixXd m{Eigen::MatrixXd::Identity(2, 2)};
0117 //
0118 //   auto mInv = Acts::safeInverse(m);
0119 // }
0120 
0121 BOOST_AUTO_TEST_SUITE_END()
0122 
0123 }  // namespace Test
0124 }  // namespace Acts