Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:20:53

0001 // Copyright (c) 2025 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _MathUtils_Convergence_HeaderFile
0015 #define _MathUtils_Convergence_HeaderFile
0016 
0017 #include <MathUtils_Config.hxx>
0018 #include <MathUtils_Core.hxx>
0019 #include <math_Vector.hxx>
0020 
0021 #include <cmath>
0022 
0023 //! Modern math solver utilities.
0024 namespace MathUtils
0025 {
0026 
0027 //! Check convergence based on relative change in X.
0028 //! Uses relative tolerance scaled by current value magnitude.
0029 //! @param theXOld previous X value
0030 //! @param theXNew current X value
0031 //! @param theTolerance relative tolerance
0032 //! @return true if converged
0033 inline bool IsXConverged(double theXOld, double theXNew, double theTolerance)
0034 {
0035   const double aDiff  = std::abs(theXNew - theXOld);
0036   const double aScale = std::max(1.0, std::abs(theXNew));
0037   return aDiff < theTolerance * aScale;
0038 }
0039 
0040 //! Check convergence based on absolute function value.
0041 //! @param theFValue function value f(x)
0042 //! @param theTolerance absolute tolerance
0043 //! @return true if |f(x)| < tolerance
0044 inline bool IsFConverged(double theFValue, double theTolerance)
0045 {
0046   return std::abs(theFValue) < theTolerance;
0047 }
0048 
0049 //! Combined convergence test for scalar root finders.
0050 //! Checks both X convergence and function value convergence.
0051 //! @param theXOld previous X value
0052 //! @param theXNew current X value
0053 //! @param theFValue function value at theXNew
0054 //! @param theConfig solver configuration
0055 //! @return true if either criterion is satisfied
0056 inline bool IsConverged(double theXOld, double theXNew, double theFValue, const Config& theConfig)
0057 {
0058   return IsXConverged(theXOld, theXNew, theConfig.XTolerance)
0059          || IsFConverged(theFValue, theConfig.FTolerance);
0060 }
0061 
0062 //! Convergence test for minimization (checks both X and F change).
0063 //! @param theXOld previous X value
0064 //! @param theXNew current X value
0065 //! @param theFOld previous function value
0066 //! @param theFNew current function value
0067 //! @param theConfig solver configuration
0068 //! @return true if converged
0069 inline bool IsMinConverged(double        theXOld,
0070                            double        theXNew,
0071                            double        theFOld,
0072                            double        theFNew,
0073                            const Config& theConfig)
0074 {
0075   // X convergence
0076   if (IsXConverged(theXOld, theXNew, theConfig.XTolerance))
0077   {
0078     return true;
0079   }
0080 
0081   // Relative function change convergence
0082   const double aFDiff  = std::abs(theFNew - theFOld);
0083   const double aFScale = std::max(1.0, std::abs(theFNew));
0084   return aFDiff < theConfig.FTolerance * aFScale;
0085 }
0086 
0087 //! Convergence test for vector solvers using infinity norm.
0088 //! @param theOld previous solution vector
0089 //! @param theNew current solution vector
0090 //! @param theTolerance relative tolerance
0091 //! @return true if max|new_i - old_i| / max(1, |new_i|) < tolerance
0092 inline bool IsVectorConverged(const math_Vector& theOld,
0093                               const math_Vector& theNew,
0094                               double             theTolerance)
0095 {
0096   double aMaxDiff  = 0.0;
0097   double aMaxScale = 1.0;
0098   for (int i = theOld.Lower(); i <= theOld.Upper(); ++i)
0099   {
0100     aMaxDiff  = std::max(aMaxDiff, std::abs(theNew(i) - theOld(i)));
0101     aMaxScale = std::max(aMaxScale, std::abs(theNew(i)));
0102   }
0103   return aMaxDiff < theTolerance * aMaxScale;
0104 }
0105 
0106 //! Convergence test using gradient norm for minimization.
0107 //! @param theGradient gradient vector
0108 //! @param theTolerance tolerance for gradient norm
0109 //! @return true if ||gradient|| < tolerance
0110 inline bool IsGradientConverged(const math_Vector& theGradient, double theTolerance)
0111 {
0112   double aNormSq = 0.0;
0113   for (int i = theGradient.Lower(); i <= theGradient.Upper(); ++i)
0114   {
0115     aNormSq += Sqr(theGradient(i));
0116   }
0117   return std::sqrt(aNormSq) < theTolerance;
0118 }
0119 
0120 //! Compute infinity norm of a vector.
0121 //! @param theVector input vector
0122 //! @return max|v_i|
0123 inline double InfinityNorm(const math_Vector& theVector)
0124 {
0125   double aMax = 0.0;
0126   for (int i = theVector.Lower(); i <= theVector.Upper(); ++i)
0127   {
0128     aMax = std::max(aMax, std::abs(theVector(i)));
0129   }
0130   return aMax;
0131 }
0132 
0133 //! Compute Euclidean (L2) norm of a vector.
0134 //! @param theVector input vector
0135 //! @return sqrt(sum(v_i^2))
0136 inline double EuclideanNorm(const math_Vector& theVector)
0137 {
0138   double aSumSq = 0.0;
0139   for (int i = theVector.Lower(); i <= theVector.Upper(); ++i)
0140   {
0141     aSumSq += Sqr(theVector(i));
0142   }
0143   return std::sqrt(aSumSq);
0144 }
0145 
0146 } // namespace MathUtils
0147 
0148 #endif // _MathUtils_Convergence_HeaderFile