Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-04 09:15:11

0001 // Created on: 1991-05-14
0002 // Created by: Laurent PAINNOT
0003 // Copyright (c) 1991-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _math_FunctionSetRoot_HeaderFile
0018 #define _math_FunctionSetRoot_HeaderFile
0019 
0020 #include <math_IntegerVector.hxx>
0021 #include <math_Matrix.hxx>
0022 #include <math_Vector.hxx>
0023 #include <Standard_OStream.hxx>
0024 #include <Standard_DimensionError.hxx>
0025 #include <StdFail_NotDone.hxx>
0026 
0027 class math_FunctionSetWithDerivatives;
0028 
0029 //! The math_FunctionSetRoot class calculates the root
0030 //! of a set of N functions of M variables (N<M, N=M or N>M). Knowing
0031 //! an initial guess of the solution and using a minimization algorithm, a search
0032 //! is made in the Newton direction and then in the Gradient direction if there
0033 //! is no success in the Newton direction. This algorithm can also be
0034 //! used for functions minimization. Knowledge of all the partial
0035 //! derivatives (the Jacobian) is required.
0036 class math_FunctionSetRoot
0037 {
0038 public:
0039   DEFINE_STANDARD_ALLOC
0040 
0041   //! is used in a sub-class to initialize correctly all the fields
0042   //! of this class.
0043   //! The range (1, F.NbVariables()) must be especially
0044   //! respected for all vectors and matrix declarations.
0045   Standard_EXPORT math_FunctionSetRoot(math_FunctionSetWithDerivatives& F,
0046                                        const math_Vector&               Tolerance,
0047                                        const int                        NbIterations = 100);
0048 
0049   //! is used in a sub-class to initialize correctly all the fields
0050   //! of this class.
0051   //! The range (1, F.NbVariables()) must be especially
0052   //! respected for all vectors and matrix declarations.
0053   //! The method SetTolerance must be called after this
0054   //! constructor.
0055   Standard_EXPORT math_FunctionSetRoot(math_FunctionSetWithDerivatives& F,
0056                                        const int                        NbIterations = 100);
0057 
0058   //! Destructor
0059   Standard_EXPORT virtual ~math_FunctionSetRoot();
0060 
0061   //! Initializes the tolerance values.
0062   Standard_EXPORT void SetTolerance(const math_Vector& Tolerance);
0063 
0064   //! This routine is called at the end of each iteration
0065   //! to check if the solution was found. It can be redefined
0066   //! in a sub-class to implement a specific test to stop the iterations.
0067   //! In this case, the solution is found when: abs(Xi - Xi-1) <= Tolerance
0068   //! for all unknowns.
0069   virtual bool IsSolutionReached(math_FunctionSetWithDerivatives&)
0070   {
0071     for (int i = 1; i <= Sol.Length(); ++i)
0072     {
0073       if (std::abs(Delta(i)) > Tol(i))
0074       {
0075         return false;
0076       }
0077     }
0078     return true;
0079   }
0080 
0081   //! Improves the root of function from the initial guess point.
0082   //! The infinum and supremum may be given to constrain the solution.
0083   //! In this case, the solution is found when: abs(Xi - Xi-1)(j) <= Tolerance(j)
0084   //! for all unknowns.
0085   Standard_EXPORT void Perform(math_FunctionSetWithDerivatives& theFunction,
0086                                const math_Vector&               theStartingPoint,
0087                                const bool                       theStopOnDivergent = false);
0088 
0089   //! Improves the root of function from the initial guess point.
0090   //! The infinum and supremum may be given to constrain the solution.
0091   //! In this case, the solution is found when: abs(Xi - Xi-1) <= Tolerance
0092   //! for all unknowns.
0093   Standard_EXPORT void Perform(math_FunctionSetWithDerivatives& theFunction,
0094                                const math_Vector&               theStartingPoint,
0095                                const math_Vector&               theInfBound,
0096                                const math_Vector&               theSupBound,
0097                                const bool                       theStopOnDivergent = false);
0098 
0099   //! Returns true if the computations are successful, otherwise returns false.
0100   bool IsDone() const { return Done; }
0101 
0102   //! Returns the number of iterations really done
0103   //! during the computation of the root.
0104   //! Exception NotDone is raised if the root was not found.
0105   int NbIterations() const
0106   {
0107     StdFail_NotDone_Raise_if(!Done, " ");
0108     return Kount;
0109   }
0110 
0111   //! returns the stateNumber (as returned by
0112   //! F.GetStateNumber()) associated to the root found.
0113   int StateNumber() const
0114   {
0115     StdFail_NotDone_Raise_if(!Done, " ");
0116     return State;
0117   }
0118 
0119   //! Returns the value of the root of function F.
0120   //! Exception NotDone is raised if the root was not found.
0121   const math_Vector& Root() const
0122   {
0123     StdFail_NotDone_Raise_if(!Done, " ");
0124     return Sol;
0125   }
0126 
0127   //! Outputs the root vector in Root.
0128   //! Exception NotDone is raised if the root was not found.
0129   //! Exception DimensionError is raised if the range of Root
0130   //! is not equal to the range of the StartingPoint.
0131   Standard_EXPORT void Root(math_Vector& Root) const;
0132 
0133   //! Returns the matrix value of the derivative at the root.
0134   //! Exception NotDone is raised if the root was not found.
0135   const math_Matrix& Derivative() const
0136   {
0137     StdFail_NotDone_Raise_if(!Done, " ");
0138     return DF;
0139   }
0140 
0141   //! outputs the matrix value of the derivative
0142   //! at the root in Der.
0143   //! Exception NotDone is raised if the root was not found.
0144   //! Exception DimensionError is raised if the column range
0145   //! of <Der> is not equal to the range of the startingPoint.
0146   void Derivative(math_Matrix& Der) const
0147   {
0148     StdFail_NotDone_Raise_if(!Done, " ");
0149     Standard_DimensionError_Raise_if(Der.ColNumber() != Sol.Length(), " ");
0150     Der = DF;
0151   }
0152 
0153   //! returns the vector value of the error done
0154   //! on the functions at the root.
0155   //! Exception NotDone is raised if the root was not found.
0156   const math_Vector& FunctionSetErrors() const
0157   {
0158     StdFail_NotDone_Raise_if(!Done, " ");
0159     return Delta;
0160   }
0161 
0162   //! outputs the vector value of the error done
0163   //! on the functions at the root in Err.
0164   //! Exception NotDone is raised if the root was not found.
0165   //! Exception DimensionError is raised if the range of Err
0166   //! is not equal to the range of the StartingPoint.
0167   Standard_EXPORT void FunctionSetErrors(math_Vector& Err) const;
0168 
0169   //! Prints on the stream o information on the current state
0170   //! of the object.
0171   //! Is used to redefine the operator <<.
0172   Standard_EXPORT void Dump(Standard_OStream& o) const;
0173 
0174   bool IsDivergent() const { return myIsDivergent; }
0175 
0176 protected:
0177   math_Vector Delta;
0178   math_Vector Sol;
0179   math_Matrix DF;
0180   math_Vector Tol;
0181 
0182 private:
0183   bool               Done;
0184   int                Kount;
0185   int                State;
0186   int                Itermax;
0187   math_Vector        InfBound;
0188   math_Vector        SupBound;
0189   math_Vector        SolSave;
0190   math_Vector        GH;
0191   math_Vector        DH;
0192   math_Vector        DHSave;
0193   math_Vector        FF;
0194   math_Vector        PreviousSolution;
0195   math_Vector        Save;
0196   math_IntegerVector Constraints;
0197   math_Vector        Temp1;
0198   math_Vector        Temp2;
0199   math_Vector        Temp3;
0200   math_Vector        Temp4;
0201   bool               myIsDivergent;
0202 };
0203 
0204 inline Standard_OStream& operator<<(Standard_OStream& theStream, const math_FunctionSetRoot& theF)
0205 {
0206   theF.Dump(theStream);
0207   return theStream;
0208 }
0209 
0210 #endif // _math_FunctionSetRoot_HeaderFile