1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "./globheads.h"
#include "./protos.h"
#define ALPHA 0.00001
/*--------------------end protos */
int PQperm(csptr mat, int bsize, int *Pord, int *Qord, int *nnod,
double tol) {
/*---------------------------------------------------------------------
| algorithm for nonsymmetric block selection -
|----------------------------------------------------------------------
| Input parameters:
| -----------------
| (mat) = matrix in SpaFmt format
|
| tol = a tolerance for excluding a row from B block
|
| bsize not used here - it is used in arms2..
|
| Output parameters:
| ------------------
| Pord = row permutation array. Row number i will become row number
| Pord[i] in permuted matrix. (Old to new labels)
| Qord = column permutation array. Column number j will become
| row number Qord[j] in permuted matrix.
| [destination lists] - i.e., old to new arrays=
|
| nnod = number of elements in the B-block
|
|---------------------------------------------------------------------*/
/*-------------------- local variables */
int *icor, *jcor, *row;
int i, j, ii, k, col, jj, rnz, nzi, n=mat->n, count, numnode;
double aij, rn, *mrow;
/*-----------------------------------------------------------------------*/
for (j=0; j<n; j++) {
Pord[j] = -1;
Qord[j] = -1;
}
icor = (int *) malloc(n*sizeof(int));
jcor = (int *) malloc(n*sizeof(int));
if ( (icor==NULL) || (jcor==NULL) ) return 1;
numnode = 0;
count = 0;
/*-------------------- wDiag selects candidate entries in a sorted oder */
i = 1;
preSel(mat, icor, jcor, i, tol, &count) ;
/*-------------------- add entries one by one to diagnl */
/* needs recoding so as to scan rows only once instead of 2 */
for (i = 0; i<count; i++){
ii = icor[i];
jj = jcor[i];
if (Qord[jj] != -1) continue;
/*-------------------- */
row = mat->ja[ii];
mrow = mat->ma[ii];
nzi = mat->nzcount[ii] ;
/*-------------------- rnz = already assigned cols (either in L or F) */
rn = fabs(mrow[0]);
rnz = (nzi-1) ;
for (k=0; k < nzi; k++) {
aij = fabs(mrow[k]);
col = row[k];
if (Qord[col] >=0 ) {
rn -= aij;
rnz-- ;
}
else if (Qord[col] == -2) {
rnz--;
}
}
if (rn < 0.0) continue;
Pord[ii] = numnode;
Qord[jj] = numnode;
numnode++;
/*-------------------- acceptance test among others */
for (k=0; k < nzi; k++) {
col = row[k];
if (Qord[col] != -1) continue;
aij = fabs(mrow[k]);
if (rnz*aij > rn)
Qord[col] = -2;
else
rn -= aij;
rnz--;
}
}
/*-------------------- number of B nodes */
*nnod = numnode;
/* printf(" nnod found = %d \n",*nnod); */
/*--------------------------------------------------
| end-main-loop - complete permutation arrays
|-------------------------------------------------*/
for (i=0; i<n; i++)
if (Pord[i] < 0)
Pord[i] = numnode++;
if (numnode != n) {
printf(" ** counting error - type 1 \n"); return 1; }
numnode = *nnod;
for (j=0; j<n; j++)
if (Qord[j] < 0)
Qord[j] = numnode++;
/*-------------------- */
if (numnode != n) {
printf(" ** counting error type 2 \n"); return 1; }
/*-------------------- debugging - check permutations */
/*
printf(" checking P and Q : --> \n") ;
check_perm(n, Pord) ;
check_perm(n, Qord) ;
*/
/*--------------------------------------------------
| clean up before returning
|-------------------------------------------------*/
free(icor);
free(jcor);
return 0;
}
/*---------------------------------------------------------------------
|-----end-of-indsetPQ--------------------------------------------------
|--------------------------------------------------------------------*/
int preSel(csptr mat, int *icor, int *jcor, int job, double tol, int *count)
{
/*---------------------------------------------------------------------
| does a preselection of possible diagonal entries. will return a list
| of "count" bi-indices representing "good" entries to be selected as
| diagonal elements -- the order is important (from best to
| to worst). The list is in the form (icor(ii), jcor(ii))
|
| ON ENTRY:
| mat = matrix in csptr format
| tol = tolerance used for selecting best rows -|
| job = indicates whether or not to permute the max entry in
| each row to first position
| NOTE: CAN RECODE BY HAVING JCOR CARRY THE INDEX IN ROW[I] WHERE
| MAX IS LOCATED..
|
| ON RETURN:
| icor = list of row indices of entries selected
| jcor = list of column indices of entries selected
| count = number of entries selected (size of B block)
|--------------------------------------------------------------------*/
int i, k, kmax, n=mat->n, col, jmax, countL;
int *nz, *jcol;
double *mrow, rownorm, *weight, t, tmax, wmax;
/*--------------------begin */
/*-----------------------------------------------------------------------*/
nz =mat->nzcount;
weight = (double *) malloc(n*sizeof(double));
if ( weight==NULL) return 1;
/*-------------------- compute max entry for each row */
wmax = 0.0;
for (i=0; i<n; i++) {
jcol = mat->ja[i];
mrow = mat->ma[i];
tmax = 0.0;
kmax = 0;
rownorm = 0.0;
for (k = 0; k<nz[i]; k++) {
col = jcol[k] ;
t = fabs(mrow[k]);
if (t != 0.0) {
rownorm += t;
if (tmax < t) {
tmax = t;
kmax = k;
}
}
}
jmax = jcol[kmax];
jcor[i] = jmax;
if (job && kmax != 0) {
t = mrow[kmax];
mrow[kmax] = mrow[0];
mrow[0] = t;
jcol[kmax] = jcol[0];
jcol[0] = jmax;
}
/*-------------------- save max diag. dominance ratio */
t = tmax / rownorm;
if (wmax < t) wmax = t;
weight[i] = t;
/* remove!! ALREADY ASSIGNED */
jcor[i] = jmax;
}
/*-------------------- now select according to tol */
countL = 0;
for (i=0; i<n; i++) {
t = weight[i] ;
col = jcor[i];
if (t < wmax*tol) continue ;
weight[countL] = t /((double) nz[i]) ;
icor[countL] = i;
jcor[countL] = col;
countL++;
}
/*-------------------- sort them */
qsortR2I(weight, icor, jcor, 0, countL-1);
*count = countL;
free(weight);
return 0;
}
/*---------------------------------------------------------------------
|---- end of preSel ---------------------------------------------------
|--------------------------------------------------------------------*/
// adds element nod to independent set
int add2is(int *last, int nod, int *iord, int *riord)
{
(*last)++;
iord[nod] = *last;
riord[*last] = nod;
return 0;
}
// adds element nod to independent set
int add2com(int *nback, int nod, int *iord, int *riord)
{
iord[nod] = *nback;
riord[*nback] = nod;
(*nback)--;
return 0;
}
// defines weights based on diagonal dominance ratios
int weightsC(csptr mat, double *w)
{
int irow, k, n=mat->n, *kj, kz;
double tdia, wmax=0.0, tnorm, *kr;
for (irow=0; irow<n; irow++) {
kz = mat->nzcount[irow];
kr = mat->ma[irow];
kj = mat->ja[irow];
tnorm = 0.0;
tdia = 0.0;
for (k=0; k<kz; k++) {
if (kj[k] == irow) tdia = fabs(kr[k]);
tnorm += fabs(kr[k]);
}
if (tnorm > 0.0)
tnorm = tdia / tnorm;
w[irow] = tnorm;
if (tnorm > wmax) wmax = tnorm;
}
for (irow=0; irow<n; irow++)
w[irow] = w[irow]/wmax;
return 0;
}
|