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