Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:23:45

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 // Project include(s)
0010 #include "detray/utils/root_finding.hpp"
0011 
0012 // Detray test include(s)
0013 #include "detray/test/framework/types.hpp"
0014 
0015 // GTest include(s)
0016 #include <gtest/gtest.h>
0017 
0018 // System include(s)
0019 #include <utility>
0020 
0021 using namespace detray;
0022 
0023 using scalar = test::scalar;
0024 
0025 namespace {
0026 
0027 // f(s) = (s + 2) * ((s - 11)^2 + 1): single root at s = -2, f(0) = 244
0028 auto evaluate_func = [](const scalar s) {
0029   const scalar q{(s - 11.f) * (s - 11.f) + 1.f};
0030   return std::make_pair((s + 2.f) * q, q + 2.f * (s + 2.f) * (s - 11.f));
0031 };
0032 
0033 constexpr scalar tol{1e-3f};
0034 
0035 }  // namespace
0036 
0037 // A start value of exactly 0 must not be returned as root unchecked
0038 GTEST_TEST(detray_utils, newton_raphson_start_at_zero) {
0039   auto func = evaluate_func;
0040   const auto [s, err] = newton_raphson(func, scalar{0.f});
0041 
0042   EXPECT_NEAR(s, -2.f, tol);
0043 }
0044 
0045 // Starting at s = 10, the bracket expands from [8, 12] to [-16, 16], so the
0046 // iteration starts at its midpoint s = 0
0047 GTEST_TEST(detray_utils, newton_raphson_safe_bracket_midpoint_at_zero) {
0048   auto func = evaluate_func;
0049 
0050   darray<scalar, 2> br{};
0051   auto f = [&func](const scalar x) { return func(x).first; };
0052   ASSERT_TRUE(expand_bracket(scalar{8.f}, scalar{12.f}, f, br));
0053   ASSERT_EQ(br[0], -16.f);
0054   ASSERT_EQ(br[1], 16.f);
0055 
0056   const auto [s, err] = newton_raphson_safe(func, scalar{10.f});
0057 
0058   EXPECT_NEAR(s, -2.f, tol);
0059 }