~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/sparse/SuperLU/SRC/sgsrfs.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * -- SuperLU routine (version 3.0) --
 
4
 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
 
5
 * and Lawrence Berkeley National Lab.
 
6
 * October 15, 2003
 
7
 *
 
8
 */
 
9
/*
 
10
 * File name:   sgsrfs.c
 
11
 * History:     Modified from lapack routine SGERFS
 
12
 */
 
13
#include <math.h>
 
14
#include "ssp_defs.h"
 
15
 
 
16
void
 
17
sgsrfs(trans_t trans, SuperMatrix *A, SuperMatrix *L, SuperMatrix *U,
 
18
       int *perm_c, int *perm_r, char *equed, float *R, float *C,
 
19
       SuperMatrix *B, SuperMatrix *X, float *ferr, float *berr,
 
20
       SuperLUStat_t *stat, int *info)
 
21
{
 
22
/*
 
23
 *   Purpose   
 
24
 *   =======   
 
25
 *
 
26
 *   SGSRFS improves the computed solution to a system of linear   
 
27
 *   equations and provides error bounds and backward error estimates for 
 
28
 *   the solution.   
 
29
 *
 
30
 *   If equilibration was performed, the system becomes:
 
31
 *           (diag(R)*A_original*diag(C)) * X = diag(R)*B_original.
 
32
 *
 
33
 *   See supermatrix.h for the definition of 'SuperMatrix' structure.
 
34
 *
 
35
 *   Arguments   
 
36
 *   =========   
 
37
 *
 
38
 * trans   (input) trans_t
 
39
 *          Specifies the form of the system of equations:
 
40
 *          = NOTRANS: A * X = B  (No transpose)
 
41
 *          = TRANS:   A'* X = B  (Transpose)
 
42
 *          = CONJ:    A**H * X = B  (Conjugate transpose)
 
43
 *   
 
44
 *   A       (input) SuperMatrix*
 
45
 *           The original matrix A in the system, or the scaled A if
 
46
 *           equilibration was done. The type of A can be:
 
47
 *           Stype = SLU_NC, Dtype = SLU_S, Mtype = SLU_GE.
 
48
 *    
 
49
 *   L       (input) SuperMatrix*
 
50
 *           The factor L from the factorization Pr*A*Pc=L*U. Use
 
51
 *           compressed row subscripts storage for supernodes, 
 
52
 *           i.e., L has types: Stype = SLU_SC, Dtype = SLU_S, Mtype = SLU_TRLU.
 
53
 * 
 
54
 *   U       (input) SuperMatrix*
 
55
 *           The factor U from the factorization Pr*A*Pc=L*U as computed by
 
56
 *           sgstrf(). Use column-wise storage scheme, 
 
57
 *           i.e., U has types: Stype = SLU_NC, Dtype = SLU_S, Mtype = SLU_TRU.
 
58
 *
 
59
 *   perm_c  (input) int*, dimension (A->ncol)
 
60
 *           Column permutation vector, which defines the 
 
61
 *           permutation matrix Pc; perm_c[i] = j means column i of A is 
 
62
 *           in position j in A*Pc.
 
63
 *
 
64
 *   perm_r  (input) int*, dimension (A->nrow)
 
65
 *           Row permutation vector, which defines the permutation matrix Pr;
 
66
 *           perm_r[i] = j means row i of A is in position j in Pr*A.
 
67
 *
 
68
 *   equed   (input) Specifies the form of equilibration that was done.
 
69
 *           = 'N': No equilibration.
 
70
 *           = 'R': Row equilibration, i.e., A was premultiplied by diag(R).
 
71
 *           = 'C': Column equilibration, i.e., A was postmultiplied by
 
72
 *                  diag(C).
 
73
 *           = 'B': Both row and column equilibration, i.e., A was replaced 
 
74
 *                  by diag(R)*A*diag(C).
 
75
 *
 
76
 *   R       (input) float*, dimension (A->nrow)
 
77
 *           The row scale factors for A.
 
78
 *           If equed = 'R' or 'B', A is premultiplied by diag(R).
 
79
 *           If equed = 'N' or 'C', R is not accessed.
 
80
 * 
 
81
 *   C       (input) float*, dimension (A->ncol)
 
82
 *           The column scale factors for A.
 
83
 *           If equed = 'C' or 'B', A is postmultiplied by diag(C).
 
84
 *           If equed = 'N' or 'R', C is not accessed.
 
85
 *
 
86
 *   B       (input) SuperMatrix*
 
87
 *           B has types: Stype = SLU_DN, Dtype = SLU_S, Mtype = SLU_GE.
 
88
 *           The right hand side matrix B.
 
89
 *           if equed = 'R' or 'B', B is premultiplied by diag(R).
 
90
 *
 
91
 *   X       (input/output) SuperMatrix*
 
92
 *           X has types: Stype = SLU_DN, Dtype = SLU_S, Mtype = SLU_GE.
 
93
 *           On entry, the solution matrix X, as computed by sgstrs().
 
94
 *           On exit, the improved solution matrix X.
 
95
 *           if *equed = 'C' or 'B', X should be premultiplied by diag(C)
 
96
 *               in order to obtain the solution to the original system.
 
97
 *
 
98
 *   FERR    (output) float*, dimension (B->ncol)   
 
99
 *           The estimated forward error bound for each solution vector   
 
100
 *           X(j) (the j-th column of the solution matrix X).   
 
101
 *           If XTRUE is the true solution corresponding to X(j), FERR(j) 
 
102
 *           is an estimated upper bound for the magnitude of the largest 
 
103
 *           element in (X(j) - XTRUE) divided by the magnitude of the   
 
104
 *           largest element in X(j).  The estimate is as reliable as   
 
105
 *           the estimate for RCOND, and is almost always a slight   
 
106
 *           overestimate of the true error.
 
107
 *
 
108
 *   BERR    (output) float*, dimension (B->ncol)   
 
109
 *           The componentwise relative backward error of each solution   
 
110
 *           vector X(j) (i.e., the smallest relative change in   
 
111
 *           any element of A or B that makes X(j) an exact solution).
 
112
 *
 
113
 *   stat     (output) SuperLUStat_t*
 
114
 *            Record the statistics on runtime and floating-point operation count.
 
115
 *            See util.h for the definition of 'SuperLUStat_t'.
 
116
 *
 
117
 *   info    (output) int*   
 
118
 *           = 0:  successful exit   
 
119
 *            < 0:  if INFO = -i, the i-th argument had an illegal value   
 
120
 *
 
121
 *    Internal Parameters   
 
122
 *    ===================   
 
123
 *
 
124
 *    ITMAX is the maximum number of steps of iterative refinement.   
 
125
 *
 
126
 */  
 
127
 
 
128
#define ITMAX 5
 
129
    
 
130
    /* Table of constant values */
 
131
    int    ione = 1;
 
132
    float ndone = -1.;
 
133
    float done = 1.;
 
134
    
 
135
    /* Local variables */
 
136
    NCformat *Astore;
 
137
    float   *Aval;
 
138
    SuperMatrix Bjcol;
 
139
    DNformat *Bstore, *Xstore, *Bjcol_store;
 
140
    float   *Bmat, *Xmat, *Bptr, *Xptr;
 
141
    int      kase;
 
142
    float   safe1, safe2;
 
143
    int      i, j, k, irow, nz, count, notran, rowequ, colequ;
 
144
    int      ldb, ldx, nrhs;
 
145
    float   s, xk, lstres, eps, safmin;
 
146
    char     transc[1];
 
147
    trans_t  transt;
 
148
    float   *work;
 
149
    float   *rwork;
 
150
    int      *iwork;
 
151
    extern double slamch_(char *);
 
152
    extern int slacon_(int *, float *, float *, int *, float *, int *);
 
153
#ifdef _CRAY
 
154
    extern int SCOPY(int *, float *, int *, float *, int *);
 
155
    extern int SSAXPY(int *, float *, float *, int *, float *, int *);
 
156
#else
 
157
    extern int scopy_(int *, float *, int *, float *, int *);
 
158
    extern int saxpy_(int *, float *, float *, int *, float *, int *);
 
159
#endif
 
160
 
 
161
    Astore = A->Store;
 
162
    Aval   = Astore->nzval;
 
163
    Bstore = B->Store;
 
164
    Xstore = X->Store;
 
165
    Bmat   = Bstore->nzval;
 
166
    Xmat   = Xstore->nzval;
 
167
    ldb    = Bstore->lda;
 
168
    ldx    = Xstore->lda;
 
169
    nrhs   = B->ncol;
 
170
    
 
171
    /* Test the input parameters */
 
172
    *info = 0;
 
173
    notran = (trans == NOTRANS);
 
174
    if ( !notran && trans != TRANS && trans != CONJ ) *info = -1;
 
175
    else if ( A->nrow != A->ncol || A->nrow < 0 ||
 
176
              A->Stype != SLU_NC || A->Dtype != SLU_S || A->Mtype != SLU_GE )
 
177
        *info = -2;
 
178
    else if ( L->nrow != L->ncol || L->nrow < 0 ||
 
179
              L->Stype != SLU_SC || L->Dtype != SLU_S || L->Mtype != SLU_TRLU )
 
180
        *info = -3;
 
181
    else if ( U->nrow != U->ncol || U->nrow < 0 ||
 
182
              U->Stype != SLU_NC || U->Dtype != SLU_S || U->Mtype != SLU_TRU )
 
183
        *info = -4;
 
184
    else if ( ldb < SUPERLU_MAX(0, A->nrow) ||
 
185
              B->Stype != SLU_DN || B->Dtype != SLU_S || B->Mtype != SLU_GE )
 
186
        *info = -10;
 
187
    else if ( ldx < SUPERLU_MAX(0, A->nrow) ||
 
188
              X->Stype != SLU_DN || X->Dtype != SLU_S || X->Mtype != SLU_GE )
 
189
        *info = -11;
 
190
    if (*info != 0) {
 
191
        i = -(*info);
 
192
        xerbla_("sgsrfs", &i);
 
193
        return;
 
194
    }
 
195
 
 
196
    /* Quick return if possible */
 
197
    if ( A->nrow == 0 || nrhs == 0) {
 
198
        for (j = 0; j < nrhs; ++j) {
 
199
            ferr[j] = 0.;
 
200
            berr[j] = 0.;
 
201
        }
 
202
        return;
 
203
    }
 
204
 
 
205
    rowequ = lsame_(equed, "R") || lsame_(equed, "B");
 
206
    colequ = lsame_(equed, "C") || lsame_(equed, "B");
 
207
    
 
208
    /* Allocate working space */
 
209
    work = floatMalloc(2*A->nrow);
 
210
    rwork = (float *) SUPERLU_MALLOC( A->nrow * sizeof(float) );
 
211
    iwork = intMalloc(2*A->nrow);
 
212
    if ( !work || !rwork || !iwork ) 
 
213
        ABORT("Malloc fails for work/rwork/iwork.");
 
214
    
 
215
    if ( notran ) {
 
216
        *(unsigned char *)transc = 'N';
 
217
        transt = TRANS;
 
218
    } else {
 
219
        *(unsigned char *)transc = 'T';
 
220
        transt = NOTRANS;
 
221
    }
 
222
 
 
223
    /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
 
224
    nz     = A->ncol + 1;
 
225
    eps    = slamch_("Epsilon");
 
226
    safmin = slamch_("Safe minimum");
 
227
    safe1  = nz * safmin;
 
228
    safe2  = safe1 / eps;
 
229
 
 
230
    /* Compute the number of nonzeros in each row (or column) of A */
 
231
    for (i = 0; i < A->nrow; ++i) iwork[i] = 0;
 
232
    if ( notran ) {
 
233
        for (k = 0; k < A->ncol; ++k)
 
234
            for (i = Astore->colptr[k]; i < Astore->colptr[k+1]; ++i) 
 
235
                ++iwork[Astore->rowind[i]];
 
236
    } else {
 
237
        for (k = 0; k < A->ncol; ++k)
 
238
            iwork[k] = Astore->colptr[k+1] - Astore->colptr[k];
 
239
    }   
 
240
 
 
241
    /* Copy one column of RHS B into Bjcol. */
 
242
    Bjcol.Stype = B->Stype;
 
243
    Bjcol.Dtype = B->Dtype;
 
244
    Bjcol.Mtype = B->Mtype;
 
245
    Bjcol.nrow  = B->nrow;
 
246
    Bjcol.ncol  = 1;
 
247
    Bjcol.Store = (void *) SUPERLU_MALLOC( sizeof(DNformat) );
 
248
    if ( !Bjcol.Store ) ABORT("SUPERLU_MALLOC fails for Bjcol.Store");
 
249
    Bjcol_store = Bjcol.Store;
 
250
    Bjcol_store->lda = ldb;
 
251
    Bjcol_store->nzval = work; /* address aliasing */
 
252
        
 
253
    /* Do for each right hand side ... */
 
254
    for (j = 0; j < nrhs; ++j) {
 
255
        count = 0;
 
256
        lstres = 3.;
 
257
        Bptr = &Bmat[j*ldb];
 
258
        Xptr = &Xmat[j*ldx];
 
259
 
 
260
        while (1) { /* Loop until stopping criterion is satisfied. */
 
261
 
 
262
            /* Compute residual R = B - op(A) * X,   
 
263
               where op(A) = A, A**T, or A**H, depending on TRANS. */
 
264
            
 
265
#ifdef _CRAY
 
266
            SCOPY(&A->nrow, Bptr, &ione, work, &ione);
 
267
#else
 
268
            scopy_(&A->nrow, Bptr, &ione, work, &ione);
 
269
#endif
 
270
            sp_sgemv(transc, ndone, A, Xptr, ione, done, work, ione);
 
271
 
 
272
            /* Compute componentwise relative backward error from formula 
 
273
               max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )   
 
274
               where abs(Z) is the componentwise absolute value of the matrix
 
275
               or vector Z.  If the i-th component of the denominator is less
 
276
               than SAFE2, then SAFE1 is added to the i-th component of the   
 
277
               numerator and denominator before dividing. */
 
278
 
 
279
            for (i = 0; i < A->nrow; ++i) rwork[i] = fabs( Bptr[i] );
 
280
            
 
281
            /* Compute abs(op(A))*abs(X) + abs(B). */
 
282
            if (notran) {
 
283
                for (k = 0; k < A->ncol; ++k) {
 
284
                    xk = fabs( Xptr[k] );
 
285
                    for (i = Astore->colptr[k]; i < Astore->colptr[k+1]; ++i)
 
286
                        rwork[Astore->rowind[i]] += fabs(Aval[i]) * xk;
 
287
                }
 
288
            } else {
 
289
                for (k = 0; k < A->ncol; ++k) {
 
290
                    s = 0.;
 
291
                    for (i = Astore->colptr[k]; i < Astore->colptr[k+1]; ++i) {
 
292
                        irow = Astore->rowind[i];
 
293
                        s += fabs(Aval[i]) * fabs(Xptr[irow]);
 
294
                    }
 
295
                    rwork[k] += s;
 
296
                }
 
297
            }
 
298
            s = 0.;
 
299
            for (i = 0; i < A->nrow; ++i) {
 
300
                if (rwork[i] > safe2)
 
301
                    s = SUPERLU_MAX( s, fabs(work[i]) / rwork[i] );
 
302
                else
 
303
                    s = SUPERLU_MAX( s, (fabs(work[i]) + safe1) / 
 
304
                                (rwork[i] + safe1) );
 
305
            }
 
306
            berr[j] = s;
 
307
 
 
308
            /* Test stopping criterion. Continue iterating if   
 
309
               1) The residual BERR(J) is larger than machine epsilon, and   
 
310
               2) BERR(J) decreased by at least a factor of 2 during the   
 
311
                  last iteration, and   
 
312
               3) At most ITMAX iterations tried. */
 
313
 
 
314
            if (berr[j] > eps && berr[j] * 2. <= lstres && count < ITMAX) {
 
315
                /* Update solution and try again. */
 
316
                sgstrs (trans, L, U, perm_c, perm_r, &Bjcol, stat, info);
 
317
                
 
318
#ifdef _CRAY
 
319
                SAXPY(&A->nrow, &done, work, &ione,
 
320
                       &Xmat[j*ldx], &ione);
 
321
#else
 
322
                saxpy_(&A->nrow, &done, work, &ione,
 
323
                       &Xmat[j*ldx], &ione);
 
324
#endif
 
325
                lstres = berr[j];
 
326
                ++count;
 
327
            } else {
 
328
                break;
 
329
            }
 
330
        
 
331
        } /* end while */
 
332
 
 
333
        stat->RefineSteps = count;
 
334
 
 
335
        /* Bound error from formula:
 
336
           norm(X - XTRUE) / norm(X) .le. FERR = norm( abs(inv(op(A)))*   
 
337
           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)   
 
338
          where   
 
339
            norm(Z) is the magnitude of the largest component of Z   
 
340
            inv(op(A)) is the inverse of op(A)   
 
341
            abs(Z) is the componentwise absolute value of the matrix or
 
342
               vector Z   
 
343
            NZ is the maximum number of nonzeros in any row of A, plus 1   
 
344
            EPS is machine epsilon   
 
345
 
 
346
          The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))   
 
347
          is incremented by SAFE1 if the i-th component of   
 
348
          abs(op(A))*abs(X) + abs(B) is less than SAFE2.   
 
349
 
 
350
          Use SLACON to estimate the infinity-norm of the matrix   
 
351
             inv(op(A)) * diag(W),   
 
352
          where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) */
 
353
        
 
354
        for (i = 0; i < A->nrow; ++i) rwork[i] = fabs( Bptr[i] );
 
355
        
 
356
        /* Compute abs(op(A))*abs(X) + abs(B). */
 
357
        if ( notran ) {
 
358
            for (k = 0; k < A->ncol; ++k) {
 
359
                xk = fabs( Xptr[k] );
 
360
                for (i = Astore->colptr[k]; i < Astore->colptr[k+1]; ++i)
 
361
                    rwork[Astore->rowind[i]] += fabs(Aval[i]) * xk;
 
362
            }
 
363
        } else {
 
364
            for (k = 0; k < A->ncol; ++k) {
 
365
                s = 0.;
 
366
                for (i = Astore->colptr[k]; i < Astore->colptr[k+1]; ++i) {
 
367
                    irow = Astore->rowind[i];
 
368
                    xk = fabs( Xptr[irow] );
 
369
                    s += fabs(Aval[i]) * xk;
 
370
                }
 
371
                rwork[k] += s;
 
372
            }
 
373
        }
 
374
        
 
375
        for (i = 0; i < A->nrow; ++i)
 
376
            if (rwork[i] > safe2)
 
377
                rwork[i] = fabs(work[i]) + (iwork[i]+1)*eps*rwork[i];
 
378
            else
 
379
                rwork[i] = fabs(work[i])+(iwork[i]+1)*eps*rwork[i]+safe1;
 
380
 
 
381
        kase = 0;
 
382
 
 
383
        do {
 
384
            slacon_(&A->nrow, &work[A->nrow], work,
 
385
                    &iwork[A->nrow], &ferr[j], &kase);
 
386
            if (kase == 0) break;
 
387
 
 
388
            if (kase == 1) {
 
389
                /* Multiply by diag(W)*inv(op(A)**T)*(diag(C) or diag(R)). */
 
390
                if ( notran && colequ )
 
391
                    for (i = 0; i < A->ncol; ++i) work[i] *= C[i];
 
392
                else if ( !notran && rowequ )
 
393
                    for (i = 0; i < A->nrow; ++i) work[i] *= R[i];
 
394
                
 
395
                sgstrs (transt, L, U, perm_c, perm_r, &Bjcol, stat, info);
 
396
                
 
397
                for (i = 0; i < A->nrow; ++i) work[i] *= rwork[i];
 
398
            } else {
 
399
                /* Multiply by (diag(C) or diag(R))*inv(op(A))*diag(W). */
 
400
                for (i = 0; i < A->nrow; ++i) work[i] *= rwork[i];
 
401
                
 
402
                sgstrs (trans, L, U, perm_c, perm_r, &Bjcol, stat, info);
 
403
                
 
404
                if ( notran && colequ )
 
405
                    for (i = 0; i < A->ncol; ++i) work[i] *= C[i];
 
406
                else if ( !notran && rowequ )
 
407
                    for (i = 0; i < A->ncol; ++i) work[i] *= R[i];  
 
408
            }
 
409
            
 
410
        } while ( kase != 0 );
 
411
 
 
412
 
 
413
        /* Normalize error. */
 
414
        lstres = 0.;
 
415
        if ( notran && colequ ) {
 
416
            for (i = 0; i < A->nrow; ++i)
 
417
                lstres = SUPERLU_MAX( lstres, C[i] * fabs( Xptr[i]) );
 
418
        } else if ( !notran && rowequ ) {
 
419
            for (i = 0; i < A->nrow; ++i)
 
420
                lstres = SUPERLU_MAX( lstres, R[i] * fabs( Xptr[i]) );
 
421
        } else {
 
422
            for (i = 0; i < A->nrow; ++i)
 
423
                lstres = SUPERLU_MAX( lstres, fabs( Xptr[i]) );
 
424
        }
 
425
        if ( lstres != 0. )
 
426
            ferr[j] /= lstres;
 
427
 
 
428
    } /* for each RHS j ... */
 
429
    
 
430
    SUPERLU_FREE(work);
 
431
    SUPERLU_FREE(rwork);
 
432
    SUPERLU_FREE(iwork);
 
433
    SUPERLU_FREE(Bjcol.Store);
 
434
 
 
435
    return;
 
436
 
 
437
} /* sgsrfs */