Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:14

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 
0040   DEFINE_STANDARD_ALLOC
0041 
0042   //! is used in a sub-class to initialize correctly all the fields
0043   //! of this class.
0044   //! The range (1, F.NbVariables()) must be especially
0045   //! respected for all vectors and matrix declarations.
0046   Standard_EXPORT math_FunctionSetRoot(math_FunctionSetWithDerivatives& F, const math_Vector& Tolerance, const Standard_Integer NbIterations = 100);
0047   
0048   //! is used in a sub-class to initialize correctly all the fields
0049   //! of this class.
0050   //! The range (1, F.NbVariables()) must be especially
0051   //! respected for all vectors and matrix declarations.
0052   //! The method SetTolerance must be called after this
0053   //! constructor.
0054   Standard_EXPORT math_FunctionSetRoot(math_FunctionSetWithDerivatives& F, const Standard_Integer NbIterations = 100);
0055   
0056   //! Destructor
0057   Standard_EXPORT virtual ~math_FunctionSetRoot();
0058   
0059   //! Initializes the tolerance values.
0060   Standard_EXPORT void SetTolerance (const math_Vector& Tolerance);
0061   
0062   //! This routine is called at the end of each iteration
0063   //! to check if the solution was found. It can be redefined
0064   //! in a sub-class to implement a specific test to stop the iterations.
0065   //! In this case, the solution is found when: abs(Xi - Xi-1) <= Tolerance
0066   //! for all unknowns.
0067   virtual Standard_Boolean IsSolutionReached (math_FunctionSetWithDerivatives& )
0068   {
0069     for (Standard_Integer i = 1; i <= Sol.Length(); ++i)
0070     {
0071       if (Abs (Delta(i)) > Tol(i))
0072       {
0073         return Standard_False;
0074       }
0075     }
0076     return Standard_True;
0077   }
0078 
0079   //! Improves the root of function from the initial guess point.
0080   //! The infinum and supremum may be given to constrain the solution.
0081   //! In this case, the solution is found when: abs(Xi - Xi-1)(j) <= Tolerance(j)
0082   //! for all unknowns.
0083   Standard_EXPORT void Perform (math_FunctionSetWithDerivatives& theFunction, const math_Vector& theStartingPoint, const Standard_Boolean theStopOnDivergent = Standard_False);
0084 
0085   //! Improves the root of function from the initial guess point.
0086   //! The infinum and supremum may be given to constrain the solution.
0087   //! In this case, the solution is found when: abs(Xi - Xi-1) <= Tolerance
0088   //! for all unknowns.
0089   Standard_EXPORT void Perform (math_FunctionSetWithDerivatives& theFunction, const math_Vector& theStartingPoint, const math_Vector& theInfBound, const math_Vector& theSupBound, const Standard_Boolean theStopOnDivergent = Standard_False);
0090 
0091   //! Returns true if the computations are successful, otherwise returns false.
0092   Standard_Boolean IsDone() const { return Done; }
0093 
0094   //! Returns the number of iterations really done
0095   //! during the computation of the root.
0096   //! Exception NotDone is raised if the root was not found.
0097   Standard_Integer NbIterations() const
0098   {
0099     StdFail_NotDone_Raise_if(!Done, " ");
0100     return Kount;
0101   }
0102   
0103   //! returns the stateNumber (as returned by
0104   //! F.GetStateNumber()) associated to the root found.
0105   Standard_Integer StateNumber() const
0106   {
0107     StdFail_NotDone_Raise_if(!Done, " ");
0108     return State;
0109   }
0110   
0111   //! Returns the value of the root of function F.
0112   //! Exception NotDone is raised if the root was not found.
0113   const math_Vector& Root() const
0114   {
0115     StdFail_NotDone_Raise_if(!Done, " ");
0116     return Sol;
0117   }
0118 
0119   //! Outputs the root vector in Root.
0120   //! Exception NotDone is raised if the root was not found.
0121   //! Exception DimensionError is raised if the range of Root
0122   //! is not equal to the range of the StartingPoint.
0123   Standard_EXPORT void Root (math_Vector& Root) const;
0124   
0125   //! Returns the matrix value of the derivative at the root.
0126   //! Exception NotDone is raised if the root was not found.
0127   const math_Matrix& Derivative() const
0128   {
0129     StdFail_NotDone_Raise_if(!Done, " ");
0130     return DF;
0131   }
0132   
0133   //! outputs the matrix value of the derivative
0134   //! at the root in Der.
0135   //! Exception NotDone is raised if the root was not found.
0136   //! Exception DimensionError is raised if the column range
0137   //! of <Der> is not equal to the range of the startingPoint.
0138   void Derivative (math_Matrix& Der) const
0139   {
0140     StdFail_NotDone_Raise_if(!Done, " ");
0141     Standard_DimensionError_Raise_if(Der.ColNumber() != Sol.Length(), " ");
0142     Der = DF;
0143   }
0144 
0145   //! returns the vector value of the error done
0146   //! on the functions at the root.
0147   //! Exception NotDone is raised if the root was not found.
0148   const math_Vector& FunctionSetErrors() const
0149   {
0150     StdFail_NotDone_Raise_if(!Done, " ");
0151     return Delta;
0152   }
0153   
0154   //! outputs the vector value of the error done
0155   //! on the functions at the root in Err.
0156   //! Exception NotDone is raised if the root was not found.
0157   //! Exception DimensionError is raised if the range of Err
0158   //! is not equal to the range of the StartingPoint.
0159   Standard_EXPORT void FunctionSetErrors (math_Vector& Err) const;
0160   
0161   //! Prints on the stream o information on the current state
0162   //! of the object.
0163   //! Is used to redefine the operator <<.
0164   Standard_EXPORT void Dump (Standard_OStream& o) const;
0165 
0166   Standard_Boolean IsDivergent() const { return myIsDivergent; }
0167 
0168 protected:
0169 
0170   math_Vector Delta;
0171   math_Vector Sol;
0172   math_Matrix DF;
0173   math_Vector Tol;
0174 
0175 private:
0176 
0177   Standard_Boolean Done;
0178   Standard_Integer Kount;
0179   Standard_Integer State;
0180   Standard_Integer Itermax;
0181   math_Vector InfBound;
0182   math_Vector SupBound;
0183   math_Vector SolSave;
0184   math_Vector GH;
0185   math_Vector DH;
0186   math_Vector DHSave;
0187   math_Vector FF;
0188   math_Vector PreviousSolution;
0189   math_Vector Save;
0190   math_IntegerVector Constraints;
0191   math_Vector Temp1;
0192   math_Vector Temp2;
0193   math_Vector Temp3;
0194   math_Vector Temp4;
0195   Standard_Boolean myIsDivergent;
0196 
0197 };
0198 
0199 inline Standard_OStream& operator<< (Standard_OStream& theStream,
0200                                      const math_FunctionSetRoot& theF)
0201 {
0202   theF.Dump (theStream);
0203   return theStream;
0204 }
0205 
0206 #endif // _math_FunctionSetRoot_HeaderFile