MathRoot Package

The MathRoot package provides a collection of root finding algorithms for scalar functions. These algorithms are designed to find solutions to the equation f(x) = 0 with various convergence guarantees and performance characteristics.

Overview

This package contains header-only template implementations of classical root finding methods, suitable for use with any functor that provides the required interface.

Available Algorithms

Single Root Finders

AlgorithmHeaderRequirementsConvergence
Newton-RaphsonMathRoot_Newton.hxxValue + DerivativeQuadratic
Newton BoundedMathRoot_Newton.hxxValue + DerivativeQuadratic (with safety)
Brent's MethodMathRoot_Brent.hxxValue onlySuperlinear
BisectionMathRoot_Bisection.hxxValue onlyLinear
Bisection-NewtonMathRoot_Bisection.hxxValue + DerivativeQuadratic (with safety)
SecantMathRoot_Secant.hxxValue onlySuperlinear (~1.618)

Multiple Root Finders

AlgorithmHeaderDescription
FindAllRootsMathRoot_Multiple.hxxFinds all roots in an interval using sampling
FindAllRootsWithDerivativeMathRoot_Multiple.hxxSame as above, with extrema detection
FindAllRootsWithIntervalsMathRoot_All.hxxFinds roots and null intervals

Specialized Solvers

AlgorithmHeaderDescription
TrigonometricMathRoot_Trig.hxxSolves trigonometric equations

Usage

Function Interface

Functions must provide one of the following interfaces:

// For value-only methods (Brent, Bisection, Secant)
class MyFunction
{
public:
  bool Value(double theX, double& theF) const;
};

// For methods requiring derivatives (Newton)
class MyFunctionWithDerivative
{
public:
  bool Values(double theX, double& theF, double& theDF) const;
};

Example: Newton's Method

#include <MathRoot_Newton.hxx>

class QuadraticFunction
{
public:
  // f(x) = x^2 - 2 (roots at +/- sqrt(2))
  bool Values(double theX, double& theF, double& theDF) const
  {
    theF = theX * theX - 2.0;
    theDF = 2.0 * theX;
    return true;
  }
};

void Example()
{
  QuadraticFunction aFunc;
  MathUtils::Config aConfig;
  aConfig.XTolerance = 1e-10;
  aConfig.FTolerance = 1e-10;

  auto aResult = MathRoot::Newton(aFunc, 1.0, aConfig);
  if (aResult.IsDone())
  {
    double aRoot = *aResult.Root; // approximately sqrt(2)
  }
}

Example: Finding All Roots

#include <MathRoot_Multiple.hxx>

class SineFunction
{
public:
  bool Value(double theX, double& theF) const
  {
    theF = std::sin(theX);
    return true;
  }
};

void Example()
{
  SineFunction aFunc;
  MathRoot::MultipleConfig aConfig;
  aConfig.NbSamples = 100;

  auto aResult = MathRoot::FindAllRoots(aFunc, 0.0, 10.0, aConfig);
  if (aResult.IsDone())
  {
    // aResult.Roots contains: 0, PI, 2*PI, 3*PI
    for (int i = 0; i < aResult.NbRoots(); ++i)
    {
      double aRoot = aResult[i];
    }
  }
}

Algorithm Selection Guide

  • Newton: Best for smooth functions when derivative is available and a good initial guess exists
  • Newton Bounded: Use when Newton might diverge; provides safety with bounds
  • Brent: Reliable choice when only function values are available; guaranteed convergence
  • Bisection: Most robust but slowest; use when reliability is paramount
  • Bisection-Newton: Good balance of robustness and speed
  • Secant: Good alternative to Newton when derivatives are expensive to compute

Dependencies

  • MathUtils_Types.hxx: Common types (Status, ScalarResult)
  • MathUtils_Config.hxx: Solver configuration
  • MathUtils_Core.hxx: Core utilities (IsZero, Clamp, etc.)
  • MathUtils_Convergence.hxx: Convergence testing utilities
  • MathPoly_Poly.hxx: Polynomial solvers (for trigonometric equations)