0027067: Avoid use of virtual methods for implementation of destructors in legacy...
[occt.git] / src / math / math_NewtonFunctionSetRoot.hxx
1 // Created on: 1991-05-14
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _math_NewtonFunctionSetRoot_HeaderFile
18 #define _math_NewtonFunctionSetRoot_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
23
24 #include <Standard_Boolean.hxx>
25 #include <Standard_Integer.hxx>
26 #include <math_Vector.hxx>
27 #include <Standard_Real.hxx>
28 #include <math_IntegerVector.hxx>
29 #include <math_Matrix.hxx>
30 #include <Standard_OStream.hxx>
31 class StdFail_NotDone;
32 class Standard_DimensionError;
33 class math_FunctionSetWithDerivatives;
34 class math_Matrix;
35
36
37
38 //! This class computes the root of a set of N functions of N variables,
39 //! knowing an initial guess at the solution and using the
40 //! Newton Raphson algorithm. Knowledge of all the partial
41 //! derivatives (Jacobian) is required.
42 class math_NewtonFunctionSetRoot 
43 {
44 public:
45
46   DEFINE_STANDARD_ALLOC
47
48   
49
50   //! Initialize correctly all the fields of this class.
51   //! The range (1, F.NbVariables()) must be especially respected for
52   //! all vectors and matrix declarations.
53   Standard_EXPORT math_NewtonFunctionSetRoot(math_FunctionSetWithDerivatives& theFunction, const math_Vector& theXTolerance, const Standard_Real theFTolerance, const Standard_Integer tehNbIterations = 100);
54   
55
56   //! This constructor should be used in a sub-class to initialize
57   //! correctly all the fields of this class.
58   //! The range (1, F.NbVariables()) must be especially respected for
59   //! all vectors and matrix declarations.
60   //! The method SetTolerance must be called before performing the algorithm.
61   Standard_EXPORT math_NewtonFunctionSetRoot(math_FunctionSetWithDerivatives& theFunction, const Standard_Real theFTolerance, const Standard_Integer theNbIterations = 100);
62   
63   //! Destructor
64   Standard_EXPORT virtual ~math_NewtonFunctionSetRoot();
65   
66   //! Initializes the tolerance values for the unknowns.
67   Standard_EXPORT void SetTolerance (const math_Vector& XTol);
68   
69
70   //! The Newton method is done to improve the root of the function
71   //! from the initial guess point. The solution is found when:
72   //! abs(Xj - Xj-1)(i) <= XTol(i) and abs(Fi) <= FTol for all i;
73   Standard_EXPORT void Perform (math_FunctionSetWithDerivatives& theFunction, const math_Vector& theStartingPoint);
74   
75
76   //! The Newton method is done to improve the root of the function
77   //! from the initial guess point. Bounds may be given, to constrain the solution.
78   //! The solution is found when:
79   //! abs(Xj - Xj-1)(i) <= XTol(i) and abs(Fi) <= FTol for all i;
80   Standard_EXPORT void Perform (math_FunctionSetWithDerivatives& theFunction, const math_Vector& theStartingPoint, const math_Vector& theInfBound, const math_Vector& theSupBound);
81   
82
83   //! This method is called at the end of each iteration to check if the
84   //! solution is found.
85   //! Vectors DeltaX, Fvalues and Jacobian Matrix are consistent with the
86   //! possible solution Vector Sol and can be inspected to decide whether
87   //! the solution is reached or not.
88     virtual Standard_Boolean IsSolutionReached (math_FunctionSetWithDerivatives& F);
89   
90   //! Returns true if the computations are successful, otherwise returns false.
91     Standard_Boolean IsDone() const;
92   
93   //! Returns the value of the root of function F.
94   //! Exceptions
95   //! StdFail_NotDone if the algorithm fails (and IsDone returns false).
96     const math_Vector& Root() const;
97   
98   //! outputs the root vector in Root.
99   //! Exception NotDone is raised if the root was not found.
100   //! Exception DimensionError is raised if the range of Root is
101   //! not equal to the range of the StartingPoint.
102     void Root (math_Vector& Root) const;
103   
104   //! Outputs the state number associated with the solution
105   //! vector root.
106     Standard_Integer StateNumber() const;
107   
108   //! Returns the matrix value of the derivative at the root.
109   //! Exception NotDone is raised if the root was not found.
110     const math_Matrix& Derivative() const;
111   
112   //! Outputs the matrix value of the derivative at the root in
113   //! Der.
114   //! Exception NotDone is raised if the root was not found.
115   //! Exception DimensionError is raised if the range of Der is
116   //! not equal to the range of the StartingPoint.
117     void Derivative (math_Matrix& Der) const;
118   
119   //! Returns the vector value of the error done on the
120   //! functions at the root.
121   //! Exception NotDone is raised if the root was not found.
122     const math_Vector& FunctionSetErrors() const;
123   
124   //! Outputs the vector value of the error done on the
125   //! functions at the root in Err.
126   //! Exception NotDone is raised if the root was not found.
127   //! Exception DimensionError is raised if the range of Err is
128   //! not equal to the range of the StartingPoint.
129     void FunctionSetErrors (math_Vector& Err) const;
130   
131   //! Returns the number of iterations really done
132   //! during the computation of the Root.
133   //! Exception NotDone is raised if the root was not found.
134     Standard_Integer NbIterations() const;
135   
136   //! Prints information on the current state of the object.
137   //! Is used to redefine the operator <<.
138   Standard_EXPORT void Dump (Standard_OStream& o) const;
139
140
141
142
143 protected:
144
145
146
147   math_Vector TolX;
148   Standard_Real TolF;
149   math_IntegerVector Indx;
150   math_Vector Scratch;
151   math_Vector Sol;
152   math_Vector DeltaX;
153   math_Vector FValues;
154   math_Matrix Jacobian;
155
156
157 private:
158
159
160
161   Standard_Boolean Done;
162   Standard_Integer State;
163   Standard_Integer Iter;
164   Standard_Integer Itermax;
165
166
167 };
168
169
170 #include <math_NewtonFunctionSetRoot.lxx>
171
172
173
174
175
176 #endif // _math_NewtonFunctionSetRoot_HeaderFile