alpar@1: /* glpscf.c (Schur complement factorization) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpenv.h" alpar@1: #include "glpscf.h" alpar@1: #define xfault xerror alpar@1: alpar@1: #define _GLPSCF_DEBUG 0 alpar@1: alpar@1: #define eps 1e-10 alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * scf_create_it - create Schur complement factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpscf.h" alpar@1: * SCF *scf_create_it(int n_max); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine scf_create_it creates the factorization of matrix C, alpar@1: * which initially has no rows and columns. alpar@1: * alpar@1: * The parameter n_max specifies the maximal order of matrix C to be alpar@1: * factorized, 1 <= n_max <= 32767. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine scf_create_it returns a pointer to the structure SCF, alpar@1: * which defines the factorization. */ alpar@1: alpar@1: SCF *scf_create_it(int n_max) alpar@1: { SCF *scf; alpar@1: #if _GLPSCF_DEBUG alpar@1: xprintf("scf_create_it: warning: debug mode enabled\n"); alpar@1: #endif alpar@1: if (!(1 <= n_max && n_max <= 32767)) alpar@1: xfault("scf_create_it: n_max = %d; invalid parameter\n", alpar@1: n_max); alpar@1: scf = xmalloc(sizeof(SCF)); alpar@1: scf->n_max = n_max; alpar@1: scf->n = 0; alpar@1: scf->f = xcalloc(1 + n_max * n_max, sizeof(double)); alpar@1: scf->u = xcalloc(1 + n_max * (n_max + 1) / 2, sizeof(double)); alpar@1: scf->p = xcalloc(1 + n_max, sizeof(int)); alpar@1: scf->t_opt = SCF_TBG; alpar@1: scf->rank = 0; alpar@1: #if _GLPSCF_DEBUG alpar@1: scf->c = xcalloc(1 + n_max * n_max, sizeof(double)); alpar@1: #else alpar@1: scf->c = NULL; alpar@1: #endif alpar@1: scf->w = xcalloc(1 + n_max, sizeof(double)); alpar@1: return scf; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine f_loc determines location of matrix element F[i,j] in alpar@1: * the one-dimensional array f. */ alpar@1: alpar@1: static int f_loc(SCF *scf, int i, int j) alpar@1: { int n_max = scf->n_max; alpar@1: int n = scf->n; alpar@1: xassert(1 <= i && i <= n); alpar@1: xassert(1 <= j && j <= n); alpar@1: return (i - 1) * n_max + j; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine u_loc determines location of matrix element U[i,j] in alpar@1: * the one-dimensional array u. */ alpar@1: alpar@1: static int u_loc(SCF *scf, int i, int j) alpar@1: { int n_max = scf->n_max; alpar@1: int n = scf->n; alpar@1: xassert(1 <= i && i <= n); alpar@1: xassert(i <= j && j <= n); alpar@1: return (i - 1) * n_max + j - i * (i - 1) / 2; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine bg_transform applies Bartels-Golub version of gaussian alpar@1: * elimination to restore triangular structure of matrix U. alpar@1: * alpar@1: * On entry matrix U has the following structure: alpar@1: * alpar@1: * 1 k n alpar@1: * 1 * * * * * * * * * * alpar@1: * . * * * * * * * * * alpar@1: * . . * * * * * * * * alpar@1: * . . . * * * * * * * alpar@1: * k . . . . * * * * * * alpar@1: * . . . . . * * * * * alpar@1: * . . . . . . * * * * alpar@1: * . . . . . . . * * * alpar@1: * . . . . . . . . * * alpar@1: * n . . . . # # # # # # alpar@1: * alpar@1: * where '#' is a row spike to be eliminated. alpar@1: * alpar@1: * Elements of n-th row are passed separately in locations un[k], ..., alpar@1: * un[n]. On exit the content of the array un is destroyed. alpar@1: * alpar@1: * REFERENCES alpar@1: * alpar@1: * R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming alpar@1: * Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */ alpar@1: alpar@1: static void bg_transform(SCF *scf, int k, double un[]) alpar@1: { int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int j, k1, kj, kk, n1, nj; alpar@1: double t; alpar@1: xassert(1 <= k && k <= n); alpar@1: /* main elimination loop */ alpar@1: for (k = k; k < n; k++) alpar@1: { /* determine location of U[k,k] */ alpar@1: kk = u_loc(scf, k, k); alpar@1: /* determine location of F[k,1] */ alpar@1: k1 = f_loc(scf, k, 1); alpar@1: /* determine location of F[n,1] */ alpar@1: n1 = f_loc(scf, n, 1); alpar@1: /* if |U[k,k]| < |U[n,k]|, interchange k-th and n-th rows to alpar@1: provide |U[k,k]| >= |U[n,k]| */ alpar@1: if (fabs(u[kk]) < fabs(un[k])) alpar@1: { /* interchange k-th and n-th rows of matrix U */ alpar@1: for (j = k, kj = kk; j <= n; j++, kj++) alpar@1: t = u[kj], u[kj] = un[j], un[j] = t; alpar@1: /* interchange k-th and n-th rows of matrix F to keep the alpar@1: main equality F * C = U * P */ alpar@1: for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++) alpar@1: t = f[kj], f[kj] = f[nj], f[nj] = t; alpar@1: } alpar@1: /* now |U[k,k]| >= |U[n,k]| */ alpar@1: /* if U[k,k] is too small in the magnitude, replace U[k,k] and alpar@1: U[n,k] by exact zero */ alpar@1: if (fabs(u[kk]) < eps) u[kk] = un[k] = 0.0; alpar@1: /* if U[n,k] is already zero, elimination is not needed */ alpar@1: if (un[k] == 0.0) continue; alpar@1: /* compute gaussian multiplier t = U[n,k] / U[k,k] */ alpar@1: t = un[k] / u[kk]; alpar@1: /* apply gaussian elimination to nullify U[n,k] */ alpar@1: /* (n-th row of U) := (n-th row of U) - t * (k-th row of U) */ alpar@1: for (j = k+1, kj = kk+1; j <= n; j++, kj++) alpar@1: un[j] -= t * u[kj]; alpar@1: /* (n-th row of F) := (n-th row of F) - t * (k-th row of F) alpar@1: to keep the main equality F * C = U * P */ alpar@1: for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++) alpar@1: f[nj] -= t * f[kj]; alpar@1: } alpar@1: /* if U[n,n] is too small in the magnitude, replace it by exact alpar@1: zero */ alpar@1: if (fabs(un[n]) < eps) un[n] = 0.0; alpar@1: /* store U[n,n] in a proper location */ alpar@1: u[u_loc(scf, n, n)] = un[n]; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine givens computes the parameters of Givens plane rotation alpar@1: * c = cos(teta) and s = sin(teta) such that: alpar@1: * alpar@1: * ( c -s ) ( a ) ( r ) alpar@1: * ( ) ( ) = ( ) , alpar@1: * ( s c ) ( b ) ( 0 ) alpar@1: * alpar@1: * where a and b are given scalars. alpar@1: * alpar@1: * REFERENCES alpar@1: * alpar@1: * G.H.Golub, C.F.Van Loan, "Matrix Computations", 2nd ed. */ alpar@1: alpar@1: static void givens(double a, double b, double *c, double *s) alpar@1: { double t; alpar@1: if (b == 0.0) alpar@1: (*c) = 1.0, (*s) = 0.0; alpar@1: else if (fabs(a) <= fabs(b)) alpar@1: t = - a / b, (*s) = 1.0 / sqrt(1.0 + t * t), (*c) = (*s) * t; alpar@1: else alpar@1: t = - b / a, (*c) = 1.0 / sqrt(1.0 + t * t), (*s) = (*c) * t; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: * The routine gr_transform applies Givens plane rotations to restore alpar@1: * triangular structure of matrix U. alpar@1: * alpar@1: * On entry matrix U has the following structure: alpar@1: * alpar@1: * 1 k n alpar@1: * 1 * * * * * * * * * * alpar@1: * . * * * * * * * * * alpar@1: * . . * * * * * * * * alpar@1: * . . . * * * * * * * alpar@1: * k . . . . * * * * * * alpar@1: * . . . . . * * * * * alpar@1: * . . . . . . * * * * alpar@1: * . . . . . . . * * * alpar@1: * . . . . . . . . * * alpar@1: * n . . . . # # # # # # alpar@1: * alpar@1: * where '#' is a row spike to be eliminated. alpar@1: * alpar@1: * Elements of n-th row are passed separately in locations un[k], ..., alpar@1: * un[n]. On exit the content of the array un is destroyed. alpar@1: * alpar@1: * REFERENCES alpar@1: * alpar@1: * R.H.Bartels, G.H.Golub, "The Simplex Method of Linear Programming alpar@1: * Using LU-decomposition", Comm. ACM, 12, pp. 266-68, 1969. */ alpar@1: alpar@1: static void gr_transform(SCF *scf, int k, double un[]) alpar@1: { int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int j, k1, kj, kk, n1, nj; alpar@1: double c, s; alpar@1: xassert(1 <= k && k <= n); alpar@1: /* main elimination loop */ alpar@1: for (k = k; k < n; k++) alpar@1: { /* determine location of U[k,k] */ alpar@1: kk = u_loc(scf, k, k); alpar@1: /* determine location of F[k,1] */ alpar@1: k1 = f_loc(scf, k, 1); alpar@1: /* determine location of F[n,1] */ alpar@1: n1 = f_loc(scf, n, 1); alpar@1: /* if both U[k,k] and U[n,k] are too small in the magnitude, alpar@1: replace them by exact zero */ alpar@1: if (fabs(u[kk]) < eps && fabs(un[k]) < eps) alpar@1: u[kk] = un[k] = 0.0; alpar@1: /* if U[n,k] is already zero, elimination is not needed */ alpar@1: if (un[k] == 0.0) continue; alpar@1: /* compute the parameters of Givens plane rotation */ alpar@1: givens(u[kk], un[k], &c, &s); alpar@1: /* apply Givens rotation to k-th and n-th rows of matrix U */ alpar@1: for (j = k, kj = kk; j <= n; j++, kj++) alpar@1: { double ukj = u[kj], unj = un[j]; alpar@1: u[kj] = c * ukj - s * unj; alpar@1: un[j] = s * ukj + c * unj; alpar@1: } alpar@1: /* apply Givens rotation to k-th and n-th rows of matrix F alpar@1: to keep the main equality F * C = U * P */ alpar@1: for (j = 1, kj = k1, nj = n1; j <= n; j++, kj++, nj++) alpar@1: { double fkj = f[kj], fnj = f[nj]; alpar@1: f[kj] = c * fkj - s * fnj; alpar@1: f[nj] = s * fkj + c * fnj; alpar@1: } alpar@1: } alpar@1: /* if U[n,n] is too small in the magnitude, replace it by exact alpar@1: zero */ alpar@1: if (fabs(un[n]) < eps) un[n] = 0.0; alpar@1: /* store U[n,n] in a proper location */ alpar@1: u[u_loc(scf, n, n)] = un[n]; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine transform restores triangular structure of matrix U. alpar@1: * It is a driver to the routines bg_transform and gr_transform (see alpar@1: * comments to these routines above). */ alpar@1: alpar@1: static void transform(SCF *scf, int k, double un[]) alpar@1: { switch (scf->t_opt) alpar@1: { case SCF_TBG: alpar@1: bg_transform(scf, k, un); alpar@1: break; alpar@1: case SCF_TGR: alpar@1: gr_transform(scf, k, un); alpar@1: break; alpar@1: default: alpar@1: xassert(scf != scf); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine estimate_rank estimates the rank of matrix C. alpar@1: * alpar@1: * Since all transformations applied to matrix F are non-singular, alpar@1: * and F is assumed to be well conditioned, from the main equaility alpar@1: * F * C = U * P it follows that rank(C) = rank(U), where rank(U) is alpar@1: * estimated as the number of non-zero diagonal elements of U. */ alpar@1: alpar@1: static int estimate_rank(SCF *scf) alpar@1: { int n_max = scf->n_max; alpar@1: int n = scf->n; alpar@1: double *u = scf->u; alpar@1: int i, ii, inc, rank = 0; alpar@1: for (i = 1, ii = u_loc(scf, i, i), inc = n_max; i <= n; alpar@1: i++, ii += inc, inc--) alpar@1: if (u[ii] != 0.0) rank++; alpar@1: return rank; alpar@1: } alpar@1: alpar@1: #if _GLPSCF_DEBUG alpar@1: /*********************************************************************** alpar@1: * The routine check_error computes the maximal relative error between alpar@1: * left- and right-hand sides of the main equality F * C = U * P. (This alpar@1: * routine is intended only for debugging.) */ alpar@1: alpar@1: static void check_error(SCF *scf, const char *func) alpar@1: { int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int *p = scf->p; alpar@1: double *c = scf->c; alpar@1: int i, j, k; alpar@1: double d, dmax = 0.0, s, t; alpar@1: xassert(c != NULL); alpar@1: for (i = 1; i <= n; i++) alpar@1: { for (j = 1; j <= n; j++) alpar@1: { /* compute element (i,j) of product F * C */ alpar@1: s = 0.0; alpar@1: for (k = 1; k <= n; k++) alpar@1: s += f[f_loc(scf, i, k)] * c[f_loc(scf, k, j)]; alpar@1: /* compute element (i,j) of product U * P */ alpar@1: k = p[j]; alpar@1: t = (i <= k ? u[u_loc(scf, i, k)] : 0.0); alpar@1: /* compute the maximal relative error */ alpar@1: d = fabs(s - t) / (1.0 + fabs(t)); alpar@1: if (dmax < d) dmax = d; alpar@1: } alpar@1: } alpar@1: if (dmax > 1e-8) alpar@1: xprintf("%s: dmax = %g; relative error too large\n", func, alpar@1: dmax); alpar@1: return; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * scf_update_exp - update factorization on expanding C alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpscf.h" alpar@1: * int scf_update_exp(SCF *scf, const double x[], const double y[], alpar@1: * double z); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine scf_update_exp updates the factorization of matrix C on alpar@1: * expanding it by adding a new row and column as follows: alpar@1: * alpar@1: * ( C x ) alpar@1: * new C = ( ) alpar@1: * ( y' z ) alpar@1: * alpar@1: * where x[1,...,n] is a new column, y[1,...,n] is a new row, and z is alpar@1: * a new diagonal element. alpar@1: * alpar@1: * If on entry the factorization is empty, the parameters x and y can alpar@1: * be specified as NULL. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * 0 The factorization has been successfully updated. alpar@1: * alpar@1: * SCF_ESING alpar@1: * The factorization has been successfully updated, however, new alpar@1: * matrix C is singular within working precision. Note that the new alpar@1: * factorization remains valid. alpar@1: * alpar@1: * SCF_ELIMIT alpar@1: * There is not enough room to expand the factorization, because alpar@1: * n = n_max. The factorization remains unchanged. alpar@1: * alpar@1: * ALGORITHM alpar@1: * alpar@1: * We can see that: alpar@1: * alpar@1: * ( F 0 ) ( C x ) ( FC Fx ) ( UP Fx ) alpar@1: * ( ) ( ) = ( ) = ( ) = alpar@1: * ( 0 1 ) ( y' z ) ( y' z ) ( y' z ) alpar@1: * alpar@1: * ( U Fx ) ( P 0 ) alpar@1: * = ( ) ( ), alpar@1: * ( y'P' z ) ( 0 1 ) alpar@1: * alpar@1: * therefore to keep the main equality F * C = U * P we can take: alpar@1: * alpar@1: * ( F 0 ) ( U Fx ) ( P 0 ) alpar@1: * new F = ( ), new U = ( ), new P = ( ), alpar@1: * ( 0 1 ) ( y'P' z ) ( 0 1 ) alpar@1: * alpar@1: * and eliminate the row spike y'P' in the last row of new U to restore alpar@1: * its upper triangular structure. */ alpar@1: alpar@1: int scf_update_exp(SCF *scf, const double x[], const double y[], alpar@1: double z) alpar@1: { int n_max = scf->n_max; alpar@1: int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int *p = scf->p; alpar@1: #if _GLPSCF_DEBUG alpar@1: double *c = scf->c; alpar@1: #endif alpar@1: double *un = scf->w; alpar@1: int i, ij, in, j, k, nj, ret = 0; alpar@1: double t; alpar@1: /* check if the factorization can be expanded */ alpar@1: if (n == n_max) alpar@1: { /* there is not enough room */ alpar@1: ret = SCF_ELIMIT; alpar@1: goto done; alpar@1: } alpar@1: /* increase the order of the factorization */ alpar@1: scf->n = ++n; alpar@1: /* fill new zero column of matrix F */ alpar@1: for (i = 1, in = f_loc(scf, i, n); i < n; i++, in += n_max) alpar@1: f[in] = 0.0; alpar@1: /* fill new zero row of matrix F */ alpar@1: for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++) alpar@1: f[nj] = 0.0; alpar@1: /* fill new unity diagonal element of matrix F */ alpar@1: f[f_loc(scf, n, n)] = 1.0; alpar@1: /* compute new column of matrix U, which is (old F) * x */ alpar@1: for (i = 1; i < n; i++) alpar@1: { /* u[i,n] := (i-th row of old F) * x */ alpar@1: t = 0.0; alpar@1: for (j = 1, ij = f_loc(scf, i, 1); j < n; j++, ij++) alpar@1: t += f[ij] * x[j]; alpar@1: u[u_loc(scf, i, n)] = t; alpar@1: } alpar@1: /* compute new (spiked) row of matrix U, which is (old P) * y */ alpar@1: for (j = 1; j < n; j++) un[j] = y[p[j]]; alpar@1: /* store new diagonal element of matrix U, which is z */ alpar@1: un[n] = z; alpar@1: /* expand matrix P */ alpar@1: p[n] = n; alpar@1: #if _GLPSCF_DEBUG alpar@1: /* expand matrix C */ alpar@1: /* fill its new column, which is x */ alpar@1: for (i = 1, in = f_loc(scf, i, n); i < n; i++, in += n_max) alpar@1: c[in] = x[i]; alpar@1: /* fill its new row, which is y */ alpar@1: for (j = 1, nj = f_loc(scf, n, j); j < n; j++, nj++) alpar@1: c[nj] = y[j]; alpar@1: /* fill its new diagonal element, which is z */ alpar@1: c[f_loc(scf, n, n)] = z; alpar@1: #endif alpar@1: /* restore upper triangular structure of matrix U */ alpar@1: for (k = 1; k < n; k++) alpar@1: if (un[k] != 0.0) break; alpar@1: transform(scf, k, un); alpar@1: /* estimate the rank of matrices C and U */ alpar@1: scf->rank = estimate_rank(scf); alpar@1: if (scf->rank != n) ret = SCF_ESING; alpar@1: #if _GLPSCF_DEBUG alpar@1: /* check that the factorization is accurate enough */ alpar@1: check_error(scf, "scf_update_exp"); alpar@1: #endif alpar@1: done: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine solve solves the system C * x = b. alpar@1: * alpar@1: * From the main equation F * C = U * P it follows that: alpar@1: * alpar@1: * C * x = b => F * C * x = F * b => U * P * x = F * b => alpar@1: * alpar@1: * P * x = inv(U) * F * b => x = P' * inv(U) * F * b. alpar@1: * alpar@1: * On entry the array x contains right-hand side vector b. On exit this alpar@1: * array contains solution vector x. */ alpar@1: alpar@1: static void solve(SCF *scf, double x[]) alpar@1: { int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int *p = scf->p; alpar@1: double *y = scf->w; alpar@1: int i, j, ij; alpar@1: double t; alpar@1: /* y := F * b */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { /* y[i] = (i-th row of F) * b */ alpar@1: t = 0.0; alpar@1: for (j = 1, ij = f_loc(scf, i, 1); j <= n; j++, ij++) alpar@1: t += f[ij] * x[j]; alpar@1: y[i] = t; alpar@1: } alpar@1: /* y := inv(U) * y */ alpar@1: for (i = n; i >= 1; i--) alpar@1: { t = y[i]; alpar@1: for (j = n, ij = u_loc(scf, i, n); j > i; j--, ij--) alpar@1: t -= u[ij] * y[j]; alpar@1: y[i] = t / u[ij]; alpar@1: } alpar@1: /* x := P' * y */ alpar@1: for (i = 1; i <= n; i++) x[p[i]] = y[i]; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * The routine tsolve solves the transposed system C' * x = b. alpar@1: * alpar@1: * From the main equation F * C = U * P it follows that: alpar@1: * alpar@1: * C' * F' = P' * U', alpar@1: * alpar@1: * therefore: alpar@1: * alpar@1: * C' * x = b => C' * F' * inv(F') * x = b => alpar@1: * alpar@1: * P' * U' * inv(F') * x = b => U' * inv(F') * x = P * b => alpar@1: * alpar@1: * inv(F') * x = inv(U') * P * b => x = F' * inv(U') * P * b. alpar@1: * alpar@1: * On entry the array x contains right-hand side vector b. On exit this alpar@1: * array contains solution vector x. */ alpar@1: alpar@1: static void tsolve(SCF *scf, double x[]) alpar@1: { int n = scf->n; alpar@1: double *f = scf->f; alpar@1: double *u = scf->u; alpar@1: int *p = scf->p; alpar@1: double *y = scf->w; alpar@1: int i, j, ij; alpar@1: double t; alpar@1: /* y := P * b */ alpar@1: for (i = 1; i <= n; i++) y[i] = x[p[i]]; alpar@1: /* y := inv(U') * y */ alpar@1: for (i = 1; i <= n; i++) alpar@1: { /* compute y[i] */ alpar@1: ij = u_loc(scf, i, i); alpar@1: t = (y[i] /= u[ij]); alpar@1: /* substitute y[i] in other equations */ alpar@1: for (j = i+1, ij++; j <= n; j++, ij++) alpar@1: y[j] -= u[ij] * t; alpar@1: } alpar@1: /* x := F' * y (computed as linear combination of rows of F) */ alpar@1: for (j = 1; j <= n; j++) x[j] = 0.0; alpar@1: for (i = 1; i <= n; i++) alpar@1: { t = y[i]; /* coefficient of linear combination */ alpar@1: for (j = 1, ij = f_loc(scf, i, 1); j <= n; j++, ij++) alpar@1: x[j] += f[ij] * t; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * scf_solve_it - solve either system C * x = b or C' * x = b alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpscf.h" alpar@1: * void scf_solve_it(SCF *scf, int tr, double x[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine scf_solve_it solves either the system C * x = b (if tr alpar@1: * is zero) or the system C' * x = b, where C' is a matrix transposed alpar@1: * to C (if tr is non-zero). C is assumed to be non-singular. alpar@1: * alpar@1: * On entry the array x should contain the right-hand side vector b in alpar@1: * locations x[1], ..., x[n], where n is the order of matrix C. On exit alpar@1: * the array x contains the solution vector x in the same locations. */ alpar@1: alpar@1: void scf_solve_it(SCF *scf, int tr, double x[]) alpar@1: { if (scf->rank < scf->n) alpar@1: xfault("scf_solve_it: singular matrix\n"); alpar@1: if (!tr) alpar@1: solve(scf, x); alpar@1: else alpar@1: tsolve(scf, x); alpar@1: return; alpar@1: } alpar@1: alpar@1: void scf_reset_it(SCF *scf) alpar@1: { /* reset factorization for empty matrix C */ alpar@1: scf->n = scf->rank = 0; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * scf_delete_it - delete Schur complement factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpscf.h" alpar@1: * void scf_delete_it(SCF *scf); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine scf_delete_it deletes the specified factorization and alpar@1: * frees all the memory allocated to this object. */ alpar@1: alpar@1: void scf_delete_it(SCF *scf) alpar@1: { xfree(scf->f); alpar@1: xfree(scf->u); alpar@1: xfree(scf->p); alpar@1: #if _GLPSCF_DEBUG alpar@1: xfree(scf->c); alpar@1: #endif alpar@1: xfree(scf->w); alpar@1: xfree(scf); alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */