alpar@1
|
1 |
/* glpscf.c (Schur complement factorization) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpenv.h"
|
alpar@1
|
26 |
#include "glpscf.h"
|
alpar@1
|
27 |
#define xfault xerror
|
alpar@1
|
28 |
|
alpar@1
|
29 |
#define _GLPSCF_DEBUG 0
|
alpar@1
|
30 |
|
alpar@1
|
31 |
#define eps 1e-10
|
alpar@1
|
32 |
|
alpar@1
|
33 |
/***********************************************************************
|
alpar@1
|
34 |
* NAME
|
alpar@1
|
35 |
*
|
alpar@1
|
36 |
* scf_create_it - create Schur complement factorization
|
alpar@1
|
37 |
*
|
alpar@1
|
38 |
* SYNOPSIS
|
alpar@1
|
39 |
*
|
alpar@1
|
40 |
* #include "glpscf.h"
|
alpar@1
|
41 |
* SCF *scf_create_it(int n_max);
|
alpar@1
|
42 |
*
|
alpar@1
|
43 |
* DESCRIPTION
|
alpar@1
|
44 |
*
|
alpar@1
|
45 |
* The routine scf_create_it creates the factorization of matrix C,
|
alpar@1
|
46 |
* which initially has no rows and columns.
|
alpar@1
|
47 |
*
|
alpar@1
|
48 |
* The parameter n_max specifies the maximal order of matrix C to be
|
alpar@1
|
49 |
* factorized, 1 <= n_max <= 32767.
|
alpar@1
|
50 |
*
|
alpar@1
|
51 |
* RETURNS
|
alpar@1
|
52 |
*
|
alpar@1
|
53 |
* The routine scf_create_it returns a pointer to the structure SCF,
|
alpar@1
|
54 |
* which defines the factorization. */
|
alpar@1
|
55 |
|
alpar@1
|
56 |
SCF *scf_create_it(int n_max)
|
alpar@1
|
57 |
{ SCF *scf;
|
alpar@1
|
58 |
#if _GLPSCF_DEBUG
|
alpar@1
|
59 |
xprintf("scf_create_it: warning: debug mode enabled\n");
|
alpar@1
|
60 |
#endif
|
alpar@1
|
61 |
if (!(1 <= n_max && n_max <= 32767))
|
alpar@1
|
62 |
xfault("scf_create_it: n_max = %d; invalid parameter\n",
|
alpar@1
|
63 |
n_max);
|
alpar@1
|
64 |
scf = xmalloc(sizeof(SCF));
|
alpar@1
|
65 |
scf->n_max = n_max;
|
alpar@1
|
66 |
scf->n = 0;
|
alpar@1
|
67 |
scf->f = xcalloc(1 + n_max * n_max, sizeof(double));
|
alpar@1
|
68 |
scf->u = xcalloc(1 + n_max * (n_max + 1) / 2, sizeof(double));
|
alpar@1
|
69 |
scf->p = xcalloc(1 + n_max, sizeof(int));
|
alpar@1
|
70 |
scf->t_opt = SCF_TBG;
|
alpar@1
|
71 |
scf->rank = 0;
|
alpar@1
|
72 |
#if _GLPSCF_DEBUG
|
alpar@1
|
73 |
scf->c = xcalloc(1 + n_max * n_max, sizeof(double));
|
alpar@1
|
74 |
#else
|
alpar@1
|
75 |
scf->c = NULL;
|
alpar@1
|
76 |
#endif
|
alpar@1
|
77 |
scf->w = xcalloc(1 + n_max, sizeof(double));
|
alpar@1
|
78 |
return scf;
|
alpar@1
|
79 |
}
|
alpar@1
|
80 |
|
alpar@1
|
81 |
/***********************************************************************
|
alpar@1
|
82 |
* The routine f_loc determines location of matrix element F[i,j] in
|
alpar@1
|
83 |
* the one-dimensional array f. */
|
alpar@1
|
84 |
|
alpar@1
|
85 |
static int f_loc(SCF *scf, int i, int j)
|
alpar@1
|
86 |
{ int n_max = scf->n_max;
|
alpar@1
|
87 |
int n = scf->n;
|
alpar@1
|
88 |
xassert(1 <= i && i <= n);
|
alpar@1
|
89 |
xassert(1 <= j && j <= n);
|
alpar@1
|
90 |
return (i - 1) * n_max + j;
|
alpar@1
|
91 |
}
|
alpar@1
|
92 |
|
alpar@1
|
93 |
/***********************************************************************
|
alpar@1
|
94 |
* The routine u_loc determines location of matrix element U[i,j] in
|
alpar@1
|
95 |
* the one-dimensional array u. */
|
alpar@1
|
96 |
|
alpar@1
|
97 |
static int u_loc(SCF *scf, int i, int j)
|
alpar@1
|
98 |
{ int n_max = scf->n_max;
|
alpar@1
|
99 |
int n = scf->n;
|
alpar@1
|
100 |
xassert(1 <= i && i <= n);
|
alpar@1
|
101 |
xassert(i <= j && j <= n);
|
alpar@1
|
102 |
return (i - 1) * n_max + j - i * (i - 1) / 2;
|
alpar@1
|
103 |
}
|
alpar@1
|
104 |
|
alpar@1
|
105 |
/***********************************************************************
|
alpar@1
|
106 |
* The routine bg_transform applies Bartels-Golub version of gaussian
|
alpar@1
|
107 |
* elimination to restore triangular structure of matrix U.
|
alpar@1
|
108 |
*
|
alpar@1
|
109 |
* On entry matrix U has the following structure:
|
alpar@1
|
110 |
*
|
alpar@1
|
111 |
* 1 k n
|
alpar@1
|
112 |
* 1 * * * * * * * * * *
|
alpar@1
|
113 |
* . * * * * * * * * *
|
alpar@1
|
114 |
* . . * * * * * * * *
|
alpar@1
|
115 |
* . . . * * * * * * *
|
alpar@1
|
116 |
* k . . . . * * * * * *
|
alpar@1
|
117 |
* . . . . . * * * * *
|
alpar@1
|
118 |
* . . . . . . * * * *
|
alpar@1
|
119 |
* . . . . . . . * * *
|
alpar@1
|
120 |
* . . . . . . . . * *
|
alpar@1
|
121 |
* n . . . . # # # # # #
|
alpar@1
|
122 |
*
|
alpar@1
|
123 |
* where '#' is a row spike to be eliminated.
|
alpar@1
|
124 |
*
|
alpar@1
|
125 |
* Elements of n-th row are passed separately in locations un[k], ...,
|
alpar@1
|
126 |
* un[n]. On exit the content of the array un is destroyed.
|
alpar@1
|
127 |
*
|
alpar@1
|
128 |
* REFERENCES
|
alpar@1
|
129 |
*
|
alpar@1
|
130 |
* R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming
|
alpar@1
|
131 |
* Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */
|
alpar@1
|
132 |
|
alpar@1
|
133 |
static void bg_transform(SCF *scf, int k, double un[])
|
alpar@1
|
134 |
{ int n = scf->n;
|
alpar@1
|
135 |
double *f = scf->f;
|
alpar@1
|
136 |
double *u = scf->u;
|
alpar@1
|
137 |
int j, k1, kj, kk, n1, nj;
|
alpar@1
|
138 |
double t;
|
alpar@1
|
139 |
xassert(1 <= k && k <= n);
|
alpar@1
|
140 |
/* main elimination loop */
|
alpar@1
|
141 |
for (k = k; k < n; k++)
|
alpar@1
|
142 |
{ /* determine location of U[k,k] */
|
alpar@1
|
143 |
kk = u_loc(scf, k, k);
|
alpar@1
|
144 |
/* determine location of F[k,1] */
|
alpar@1
|
145 |
k1 = f_loc(scf, k, 1);
|
alpar@1
|
146 |
/* determine location of F[n,1] */
|
alpar@1
|
147 |
n1 = f_loc(scf, n, 1);
|
alpar@1
|
148 |
/* if |U[k,k]| < |U[n,k]|, interchange k-th and n-th rows to
|
alpar@1
|
149 |
provide |U[k,k]| >= |U[n,k]| */
|
alpar@1
|
150 |
if (fabs(u[kk]) < fabs(un[k]))
|
alpar@1
|
151 |
{ /* interchange k-th and n-th rows of matrix U */
|
alpar@1
|
152 |
for (j = k, kj = kk; j <= n; j++, kj++)
|
alpar@1
|
153 |
t = u[kj], u[kj] = un[j], un[j] = t;
|
alpar@1
|
154 |
/* interchange k-th and n-th rows of matrix F to keep the
|
alpar@1
|
155 |
main equality F * C = U * P */
|
alpar@1
|
156 |
for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++)
|
alpar@1
|
157 |
t = f[kj], f[kj] = f[nj], f[nj] = t;
|
alpar@1
|
158 |
}
|
alpar@1
|
159 |
/* now |U[k,k]| >= |U[n,k]| */
|
alpar@1
|
160 |
/* if U[k,k] is too small in the magnitude, replace U[k,k] and
|
alpar@1
|
161 |
U[n,k] by exact zero */
|
alpar@1
|
162 |
if (fabs(u[kk]) < eps) u[kk] = un[k] = 0.0;
|
alpar@1
|
163 |
/* if U[n,k] is already zero, elimination is not needed */
|
alpar@1
|
164 |
if (un[k] == 0.0) continue;
|
alpar@1
|
165 |
/* compute gaussian multiplier t = U[n,k] / U[k,k] */
|
alpar@1
|
166 |
t = un[k] / u[kk];
|
alpar@1
|
167 |
/* apply gaussian elimination to nullify U[n,k] */
|
alpar@1
|
168 |
/* (n-th row of U) := (n-th row of U) - t * (k-th row of U) */
|
alpar@1
|
169 |
for (j = k+1, kj = kk+1; j <= n; j++, kj++)
|
alpar@1
|
170 |
un[j] -= t * u[kj];
|
alpar@1
|
171 |
/* (n-th row of F) := (n-th row of F) - t * (k-th row of F)
|
alpar@1
|
172 |
to keep the main equality F * C = U * P */
|
alpar@1
|
173 |
for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++)
|
alpar@1
|
174 |
f[nj] -= t * f[kj];
|
alpar@1
|
175 |
}
|
alpar@1
|
176 |
/* if U[n,n] is too small in the magnitude, replace it by exact
|
alpar@1
|
177 |
zero */
|
alpar@1
|
178 |
if (fabs(un[n]) < eps) un[n] = 0.0;
|
alpar@1
|
179 |
/* store U[n,n] in a proper location */
|
alpar@1
|
180 |
u[u_loc(scf, n, n)] = un[n];
|
alpar@1
|
181 |
return;
|
alpar@1
|
182 |
}
|
alpar@1
|
183 |
|
alpar@1
|
184 |
/***********************************************************************
|
alpar@1
|
185 |
* The routine givens computes the parameters of Givens plane rotation
|
alpar@1
|
186 |
* c = cos(teta) and s = sin(teta) such that:
|
alpar@1
|
187 |
*
|
alpar@1
|
188 |
* ( c -s ) ( a ) ( r )
|
alpar@1
|
189 |
* ( ) ( ) = ( ) ,
|
alpar@1
|
190 |
* ( s c ) ( b ) ( 0 )
|
alpar@1
|
191 |
*
|
alpar@1
|
192 |
* where a and b are given scalars.
|
alpar@1
|
193 |
*
|
alpar@1
|
194 |
* REFERENCES
|
alpar@1
|
195 |
*
|
alpar@1
|
196 |
* G.H.Golub, C.F.Van Loan, "Matrix Computations", 2nd ed. */
|
alpar@1
|
197 |
|
alpar@1
|
198 |
static void givens(double a, double b, double *c, double *s)
|
alpar@1
|
199 |
{ double t;
|
alpar@1
|
200 |
if (b == 0.0)
|
alpar@1
|
201 |
(*c) = 1.0, (*s) = 0.0;
|
alpar@1
|
202 |
else if (fabs(a) <= fabs(b))
|
alpar@1
|
203 |
t = - a / b, (*s) = 1.0 / sqrt(1.0 + t * t), (*c) = (*s) * t;
|
alpar@1
|
204 |
else
|
alpar@1
|
205 |
t = - b / a, (*c) = 1.0 / sqrt(1.0 + t * t), (*s) = (*c) * t;
|
alpar@1
|
206 |
return;
|
alpar@1
|
207 |
}
|
alpar@1
|
208 |
|
alpar@1
|
209 |
/*----------------------------------------------------------------------
|
alpar@1
|
210 |
* The routine gr_transform applies Givens plane rotations to restore
|
alpar@1
|
211 |
* triangular structure of matrix U.
|
alpar@1
|
212 |
*
|
alpar@1
|
213 |
* On entry matrix U has the following structure:
|
alpar@1
|
214 |
*
|
alpar@1
|
215 |
* 1 k n
|
alpar@1
|
216 |
* 1 * * * * * * * * * *
|
alpar@1
|
217 |
* . * * * * * * * * *
|
alpar@1
|
218 |
* . . * * * * * * * *
|
alpar@1
|
219 |
* . . . * * * * * * *
|
alpar@1
|
220 |
* k . . . . * * * * * *
|
alpar@1
|
221 |
* . . . . . * * * * *
|
alpar@1
|
222 |
* . . . . . . * * * *
|
alpar@1
|
223 |
* . . . . . . . * * *
|
alpar@1
|
224 |
* . . . . . . . . * *
|
alpar@1
|
225 |
* n . . . . # # # # # #
|
alpar@1
|
226 |
*
|
alpar@1
|
227 |
* where '#' is a row spike to be eliminated.
|
alpar@1
|
228 |
*
|
alpar@1
|
229 |
* Elements of n-th row are passed separately in locations un[k], ...,
|
alpar@1
|
230 |
* un[n]. On exit the content of the array un is destroyed.
|
alpar@1
|
231 |
*
|
alpar@1
|
232 |
* REFERENCES
|
alpar@1
|
233 |
*
|
alpar@1
|
234 |
* R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming
|
alpar@1
|
235 |
* Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */
|
alpar@1
|
236 |
|
alpar@1
|
237 |
static void gr_transform(SCF *scf, int k, double un[])
|
alpar@1
|
238 |
{ int n = scf->n;
|
alpar@1
|
239 |
double *f = scf->f;
|
alpar@1
|
240 |
double *u = scf->u;
|
alpar@1
|
241 |
int j, k1, kj, kk, n1, nj;
|
alpar@1
|
242 |
double c, s;
|
alpar@1
|
243 |
xassert(1 <= k && k <= n);
|
alpar@1
|
244 |
/* main elimination loop */
|
alpar@1
|
245 |
for (k = k; k < n; k++)
|
alpar@1
|
246 |
{ /* determine location of U[k,k] */
|
alpar@1
|
247 |
kk = u_loc(scf, k, k);
|
alpar@1
|
248 |
/* determine location of F[k,1] */
|
alpar@1
|
249 |
k1 = f_loc(scf, k, 1);
|
alpar@1
|
250 |
/* determine location of F[n,1] */
|
alpar@1
|
251 |
n1 = f_loc(scf, n, 1);
|
alpar@1
|
252 |
/* if both U[k,k] and U[n,k] are too small in the magnitude,
|
alpar@1
|
253 |
replace them by exact zero */
|
alpar@1
|
254 |
if (fabs(u[kk]) < eps && fabs(un[k]) < eps)
|
alpar@1
|
255 |
u[kk] = un[k] = 0.0;
|
alpar@1
|
256 |
/* if U[n,k] is already zero, elimination is not needed */
|
alpar@1
|
257 |
if (un[k] == 0.0) continue;
|
alpar@1
|
258 |
/* compute the parameters of Givens plane rotation */
|
alpar@1
|
259 |
givens(u[kk], un[k], &c, &s);
|
alpar@1
|
260 |
/* apply Givens rotation to k-th and n-th rows of matrix U */
|
alpar@1
|
261 |
for (j = k, kj = kk; j <= n; j++, kj++)
|
alpar@1
|
262 |
{ double ukj = u[kj], unj = un[j];
|
alpar@1
|
263 |
u[kj] = c * ukj - s * unj;
|
alpar@1
|
264 |
un[j] = s * ukj + c * unj;
|
alpar@1
|
265 |
}
|
alpar@1
|
266 |
/* apply Givens rotation to k-th and n-th rows of matrix F
|
alpar@1
|
267 |
to keep the main equality F * C = U * P */
|
alpar@1
|
268 |
for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++)
|
alpar@1
|
269 |
{ double fkj = f[kj], fnj = f[nj];
|
alpar@1
|
270 |
f[kj] = c * fkj - s * fnj;
|
alpar@1
|
271 |
f[nj] = s * fkj + c * fnj;
|
alpar@1
|
272 |
}
|
alpar@1
|
273 |
}
|
alpar@1
|
274 |
/* if U[n,n] is too small in the magnitude, replace it by exact
|
alpar@1
|
275 |
zero */
|
alpar@1
|
276 |
if (fabs(un[n]) < eps) un[n] = 0.0;
|
alpar@1
|
277 |
/* store U[n,n] in a proper location */
|
alpar@1
|
278 |
u[u_loc(scf, n, n)] = un[n];
|
alpar@1
|
279 |
return;
|
alpar@1
|
280 |
}
|
alpar@1
|
281 |
|
alpar@1
|
282 |
/***********************************************************************
|
alpar@1
|
283 |
* The routine transform restores triangular structure of matrix U.
|
alpar@1
|
284 |
* It is a driver to the routines bg_transform and gr_transform (see
|
alpar@1
|
285 |
* comments to these routines above). */
|
alpar@1
|
286 |
|
alpar@1
|
287 |
static void transform(SCF *scf, int k, double un[])
|
alpar@1
|
288 |
{ switch (scf->t_opt)
|
alpar@1
|
289 |
{ case SCF_TBG:
|
alpar@1
|
290 |
bg_transform(scf, k, un);
|
alpar@1
|
291 |
break;
|
alpar@1
|
292 |
case SCF_TGR:
|
alpar@1
|
293 |
gr_transform(scf, k, un);
|
alpar@1
|
294 |
break;
|
alpar@1
|
295 |
default:
|
alpar@1
|
296 |
xassert(scf != scf);
|
alpar@1
|
297 |
}
|
alpar@1
|
298 |
return;
|
alpar@1
|
299 |
}
|
alpar@1
|
300 |
|
alpar@1
|
301 |
/***********************************************************************
|
alpar@1
|
302 |
* The routine estimate_rank estimates the rank of matrix C.
|
alpar@1
|
303 |
*
|
alpar@1
|
304 |
* Since all transformations applied to matrix F are non-singular,
|
alpar@1
|
305 |
* and F is assumed to be well conditioned, from the main equaility
|
alpar@1
|
306 |
* F * C = U * P it follows that rank(C) = rank(U), where rank(U) is
|
alpar@1
|
307 |
* estimated as the number of non-zero diagonal elements of U. */
|
alpar@1
|
308 |
|
alpar@1
|
309 |
static int estimate_rank(SCF *scf)
|
alpar@1
|
310 |
{ int n_max = scf->n_max;
|
alpar@1
|
311 |
int n = scf->n;
|
alpar@1
|
312 |
double *u = scf->u;
|
alpar@1
|
313 |
int i, ii, inc, rank = 0;
|
alpar@1
|
314 |
for (i = 1, ii = u_loc(scf, i, i), inc = n_max; i <= n;
|
alpar@1
|
315 |
i++, ii += inc, inc--)
|
alpar@1
|
316 |
if (u[ii] != 0.0) rank++;
|
alpar@1
|
317 |
return rank;
|
alpar@1
|
318 |
}
|
alpar@1
|
319 |
|
alpar@1
|
320 |
#if _GLPSCF_DEBUG
|
alpar@1
|
321 |
/***********************************************************************
|
alpar@1
|
322 |
* The routine check_error computes the maximal relative error between
|
alpar@1
|
323 |
* left- and right-hand sides of the main equality F * C = U * P. (This
|
alpar@1
|
324 |
* routine is intended only for debugging.) */
|
alpar@1
|
325 |
|
alpar@1
|
326 |
static void check_error(SCF *scf, const char *func)
|
alpar@1
|
327 |
{ int n = scf->n;
|
alpar@1
|
328 |
double *f = scf->f;
|
alpar@1
|
329 |
double *u = scf->u;
|
alpar@1
|
330 |
int *p = scf->p;
|
alpar@1
|
331 |
double *c = scf->c;
|
alpar@1
|
332 |
int i, j, k;
|
alpar@1
|
333 |
double d, dmax = 0.0, s, t;
|
alpar@1
|
334 |
xassert(c != NULL);
|
alpar@1
|
335 |
for (i = 1; i <= n; i++)
|
alpar@1
|
336 |
{ for (j = 1; j <= n; j++)
|
alpar@1
|
337 |
{ /* compute element (i,j) of product F * C */
|
alpar@1
|
338 |
s = 0.0;
|
alpar@1
|
339 |
for (k = 1; k <= n; k++)
|
alpar@1
|
340 |
s += f[f_loc(scf, i, k)] * c[f_loc(scf, k, j)];
|
alpar@1
|
341 |
/* compute element (i,j) of product U * P */
|
alpar@1
|
342 |
k = p[j];
|
alpar@1
|
343 |
t = (i <= k ? u[u_loc(scf, i, k)] : 0.0);
|
alpar@1
|
344 |
/* compute the maximal relative error */
|
alpar@1
|
345 |
d = fabs(s - t) / (1.0 + fabs(t));
|
alpar@1
|
346 |
if (dmax < d) dmax = d;
|
alpar@1
|
347 |
}
|
alpar@1
|
348 |
}
|
alpar@1
|
349 |
if (dmax > 1e-8)
|
alpar@1
|
350 |
xprintf("%s: dmax = %g; relative error too large\n", func,
|
alpar@1
|
351 |
dmax);
|
alpar@1
|
352 |
return;
|
alpar@1
|
353 |
}
|
alpar@1
|
354 |
#endif
|
alpar@1
|
355 |
|
alpar@1
|
356 |
/***********************************************************************
|
alpar@1
|
357 |
* NAME
|
alpar@1
|
358 |
*
|
alpar@1
|
359 |
* scf_update_exp - update factorization on expanding C
|
alpar@1
|
360 |
*
|
alpar@1
|
361 |
* SYNOPSIS
|
alpar@1
|
362 |
*
|
alpar@1
|
363 |
* #include "glpscf.h"
|
alpar@1
|
364 |
* int scf_update_exp(SCF *scf, const double x[], const double y[],
|
alpar@1
|
365 |
* double z);
|
alpar@1
|
366 |
*
|
alpar@1
|
367 |
* DESCRIPTION
|
alpar@1
|
368 |
*
|
alpar@1
|
369 |
* The routine scf_update_exp updates the factorization of matrix C on
|
alpar@1
|
370 |
* expanding it by adding a new row and column as follows:
|
alpar@1
|
371 |
*
|
alpar@1
|
372 |
* ( C x )
|
alpar@1
|
373 |
* new C = ( )
|
alpar@1
|
374 |
* ( y' z )
|
alpar@1
|
375 |
*
|
alpar@1
|
376 |
* where x[1,...,n] is a new column, y[1,...,n] is a new row, and z is
|
alpar@1
|
377 |
* a new diagonal element.
|
alpar@1
|
378 |
*
|
alpar@1
|
379 |
* If on entry the factorization is empty, the parameters x and y can
|
alpar@1
|
380 |
* be specified as NULL.
|
alpar@1
|
381 |
*
|
alpar@1
|
382 |
* RETURNS
|
alpar@1
|
383 |
*
|
alpar@1
|
384 |
* 0 The factorization has been successfully updated.
|
alpar@1
|
385 |
*
|
alpar@1
|
386 |
* SCF_ESING
|
alpar@1
|
387 |
* The factorization has been successfully updated, however, new
|
alpar@1
|
388 |
* matrix C is singular within working precision. Note that the new
|
alpar@1
|
389 |
* factorization remains valid.
|
alpar@1
|
390 |
*
|
alpar@1
|
391 |
* SCF_ELIMIT
|
alpar@1
|
392 |
* There is not enough room to expand the factorization, because
|
alpar@1
|
393 |
* n = n_max. The factorization remains unchanged.
|
alpar@1
|
394 |
*
|
alpar@1
|
395 |
* ALGORITHM
|
alpar@1
|
396 |
*
|
alpar@1
|
397 |
* We can see that:
|
alpar@1
|
398 |
*
|
alpar@1
|
399 |
* ( F 0 ) ( C x ) ( FC Fx ) ( UP Fx )
|
alpar@1
|
400 |
* ( ) ( ) = ( ) = ( ) =
|
alpar@1
|
401 |
* ( 0 1 ) ( y' z ) ( y' z ) ( y' z )
|
alpar@1
|
402 |
*
|
alpar@1
|
403 |
* ( U Fx ) ( P 0 )
|
alpar@1
|
404 |
* = ( ) ( ),
|
alpar@1
|
405 |
* ( y'P' z ) ( 0 1 )
|
alpar@1
|
406 |
*
|
alpar@1
|
407 |
* therefore to keep the main equality F * C = U * P we can take:
|
alpar@1
|
408 |
*
|
alpar@1
|
409 |
* ( F 0 ) ( U Fx ) ( P 0 )
|
alpar@1
|
410 |
* new F = ( ), new U = ( ), new P = ( ),
|
alpar@1
|
411 |
* ( 0 1 ) ( y'P' z ) ( 0 1 )
|
alpar@1
|
412 |
*
|
alpar@1
|
413 |
* and eliminate the row spike y'P' in the last row of new U to restore
|
alpar@1
|
414 |
* its upper triangular structure. */
|
alpar@1
|
415 |
|
alpar@1
|
416 |
int scf_update_exp(SCF *scf, const double x[], const double y[],
|
alpar@1
|
417 |
double z)
|
alpar@1
|
418 |
{ int n_max = scf->n_max;
|
alpar@1
|
419 |
int n = scf->n;
|
alpar@1
|
420 |
double *f = scf->f;
|
alpar@1
|
421 |
double *u = scf->u;
|
alpar@1
|
422 |
int *p = scf->p;
|
alpar@1
|
423 |
#if _GLPSCF_DEBUG
|
alpar@1
|
424 |
double *c = scf->c;
|
alpar@1
|
425 |
#endif
|
alpar@1
|
426 |
double *un = scf->w;
|
alpar@1
|
427 |
int i, ij, in, j, k, nj, ret = 0;
|
alpar@1
|
428 |
double t;
|
alpar@1
|
429 |
/* check if the factorization can be expanded */
|
alpar@1
|
430 |
if (n == n_max)
|
alpar@1
|
431 |
{ /* there is not enough room */
|
alpar@1
|
432 |
ret = SCF_ELIMIT;
|
alpar@1
|
433 |
goto done;
|
alpar@1
|
434 |
}
|
alpar@1
|
435 |
/* increase the order of the factorization */
|
alpar@1
|
436 |
scf->n = ++n;
|
alpar@1
|
437 |
/* fill new zero column of matrix F */
|
alpar@1
|
438 |
for (i = 1, in = f_loc(scf, i, n); i < n; i++, in += n_max)
|
alpar@1
|
439 |
f[in] = 0.0;
|
alpar@1
|
440 |
/* fill new zero row of matrix F */
|
alpar@1
|
441 |
for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++)
|
alpar@1
|
442 |
f[nj] = 0.0;
|
alpar@1
|
443 |
/* fill new unity diagonal element of matrix F */
|
alpar@1
|
444 |
f[f_loc(scf, n, n)] = 1.0;
|
alpar@1
|
445 |
/* compute new column of matrix U, which is (old F) * x */
|
alpar@1
|
446 |
for (i = 1; i < n; i++)
|
alpar@1
|
447 |
{ /* u[i,n] := (i-th row of old F) * x */
|
alpar@1
|
448 |
t = 0.0;
|
alpar@1
|
449 |
for (j = 1, ij = f_loc(scf, i, 1); j < n; j++, ij++)
|
alpar@1
|
450 |
t += f[ij] * x[j];
|
alpar@1
|
451 |
u[u_loc(scf, i, n)] = t;
|
alpar@1
|
452 |
}
|
alpar@1
|
453 |
/* compute new (spiked) row of matrix U, which is (old P) * y */
|
alpar@1
|
454 |
for (j = 1; j < n; j++) un[j] = y[p[j]];
|
alpar@1
|
455 |
/* store new diagonal element of matrix U, which is z */
|
alpar@1
|
456 |
un[n] = z;
|
alpar@1
|
457 |
/* expand matrix P */
|
alpar@1
|
458 |
p[n] = n;
|
alpar@1
|
459 |
#if _GLPSCF_DEBUG
|
alpar@1
|
460 |
/* expand matrix C */
|
alpar@1
|
461 |
/* fill its new column, which is x */
|
alpar@1
|
462 |
for (i = 1, in = f_loc(scf, i, n); i < n; i++, in += n_max)
|
alpar@1
|
463 |
c[in] = x[i];
|
alpar@1
|
464 |
/* fill its new row, which is y */
|
alpar@1
|
465 |
for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++)
|
alpar@1
|
466 |
c[nj] = y[j];
|
alpar@1
|
467 |
/* fill its new diagonal element, which is z */
|
alpar@1
|
468 |
c[f_loc(scf, n, n)] = z;
|
alpar@1
|
469 |
#endif
|
alpar@1
|
470 |
/* restore upper triangular structure of matrix U */
|
alpar@1
|
471 |
for (k = 1; k < n; k++)
|
alpar@1
|
472 |
if (un[k] != 0.0) break;
|
alpar@1
|
473 |
transform(scf, k, un);
|
alpar@1
|
474 |
/* estimate the rank of matrices C and U */
|
alpar@1
|
475 |
scf->rank = estimate_rank(scf);
|
alpar@1
|
476 |
if (scf->rank != n) ret = SCF_ESING;
|
alpar@1
|
477 |
#if _GLPSCF_DEBUG
|
alpar@1
|
478 |
/* check that the factorization is accurate enough */
|
alpar@1
|
479 |
check_error(scf, "scf_update_exp");
|
alpar@1
|
480 |
#endif
|
alpar@1
|
481 |
done: return ret;
|
alpar@1
|
482 |
}
|
alpar@1
|
483 |
|
alpar@1
|
484 |
/***********************************************************************
|
alpar@1
|
485 |
* The routine solve solves the system C * x = b.
|
alpar@1
|
486 |
*
|
alpar@1
|
487 |
* From the main equation F * C = U * P it follows that:
|
alpar@1
|
488 |
*
|
alpar@1
|
489 |
* C * x = b => F * C * x = F * b => U * P * x = F * b =>
|
alpar@1
|
490 |
*
|
alpar@1
|
491 |
* P * x = inv(U) * F * b => x = P' * inv(U) * F * b.
|
alpar@1
|
492 |
*
|
alpar@1
|
493 |
* On entry the array x contains right-hand side vector b. On exit this
|
alpar@1
|
494 |
* array contains solution vector x. */
|
alpar@1
|
495 |
|
alpar@1
|
496 |
static void solve(SCF *scf, double x[])
|
alpar@1
|
497 |
{ int n = scf->n;
|
alpar@1
|
498 |
double *f = scf->f;
|
alpar@1
|
499 |
double *u = scf->u;
|
alpar@1
|
500 |
int *p = scf->p;
|
alpar@1
|
501 |
double *y = scf->w;
|
alpar@1
|
502 |
int i, j, ij;
|
alpar@1
|
503 |
double t;
|
alpar@1
|
504 |
/* y := F * b */
|
alpar@1
|
505 |
for (i = 1; i <= n; i++)
|
alpar@1
|
506 |
{ /* y[i] = (i-th row of F) * b */
|
alpar@1
|
507 |
t = 0.0;
|
alpar@1
|
508 |
for (j = 1, ij = f_loc(scf, i, 1); j <= n; j++, ij++)
|
alpar@1
|
509 |
t += f[ij] * x[j];
|
alpar@1
|
510 |
y[i] = t;
|
alpar@1
|
511 |
}
|
alpar@1
|
512 |
/* y := inv(U) * y */
|
alpar@1
|
513 |
for (i = n; i >= 1; i--)
|
alpar@1
|
514 |
{ t = y[i];
|
alpar@1
|
515 |
for (j = n, ij = u_loc(scf, i, n); j > i; j--, ij--)
|
alpar@1
|
516 |
t -= u[ij] * y[j];
|
alpar@1
|
517 |
y[i] = t / u[ij];
|
alpar@1
|
518 |
}
|
alpar@1
|
519 |
/* x := P' * y */
|
alpar@1
|
520 |
for (i = 1; i <= n; i++) x[p[i]] = y[i];
|
alpar@1
|
521 |
return;
|
alpar@1
|
522 |
}
|
alpar@1
|
523 |
|
alpar@1
|
524 |
/***********************************************************************
|
alpar@1
|
525 |
* The routine tsolve solves the transposed system C' * x = b.
|
alpar@1
|
526 |
*
|
alpar@1
|
527 |
* From the main equation F * C = U * P it follows that:
|
alpar@1
|
528 |
*
|
alpar@1
|
529 |
* C' * F' = P' * U',
|
alpar@1
|
530 |
*
|
alpar@1
|
531 |
* therefore:
|
alpar@1
|
532 |
*
|
alpar@1
|
533 |
* C' * x = b => C' * F' * inv(F') * x = b =>
|
alpar@1
|
534 |
*
|
alpar@1
|
535 |
* P' * U' * inv(F') * x = b => U' * inv(F') * x = P * b =>
|
alpar@1
|
536 |
*
|
alpar@1
|
537 |
* inv(F') * x = inv(U') * P * b => x = F' * inv(U') * P * b.
|
alpar@1
|
538 |
*
|
alpar@1
|
539 |
* On entry the array x contains right-hand side vector b. On exit this
|
alpar@1
|
540 |
* array contains solution vector x. */
|
alpar@1
|
541 |
|
alpar@1
|
542 |
static void tsolve(SCF *scf, double x[])
|
alpar@1
|
543 |
{ int n = scf->n;
|
alpar@1
|
544 |
double *f = scf->f;
|
alpar@1
|
545 |
double *u = scf->u;
|
alpar@1
|
546 |
int *p = scf->p;
|
alpar@1
|
547 |
double *y = scf->w;
|
alpar@1
|
548 |
int i, j, ij;
|
alpar@1
|
549 |
double t;
|
alpar@1
|
550 |
/* y := P * b */
|
alpar@1
|
551 |
for (i = 1; i <= n; i++) y[i] = x[p[i]];
|
alpar@1
|
552 |
/* y := inv(U') * y */
|
alpar@1
|
553 |
for (i = 1; i <= n; i++)
|
alpar@1
|
554 |
{ /* compute y[i] */
|
alpar@1
|
555 |
ij = u_loc(scf, i, i);
|
alpar@1
|
556 |
t = (y[i] /= u[ij]);
|
alpar@1
|
557 |
/* substitute y[i] in other equations */
|
alpar@1
|
558 |
for (j = i+1, ij++; j <= n; j++, ij++)
|
alpar@1
|
559 |
y[j] -= u[ij] * t;
|
alpar@1
|
560 |
}
|
alpar@1
|
561 |
/* x := F' * y (computed as linear combination of rows of F) */
|
alpar@1
|
562 |
for (j = 1; j <= n; j++) x[j] = 0.0;
|
alpar@1
|
563 |
for (i = 1; i <= n; i++)
|
alpar@1
|
564 |
{ t = y[i]; /* coefficient of linear combination */
|
alpar@1
|
565 |
for (j = 1, ij = f_loc(scf, i, 1); j <= n; j++, ij++)
|
alpar@1
|
566 |
x[j] += f[ij] * t;
|
alpar@1
|
567 |
}
|
alpar@1
|
568 |
return;
|
alpar@1
|
569 |
}
|
alpar@1
|
570 |
|
alpar@1
|
571 |
/***********************************************************************
|
alpar@1
|
572 |
* NAME
|
alpar@1
|
573 |
*
|
alpar@1
|
574 |
* scf_solve_it - solve either system C * x = b or C' * x = b
|
alpar@1
|
575 |
*
|
alpar@1
|
576 |
* SYNOPSIS
|
alpar@1
|
577 |
*
|
alpar@1
|
578 |
* #include "glpscf.h"
|
alpar@1
|
579 |
* void scf_solve_it(SCF *scf, int tr, double x[]);
|
alpar@1
|
580 |
*
|
alpar@1
|
581 |
* DESCRIPTION
|
alpar@1
|
582 |
*
|
alpar@1
|
583 |
* The routine scf_solve_it solves either the system C * x = b (if tr
|
alpar@1
|
584 |
* is zero) or the system C' * x = b, where C' is a matrix transposed
|
alpar@1
|
585 |
* to C (if tr is non-zero). C is assumed to be non-singular.
|
alpar@1
|
586 |
*
|
alpar@1
|
587 |
* On entry the array x should contain the right-hand side vector b in
|
alpar@1
|
588 |
* locations x[1], ..., x[n], where n is the order of matrix C. On exit
|
alpar@1
|
589 |
* the array x contains the solution vector x in the same locations. */
|
alpar@1
|
590 |
|
alpar@1
|
591 |
void scf_solve_it(SCF *scf, int tr, double x[])
|
alpar@1
|
592 |
{ if (scf->rank < scf->n)
|
alpar@1
|
593 |
xfault("scf_solve_it: singular matrix\n");
|
alpar@1
|
594 |
if (!tr)
|
alpar@1
|
595 |
solve(scf, x);
|
alpar@1
|
596 |
else
|
alpar@1
|
597 |
tsolve(scf, x);
|
alpar@1
|
598 |
return;
|
alpar@1
|
599 |
}
|
alpar@1
|
600 |
|
alpar@1
|
601 |
void scf_reset_it(SCF *scf)
|
alpar@1
|
602 |
{ /* reset factorization for empty matrix C */
|
alpar@1
|
603 |
scf->n = scf->rank = 0;
|
alpar@1
|
604 |
return;
|
alpar@1
|
605 |
}
|
alpar@1
|
606 |
|
alpar@1
|
607 |
/***********************************************************************
|
alpar@1
|
608 |
* NAME
|
alpar@1
|
609 |
*
|
alpar@1
|
610 |
* scf_delete_it - delete Schur complement factorization
|
alpar@1
|
611 |
*
|
alpar@1
|
612 |
* SYNOPSIS
|
alpar@1
|
613 |
*
|
alpar@1
|
614 |
* #include "glpscf.h"
|
alpar@1
|
615 |
* void scf_delete_it(SCF *scf);
|
alpar@1
|
616 |
*
|
alpar@1
|
617 |
* DESCRIPTION
|
alpar@1
|
618 |
*
|
alpar@1
|
619 |
* The routine scf_delete_it deletes the specified factorization and
|
alpar@1
|
620 |
* frees all the memory allocated to this object. */
|
alpar@1
|
621 |
|
alpar@1
|
622 |
void scf_delete_it(SCF *scf)
|
alpar@1
|
623 |
{ xfree(scf->f);
|
alpar@1
|
624 |
xfree(scf->u);
|
alpar@1
|
625 |
xfree(scf->p);
|
alpar@1
|
626 |
#if _GLPSCF_DEBUG
|
alpar@1
|
627 |
xfree(scf->c);
|
alpar@1
|
628 |
#endif
|
alpar@1
|
629 |
xfree(scf->w);
|
alpar@1
|
630 |
xfree(scf);
|
alpar@1
|
631 |
return;
|
alpar@1
|
632 |
}
|
alpar@1
|
633 |
|
alpar@1
|
634 |
/* eof */
|