kusano 7d535a
kusano 7d535a
/*! @file dgsequ.c
kusano 7d535a
 * \brief Computes row and column scalings
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 * -- SuperLU routine (version 2.0) --
kusano 7d535a
 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
kusano 7d535a
 * and Lawrence Berkeley National Lab.
kusano 7d535a
 * November 15, 1997
kusano 7d535a
 *
kusano 7d535a
 * Modified from LAPACK routine DGEEQU
kusano 7d535a
 * 
kusano 7d535a
 */
kusano 7d535a
/*
kusano 7d535a
 * File name:	dgsequ.c
kusano 7d535a
 * History:     Modified from LAPACK routine DGEEQU
kusano 7d535a
 */
kusano 7d535a
#include <math.h></math.h>
kusano 7d535a
#include "slu_ddefs.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*! \brief
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 * Purpose   
kusano 7d535a
 *   =======   
kusano 7d535a
 *
kusano 7d535a
 *   DGSEQU computes row and column scalings intended to equilibrate an   
kusano 7d535a
 *   M-by-N sparse matrix A and reduce its condition number. R returns the row
kusano 7d535a
 *   scale factors and C the column scale factors, chosen to try to make   
kusano 7d535a
 *   the largest element in each row and column of the matrix B with   
kusano 7d535a
 *   elements B(i,j)=R(i)*A(i,j)*C(j) have absolute value 1.   
kusano 7d535a
 *
kusano 7d535a
 *   R(i) and C(j) are restricted to be between SMLNUM = smallest safe   
kusano 7d535a
 *   number and BIGNUM = largest safe number.  Use of these scaling   
kusano 7d535a
 *   factors is not guaranteed to reduce the condition number of A but   
kusano 7d535a
 *   works well in practice.   
kusano 7d535a
 *
kusano 7d535a
 *   See supermatrix.h for the definition of 'SuperMatrix' structure.
kusano 7d535a
 *
kusano 7d535a
 *   Arguments   
kusano 7d535a
 *   =========   
kusano 7d535a
 *
kusano 7d535a
 *   A       (input) SuperMatrix*
kusano 7d535a
 *           The matrix of dimension (A->nrow, A->ncol) whose equilibration
kusano 7d535a
 *           factors are to be computed. The type of A can be:
kusano 7d535a
 *           Stype = SLU_NC; Dtype = SLU_D; Mtype = SLU_GE.
kusano 7d535a
 *	    
kusano 7d535a
 *   R       (output) double*, size A->nrow
kusano 7d535a
 *           If INFO = 0 or INFO > M, R contains the row scale factors   
kusano 7d535a
 *           for A.
kusano 7d535a
 *	    
kusano 7d535a
 *   C       (output) double*, size A->ncol
kusano 7d535a
 *           If INFO = 0,  C contains the column scale factors for A.
kusano 7d535a
 *	    
kusano 7d535a
 *   ROWCND  (output) double*
kusano 7d535a
 *           If INFO = 0 or INFO > M, ROWCND contains the ratio of the   
kusano 7d535a
 *           smallest R(i) to the largest R(i).  If ROWCND >= 0.1 and   
kusano 7d535a
 *           AMAX is neither too large nor too small, it is not worth   
kusano 7d535a
 *           scaling by R.
kusano 7d535a
 *	    
kusano 7d535a
 *   COLCND  (output) double*
kusano 7d535a
 *           If INFO = 0, COLCND contains the ratio of the smallest   
kusano 7d535a
 *           C(i) to the largest C(i).  If COLCND >= 0.1, it is not   
kusano 7d535a
 *           worth scaling by C.
kusano 7d535a
 *	    
kusano 7d535a
 *   AMAX    (output) double*
kusano 7d535a
 *           Absolute value of largest matrix element.  If AMAX is very   
kusano 7d535a
 *           close to overflow or very close to underflow, the matrix   
kusano 7d535a
 *           should be scaled.
kusano 7d535a
 *	    
kusano 7d535a
 *   INFO    (output) int*
kusano 7d535a
 *           = 0:  successful exit   
kusano 7d535a
 *           < 0:  if INFO = -i, the i-th argument had an illegal value   
kusano 7d535a
 *           > 0:  if INFO = i,  and i is   
kusano 7d535a
 *                 <= A->nrow:  the i-th row of A is exactly zero   
kusano 7d535a
 *                 >  A->ncol:  the (i-M)-th column of A is exactly zero   
kusano 7d535a
 *
kusano 7d535a
 *   ===================================================================== 
kusano 7d535a
 * 
kusano 7d535a
 */
kusano 7d535a
void
kusano 7d535a
dgsequ(SuperMatrix *A, double *r, double *c, double *rowcnd,
kusano 7d535a
	double *colcnd, double *amax, int *info)
kusano 7d535a
{
kusano 7d535a
kusano 7d535a
kusano 7d535a
    /* Local variables */
kusano 7d535a
    NCformat *Astore;
kusano 7d535a
    double   *Aval;
kusano 7d535a
    int i, j, irow;
kusano 7d535a
    double rcmin, rcmax;
kusano 7d535a
    double bignum, smlnum;
kusano 7d535a
    extern double dlamch_(char *);
kusano 7d535a
    
kusano 7d535a
    /* Test the input parameters. */
kusano 7d535a
    *info = 0;
kusano 7d535a
    if ( A->nrow < 0 || A->ncol < 0 ||
kusano 7d535a
	 A->Stype != SLU_NC || A->Dtype != SLU_D || A->Mtype != SLU_GE )
kusano 7d535a
	*info = -1;
kusano 7d535a
    if (*info != 0) {
kusano 7d535a
	i = -(*info);
kusano 7d535a
	xerbla_("dgsequ", &i);
kusano 7d535a
	return;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* Quick return if possible */
kusano 7d535a
    if ( A->nrow == 0 || A->ncol == 0 ) {
kusano 7d535a
	*rowcnd = 1.;
kusano 7d535a
	*colcnd = 1.;
kusano 7d535a
	*amax = 0.;
kusano 7d535a
	return;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    Astore = A->Store;
kusano 7d535a
    Aval = Astore->nzval;
kusano 7d535a
    
kusano 7d535a
    /* Get machine constants. */
kusano 7d535a
    smlnum = dlamch_("S");
kusano 7d535a
    bignum = 1. / smlnum;
kusano 7d535a
kusano 7d535a
    /* Compute row scale factors. */
kusano 7d535a
    for (i = 0; i < A->nrow; ++i) r[i] = 0.;
kusano 7d535a
kusano 7d535a
    /* Find the maximum element in each row. */
kusano 7d535a
    for (j = 0; j < A->ncol; ++j)
kusano 7d535a
	for (i = Astore->colptr[j]; i < Astore->colptr[j+1]; ++i) {
kusano 7d535a
	    irow = Astore->rowind[i];
kusano 7d535a
	    r[irow] = SUPERLU_MAX( r[irow], fabs(Aval[i]) );
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
    /* Find the maximum and minimum scale factors. */
kusano 7d535a
    rcmin = bignum;
kusano 7d535a
    rcmax = 0.;
kusano 7d535a
    for (i = 0; i < A->nrow; ++i) {
kusano 7d535a
	rcmax = SUPERLU_MAX(rcmax, r[i]);
kusano 7d535a
	rcmin = SUPERLU_MIN(rcmin, r[i]);
kusano 7d535a
    }
kusano 7d535a
    *amax = rcmax;
kusano 7d535a
kusano 7d535a
    if (rcmin == 0.) {
kusano 7d535a
	/* Find the first zero scale factor and return an error code. */
kusano 7d535a
	for (i = 0; i < A->nrow; ++i)
kusano 7d535a
	    if (r[i] == 0.) {
kusano 7d535a
		*info = i + 1;
kusano 7d535a
		return;
kusano 7d535a
	    }
kusano 7d535a
    } else {
kusano 7d535a
	/* Invert the scale factors. */
kusano 7d535a
	for (i = 0; i < A->nrow; ++i)
kusano 7d535a
	    r[i] = 1. / SUPERLU_MIN( SUPERLU_MAX( r[i], smlnum ), bignum );
kusano 7d535a
	/* Compute ROWCND = min(R(I)) / max(R(I)) */
kusano 7d535a
	*rowcnd = SUPERLU_MAX( rcmin, smlnum ) / SUPERLU_MIN( rcmax, bignum );
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* Compute column scale factors */
kusano 7d535a
    for (j = 0; j < A->ncol; ++j) c[j] = 0.;
kusano 7d535a
kusano 7d535a
    /* Find the maximum element in each column, assuming the row
kusano 7d535a
       scalings computed above. */
kusano 7d535a
    for (j = 0; j < A->ncol; ++j)
kusano 7d535a
	for (i = Astore->colptr[j]; i < Astore->colptr[j+1]; ++i) {
kusano 7d535a
	    irow = Astore->rowind[i];
kusano 7d535a
	    c[j] = SUPERLU_MAX( c[j], fabs(Aval[i]) * r[irow] );
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
    /* Find the maximum and minimum scale factors. */
kusano 7d535a
    rcmin = bignum;
kusano 7d535a
    rcmax = 0.;
kusano 7d535a
    for (j = 0; j < A->ncol; ++j) {
kusano 7d535a
	rcmax = SUPERLU_MAX(rcmax, c[j]);
kusano 7d535a
	rcmin = SUPERLU_MIN(rcmin, c[j]);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    if (rcmin == 0.) {
kusano 7d535a
	/* Find the first zero scale factor and return an error code. */
kusano 7d535a
	for (j = 0; j < A->ncol; ++j)
kusano 7d535a
	    if ( c[j] == 0. ) {
kusano 7d535a
		*info = A->nrow + j + 1;
kusano 7d535a
		return;
kusano 7d535a
	    }
kusano 7d535a
    } else {
kusano 7d535a
	/* Invert the scale factors. */
kusano 7d535a
	for (j = 0; j < A->ncol; ++j)
kusano 7d535a
	    c[j] = 1. / SUPERLU_MIN( SUPERLU_MAX( c[j], smlnum ), bignum);
kusano 7d535a
	/* Compute COLCND = min(C(J)) / max(C(J)) */
kusano 7d535a
	*colcnd = SUPERLU_MAX( rcmin, smlnum ) / SUPERLU_MIN( rcmax, bignum );
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return;
kusano 7d535a
kusano 7d535a
} /* dgsequ */
kusano 7d535a
kusano 7d535a