15 static double _norm(std::vector<double> &x)
18 for(std::size_t i = 0; i < x.size(); i++) n += x[i] * x[i];
21 static void scale(std::vector<double> &x,
double s)
23 for(std::size_t i = 0; i < x.size(); i++) x[i] *= s;
28 std::vector<double> &gradObj,
31 std::vector<double> &x,
32 std::vector<double> &p,
33 std::vector<double> &g,
34 double &
f,
double stpmax,
int &check)
37 double alam, alam2 = 1., alamin, f2 = 0., fold2 = 0., rhs1, rhs2, temp,
40 const double ALF = 1.e-4;
41 const double TOLX = 1.0e-9;
43 std::vector<double> xold(x), grad(x);
45 (*func)(xold, fold,
false, grad, data);
52 for(i = 0; i < n; i++) slope += g[i] * p[i];
54 for(i = 0; i < n; i++) {
55 temp = fabs(p[i]) / std::max(fabs(xold[i]), 1.0);
56 if(temp > test) test = temp;
63 for(i = 0; i < n; i++) x[i] = xold[i] + alam * p[i];
64 (*func)(x,
f,
false, grad, data);
72 for(i = 0; i < n; i++) x[i] = xold[i] + alam * p[i];
73 (*func)(x,
f,
false, grad, data);
81 for(i = 0; i < n; i++) x[i] = xold[i];
85 else if(
f <= fold + ALF * alam * slope)
89 tmplam = -slope / (2.0 * (
f - fold - slope));
91 rhs1 =
f - fold - alam * slope;
92 rhs2 = f2 - fold2 - alam2 * slope;
94 (rhs1 / (alam * alam) - rhs2 / (alam2 * alam2)) / (alam - alam2);
96 (-alam2 * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) /
99 tmplam = -slope / (2.0 * b);
101 const double disc = b * b - 3.0 * a * slope;
103 Msg::Error(
"Roundoff problem in gmshLineSearch.");
105 tmplam = (-b + sqrt(disc)) / (3.0 * a);
107 if(tmplam > 0.5 * alam) tmplam = 0.5 * alam;
113 alam = std::max(tmplam, 0.1 * alam);
121 bool needGrad, std::vector<double> &gradObj,
123 std::vector<double> &x,
127 const int N = x.size();
129 std::vector<double> grad(N);
130 std::vector<double> dir(N);
135 for(
int iter = 0; iter < MAXIT; iter++) {
137 double stpmax = 100000;
138 func(x,
f,
true, grad, data);
140 for(
int i = 0; i < N; i++) dir[i] = -grad[i];
145 if(check == 1)
break;