summaryrefslogtreecommitdiff
path: root/src/math/math_NewtonFunctionSetRoot.cxx
blob: 6e12ddfd6b702e6118389305d1770dff8fcaf5dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//static const char* sccsid = "@(#)math_NewtonFunctionSetRoot.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.

//#ifndef DEB
#define No_Standard_RangeError
#define No_Standard_OutOfRange
#define No_Standard_DimensionError
//#endif

#include <math_NewtonFunctionSetRoot.ixx>
#include <math_Recipes.hxx>
#include <math_FunctionSetWithDerivatives.hxx>

Standard_Boolean math_NewtonFunctionSetRoot::IsSolutionReached
//                              (math_FunctionSetWithDerivatives& F) 
                              (math_FunctionSetWithDerivatives& ) 
{
  
  for(Standard_Integer i = DeltaX.Lower(); i <= DeltaX.Upper(); i++) {
    if(Abs(DeltaX(i)) > TolX(i) || Abs(FValues(i)) > TolF) return Standard_False;
  } 
  return Standard_True;
}


// Constructeurs d'initialisation des champs (pour utiliser Perform)

math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
			    math_FunctionSetWithDerivatives& F,
			    const math_Vector& XTol,
			    const Standard_Real FTol,
			    const Standard_Integer NbIterations):
                            TolX(1, F.NbVariables()),
                            TolF(FTol),
                            Indx(1, F.NbVariables()),
                            Scratch(1, F.NbVariables()),
                            Sol(1, F.NbVariables()),
                            DeltaX(1, F.NbVariables()),
                            FValues(1, F.NbVariables()),
                            Jacobian(1, F.NbVariables(),
				     1, F.NbVariables()),
                            Itermax(NbIterations)
{
  for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
    TolX(i) = XTol(i);
  }
}


math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
			    math_FunctionSetWithDerivatives& F,
			    const Standard_Real FTol,
			    const Standard_Integer NbIterations):
                            TolX(1, F.NbVariables()),
                            TolF(FTol),
                            Indx(1, F.NbVariables()),
                            Scratch(1, F.NbVariables()),
                            Sol(1, F.NbVariables()),
                            DeltaX(1, F.NbVariables()),
                            FValues(1, F.NbVariables()),
                            Jacobian(1, F.NbVariables(),
				     1, F.NbVariables()),
                            Itermax(NbIterations)
{
}


math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot
                           (math_FunctionSetWithDerivatives& F,
			    const math_Vector& StartingPoint,
			    const math_Vector&    XTol,
			    const Standard_Real    FTol,
			    const Standard_Integer NbIterations) :
			    
			    TolX(1, F.NbVariables()),
			    TolF(FTol),
			    Indx    (1, F.NbVariables()),
			    Scratch (1, F.NbVariables()),
			    Sol     (1, F.NbVariables()),
			    DeltaX  (1, F.NbVariables()),
			    FValues (1, F.NbVariables()),
			    Jacobian(1, F.NbVariables(),
				     1, F.NbVariables()),
			    Itermax(NbIterations)
{
  for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
    TolX(i) = XTol(i);
  }
  math_Vector UFirst(1, F.NbVariables()),
              ULast(1, F.NbVariables());
  UFirst.Init(RealFirst());
  ULast.Init(RealLast());
  Perform(F, StartingPoint, UFirst, ULast);
}

math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot
                              (math_FunctionSetWithDerivatives& F,
			       const math_Vector& StartingPoint,
                               const math_Vector&    InfBound,
                               const math_Vector&    SupBound,
			       const math_Vector&    XTol,
                               const Standard_Real    FTol,
			       const Standard_Integer NbIterations) :
                                                            
                               TolX(1, F.NbVariables()),
                               TolF(FTol),
                               Indx    (1, F.NbVariables()),
                               Scratch (1, F.NbVariables()),
                               Sol     (1, F.NbVariables()),
                               DeltaX  (1, F.NbVariables()),
                               FValues (1, F.NbVariables()),
                               Jacobian(1, F.NbVariables(),
                                        1, F.NbVariables()),
                               Itermax(NbIterations)
{
  for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
    TolX(i) = XTol(i);
  }
  Perform(F, StartingPoint, InfBound, SupBound);
}

void math_NewtonFunctionSetRoot::Delete()
{}

void math_NewtonFunctionSetRoot::SetTolerance
                              (const math_Vector& XTol)
{
  for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
    TolX(i) = XTol(i);
  }
}



void math_NewtonFunctionSetRoot::Perform(
                           math_FunctionSetWithDerivatives& F,
                           const math_Vector& StartingPoint,
                           const math_Vector& InfBound,
                           const math_Vector& SupBound) 
{

  Standard_Real d;
  Standard_Boolean OK;
  Standard_Integer Error;
  
  Done = Standard_False;
  Sol = StartingPoint;
  OK = F.Values(Sol, FValues, Jacobian);
  if(!OK) return;
  for(Iter = 1; Iter <= Itermax; Iter++) {
    for(Standard_Integer k = 1; k <= DeltaX.Length(); k++) {
      DeltaX(k) = -FValues(k);
    }
    Error = LU_Decompose(Jacobian, Indx, d, Scratch, 1.0e-30);
    if(Error) return; 
    LU_Solve(Jacobian, Indx, DeltaX);
    for(Standard_Integer i = 1; i <= Sol.Length(); i++) { 
      Sol(i) += DeltaX(i);
      
      // Limitation de Sol dans les bornes [InfBound, SupBound] :
      if (Sol(i) <= InfBound(i)) Sol(i) = InfBound(i);
      if (Sol(i) >= SupBound(i)) Sol(i) = SupBound(i);
      
    }
    OK = F.Values(Sol, FValues, Jacobian);
    if(!OK) return;
    if(IsSolutionReached(F)) { 
      State = F.GetStateNumber();
      Done = Standard_True; 
      return;
    }
  }               
}

void math_NewtonFunctionSetRoot::Dump(Standard_OStream& o) const 
{
  o <<"math_NewtonFunctionSetRoot ";
  if (Done) {
    o << " Status = Done \n";
    o << " Vector solution = " << Sol <<"\n";
    o << " Value of the function at this solution = \n";
    o << FValues <<"\n";
    o << " Number of iterations = " << Iter <<"\n";
  }
  else {
    o << "Status = not Done \n";
  }
}