Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:55

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2018 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 ///////////////////////////////////////////////////////////////////
0010 // RealQuadraticEquation.h, Acts project
0011 ///////////////////////////////////////////////////////////////////
0012 
0013 #pragma once
0014 #include <cmath>
0015 #include <utility>
0016 
0017 namespace Acts::detail {
0018 
0019 /// @struct RealQuadradicEquation
0020 ///   Mathematic struct for solving real quadratic equations
0021 ///
0022 ///  <b>Mathematical motivation</b>:<br>
0023 ///  The equation is given by:<br>
0024 ///  @f$ \alpha x^{2} + \beta x + \gamma = 0  @f$
0025 ///  and has therefore the analytical solution:<br>
0026 ///  @f$ x_{1, 2} = - \frac{\beta \pm
0027 ///  \sqrt{\beta^{2}-4\alpha\gamma}}{2\alpha}@f$
0028 /// <br>
0029 /// <br>
0030 ///  - case @f$ \beta > 0 @f$:<br>
0031 ///  @f$ x_{1} = - \frac{\beta + \sqrt{\beta^{2}-4\alpha\gamma}}{2\alpha}  :=
0032 /// \frac{q}{\alpha}@f$, <br>
0033 ///  so that @f$ q= -\frac{1}{2}(\beta+sqrt{\beta^{2}-4\alpha\gamma})@f$.
0034 ///  @f$ x_{2} @f$ can now be written as: @f$ x_{2} = \frac{\gamma}{q} =
0035 /// -\frac{2\gamma}{\beta+sqrt{\beta^{2}-4\alpha\gamma}}@f$, since: <br>
0036 ///  @f$ -\frac{2\gamma}{\beta+sqrt{\beta^{2}-4\alpha\gamma}} =
0037 /// -\frac{2\gamma}{\beta}\frac{1}{1+\sqrt{1-4\alpha\gamma/\beta^{2}}}@f$, and
0038 /// <br>
0039 ///  @f$ x_{2}\frac{1}{1-\sqrt{1-4\alpha\gamma/\beta^{2}}} =
0040 /// -\frac{2\gamma}{\beta}\frac{1}{1-1+4\alpha\gamma/\beta^{2}}=-\frac{\beta}{2\alpha}.@f$<br>
0041 ///  Hence,@f$ -\frac{\beta(1-\sqrt{1-4\alpha\gamma/\beta^{2}}}{2\alpha} = -
0042 /// \frac{\beta - \sqrt{\beta^{2}-4\alpha\gamma}}{2\alpha} @f$.<br>
0043 ///  - case @f$ \beta > 0 @f$ is similar.
0044 ///
0045 
0046 struct RealQuadraticEquation {
0047   double first = 0;
0048   double second = 0;
0049   int solutions = 0;
0050 
0051   /// Constructor
0052   ///
0053   /// @param alpha is the first parameter of the quad equation
0054   /// @param beta is the second parameter of the quad equation
0055   /// @param gamma is the third parameter of the quad equation
0056   RealQuadraticEquation(double alpha, double beta, double gamma) {
0057     double discriminant = beta * beta - 4 * alpha * gamma;
0058     if (discriminant >= 0) {
0059       solutions = (discriminant == 0) ? 1 : 2;
0060       double q = -0.5 * (beta + (beta > 0 ? std::sqrt(discriminant)
0061                                           : -std::sqrt(discriminant)));
0062       first = q / alpha;
0063       second = gamma / q;
0064     }
0065   }
0066 };
0067 
0068 }  // namespace Acts::detail