quadraticEqn.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::quadraticEqn
29 
30 Description
31  Container to encapsulate various operations for
32  quadratic equation of the forms with real coefficients:
33 
34  \f[
35  a*x^2 + b*x + c = 0
36  x^2 + B*x + C = 0
37  \f]
38 
39  The expressions for the roots of \c quadraticEqn are as follows:
40 
41  \f[
42  x1 = - (b + sign(b) sqrt(b^2 - 4ac)/(2*a))
43 
44  x2 = c/(a*x1)
45  \f]
46 
47  where \c (b^2 - 4ac) is evaluated by fused multiply-adds to avoid
48  detrimental cancellation.
49 
50  Reference:
51  \verbatim
52  Cancellation-avoiding quadratic formula (tag:F):
53  Ford, W. (2014).
54  Numerical linear algebra with applications: Using MATLAB.
55  London: Elsevier/Academic Press.
56  DOI:10.1016/C2011-0-07533-6
57 
58  Kahan's algo. to compute 'b^2-a*c' using fused multiply-adds (tag:JML):
59  Jeannerod, C. P., Louvet, N., & Muller, J. M. (2013).
60  Further analysis of Kahan's algorithm for the accurate
61  computation of 2× 2 determinants.
62  Mathematics of Computation, 82(284), 2245-2264.
63  DOI:10.1090/S0025-5718-2013-02679-8
64  \endverbatim
65 
66 See also
67  Test-quadraticEqn.C
68 
69 SourceFiles
70  quadraticEqnI.H
71  quadraticEqn.C
72 
73 \*---------------------------------------------------------------------------*/
74 
75 #ifndef quadraticEqn_H
76 #define quadraticEqn_H
77 
78 #include "Roots.H"
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 /*---------------------------------------------------------------------------*\
86  Class quadraticEqn Declaration
87 \*---------------------------------------------------------------------------*/
88 
89 class quadraticEqn
90 :
91  public VectorSpace<quadraticEqn, scalar, 3>
92 {
93 public:
94 
95  //- Component labeling enumeration
96  enum components { A, B, C };
97 
98 
99  // Constructors
100 
101  //- Construct null
102  inline quadraticEqn();
103 
104  //- Construct initialized to zero
105  inline quadraticEqn(const Foam::zero);
106 
107  //- Construct from components
108  inline quadraticEqn(const scalar a, const scalar b, const scalar c);
109 
110 
111  // Member Functions
112 
113  // Access
114 
115  inline scalar a() const;
116  inline scalar b() const;
117  inline scalar c() const;
118 
119  inline scalar& a();
120  inline scalar& b();
121  inline scalar& c();
122 
123  // Evaluate
124 
125  //- Evaluate the quadratic equation at x
126  inline scalar value(const scalar x) const;
127 
128  //- Evaluate the derivative of the quadratic equation at x
129  inline scalar derivative(const scalar x) const;
130 
131  //- Estimate the error of evaluation of the quadratic equation at x
132  inline scalar error(const scalar x) const;
133 
134  //- Return the roots of the quadratic equation with no particular order
135  // if discriminant > 0: return two distinct real roots
136  // if discriminant < 0: return one of the complex conjugate-pair roots
137  // otherwise : return two identical real roots
138  Roots<2> roots() const;
139 };
140 
141 
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 
144 } // End namespace Foam
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 #include "quadraticEqnI.H"
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 #endif
153 
154 // ************************************************************************* //
Foam::quadraticEqn::A
Definition: quadraticEqn.H:95
Foam::quadraticEqn::B
Definition: quadraticEqn.H:95
Foam::quadraticEqn::roots
Roots< 2 > roots() const
Return the roots of the quadratic equation with no particular order.
Definition: quadraticEqn.C:34
Foam::quadraticEqn::b
scalar b() const
Definition: quadraticEqnI.H:61
Foam::quadraticEqn::value
scalar value(const scalar x) const
Evaluate the quadratic equation at x.
Definition: quadraticEqnI.H:91
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:56
Foam::quadraticEqn::components
components
Component labeling enumeration.
Definition: quadraticEqn.H:95
Foam::quadraticEqn::quadraticEqn
quadraticEqn()
Construct null.
Definition: quadraticEqnI.H:30
Roots.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::quadraticEqn::a
scalar a() const
Definition: quadraticEqnI.H:55
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::C
Graphite solid properties.
Definition: C.H:50
Foam::Roots
Templated storage for the roots of polynomial equations, plus flags to indicate the nature of the roo...
Definition: Roots.H:70
Foam::quadraticEqn::derivative
scalar derivative(const scalar x) const
Evaluate the derivative of the quadratic equation at x.
Definition: quadraticEqnI.H:97
Foam::quadraticEqn::c
scalar c() const
Definition: quadraticEqnI.H:67
Foam::quadraticEqn
Container to encapsulate various operations for quadratic equation of the forms with real coefficient...
Definition: quadraticEqn.H:88
Foam::quadraticEqn::error
scalar error(const scalar x) const
Estimate the error of evaluation of the quadratic equation at x.
Definition: quadraticEqnI.H:103
quadraticEqnI.H
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62