1 /* glpscf.c (Schur complement factorization) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
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: <mao@gnu.org>.
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.
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.
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 ***********************************************************************/
29 #define _GLPSCF_DEBUG 0
33 /***********************************************************************
36 * scf_create_it - create Schur complement factorization
41 * SCF *scf_create_it(int n_max);
45 * The routine scf_create_it creates the factorization of matrix C,
46 * which initially has no rows and columns.
48 * The parameter n_max specifies the maximal order of matrix C to be
49 * factorized, 1 <= n_max <= 32767.
53 * The routine scf_create_it returns a pointer to the structure SCF,
54 * which defines the factorization. */
56 SCF *scf_create_it(int n_max)
59 xprintf("scf_create_it: warning: debug mode enabled\n");
61 if (!(1 <= n_max && n_max <= 32767))
62 xfault("scf_create_it: n_max = %d; invalid parameter\n",
64 scf = xmalloc(sizeof(SCF));
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));
73 scf->c = xcalloc(1 + n_max * n_max, sizeof(double));
77 scf->w = xcalloc(1 + n_max, sizeof(double));
81 /***********************************************************************
82 * The routine f_loc determines location of matrix element F[i,j] in
83 * the one-dimensional array f. */
85 static int f_loc(SCF *scf, int i, int j)
86 { int n_max = scf->n_max;
88 xassert(1 <= i && i <= n);
89 xassert(1 <= j && j <= n);
90 return (i - 1) * n_max + j;
93 /***********************************************************************
94 * The routine u_loc determines location of matrix element U[i,j] in
95 * the one-dimensional array u. */
97 static int u_loc(SCF *scf, int i, int j)
98 { int n_max = scf->n_max;
100 xassert(1 <= i && i <= n);
101 xassert(i <= j && j <= n);
102 return (i - 1) * n_max + j - i * (i - 1) / 2;
105 /***********************************************************************
106 * The routine bg_transform applies Bartels-Golub version of gaussian
107 * elimination to restore triangular structure of matrix U.
109 * On entry matrix U has the following structure:
112 * 1 * * * * * * * * * *
113 * . * * * * * * * * *
114 * . . * * * * * * * *
115 * . . . * * * * * * *
116 * k . . . . * * * * * *
117 * . . . . . * * * * *
118 * . . . . . . * * * *
119 * . . . . . . . * * *
120 * . . . . . . . . * *
121 * n . . . . # # # # # #
123 * where '#' is a row spike to be eliminated.
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.
130 * R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming
131 * Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */
133 static void bg_transform(SCF *scf, int k, double un[])
137 int j, k1, kj, kk, n1, nj;
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;
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] */
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++)
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++)
176 /* if U[n,n] is too small in the magnitude, replace it by exact
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];
184 /***********************************************************************
185 * The routine givens computes the parameters of Givens plane rotation
186 * c = cos(teta) and s = sin(teta) such that:
188 * ( c -s ) ( a ) ( r )
190 * ( s c ) ( b ) ( 0 )
192 * where a and b are given scalars.
196 * G.H.Golub, C.F.Van Loan, "Matrix Computations", 2nd ed. */
198 static void givens(double a, double b, double *c, double *s)
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;
205 t = - b / a, (*c) = 1.0 / sqrt(1.0 + t * t), (*s) = (*c) * t;
209 /*----------------------------------------------------------------------
210 * The routine gr_transform applies Givens plane rotations to restore
211 * triangular structure of matrix U.
213 * On entry matrix U has the following structure:
216 * 1 * * * * * * * * * *
217 * . * * * * * * * * *
218 * . . * * * * * * * *
219 * . . . * * * * * * *
220 * k . . . . * * * * * *
221 * . . . . . * * * * *
222 * . . . . . . * * * *
223 * . . . . . . . * * *
224 * . . . . . . . . * *
225 * n . . . . # # # # # #
227 * where '#' is a row spike to be eliminated.
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.
234 * R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming
235 * Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */
237 static void gr_transform(SCF *scf, int k, double un[])
241 int j, k1, kj, kk, n1, nj;
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)
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;
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;
274 /* if U[n,n] is too small in the magnitude, replace it by exact
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];
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). */
287 static void transform(SCF *scf, int k, double un[])
288 { switch (scf->t_opt)
290 bg_transform(scf, k, un);
293 gr_transform(scf, k, un);
301 /***********************************************************************
302 * The routine estimate_rank estimates the rank of matrix C.
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. */
309 static int estimate_rank(SCF *scf)
310 { int n_max = scf->n_max;
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++;
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.) */
326 static void check_error(SCF *scf, const char *func)
333 double d, dmax = 0.0, s, t;
335 for (i = 1; i <= n; i++)
336 { for (j = 1; j <= n; j++)
337 { /* compute element (i,j) of product F * C */
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 */
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;
350 xprintf("%s: dmax = %g; relative error too large\n", func,
356 /***********************************************************************
359 * scf_update_exp - update factorization on expanding C
363 * #include "glpscf.h"
364 * int scf_update_exp(SCF *scf, const double x[], const double y[],
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:
376 * where x[1,...,n] is a new column, y[1,...,n] is a new row, and z is
377 * a new diagonal element.
379 * If on entry the factorization is empty, the parameters x and y can
380 * be specified as NULL.
384 * 0 The factorization has been successfully updated.
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.
392 * There is not enough room to expand the factorization, because
393 * n = n_max. The factorization remains unchanged.
399 * ( F 0 ) ( C x ) ( FC Fx ) ( UP Fx )
400 * ( ) ( ) = ( ) = ( ) =
401 * ( 0 1 ) ( y' z ) ( y' z ) ( y' z )
407 * therefore to keep the main equality F * C = U * P we can take:
409 * ( F 0 ) ( U Fx ) ( P 0 )
410 * new F = ( ), new U = ( ), new P = ( ),
411 * ( 0 1 ) ( y'P' z ) ( 0 1 )
413 * and eliminate the row spike y'P' in the last row of new U to restore
414 * its upper triangular structure. */
416 int scf_update_exp(SCF *scf, const double x[], const double y[],
418 { int n_max = scf->n_max;
427 int i, ij, in, j, k, nj, ret = 0;
429 /* check if the factorization can be expanded */
431 { /* there is not enough room */
435 /* increase the order of the factorization */
437 /* fill new zero column of matrix F */
438 for (i = 1, in = f_loc(scf, i, n); i < n; i++, in += n_max)
440 /* fill new zero row of matrix F */
441 for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++)
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 */
449 for (j = 1, ij = f_loc(scf, i, 1); j < n; j++, ij++)
451 u[u_loc(scf, i, n)] = t;
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 */
457 /* expand matrix P */
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)
464 /* fill its new row, which is y */
465 for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++)
467 /* fill its new diagonal element, which is z */
468 c[f_loc(scf, n, n)] = z;
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;
478 /* check that the factorization is accurate enough */
479 check_error(scf, "scf_update_exp");
484 /***********************************************************************
485 * The routine solve solves the system C * x = b.
487 * From the main equation F * C = U * P it follows that:
489 * C * x = b => F * C * x = F * b => U * P * x = F * b =>
491 * P * x = inv(U) * F * b => x = P' * inv(U) * F * b.
493 * On entry the array x contains right-hand side vector b. On exit this
494 * array contains solution vector x. */
496 static void solve(SCF *scf, double x[])
505 for (i = 1; i <= n; i++)
506 { /* y[i] = (i-th row of F) * b */
508 for (j = 1, ij = f_loc(scf, i, 1); j <= n; j++, ij++)
512 /* y := inv(U) * y */
513 for (i = n; i >= 1; i--)
515 for (j = n, ij = u_loc(scf, i, n); j > i; j--, ij--)
520 for (i = 1; i <= n; i++) x[p[i]] = y[i];
524 /***********************************************************************
525 * The routine tsolve solves the transposed system C' * x = b.
527 * From the main equation F * C = U * P it follows that:
533 * C' * x = b => C' * F' * inv(F') * x = b =>
535 * P' * U' * inv(F') * x = b => U' * inv(F') * x = P * b =>
537 * inv(F') * x = inv(U') * P * b => x = F' * inv(U') * P * b.
539 * On entry the array x contains right-hand side vector b. On exit this
540 * array contains solution vector x. */
542 static void tsolve(SCF *scf, double x[])
551 for (i = 1; i <= n; i++) y[i] = x[p[i]];
552 /* y := inv(U') * y */
553 for (i = 1; i <= n; i++)
555 ij = u_loc(scf, i, i);
557 /* substitute y[i] in other equations */
558 for (j = i+1, ij++; j <= n; j++, ij++)
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++)
571 /***********************************************************************
574 * scf_solve_it - solve either system C * x = b or C' * x = b
578 * #include "glpscf.h"
579 * void scf_solve_it(SCF *scf, int tr, double x[]);
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.
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. */
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");
601 void scf_reset_it(SCF *scf)
602 { /* reset factorization for empty matrix C */
603 scf->n = scf->rank = 0;
607 /***********************************************************************
610 * scf_delete_it - delete Schur complement factorization
614 * #include "glpscf.h"
615 * void scf_delete_it(SCF *scf);
619 * The routine scf_delete_it deletes the specified factorization and
620 * frees all the memory allocated to this object. */
622 void scf_delete_it(SCF *scf)