1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #define No_Standard_RangeError
17 #define No_Standard_OutOfRange
18 #define No_Standard_DimensionError
22 #include <math_FunctionRoot.hxx>
23 #include <math_FunctionSetRoot.hxx>
24 #include <math_FunctionSetWithDerivatives.hxx>
25 #include <math_FunctionWithDerivative.hxx>
26 #include <Standard_Failure.hxx>
27 #include <StdFail_NotDone.hxx>
29 class math_MyFunctionSetWithDerivatives : public math_FunctionSetWithDerivatives {
32 math_FunctionWithDerivative *Ff;
36 math_MyFunctionSetWithDerivatives (math_FunctionWithDerivative& F );
38 Standard_Integer NbVariables () const;
39 Standard_Integer NbEquations () const;
40 Standard_Boolean Value (const math_Vector& X, math_Vector& F) ;
41 Standard_Boolean Derivatives (const math_Vector& X, math_Matrix& D) ;
42 Standard_Boolean Values (const math_Vector& X, math_Vector& F, math_Matrix& D) ;
46 math_MyFunctionSetWithDerivatives::math_MyFunctionSetWithDerivatives
47 (math_FunctionWithDerivative& F ) {
52 Standard_Integer math_MyFunctionSetWithDerivatives::NbVariables () const {
55 Standard_Integer math_MyFunctionSetWithDerivatives::NbEquations () const {
58 Standard_Boolean math_MyFunctionSetWithDerivatives::Value (const math_Vector& X, math_Vector& Fs) {
59 return Ff->Value(X(1),Fs(1));
61 Standard_Boolean math_MyFunctionSetWithDerivatives::Derivatives (const math_Vector& X, math_Matrix& D) {
62 return Ff->Derivative(X(1),D(1,1));
64 Standard_Boolean math_MyFunctionSetWithDerivatives::Values (const math_Vector& X, math_Vector& F, math_Matrix& D) {
65 return Ff->Values(X(1),F(1),D(1,1));
71 math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative& F,
72 const Standard_Real Guess,
73 const Standard_Real Tolerance,
74 const Standard_Integer NbIterations ){
75 math_Vector V(1,1), Tol(1,1);
76 math_MyFunctionSetWithDerivatives Ff(F);
79 math_FunctionSetRoot Sol(Ff, Tol, NbIterations);
84 TheRoot = Sol.Root()(1);
85 TheDerivative = Sol.Derivative()(1,1);
86 F.Value(TheRoot,TheError);
87 NbIter = Sol.NbIterations();
90 math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative& F,
91 const Standard_Real Guess,
92 const Standard_Real Tolerance,
93 const Standard_Real A,
94 const Standard_Real B,
95 const Standard_Integer NbIterations ){
96 math_Vector V(1,1),Aa(1,1),Bb(1,1), Tol(1,1);
97 math_MyFunctionSetWithDerivatives Ff(F);
102 math_FunctionSetRoot Sol(Ff, Tol, NbIterations);
103 Sol.Perform(Ff, V, Aa, Bb);
107 TheRoot = Sol.Root()(1);
108 TheDerivative = Sol.Derivative()(1,1);
109 F.Value(TheRoot,TheError);
110 NbIter = Sol.NbIterations();
114 void math_FunctionRoot::Dump(Standard_OStream& o) const {
116 o<< "math_FunctionRoot ";
118 o<< " Status = Done \n";
119 o << " Number of iterations = " << NbIter << std::endl;
120 o << " The Root is: " << TheRoot << std::endl;
121 o << "The value at the root is: " << TheError << std::endl;
124 o<< " Status = not Done \n";