60 Brent(
const double epsilon, std::function<
double(
double)>
f) noexcept :
74 Brent(
const double epsilon,
const uint32 maxNumIterations, std::function<
double(
double)>
f) noexcept :
94 double solve(
double a,
double b)
101 checkAndFixAlgorithmCriteria(a, b, fa, fb);
107 double penultimateB = a;
109 bool bisection =
true;
112 if (useInverseQuadraticInterpolation(fa, fb, lastFb))
114 s = calculateInverseQuadraticInterpolation(a, b, lastB, fa, fb, lastFb);
118 s = calculateSecant(a, b, fa, fb);
121 if (useBisection(bisection, b, lastB, penultimateB, s))
123 s = calculateBisection(a, b);
132 penultimateB = lastB;
147 checkAndFixAlgorithmCriteria(a, b, fa, fb);
152 return fb < fs ? b : s;
157 const std::function<double(
double)> f_;
167 static double calculateBisection(
const double a,
const double b)
noexcept
169 return 0.5 * (a + b);
182 static double calculateSecant(
const double a,
const double b,
const double fa,
const double fb)
noexcept
186 return b - fb * (b - a) / (fb - fa);
201 static double calculateInverseQuadraticInterpolation(
const double a,
206 const double lastFb)
noexcept
208 return a * fb * lastFb / ((fa - fb) * (fa - lastFb)) + b * fa * lastFb / ((fb - fa) * (fb - lastFb)) +
209 lastB * fa * fb / ((lastFb - fa) * (lastFb - fb));
221 static bool useInverseQuadraticInterpolation(
const double fa,
const double fb,
const double lastFb)
noexcept
235 static void checkAndFixAlgorithmCriteria(
double &a,
double &b,
double &fa,
double &fb)
noexcept
238 if (std::fabs(fa) < std::fabs(fb))
256 [[nodiscard]]
bool useBisection(
const bool bisection,
259 const double penultimateB,
260 const double s)
const noexcept
266 0.5 * std::fabs(b - lastB)) ||
268 (!bisection && std::fabs(s - b) >=
269 0.5 * std::fabs(lastB - penultimateB)) ||
273 std::fabs(b - lastB) < DELTA) ||
275 (!bisection && std::fabs(lastB - penultimateB) <
static constexpr dtype max() noexcept
Definition: DtypeInfo.hpp:110
Brent(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Brent.hpp:74
double solve(double a, double b)
Definition: Brent.hpp:94
~Brent() override=default
Brent(const double epsilon, std::function< double(double)> f) noexcept
Definition: Brent.hpp:60
ABC for iteration classes to derive from.
Definition: Iteration.hpp:46
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:54
const double epsilon_
Definition: Iteration.hpp:114
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
void incrementNumberOfIterations()
Definition: Iteration.hpp:103
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: Bisection.hpp:43
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:49
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:44
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40