7fd59977 |
1 | //static const char* sccsid = "@(#)math_FunctionRoot.cxx 3.2 95/01/10"; // Do not delete this line. Used by sccs. |
2 | |
3 | //#ifndef DEB |
4 | #define No_Standard_RangeError |
5 | #define No_Standard_OutOfRange |
6 | #define No_Standard_DimensionError |
7 | //#endif |
8 | |
9 | #include <Standard_Failure.hxx> |
10 | #include <math_FunctionRoot.ixx> |
11 | #include <math_FunctionSetRoot.hxx> |
12 | |
13 | #include <math_FunctionSetWithDerivatives.hxx> |
14 | #include <math_FunctionWithDerivative.hxx> |
15 | |
16 | |
17 | class math_MyFunctionSetWithDerivatives : public math_FunctionSetWithDerivatives { |
18 | |
19 | private: |
20 | math_FunctionWithDerivative *Ff; |
21 | |
22 | public : |
23 | |
24 | math_MyFunctionSetWithDerivatives (math_FunctionWithDerivative& F ); |
25 | |
26 | Standard_Integer NbVariables () const; |
27 | Standard_Integer NbEquations () const; |
28 | Standard_Boolean Value (const math_Vector& X, math_Vector& F) ; |
29 | Standard_Boolean Derivatives (const math_Vector& X, math_Matrix& D) ; |
30 | Standard_Boolean Values (const math_Vector& X, math_Vector& F, math_Matrix& D) ; |
31 | }; |
32 | |
33 | |
34 | math_MyFunctionSetWithDerivatives::math_MyFunctionSetWithDerivatives |
35 | (math_FunctionWithDerivative& F ) { |
36 | Ff = &F ; |
37 | |
38 | } |
39 | |
40 | Standard_Integer math_MyFunctionSetWithDerivatives::NbVariables () const { |
41 | return 1; |
42 | } |
43 | Standard_Integer math_MyFunctionSetWithDerivatives::NbEquations () const { |
44 | return 1; |
45 | } |
46 | Standard_Boolean math_MyFunctionSetWithDerivatives::Value (const math_Vector& X, math_Vector& Fs) { |
47 | return Ff->Value(X(1),Fs(1)); |
48 | } |
49 | Standard_Boolean math_MyFunctionSetWithDerivatives::Derivatives (const math_Vector& X, math_Matrix& D) { |
50 | return Ff->Derivative(X(1),D(1,1)); |
51 | } |
52 | Standard_Boolean math_MyFunctionSetWithDerivatives::Values (const math_Vector& X, math_Vector& F, math_Matrix& D) { |
53 | return Ff->Values(X(1),F(1),D(1,1)); |
54 | } |
55 | |
56 | |
57 | |
58 | |
59 | math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative& F, |
60 | const Standard_Real Guess, |
61 | const Standard_Real Tolerance, |
62 | const Standard_Integer NbIterations ){ |
63 | Standard_Boolean Ok; |
64 | math_Vector V(1,1), Tol(1,1); |
65 | math_MyFunctionSetWithDerivatives Ff(F); |
66 | V(1)=Guess; |
67 | Tol(1) = Tolerance; |
68 | math_FunctionSetRoot Sol(Ff,V,Tol,NbIterations); |
69 | Done = Sol.IsDone(); |
70 | if (Done) { |
71 | #ifdef DEB |
72 | Standard_Integer Ier=F.GetStateNumber(); |
73 | #else |
74 | F.GetStateNumber(); |
75 | #endif |
76 | TheRoot = Sol.Root()(1); |
77 | TheDerivative = Sol.Derivative()(1,1); |
78 | Ok = F.Value(TheRoot,TheError); |
79 | NbIter = Sol.NbIterations(); |
80 | } |
81 | } |
82 | math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative& F, |
83 | const Standard_Real Guess, |
84 | const Standard_Real Tolerance, |
85 | const Standard_Real A, |
86 | const Standard_Real B, |
87 | const Standard_Integer NbIterations ){ |
88 | Standard_Boolean Ok; |
89 | math_Vector V(1,1),Aa(1,1),Bb(1,1), Tol(1,1); |
90 | math_MyFunctionSetWithDerivatives Ff(F); |
91 | V(1)=Guess; |
92 | Tol(1) = Tolerance; |
93 | Aa(1)=A; |
94 | Bb(1)=B; |
95 | math_FunctionSetRoot Sol(Ff,V,Tol,Aa,Bb,NbIterations); |
96 | Done = Sol.IsDone(); |
97 | if (Done) { |
98 | #ifdef DEB |
99 | Standard_Integer Ier =F.GetStateNumber(); |
100 | #else |
101 | F.GetStateNumber(); |
102 | #endif |
103 | TheRoot = Sol.Root()(1); |
104 | TheDerivative = Sol.Derivative()(1,1); |
105 | Ok = F.Value(TheRoot,TheError); |
106 | NbIter = Sol.NbIterations(); |
107 | } |
108 | } |
109 | |
110 | void math_FunctionRoot::Dump(Standard_OStream& o) const { |
111 | |
112 | o<< "math_FunctionRoot "; |
113 | if(Done) { |
114 | o<< " Status = Done \n"; |
115 | o << " Number of iterations = " << NbIter << endl; |
116 | o << " The Root is: " << TheRoot << endl; |
117 | o << "The value at the root is: " << TheError << endl; |
118 | } |
119 | else { |
120 | o<< " Status = not Done \n"; |
121 | } |
122 | } |