kusano 7d535a
kusano 7d535a
/*! @file zdiagonal.c
kusano 7d535a
 * \brief Auxiliary routines to work with diagonal elements
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 * -- SuperLU routine (version 4.0) --
kusano 7d535a
 * Lawrence Berkeley National Laboratory
kusano 7d535a
 * June 30, 2009
kusano 7d535a
 * 
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "slu_zdefs.h"
kusano 7d535a
kusano 7d535a
int zfill_diag(int n, NCformat *Astore)
kusano 7d535a
/* fill explicit zeros on the diagonal entries, so that the matrix is not
kusano 7d535a
   structurally singular. */
kusano 7d535a
{
kusano 7d535a
    doublecomplex *nzval = (doublecomplex *)Astore->nzval;
kusano 7d535a
    int *rowind = Astore->rowind;
kusano 7d535a
    int *colptr = Astore->colptr;
kusano 7d535a
    int nnz = colptr[n];
kusano 7d535a
    int fill = 0;
kusano 7d535a
    doublecomplex *nzval_new;
kusano 7d535a
    doublecomplex zero = {1.0, 0.0};
kusano 7d535a
    int *rowind_new;
kusano 7d535a
    int i, j, diag;
kusano 7d535a
kusano 7d535a
    for (i = 0; i < n; i++)
kusano 7d535a
    {
kusano 7d535a
	diag = -1;
kusano 7d535a
	for (j = colptr[i]; j < colptr[i + 1]; j++)
kusano 7d535a
	    if (rowind[j] == i) diag = j;
kusano 7d535a
	if (diag < 0) fill++;
kusano 7d535a
    }
kusano 7d535a
    if (fill)
kusano 7d535a
    {
kusano 7d535a
	nzval_new = doublecomplexMalloc(nnz + fill);
kusano 7d535a
	rowind_new = intMalloc(nnz + fill);
kusano 7d535a
	fill = 0;
kusano 7d535a
	for (i = 0; i < n; i++)
kusano 7d535a
	{
kusano 7d535a
	    diag = -1;
kusano 7d535a
	    for (j = colptr[i] - fill; j < colptr[i + 1]; j++)
kusano 7d535a
	    {
kusano 7d535a
		if ((rowind_new[j + fill] = rowind[j]) == i) diag = j;
kusano 7d535a
		nzval_new[j + fill] = nzval[j];
kusano 7d535a
	    }
kusano 7d535a
	    if (diag < 0)
kusano 7d535a
	    {
kusano 7d535a
		rowind_new[colptr[i + 1] + fill] = i;
kusano 7d535a
		nzval_new[colptr[i + 1] + fill] = zero;
kusano 7d535a
		fill++;
kusano 7d535a
	    }
kusano 7d535a
	    colptr[i + 1] += fill;
kusano 7d535a
	}
kusano 7d535a
	Astore->nzval = nzval_new;
kusano 7d535a
	Astore->rowind = rowind_new;
kusano 7d535a
	SUPERLU_FREE(nzval);
kusano 7d535a
	SUPERLU_FREE(rowind);
kusano 7d535a
    }
kusano 7d535a
    Astore->nnz += fill;
kusano 7d535a
    return fill;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int zdominate(int n, NCformat *Astore)
kusano 7d535a
/* make the matrix diagonally dominant */
kusano 7d535a
{
kusano 7d535a
    doublecomplex *nzval = (doublecomplex *)Astore->nzval;
kusano 7d535a
    int *rowind = Astore->rowind;
kusano 7d535a
    int *colptr = Astore->colptr;
kusano 7d535a
    int nnz = colptr[n];
kusano 7d535a
    int fill = 0;
kusano 7d535a
    doublecomplex *nzval_new;
kusano 7d535a
    int *rowind_new;
kusano 7d535a
    int i, j, diag;
kusano 7d535a
    double s;
kusano 7d535a
kusano 7d535a
    for (i = 0; i < n; i++)
kusano 7d535a
    {
kusano 7d535a
	diag = -1;
kusano 7d535a
	for (j = colptr[i]; j < colptr[i + 1]; j++)
kusano 7d535a
	    if (rowind[j] == i) diag = j;
kusano 7d535a
	if (diag < 0) fill++;
kusano 7d535a
    }
kusano 7d535a
    if (fill)
kusano 7d535a
    {
kusano 7d535a
	nzval_new = doublecomplexMalloc(nnz + fill);
kusano 7d535a
	rowind_new = intMalloc(nnz+ fill);
kusano 7d535a
	fill = 0;
kusano 7d535a
	for (i = 0; i < n; i++)
kusano 7d535a
	{
kusano 7d535a
	    s = 1e-6;
kusano 7d535a
	    diag = -1;
kusano 7d535a
	    for (j = colptr[i] - fill; j < colptr[i + 1]; j++)
kusano 7d535a
	    {
kusano 7d535a
		if ((rowind_new[j + fill] = rowind[j]) == i) diag = j;
kusano 7d535a
                nzval_new[j + fill] = nzval[j];
kusano 7d535a
		s += z_abs1(&nzval_new[j + fill]);
kusano 7d535a
	    }
kusano 7d535a
	    if (diag >= 0) {
kusano 7d535a
		nzval_new[diag+fill].r = s * 3.0;
kusano 7d535a
		nzval_new[diag+fill].i = 0.0;
kusano 7d535a
	    } else {
kusano 7d535a
		rowind_new[colptr[i + 1] + fill] = i;
kusano 7d535a
		nzval_new[colptr[i + 1] + fill].r = s * 3.0;
kusano 7d535a
		nzval_new[colptr[i + 1] + fill].i = 0.0;
kusano 7d535a
		fill++;
kusano 7d535a
	    }
kusano 7d535a
	    colptr[i + 1] += fill;
kusano 7d535a
	}
kusano 7d535a
	Astore->nzval = nzval_new;
kusano 7d535a
	Astore->rowind = rowind_new;
kusano 7d535a
	SUPERLU_FREE(nzval);
kusano 7d535a
	SUPERLU_FREE(rowind);
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
	for (i = 0; i < n; i++)
kusano 7d535a
	{
kusano 7d535a
	    s = 1e-6;
kusano 7d535a
	    diag = -1;
kusano 7d535a
	    for (j = colptr[i]; j < colptr[i + 1]; j++)
kusano 7d535a
	    {
kusano 7d535a
		if (rowind[j] == i) diag = j;
kusano 7d535a
		s += z_abs1(&nzval[j]);
kusano 7d535a
	    }
kusano 7d535a
	    nzval[diag].r = s * 3.0;
kusano 7d535a
	    nzval[diag].i = 0.0;
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
    Astore->nnz += fill;
kusano 7d535a
    return fill;
kusano 7d535a
}