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