-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
132 lines (111 loc) · 4.07 KB
/
common.c
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
/* multilarge_nlinear/common.c
*
* Copyright (C) 2015, 2016 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
static double scaled_enorm (const gsl_vector * d, const gsl_vector * f);
static void scaled_addition (const double alpha, const gsl_vector * x,
const double beta, const gsl_vector * y,
gsl_vector * z);
static double quadratic_preduction(const gsl_multilarge_nlinear_trust_state * trust_state,
const gsl_vector * dx, gsl_vector * work);
/* compute || diag(d) f || */
static double
scaled_enorm (const gsl_vector * d, const gsl_vector * f)
{
double e2 = 0;
size_t i, n = f->size;
for (i = 0; i < n; i++)
{
double fi = gsl_vector_get (f, i);
double di = gsl_vector_get (d, i);
double u = di * fi;
e2 += u * u;
}
return sqrt (e2);
}
/* compute z = alpha*x + beta*y */
static void
scaled_addition (const double alpha, const gsl_vector * x,
const double beta, const gsl_vector * y, gsl_vector * z)
{
const size_t N = z->size;
size_t i;
for (i = 0; i < N; i++)
{
double xi = gsl_vector_get (x, i);
double yi = gsl_vector_get (y, i);
gsl_vector_set (z, i, alpha * xi + beta * yi);
}
}
/*
quadratic_preduction()
Calculate predicted reduction based on standard
quadratic model:
m_k(dx) = Phi(x_k) + dx' g + 1/2 dx' B_k dx
predicted_reduction = m_k(0) - m_k(dx)
= -2 g^T dx / ||f||^2 - ( ||J*dx|| / ||f|| )^2
= -2 fhat . beta - ||beta||^2
where: beta = J*dx / ||f||
Inputs: trust_state - trust state
dx - proposed step, size p
work - workspace, size n
Return: predicted reduction
*/
static double
quadratic_preduction(const gsl_multilarge_nlinear_trust_state * trust_state,
const gsl_vector * dx, gsl_vector * work)
{
const gsl_vector * f = trust_state->f;
const gsl_multilarge_nlinear_parameters * params = trust_state->params;
const double normf = gsl_blas_dnrm2(f);
double gTdx; /* g^T dx */
gsl_multilarge_nlinear_fdf * fdf = trust_state->fdf;
double pred_reduction, u;
/* compute g^T dx */
gsl_blas_ddot(trust_state->g, dx, &gTdx);
/* first term: -2 g^T dx / ||f||^2 */
pred_reduction = -2.0 * gTdx / (normf * normf);
if (params->solver == gsl_multilarge_nlinear_solver_cholesky ||
params->solver == gsl_multilarge_nlinear_solver_mcholesky)
{
const size_t p = fdf->p;
gsl_vector_view workp = gsl_vector_subvector(work, 0, p);
/* compute workp = J^T J dx */
gsl_blas_dsymv(CblasLower, 1.0, trust_state->JTJ, dx, 0.0, &workp.vector);
/* compute u = dx^T J^T J dx = ||J dx||^2 */
gsl_blas_ddot(&workp.vector, dx, &u);
pred_reduction -= u / (normf * normf);
}
else
{
int status;
const gsl_vector * x = trust_state->x;
const gsl_vector * swts = trust_state->sqrt_wts;
/* compute work = J*dx */
status = gsl_multilarge_nlinear_eval_df(CblasNoTrans, x, f, dx,
swts, params->h_df, params->fdtype,
fdf, work, NULL, NULL);
if (status)
{
GSL_ERROR_VAL("error computing preduction", status, 0.0);
}
/* compute u = ||J*dx|| / ||f|| */
u = gsl_blas_dnrm2(work) / normf;
pred_reduction -= u * u;
}
return pred_reduction;
}