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 <math_NewtonFunctionSetRoot.ixx>
22 #include <math_Recipes.hxx>
23 #include <math_FunctionSetWithDerivatives.hxx>
26 //=======================================================================
27 //function : math_NewtonFunctionSetRoot
28 //purpose : Constructor
29 //=======================================================================
30 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
31 math_FunctionSetWithDerivatives& theFunction,
32 const math_Vector& theXTolerance,
33 const Standard_Real theFTolerance,
34 const Standard_Integer theNbIterations)
36 : TolX (1, theFunction.NbVariables()),
38 Indx (1, theFunction.NbVariables()),
39 Scratch (1, theFunction.NbVariables()),
40 Sol (1, theFunction.NbVariables()),
41 DeltaX (1, theFunction.NbVariables()),
42 FValues (1, theFunction.NbVariables()),
43 Jacobian(1, theFunction.NbVariables(), 1, theFunction.NbVariables()),
44 Done (Standard_False),
47 Itermax (theNbIterations)
49 SetTolerance(theXTolerance);
52 //=======================================================================
53 //function : math_NewtonFunctionSetRoot
54 //purpose : Constructor
55 //=======================================================================
56 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
57 math_FunctionSetWithDerivatives& theFunction,
58 const Standard_Real theFTolerance,
59 const Standard_Integer theNbIterations)
61 : TolX (1, theFunction.NbVariables()),
63 Indx (1, theFunction.NbVariables()),
64 Scratch (1, theFunction.NbVariables()),
65 Sol (1, theFunction.NbVariables()),
66 DeltaX (1, theFunction.NbVariables()),
67 FValues (1, theFunction.NbVariables()),
68 Jacobian(1, theFunction.NbVariables(), 1, theFunction.NbVariables()),
69 Done (Standard_False),
72 Itermax (theNbIterations)
76 //=======================================================================
77 //function : ~math_NewtonFunctionSetRoot
78 //purpose : Destructor
79 //=======================================================================
80 math_NewtonFunctionSetRoot::~math_NewtonFunctionSetRoot()
84 //=======================================================================
85 //function : SetTolerance
87 //=======================================================================
88 void math_NewtonFunctionSetRoot::SetTolerance(const math_Vector& theXTolerance)
90 for (Standard_Integer i = 1; i <= TolX.Length(); ++i)
91 TolX(i) = theXTolerance(i);
94 //=======================================================================
97 //=======================================================================
98 void math_NewtonFunctionSetRoot::Perform(
99 math_FunctionSetWithDerivatives& theFunction,
100 const math_Vector& theStartingPoint)
102 const math_Vector anInf(1, theFunction.NbVariables(), RealFirst());
103 const math_Vector aSup (1, theFunction.NbVariables(), RealLast ());
105 Perform(theFunction, theStartingPoint, anInf, aSup);
108 //=======================================================================
111 //=======================================================================
112 void math_NewtonFunctionSetRoot::Perform(
113 math_FunctionSetWithDerivatives& F,
114 const math_Vector& StartingPoint,
115 const math_Vector& InfBound,
116 const math_Vector& SupBound)
121 Standard_Integer Error;
123 Done = Standard_False;
125 OK = F.Values(Sol, FValues, Jacobian);
127 for(Iter = 1; Iter <= Itermax; Iter++) {
128 for(Standard_Integer k = 1; k <= DeltaX.Length(); k++) {
129 DeltaX(k) = -FValues(k);
131 Error = LU_Decompose(Jacobian, Indx, d, Scratch, 1.0e-30);
133 LU_Solve(Jacobian, Indx, DeltaX);
134 for(Standard_Integer i = 1; i <= Sol.Length(); i++) {
137 // Limitation de Sol dans les bornes [InfBound, SupBound] :
138 if (Sol(i) <= InfBound(i)) Sol(i) = InfBound(i);
139 if (Sol(i) >= SupBound(i)) Sol(i) = SupBound(i);
142 OK = F.Values(Sol, FValues, Jacobian);
144 if(IsSolutionReached(F)) {
145 State = F.GetStateNumber();
146 Done = Standard_True;
152 //=======================================================================
155 //=======================================================================
156 void math_NewtonFunctionSetRoot::Dump(Standard_OStream& o) const
158 o <<"math_NewtonFunctionSetRoot ";
160 o << " Status = Done \n";
161 o << " Vector solution = " << Sol <<"\n";
162 o << " Value of the function at this solution = \n";
164 o << " Number of iterations = " << Iter <<"\n";
167 o << "Status = not Done \n";