-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add ModAB root finding algorithm #21535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d2d910
Add ModAB root finding algorithm
Proektsoft-EOOD 48bc400
Change algorithm name to 'ModABRootFinder'
Proektsoft-EOOD cab5405
Fix test case for GammaCDF using GSL Brent
Proektsoft-EOOD f098eda
Update ModABRootFinder.h
Proektsoft-EOOD 98311c4
Avoid redundant re-evaluation when clamping x3
Proektsoft-EOOD c7e0c30
Removed variable initialization in constructor.
Proektsoft-EOOD 5fa2fbf
Added private variable initialization
Proektsoft-EOOD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,3 +50,4 @@ documentation/doxygen/*.dot | |
|
|
||
| # Pycache | ||
| __pycache__ | ||
| /out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // @(#)root/mathcore:$Id$ | ||
| // Authors: Nedelcho Ganchovski 03/03/2026 | ||
|
|
||
| /********************************************************************** | ||
| * * | ||
| * Copyright (c) 2026 CERN * | ||
| * All rights reserved. * | ||
| * * | ||
| * For the licensing terms see $ROOTSYS/LICENSE. * | ||
| * For the list of contributors see $ROOTSYS/README/CREDITS. * | ||
| * * | ||
| **********************************************************************/ | ||
|
|
||
| // Header for the RootFinder | ||
| // | ||
| // Created by: Nedelcho Ganchovski : Wed March 03 2026 | ||
| // | ||
|
|
||
| #ifndef ROOT_Math_ModABRootFinder | ||
| #define ROOT_Math_ModABRootFinder | ||
|
|
||
| #include "Math/IFunction.h" | ||
|
|
||
| #include "Math/IRootFinderMethod.h" | ||
|
|
||
| namespace ROOT { | ||
| namespace Math { | ||
|
|
||
| //___________________________________________________________________________________________ | ||
| /** | ||
| Class for finding the root of a one dimensional function using the ModAB algorithm. | ||
| It is based on the Modified Anderson-Björck method (2022 Ganchovski & Traykov) that | ||
| adaptively switches between bisection and regula-falsi with | ||
| side-correction, yielding superlinear convergence on well-behaved | ||
| functions while retaining the robustness of bisection. | ||
|
|
||
| @ingroup RootFinders | ||
|
|
||
| */ | ||
|
|
||
| class ModABRootFinder : public IRootFinderMethod { | ||
| public: | ||
| /** Default Constructor. */ | ||
| ModABRootFinder(); | ||
|
|
||
| /** Default Destructor. */ | ||
| ~ModABRootFinder() override {} | ||
|
|
||
| /** Set function to solve and the interval in where to look for the root. | ||
|
|
||
| \@param f Function to be minimized. | ||
| \@param xlow Lower bound of the search interval. | ||
| \@param xup Upper bound of the search interval. | ||
| */ | ||
| using IRootFinderMethod::SetFunction; | ||
| bool SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup) override; | ||
|
|
||
| /** Returns the X value corresponding to the function value fy for (xmin<x<xmax). | ||
| Method: | ||
| Modified Anderson-Björck method is applied on the bracketed interval. | ||
|
|
||
| \@param maxIter maximum number of iterations. | ||
| \@param absTol desired absolute error in the minimum position. | ||
| \@param absTol desired relative error in the minimum position. | ||
| */ | ||
| bool Solve(int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) override; | ||
|
|
||
| /** Returns root value. Need to call first Solve(). */ | ||
| double Root() const override { return fRoot; } | ||
|
|
||
| /** Returns status of last estimate. If = 0 is OK */ | ||
| int Status() const override { return fStatus; } | ||
|
|
||
| /** Return number of iteration used to find minimum */ | ||
| int Iterations() const override { return fNIter; } | ||
|
|
||
| /** Return name of root finder algorithm ("ModABRootFinder"). */ | ||
| const char *Name() const override; | ||
|
|
||
| // static function used to modify the default parameters | ||
|
|
||
| /** set number of default Npx used at construction time (when SetNpx is not called) | ||
| Default value is 100 | ||
| */ | ||
|
|
||
| private: | ||
| const IGenFunction *fFunction; ///< Pointer to the function. | ||
| int fNIter; ///< Number of iterations needed for the last estimation. | ||
| int fStatus; ///< Status of code of the last estimate | ||
| double fXMin; ///< Lower bound of the search interval. | ||
| double fXMax; ///< Upper bound of the search interval | ||
| double fRoot; ///< Current estimation of the function root. | ||
| }; | ||
|
|
||
| } // namespace Math | ||
| } // namespace ROOT | ||
|
|
||
| #endif /* ROOT_Math_ModABRootFinder */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // @(#)root/mathcore:$Id$ | ||
| // Authors: Nedelcho Ganchovski 03/03/2026 | ||
|
|
||
| /********************************************************************** | ||
| * * | ||
| * Copyright (c) 2026 CERN * | ||
| * All rights reserved. * | ||
| * * | ||
| * For the licensing terms see $ROOTSYS/LICENSE. * | ||
| * For the list of contributors see $ROOTSYS/README/CREDITS. * | ||
| * * | ||
| **********************************************************************/ | ||
|
|
||
| #include "Math/ModABRootFinder.h" | ||
| #include "Math/IFunctionfwd.h" | ||
| #include <cmath> | ||
|
|
||
| #include "Math/Error.h" | ||
|
|
||
| namespace ROOT { | ||
| namespace Math { | ||
|
|
||
| ModABRootFinder::ModABRootFinder() | ||
| : fFunction(nullptr), fNIter(0), fStatus(-1), fXMin(0), fXMax(0), fRoot(0) | ||
|
Proektsoft-EOOD marked this conversation as resolved.
Outdated
|
||
| { | ||
|
|
||
| } | ||
|
|
||
| bool ModABRootFinder::SetFunction(const ROOT::Math::IGenFunction &f, double xmin, double xmax) | ||
| { | ||
| // Set function to solve and the interval in where to look for the root. | ||
|
|
||
| fFunction = &f; | ||
| // invalid previous status | ||
| fStatus = -1; | ||
| if (xmin > xmax) { | ||
| fXMin = xmax; | ||
| fXMax = xmin; | ||
| } | ||
| else { | ||
| fXMin = xmin; | ||
| fXMax = xmax; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| const char *ModABRootFinder::Name() const | ||
| { | ||
| return "ModABRootFinder"; | ||
| } | ||
|
|
||
| int sign(double x) { | ||
|
silverweed marked this conversation as resolved.
|
||
| return (x > 0) - (x < 0); | ||
| } | ||
|
|
||
| bool ModABRootFinder::Solve(int maxIter, double absTol, double relTol) | ||
| { | ||
| // Returns the X value corresponding to the function value fy for (xmin<x<xmax). | ||
|
|
||
| if (!fFunction) { | ||
| MATH_ERROR_MSG("ModABRootFinder::Solve", "Function has not been set"); | ||
| return false; | ||
| } | ||
| double x1 = fXMin, y1 = (*fFunction)(x1); | ||
| if (y1 == 0.0) { | ||
| fRoot = x1; | ||
| fStatus = 0; | ||
| return true; | ||
| } | ||
| double x2 = fXMax, y2 = (*fFunction)(x2); | ||
| if (y2 == 0.0) { | ||
| fRoot = x2; | ||
| fStatus = 0; | ||
| return true; | ||
| } | ||
| if (sign(y1) == sign(y2)) { | ||
| MATH_ERROR_MSG("ModABRootFinder::Solve", "Function values at the interval endpoints have the same sign"); | ||
| return false; | ||
| } | ||
| int side = 0; | ||
| bool bisection = true; | ||
| double threshold = x2 - x1; | ||
| const double C = 16; | ||
| for (int i = 1; i <= maxIter; ++i) { | ||
| double x3, y3; | ||
| if (bisection) { | ||
| x3 = (x1 + x2) / 2.0; | ||
| y3 = (*fFunction)(x3); | ||
| double ym = (y1 + y2) / 2.0; | ||
| double r = 1 - std::fabs(ym / (y2 - y1)); | ||
| double k = r * r; | ||
| if (std::fabs(ym - y3) < k * (std::fabs(y3) + std::fabs(ym))) { | ||
| bisection = false; | ||
| threshold = (x2 - x1) * C; | ||
| } | ||
| } else { | ||
| x3 = (x1 * y2 - y1 * x2) / (y2 - y1); | ||
| // Clamp x3 when floating point round-off errors shoots it out of the bracketing interval. Assign also y values to avoid redundant re-evaluation | ||
| if (x3 <= x1) | ||
| { | ||
| x3 = x1; | ||
| y3 = y1; | ||
| } | ||
| else if (x3 >= x2) | ||
| { | ||
| x3 = x2; | ||
| y3 = y2; | ||
| } | ||
| else | ||
| y3 = (*fFunction)(x3); | ||
|
|
||
| threshold /= 2.0; | ||
| } | ||
| fRoot = x3; | ||
| fNIter = i; | ||
| double eps = absTol + relTol * std::abs(x3); | ||
| if (std::fabs(y3) == 0.0 || x2 - x1 <= eps) { | ||
| fStatus = 0; | ||
| return true; | ||
| } | ||
| if (y1 * y3 > 0.0) { | ||
| if (side == 1) { | ||
| double m = 1 - y3 / y1; | ||
| if (m <= 0) | ||
| y2 /= 2; | ||
| else | ||
| y2 *= m; | ||
| } else if (!bisection) | ||
| side = 1; | ||
|
|
||
| x1 = x3; | ||
| y1 = y3; | ||
| } else { | ||
| if (side == -1) { | ||
| double m = 1 - y3 / y2; | ||
| if (m <= 0) | ||
| y1 /= 2; | ||
| else | ||
| y1 *= m; | ||
| } else if (!bisection) | ||
| side = -1; | ||
|
|
||
| x2 = x3; | ||
| y2 = y3; | ||
| } | ||
| if (x2 - x1 > threshold) | ||
| { | ||
| bisection = true; | ||
| side = 0; | ||
| } | ||
| } | ||
| fNIter = maxIter; | ||
| MATH_ERROR_MSG("ModABRootFinder::Solve", "Search didn't converge"); | ||
| fStatus = -2; | ||
| return false; | ||
| } | ||
| } // namespace Math | ||
| } // namespace ROOT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.