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
21 #include <Standard_Failure.hxx>
22 #include <math_FunctionRoot.ixx>
23 #include <math_FunctionSetRoot.hxx>
25 #include <math_FunctionSetWithDerivatives.hxx>
26 #include <math_FunctionWithDerivative.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,V,Tol,NbIterations);
83 TheRoot = Sol.Root()(1);
84 TheDerivative = Sol.Derivative()(1,1);
85 F.Value(TheRoot,TheError);
86 NbIter = Sol.NbIterations();
89 math_FunctionRoot::math_FunctionRoot(math_FunctionWithDerivative& F,
90 const Standard_Real Guess,
91 const Standard_Real Tolerance,
92 const Standard_Real A,
93 const Standard_Real B,
94 const Standard_Integer NbIterations ){
95 math_Vector V(1,1),Aa(1,1),Bb(1,1), Tol(1,1);
96 math_MyFunctionSetWithDerivatives Ff(F);
101 math_FunctionSetRoot Sol(Ff,V,Tol,Aa,Bb,NbIterations);
105 TheRoot = Sol.Root()(1);
106 TheDerivative = Sol.Derivative()(1,1);
107 F.Value(TheRoot,TheError);
108 NbIter = Sol.NbIterations();
112 void math_FunctionRoot::Dump(Standard_OStream& o) const {
114 o<< "math_FunctionRoot ";
116 o<< " Status = Done \n";
117 o << " Number of iterations = " << NbIter << endl;
118 o << " The Root is: " << TheRoot << endl;
119 o << "The value at the root is: " << TheError << endl;
122 o<< " Status = not Done \n";