lemon-project-template-glpk

annotate deps/glpk/src/glpscf.c @ 9:33de93886c88

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