alpar@1: /* glpfhv.c (LP basis factorization, FHV eta file version) */ 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 "glpfhv.h" alpar@1: #include "glpenv.h" alpar@1: #define xfault xerror alpar@1: alpar@1: /* CAUTION: DO NOT CHANGE THE LIMIT BELOW */ alpar@1: alpar@1: #define M_MAX 100000000 /* = 100*10^6 */ alpar@1: /* maximal order of the basis matrix */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_create_it - create LP basis factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * FHV *fhv_create_it(void); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_create_it creates a program object, which represents alpar@1: * a factorization of LP basis. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine fhv_create_it returns a pointer to the object created. */ alpar@1: alpar@1: FHV *fhv_create_it(void) alpar@1: { FHV *fhv; alpar@1: fhv = xmalloc(sizeof(FHV)); alpar@1: fhv->m_max = fhv->m = 0; alpar@1: fhv->valid = 0; alpar@1: fhv->luf = luf_create_it(); alpar@1: fhv->hh_max = 50; alpar@1: fhv->hh_nfs = 0; alpar@1: fhv->hh_ind = fhv->hh_ptr = fhv->hh_len = NULL; alpar@1: fhv->p0_row = fhv->p0_col = NULL; alpar@1: fhv->cc_ind = NULL; alpar@1: fhv->cc_val = NULL; alpar@1: fhv->upd_tol = 1e-6; alpar@1: fhv->nnz_h = 0; alpar@1: return fhv; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_factorize - compute LP basis factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * int fhv_factorize(FHV *fhv, int m, int (*col)(void *info, int j, alpar@1: * int ind[], double val[]), void *info); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_factorize computes the factorization of the basis alpar@1: * matrix B specified by the routine col. alpar@1: * alpar@1: * The parameter fhv specified the basis factorization data structure alpar@1: * created by the routine fhv_create_it. alpar@1: * alpar@1: * The parameter m specifies the order of B, m > 0. alpar@1: * alpar@1: * The formal routine col specifies the matrix B to be factorized. To alpar@1: * obtain j-th column of A the routine fhv_factorize calls the routine alpar@1: * col with the parameter j (1 <= j <= n). In response the routine col alpar@1: * should store row indices and numerical values of non-zero elements alpar@1: * of j-th column of B to locations ind[1,...,len] and val[1,...,len], alpar@1: * respectively, where len is the number of non-zeros in j-th column alpar@1: * returned on exit. Neither zero nor duplicate elements are allowed. alpar@1: * alpar@1: * The parameter info is a transit pointer passed to the routine col. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * 0 The factorization has been successfully computed. alpar@1: * alpar@1: * FHV_ESING alpar@1: * The specified matrix is singular within the working precision. alpar@1: * alpar@1: * FHV_ECOND alpar@1: * The specified matrix is ill-conditioned. alpar@1: * alpar@1: * For more details see comments to the routine luf_factorize. alpar@1: * alpar@1: * ALGORITHM alpar@1: * alpar@1: * The routine fhv_factorize calls the routine luf_factorize (see the alpar@1: * module GLPLUF), which actually computes LU-factorization of the basis alpar@1: * matrix B in the form alpar@1: * alpar@1: * [B] = (F, V, P, Q), alpar@1: * alpar@1: * where F and V are such matrices that alpar@1: * alpar@1: * B = F * V, alpar@1: * alpar@1: * and P and Q are such permutation matrices that the matrix alpar@1: * alpar@1: * L = P * F * inv(P) alpar@1: * alpar@1: * is lower triangular with unity diagonal, and the matrix alpar@1: * alpar@1: * U = P * V * Q alpar@1: * alpar@1: * is upper triangular. alpar@1: * alpar@1: * In order to build the complete representation of the factorization alpar@1: * (see formula (1) in the file glpfhv.h) the routine fhv_factorize just alpar@1: * additionally sets H = I and P0 = P. */ alpar@1: alpar@1: int fhv_factorize(FHV *fhv, int m, int (*col)(void *info, int j, alpar@1: int ind[], double val[]), void *info) alpar@1: { int ret; alpar@1: if (m < 1) alpar@1: xfault("fhv_factorize: m = %d; invalid parameter\n", m); alpar@1: if (m > M_MAX) alpar@1: xfault("fhv_factorize: m = %d; matrix too big\n", m); alpar@1: fhv->m = m; alpar@1: /* invalidate the factorization */ alpar@1: fhv->valid = 0; alpar@1: /* allocate/reallocate arrays, if necessary */ alpar@1: if (fhv->hh_ind == NULL) alpar@1: fhv->hh_ind = xcalloc(1+fhv->hh_max, sizeof(int)); alpar@1: if (fhv->hh_ptr == NULL) alpar@1: fhv->hh_ptr = xcalloc(1+fhv->hh_max, sizeof(int)); alpar@1: if (fhv->hh_len == NULL) alpar@1: fhv->hh_len = xcalloc(1+fhv->hh_max, sizeof(int)); alpar@1: if (fhv->m_max < m) alpar@1: { if (fhv->p0_row != NULL) xfree(fhv->p0_row); alpar@1: if (fhv->p0_col != NULL) xfree(fhv->p0_col); alpar@1: if (fhv->cc_ind != NULL) xfree(fhv->cc_ind); alpar@1: if (fhv->cc_val != NULL) xfree(fhv->cc_val); alpar@1: fhv->m_max = m + 100; alpar@1: fhv->p0_row = xcalloc(1+fhv->m_max, sizeof(int)); alpar@1: fhv->p0_col = xcalloc(1+fhv->m_max, sizeof(int)); alpar@1: fhv->cc_ind = xcalloc(1+fhv->m_max, sizeof(int)); alpar@1: fhv->cc_val = xcalloc(1+fhv->m_max, sizeof(double)); alpar@1: } alpar@1: /* try to factorize the basis matrix */ alpar@1: switch (luf_factorize(fhv->luf, m, col, info)) alpar@1: { case 0: alpar@1: break; alpar@1: case LUF_ESING: alpar@1: ret = FHV_ESING; alpar@1: goto done; alpar@1: case LUF_ECOND: alpar@1: ret = FHV_ECOND; alpar@1: goto done; alpar@1: default: alpar@1: xassert(fhv != fhv); alpar@1: } alpar@1: /* the basis matrix has been successfully factorized */ alpar@1: fhv->valid = 1; alpar@1: /* H := I */ alpar@1: fhv->hh_nfs = 0; alpar@1: /* P0 := P */ alpar@1: memcpy(&fhv->p0_row[1], &fhv->luf->pp_row[1], sizeof(int) * m); alpar@1: memcpy(&fhv->p0_col[1], &fhv->luf->pp_col[1], sizeof(int) * m); alpar@1: /* currently H has no factors */ alpar@1: fhv->nnz_h = 0; alpar@1: ret = 0; alpar@1: done: /* return to the calling program */ alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_h_solve - solve system H*x = b or H'*x = b alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * void fhv_h_solve(FHV *fhv, int tr, double x[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_h_solve solves either the system H*x = b (if the alpar@1: * flag tr is zero) or the system H'*x = b (if the flag tr is non-zero), alpar@1: * where the matrix H is a component of the factorization specified by alpar@1: * the parameter fhv, H' is a matrix transposed to H. alpar@1: * alpar@1: * On entry the array x should contain elements of the right-hand side alpar@1: * vector b in locations x[1], ..., x[m], where m is the order of the alpar@1: * matrix H. On exit this array will contain elements of the solution alpar@1: * vector x in the same locations. */ alpar@1: alpar@1: void fhv_h_solve(FHV *fhv, int tr, double x[]) alpar@1: { int nfs = fhv->hh_nfs; alpar@1: int *hh_ind = fhv->hh_ind; alpar@1: int *hh_ptr = fhv->hh_ptr; alpar@1: int *hh_len = fhv->hh_len; alpar@1: int *sv_ind = fhv->luf->sv_ind; alpar@1: double *sv_val = fhv->luf->sv_val; alpar@1: int i, k, beg, end, ptr; alpar@1: double temp; alpar@1: if (!fhv->valid) alpar@1: xfault("fhv_h_solve: the factorization is not valid\n"); alpar@1: if (!tr) alpar@1: { /* solve the system H*x = b */ alpar@1: for (k = 1; k <= nfs; k++) alpar@1: { i = hh_ind[k]; alpar@1: temp = x[i]; alpar@1: beg = hh_ptr[k]; alpar@1: end = beg + hh_len[k] - 1; alpar@1: for (ptr = beg; ptr <= end; ptr++) alpar@1: temp -= sv_val[ptr] * x[sv_ind[ptr]]; alpar@1: x[i] = temp; alpar@1: } alpar@1: } alpar@1: else alpar@1: { /* solve the system H'*x = b */ alpar@1: for (k = nfs; k >= 1; k--) alpar@1: { i = hh_ind[k]; alpar@1: temp = x[i]; alpar@1: if (temp == 0.0) continue; alpar@1: beg = hh_ptr[k]; alpar@1: end = beg + hh_len[k] - 1; alpar@1: for (ptr = beg; ptr <= end; ptr++) alpar@1: x[sv_ind[ptr]] -= sv_val[ptr] * temp; alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_ftran - perform forward transformation (solve system B*x = b) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * void fhv_ftran(FHV *fhv, double x[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_ftran performs forward transformation, i.e. solves alpar@1: * the system B*x = b, where B is the basis matrix, x is the vector of alpar@1: * unknowns to be computed, b is the vector of right-hand sides. alpar@1: * alpar@1: * On entry elements of the vector b should be stored in dense format alpar@1: * in locations x[1], ..., x[m], where m is the number of rows. On exit alpar@1: * the routine stores elements of the vector x in the same locations. */ alpar@1: alpar@1: void fhv_ftran(FHV *fhv, double x[]) alpar@1: { int *pp_row = fhv->luf->pp_row; alpar@1: int *pp_col = fhv->luf->pp_col; alpar@1: int *p0_row = fhv->p0_row; alpar@1: int *p0_col = fhv->p0_col; alpar@1: if (!fhv->valid) alpar@1: xfault("fhv_ftran: the factorization is not valid\n"); alpar@1: /* B = F*H*V, therefore inv(B) = inv(V)*inv(H)*inv(F) */ alpar@1: fhv->luf->pp_row = p0_row; alpar@1: fhv->luf->pp_col = p0_col; alpar@1: luf_f_solve(fhv->luf, 0, x); alpar@1: fhv->luf->pp_row = pp_row; alpar@1: fhv->luf->pp_col = pp_col; alpar@1: fhv_h_solve(fhv, 0, x); alpar@1: luf_v_solve(fhv->luf, 0, x); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_btran - perform backward transformation (solve system B'*x = b) alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * void fhv_btran(FHV *fhv, double x[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_btran performs backward transformation, i.e. solves alpar@1: * the system B'*x = b, where B' is a matrix transposed to the basis alpar@1: * matrix B, x is the vector of unknowns to be computed, b is the vector alpar@1: * of right-hand sides. alpar@1: * alpar@1: * On entry elements of the vector b should be stored in dense format alpar@1: * in locations x[1], ..., x[m], where m is the number of rows. On exit alpar@1: * the routine stores elements of the vector x in the same locations. */ alpar@1: alpar@1: void fhv_btran(FHV *fhv, double x[]) alpar@1: { int *pp_row = fhv->luf->pp_row; alpar@1: int *pp_col = fhv->luf->pp_col; alpar@1: int *p0_row = fhv->p0_row; alpar@1: int *p0_col = fhv->p0_col; alpar@1: if (!fhv->valid) alpar@1: xfault("fhv_btran: the factorization is not valid\n"); alpar@1: /* B = F*H*V, therefore inv(B') = inv(F')*inv(H')*inv(V') */ alpar@1: luf_v_solve(fhv->luf, 1, x); alpar@1: fhv_h_solve(fhv, 1, x); alpar@1: fhv->luf->pp_row = p0_row; alpar@1: fhv->luf->pp_col = p0_col; alpar@1: luf_f_solve(fhv->luf, 1, x); alpar@1: fhv->luf->pp_row = pp_row; alpar@1: fhv->luf->pp_col = pp_col; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_update_it - update LP basis factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * int fhv_update_it(FHV *fhv, int j, int len, const int ind[], alpar@1: * const double val[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_update_it updates the factorization of the basis alpar@1: * matrix B after replacing its j-th column by a new vector. alpar@1: * alpar@1: * The parameter j specifies the number of column of B, which has been alpar@1: * replaced, 1 <= j <= m, where m is the order of B. alpar@1: * alpar@1: * Row indices and numerical values of non-zero elements of the new alpar@1: * column of B should be placed in locations ind[1], ..., ind[len] and alpar@1: * val[1], ..., val[len], resp., where len is the number of non-zeros alpar@1: * in the column. Neither zero nor duplicate elements are allowed. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * 0 The factorization has been successfully updated. alpar@1: * alpar@1: * FHV_ESING alpar@1: * The adjacent basis matrix is structurally singular, since after alpar@1: * changing j-th column of matrix V by the new column (see algorithm alpar@1: * below) the case k1 > k2 occured. alpar@1: * alpar@1: * FHV_ECHECK alpar@1: * The factorization is inaccurate, since after transforming k2-th alpar@1: * row of matrix U = P*V*Q, its diagonal element u[k2,k2] is zero or alpar@1: * close to zero, alpar@1: * alpar@1: * FHV_ELIMIT alpar@1: * Maximal number of H factors has been reached. alpar@1: * alpar@1: * FHV_EROOM alpar@1: * Overflow of the sparse vector area. alpar@1: * alpar@1: * In case of non-zero return code the factorization becomes invalid. alpar@1: * It should not be used until it has been recomputed with the routine alpar@1: * fhv_factorize. alpar@1: * alpar@1: * ALGORITHM alpar@1: * alpar@1: * The routine fhv_update_it is based on the transformation proposed by alpar@1: * Forrest and Tomlin. alpar@1: * alpar@1: * Let j-th column of the basis matrix B have been replaced by new alpar@1: * column B[j]. In order to keep the equality B = F*H*V j-th column of alpar@1: * matrix V should be replaced by the column inv(F*H)*B[j]. alpar@1: * alpar@1: * From the standpoint of matrix U = P*V*Q, replacement of j-th column alpar@1: * of matrix V is equivalent to replacement of k1-th column of matrix U, alpar@1: * where k1 is determined by permutation matrix Q. Thus, matrix U loses alpar@1: * its upper triangular form and becomes the following: alpar@1: * alpar@1: * 1 k1 k2 m alpar@1: * 1 x x * x x x x x x x alpar@1: * . x * x x x x x x x alpar@1: * k1 . . * x x x x x x x alpar@1: * . . * x x x x x x x alpar@1: * . . * . x x x x x x alpar@1: * . . * . . x x x x x alpar@1: * . . * . . . x x x x alpar@1: * k2 . . * . . . . x x x alpar@1: * . . . . . . . . x x alpar@1: * m . . . . . . . . . x alpar@1: * alpar@1: * where row index k2 corresponds to the lowest non-zero element of alpar@1: * k1-th column. alpar@1: * alpar@1: * The routine moves rows and columns k1+1, k1+2, ..., k2 of matrix U alpar@1: * by one position to the left and upwards and moves k1-th row and k1-th alpar@1: * column to position k2. As the result of such symmetric permutations alpar@1: * matrix U becomes the following: alpar@1: * alpar@1: * 1 k1 k2 m alpar@1: * 1 x x x x x x x * x x alpar@1: * . x x x x x x * x x alpar@1: * k1 . . x x x x x * x x alpar@1: * . . . x x x x * x x alpar@1: * . . . . x x x * x x alpar@1: * . . . . . x x * x x alpar@1: * . . . . . . x * x x alpar@1: * k2 . . x x x x x * x x alpar@1: * . . . . . . . . x x alpar@1: * m . . . . . . . . . x alpar@1: * alpar@1: * Then the routine performs gaussian elimination to eliminate elements alpar@1: * u[k2,k1], u[k2,k1+1], ..., u[k2,k2-1] using diagonal elements alpar@1: * u[k1,k1], u[k1+1,k1+1], ..., u[k2-1,k2-1] as pivots in the same way alpar@1: * as described in comments to the routine luf_factorize (see the module alpar@1: * GLPLUF). Note that actually all operations are performed on matrix V, alpar@1: * not on matrix U. During the elimination process the routine permutes alpar@1: * neither rows nor columns, so only k2-th row of matrix U is changed. alpar@1: * alpar@1: * To keep the main equality B = F*H*V, each time when the routine alpar@1: * applies elementary gaussian transformation to the transformed row of alpar@1: * matrix V (which corresponds to k2-th row of matrix U), it also adds alpar@1: * a new element (gaussian multiplier) to the current row-like factor alpar@1: * of matrix H, which corresponds to the transformed row of matrix V. */ alpar@1: alpar@1: int fhv_update_it(FHV *fhv, int j, int len, const int ind[], alpar@1: const double val[]) alpar@1: { int m = fhv->m; alpar@1: LUF *luf = fhv->luf; alpar@1: int *vr_ptr = luf->vr_ptr; alpar@1: int *vr_len = luf->vr_len; alpar@1: int *vr_cap = luf->vr_cap; alpar@1: double *vr_piv = luf->vr_piv; alpar@1: int *vc_ptr = luf->vc_ptr; alpar@1: int *vc_len = luf->vc_len; alpar@1: int *vc_cap = luf->vc_cap; alpar@1: int *pp_row = luf->pp_row; alpar@1: int *pp_col = luf->pp_col; alpar@1: int *qq_row = luf->qq_row; alpar@1: int *qq_col = luf->qq_col; alpar@1: int *sv_ind = luf->sv_ind; alpar@1: double *sv_val = luf->sv_val; alpar@1: double *work = luf->work; alpar@1: double eps_tol = luf->eps_tol; alpar@1: int *hh_ind = fhv->hh_ind; alpar@1: int *hh_ptr = fhv->hh_ptr; alpar@1: int *hh_len = fhv->hh_len; alpar@1: int *p0_row = fhv->p0_row; alpar@1: int *p0_col = fhv->p0_col; alpar@1: int *cc_ind = fhv->cc_ind; alpar@1: double *cc_val = fhv->cc_val; alpar@1: double upd_tol = fhv->upd_tol; alpar@1: int i, i_beg, i_end, i_ptr, j_beg, j_end, j_ptr, k, k1, k2, p, q, alpar@1: p_beg, p_end, p_ptr, ptr, ret; alpar@1: double f, temp; alpar@1: if (!fhv->valid) alpar@1: xfault("fhv_update_it: the factorization is not valid\n"); alpar@1: if (!(1 <= j && j <= m)) alpar@1: xfault("fhv_update_it: j = %d; column number out of range\n", alpar@1: j); alpar@1: /* check if the new factor of matrix H can be created */ alpar@1: if (fhv->hh_nfs == fhv->hh_max) alpar@1: { /* maximal number of updates has been reached */ alpar@1: fhv->valid = 0; alpar@1: ret = FHV_ELIMIT; alpar@1: goto done; alpar@1: } alpar@1: /* convert new j-th column of B to dense format */ alpar@1: for (i = 1; i <= m; i++) alpar@1: cc_val[i] = 0.0; alpar@1: for (k = 1; k <= len; k++) alpar@1: { i = ind[k]; alpar@1: if (!(1 <= i && i <= m)) alpar@1: xfault("fhv_update_it: ind[%d] = %d; row number out of rang" alpar@1: "e\n", k, i); alpar@1: if (cc_val[i] != 0.0) alpar@1: xfault("fhv_update_it: ind[%d] = %d; duplicate row index no" alpar@1: "t allowed\n", k, i); alpar@1: if (val[k] == 0.0) alpar@1: xfault("fhv_update_it: val[%d] = %g; zero element not allow" alpar@1: "ed\n", k, val[k]); alpar@1: cc_val[i] = val[k]; alpar@1: } alpar@1: /* new j-th column of V := inv(F * H) * (new B[j]) */ alpar@1: fhv->luf->pp_row = p0_row; alpar@1: fhv->luf->pp_col = p0_col; alpar@1: luf_f_solve(fhv->luf, 0, cc_val); alpar@1: fhv->luf->pp_row = pp_row; alpar@1: fhv->luf->pp_col = pp_col; alpar@1: fhv_h_solve(fhv, 0, cc_val); alpar@1: /* convert new j-th column of V to sparse format */ alpar@1: len = 0; alpar@1: for (i = 1; i <= m; i++) alpar@1: { temp = cc_val[i]; alpar@1: if (temp == 0.0 || fabs(temp) < eps_tol) continue; alpar@1: len++, cc_ind[len] = i, cc_val[len] = temp; alpar@1: } alpar@1: /* clear old content of j-th column of matrix V */ alpar@1: j_beg = vc_ptr[j]; alpar@1: j_end = j_beg + vc_len[j] - 1; alpar@1: for (j_ptr = j_beg; j_ptr <= j_end; j_ptr++) alpar@1: { /* get row index of v[i,j] */ alpar@1: i = sv_ind[j_ptr]; alpar@1: /* find v[i,j] in the i-th row */ alpar@1: i_beg = vr_ptr[i]; alpar@1: i_end = i_beg + vr_len[i] - 1; alpar@1: for (i_ptr = i_beg; sv_ind[i_ptr] != j; i_ptr++) /* nop */; alpar@1: xassert(i_ptr <= i_end); alpar@1: /* remove v[i,j] from the i-th row */ alpar@1: sv_ind[i_ptr] = sv_ind[i_end]; alpar@1: sv_val[i_ptr] = sv_val[i_end]; alpar@1: vr_len[i]--; alpar@1: } alpar@1: /* now j-th column of matrix V is empty */ alpar@1: luf->nnz_v -= vc_len[j]; alpar@1: vc_len[j] = 0; alpar@1: /* add new elements of j-th column of matrix V to corresponding alpar@1: row lists; determine indices k1 and k2 */ alpar@1: k1 = qq_row[j], k2 = 0; alpar@1: for (ptr = 1; ptr <= len; ptr++) alpar@1: { /* get row index of v[i,j] */ alpar@1: i = cc_ind[ptr]; alpar@1: /* at least one unused location is needed in i-th row */ alpar@1: if (vr_len[i] + 1 > vr_cap[i]) alpar@1: { if (luf_enlarge_row(luf, i, vr_len[i] + 10)) alpar@1: { /* overflow of the sparse vector area */ alpar@1: fhv->valid = 0; alpar@1: luf->new_sva = luf->sv_size + luf->sv_size; alpar@1: xassert(luf->new_sva > luf->sv_size); alpar@1: ret = FHV_EROOM; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* add v[i,j] to i-th row */ alpar@1: i_ptr = vr_ptr[i] + vr_len[i]; alpar@1: sv_ind[i_ptr] = j; alpar@1: sv_val[i_ptr] = cc_val[ptr]; alpar@1: vr_len[i]++; alpar@1: /* adjust index k2 */ alpar@1: if (k2 < pp_col[i]) k2 = pp_col[i]; alpar@1: } alpar@1: /* capacity of j-th column (which is currently empty) should be alpar@1: not less than len locations */ alpar@1: if (vc_cap[j] < len) alpar@1: { if (luf_enlarge_col(luf, j, len)) alpar@1: { /* overflow of the sparse vector area */ alpar@1: fhv->valid = 0; alpar@1: luf->new_sva = luf->sv_size + luf->sv_size; alpar@1: xassert(luf->new_sva > luf->sv_size); alpar@1: ret = FHV_EROOM; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* add new elements of matrix V to j-th column list */ alpar@1: j_ptr = vc_ptr[j]; alpar@1: memmove(&sv_ind[j_ptr], &cc_ind[1], len * sizeof(int)); alpar@1: memmove(&sv_val[j_ptr], &cc_val[1], len * sizeof(double)); alpar@1: vc_len[j] = len; alpar@1: luf->nnz_v += len; alpar@1: /* if k1 > k2, diagonal element u[k2,k2] of matrix U is zero and alpar@1: therefore the adjacent basis matrix is structurally singular */ alpar@1: if (k1 > k2) alpar@1: { fhv->valid = 0; alpar@1: ret = FHV_ESING; alpar@1: goto done; alpar@1: } alpar@1: /* perform implicit symmetric permutations of rows and columns of alpar@1: matrix U */ alpar@1: i = pp_row[k1], j = qq_col[k1]; alpar@1: for (k = k1; k < k2; k++) alpar@1: { pp_row[k] = pp_row[k+1], pp_col[pp_row[k]] = k; alpar@1: qq_col[k] = qq_col[k+1], qq_row[qq_col[k]] = k; alpar@1: } alpar@1: pp_row[k2] = i, pp_col[i] = k2; alpar@1: qq_col[k2] = j, qq_row[j] = k2; alpar@1: /* now i-th row of the matrix V is k2-th row of matrix U; since alpar@1: no pivoting is used, only this row will be transformed */ alpar@1: /* copy elements of i-th row of matrix V to the working array and alpar@1: remove these elements from matrix V */ alpar@1: for (j = 1; j <= m; j++) work[j] = 0.0; alpar@1: i_beg = vr_ptr[i]; alpar@1: i_end = i_beg + vr_len[i] - 1; alpar@1: for (i_ptr = i_beg; i_ptr <= i_end; i_ptr++) alpar@1: { /* get column index of v[i,j] */ alpar@1: j = sv_ind[i_ptr]; alpar@1: /* store v[i,j] to the working array */ alpar@1: work[j] = sv_val[i_ptr]; alpar@1: /* find v[i,j] in the j-th column */ alpar@1: j_beg = vc_ptr[j]; alpar@1: j_end = j_beg + vc_len[j] - 1; alpar@1: for (j_ptr = j_beg; sv_ind[j_ptr] != i; j_ptr++) /* nop */; alpar@1: xassert(j_ptr <= j_end); alpar@1: /* remove v[i,j] from the j-th column */ alpar@1: sv_ind[j_ptr] = sv_ind[j_end]; alpar@1: sv_val[j_ptr] = sv_val[j_end]; alpar@1: vc_len[j]--; alpar@1: } alpar@1: /* now i-th row of matrix V is empty */ alpar@1: luf->nnz_v -= vr_len[i]; alpar@1: vr_len[i] = 0; alpar@1: /* create the next row-like factor of the matrix H; this factor alpar@1: corresponds to i-th (transformed) row */ alpar@1: fhv->hh_nfs++; alpar@1: hh_ind[fhv->hh_nfs] = i; alpar@1: /* hh_ptr[] will be set later */ alpar@1: hh_len[fhv->hh_nfs] = 0; alpar@1: /* up to (k2 - k1) free locations are needed to add new elements alpar@1: to the non-trivial row of the row-like factor */ alpar@1: if (luf->sv_end - luf->sv_beg < k2 - k1) alpar@1: { luf_defrag_sva(luf); alpar@1: if (luf->sv_end - luf->sv_beg < k2 - k1) alpar@1: { /* overflow of the sparse vector area */ alpar@1: fhv->valid = luf->valid = 0; alpar@1: luf->new_sva = luf->sv_size + luf->sv_size; alpar@1: xassert(luf->new_sva > luf->sv_size); alpar@1: ret = FHV_EROOM; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* eliminate subdiagonal elements of matrix U */ alpar@1: for (k = k1; k < k2; k++) alpar@1: { /* v[p,q] = u[k,k] */ alpar@1: p = pp_row[k], q = qq_col[k]; alpar@1: /* this is the crucial point, where even tiny non-zeros should alpar@1: not be dropped */ alpar@1: if (work[q] == 0.0) continue; alpar@1: /* compute gaussian multiplier f = v[i,q] / v[p,q] */ alpar@1: f = work[q] / vr_piv[p]; alpar@1: /* perform gaussian transformation: alpar@1: (i-th row) := (i-th row) - f * (p-th row) alpar@1: in order to eliminate v[i,q] = u[k2,k] */ alpar@1: p_beg = vr_ptr[p]; alpar@1: p_end = p_beg + vr_len[p] - 1; alpar@1: for (p_ptr = p_beg; p_ptr <= p_end; p_ptr++) alpar@1: work[sv_ind[p_ptr]] -= f * sv_val[p_ptr]; alpar@1: /* store new element (gaussian multiplier that corresponds to alpar@1: p-th row) in the current row-like factor */ alpar@1: luf->sv_end--; alpar@1: sv_ind[luf->sv_end] = p; alpar@1: sv_val[luf->sv_end] = f; alpar@1: hh_len[fhv->hh_nfs]++; alpar@1: } alpar@1: /* set pointer to the current row-like factor of the matrix H alpar@1: (if no elements were added to this factor, it is unity matrix alpar@1: and therefore can be discarded) */ alpar@1: if (hh_len[fhv->hh_nfs] == 0) alpar@1: fhv->hh_nfs--; alpar@1: else alpar@1: { hh_ptr[fhv->hh_nfs] = luf->sv_end; alpar@1: fhv->nnz_h += hh_len[fhv->hh_nfs]; alpar@1: } alpar@1: /* store new pivot which corresponds to u[k2,k2] */ alpar@1: vr_piv[i] = work[qq_col[k2]]; alpar@1: /* new elements of i-th row of matrix V (which are non-diagonal alpar@1: elements u[k2,k2+1], ..., u[k2,m] of matrix U = P*V*Q) now are alpar@1: contained in the working array; add them to matrix V */ alpar@1: len = 0; alpar@1: for (k = k2+1; k <= m; k++) alpar@1: { /* get column index and value of v[i,j] = u[k2,k] */ alpar@1: j = qq_col[k]; alpar@1: temp = work[j]; alpar@1: /* if v[i,j] is close to zero, skip it */ alpar@1: if (fabs(temp) < eps_tol) continue; alpar@1: /* at least one unused location is needed in j-th column */ alpar@1: if (vc_len[j] + 1 > vc_cap[j]) alpar@1: { if (luf_enlarge_col(luf, j, vc_len[j] + 10)) alpar@1: { /* overflow of the sparse vector area */ alpar@1: fhv->valid = 0; alpar@1: luf->new_sva = luf->sv_size + luf->sv_size; alpar@1: xassert(luf->new_sva > luf->sv_size); alpar@1: ret = FHV_EROOM; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* add v[i,j] to j-th column */ alpar@1: j_ptr = vc_ptr[j] + vc_len[j]; alpar@1: sv_ind[j_ptr] = i; alpar@1: sv_val[j_ptr] = temp; alpar@1: vc_len[j]++; alpar@1: /* also store v[i,j] to the auxiliary array */ alpar@1: len++, cc_ind[len] = j, cc_val[len] = temp; alpar@1: } alpar@1: /* capacity of i-th row (which is currently empty) should be not alpar@1: less than len locations */ alpar@1: if (vr_cap[i] < len) alpar@1: { if (luf_enlarge_row(luf, i, len)) alpar@1: { /* overflow of the sparse vector area */ alpar@1: fhv->valid = 0; alpar@1: luf->new_sva = luf->sv_size + luf->sv_size; alpar@1: xassert(luf->new_sva > luf->sv_size); alpar@1: ret = FHV_EROOM; alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: /* add new elements to i-th row list */ alpar@1: i_ptr = vr_ptr[i]; alpar@1: memmove(&sv_ind[i_ptr], &cc_ind[1], len * sizeof(int)); alpar@1: memmove(&sv_val[i_ptr], &cc_val[1], len * sizeof(double)); alpar@1: vr_len[i] = len; alpar@1: luf->nnz_v += len; alpar@1: /* updating is finished; check that diagonal element u[k2,k2] is alpar@1: not very small in absolute value among other elements in k2-th alpar@1: row and k2-th column of matrix U = P*V*Q */ alpar@1: /* temp = max(|u[k2,*]|, |u[*,k2]|) */ alpar@1: temp = 0.0; alpar@1: /* walk through k2-th row of U which is i-th row of V */ alpar@1: i = pp_row[k2]; alpar@1: i_beg = vr_ptr[i]; alpar@1: i_end = i_beg + vr_len[i] - 1; alpar@1: for (i_ptr = i_beg; i_ptr <= i_end; i_ptr++) alpar@1: if (temp < fabs(sv_val[i_ptr])) temp = fabs(sv_val[i_ptr]); alpar@1: /* walk through k2-th column of U which is j-th column of V */ alpar@1: j = qq_col[k2]; alpar@1: j_beg = vc_ptr[j]; alpar@1: j_end = j_beg + vc_len[j] - 1; alpar@1: for (j_ptr = j_beg; j_ptr <= j_end; j_ptr++) alpar@1: if (temp < fabs(sv_val[j_ptr])) temp = fabs(sv_val[j_ptr]); alpar@1: /* check that u[k2,k2] is not very small */ alpar@1: if (fabs(vr_piv[i]) < upd_tol * temp) alpar@1: { /* the factorization seems to be inaccurate and therefore must alpar@1: be recomputed */ alpar@1: fhv->valid = 0; alpar@1: ret = FHV_ECHECK; alpar@1: goto done; alpar@1: } alpar@1: /* the factorization has been successfully updated */ alpar@1: ret = 0; alpar@1: done: /* return to the calling program */ alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * fhv_delete_it - delete LP basis factorization alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glpfhv.h" alpar@1: * void fhv_delete_it(FHV *fhv); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine fhv_delete_it deletes LP basis factorization specified alpar@1: * by the parameter fhv and frees all memory allocated to this program alpar@1: * object. */ alpar@1: alpar@1: void fhv_delete_it(FHV *fhv) alpar@1: { luf_delete_it(fhv->luf); alpar@1: if (fhv->hh_ind != NULL) xfree(fhv->hh_ind); alpar@1: if (fhv->hh_ptr != NULL) xfree(fhv->hh_ptr); alpar@1: if (fhv->hh_len != NULL) xfree(fhv->hh_len); alpar@1: if (fhv->p0_row != NULL) xfree(fhv->p0_row); alpar@1: if (fhv->p0_col != NULL) xfree(fhv->p0_col); alpar@1: if (fhv->cc_ind != NULL) xfree(fhv->cc_ind); alpar@1: if (fhv->cc_val != NULL) xfree(fhv->cc_val); alpar@1: xfree(fhv); alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */