00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #include "MALLOC.h"
00073 #include "stack-c.h"
00074 #include "sciumfpack.h"
00075 #include "taucs_scilab.h"
00076 #include "common_umfpack.h"
00077 #include "localization.h"
00078
00079 int AddAdrToList(Adr adr, int it_flag, CellAdr **L)
00080 {
00081 CellAdr *NewCell;
00082 if ( (NewCell = MALLOC(sizeof(CellAdr))) == NULL )
00083 return 0;
00084 else
00085 {
00086 NewCell->adr = adr;
00087 NewCell->it = it_flag;
00088 NewCell->next = *L;
00089 *L = NewCell;
00090 return 1;
00091 }
00092 }
00093
00094 int RetrieveAdrFromList(Adr adr, CellAdr **L, int *it_flag)
00095 {
00096
00097
00098 CellAdr * Cell;
00099
00100 if ( *L == NULL )
00101 return 0;
00102 else if ( (*L)->adr == adr )
00103 {
00104 Cell = *L;
00105 *it_flag = Cell->it;
00106 *L = (*L)->next;
00107 FREE(Cell);
00108 return 1;
00109 }
00110 else
00111 return ( RetrieveAdrFromList(adr, &((*L)->next), it_flag));
00112 }
00113
00114 int IsAdrInList(Adr adr, CellAdr *L, int *it_flag)
00115 {
00116
00117
00118
00119 if ( L == NULL )
00120 return 0;
00121 else if ( L->adr == adr )
00122 {
00123 *it_flag = L->it;
00124 return 1;
00125 }
00126 else
00127 return ( IsAdrInList(adr, L->next, it_flag) );
00128 }
00129
00130
00131 void TransposeMatrix(double A[], int ma, int na, double At[])
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141 int i, j;
00142 for ( j = 0 ; j < ma ; j++ )
00143 for ( i = 0 ; i < na ; i++ )
00144 At[i + na*j] = A[j + ma*i];
00145 }
00146
00147 int sci_sparse_to_ccs_sparse(int num, SciSparse *A, CcsSparse *B)
00148 {
00149 int taille, one=1, nel = A->nel, m = A->m, n = A->n, it = A->it;
00150 int l0, l1, l2, k, kb, i, j, count;
00151
00152 taille = ((2*it+3)*nel + n+1 )/2 + 2;
00153
00154 CreateVar(num, MATRIX_OF_DOUBLE_DATATYPE, &taille, &one, &l0);
00155
00156 B->m = m; B->n = n; B->nel = nel; B->it = it;
00157 B->R = stk(l0);
00158 if ( it == 1 )
00159 {
00160 B->I = stk(l0 + nel); l1 = l0 + 2*nel;
00161 }
00162 else
00163 {
00164 B->I = NULL; l1 = l0 + nel;
00165 }
00166 B->p = (int *) stk(l1); l2 = l1 + (n+1)/2 + 1;
00167 B->irow = (int *) stk(l2);
00168
00169 for ( i = 0 ; i <= n ; i++ )
00170 B->p[i] = 0;
00171
00172 for ( k = 0 ; k < nel ; k++ )
00173 B->p[A->icol[k]]++;
00174
00175 for ( i = 2 ; i <= n ; i++ )
00176 B->p[i] += B->p[i-1];
00177
00178 k = 0;
00179 for ( i = 0 ; i < m ; i++ )
00180 for ( count = 0 ; count < A->mnel[i] ; count++ )
00181 {
00182 j = A->icol[k]-1;
00183 kb = B->p[j];
00184 B->irow[kb] = i;
00185 B->R[kb] = A->R[k];
00186 if (it == 1) B->I[kb] = A->I[k];
00187 B->p[j]++;
00188 k++;
00189 }
00190
00191 for ( i = n-1 ; i > 0 ; i-- )
00192 B->p[i] = B->p[i-1];
00193 B->p[0] = 0;
00194
00195 return 1;
00196 }
00197
00198 char *UmfErrorMes(int num_error)
00199 {
00200
00201 char *mes1 = _("singular matrix");
00202 char *mes2 = _("not enough memory");
00203 char *mes3 = _("internal error");
00204 char *mes4 = _("invalid matrix");
00205
00206
00207
00208
00209 char *mes5 = "unidentified error";
00210
00211 switch (num_error) {
00212 case UMFPACK_WARNING_singular_matrix: return(mes1);
00213 case UMFPACK_ERROR_out_of_memory: return(mes2);
00214 case UMFPACK_ERROR_internal_error: return(mes3);
00215 case UMFPACK_ERROR_invalid_matrix: return(mes4);
00216 default: return(mes5);
00217 };
00218 }
00219
00220 int is_sparse_upper_triangular(SciSparse *A)
00221 {
00222 int i, k=0, nb_elem_row_i;
00223 for ( i = 0 ; i < A->m ; i++ )
00224 {
00225 nb_elem_row_i = A->mnel[i];
00226 if (nb_elem_row_i > 0 && A->icol[k] <= i)
00227 return 0;
00228 k += nb_elem_row_i;
00229 }
00230 return 1;
00231 }
00232
00233 int spd_sci_sparse_to_taucs_sparse(int num, SciSparse *A, taucs_ccs_matrix *B)
00234 {
00235
00236
00237
00238
00239
00240
00241
00242 int taille, one=1, B_nel, n = A->n;
00243 int l0, l1, l2, i, k, l, p, nnz;
00244
00245 if ( A->m != A->n || A->m <= 0 || A->it == 1 )
00246 return ( MAT_IS_NOT_SPD );
00247
00248 if ( is_sparse_upper_triangular(A) )
00249 B_nel = A->nel;
00250 else
00251 B_nel = n + (A->nel - n)/2;
00252 taille = (3*B_nel + n+1 )/2 + 2;
00253
00254 CreateVar(num, MATRIX_OF_DOUBLE_DATATYPE, &taille, &one, &l0);
00255
00256
00257 B->m = n; B->n = n;
00258 B->flags = TAUCS_SYMMETRIC | TAUCS_LOWER;
00259
00260 B->values = stk(l0); l1 = l0 + B_nel;
00261 B->colptr = (int *) stk(l1); l2 = l1 + (n+1)/2 + 1;
00262 B->rowind = (int *) stk(l2);
00263
00264 nnz = 0;
00265 k = 0;
00266 for ( i = 0 ; i < n ; i++ )
00267 {
00268 if ( A->mnel[i] > 0 )
00269 {
00270 l = 0;
00271 while ( l < A->mnel[i] && A->icol[k+l] < i+1 )
00272 l++;
00273 if ( l >= A->mnel[i] )
00274 return ( MAT_IS_NOT_SPD );
00275 else if ( A->icol[k+l] > i+1 )
00276 return ( MAT_IS_NOT_SPD );
00277 else if ( A->R[k+l] <= 0 )
00278 return ( MAT_IS_NOT_SPD );
00279 else
00280 {
00281 if ( nnz + A->mnel[i] - l > B_nel )
00282 return ( MAT_IS_NOT_SPD );
00283 B->colptr[i] = nnz;
00284 for ( p = l ; p < A->mnel[i] ; p++)
00285 {
00286 B->values[nnz] = A->R[k+p];
00287 B->rowind[nnz] = A->icol[k+p]-1;
00288 nnz++;
00289 }
00290 k = k + A->mnel[i];
00291 }
00292 }
00293 else
00294 return ( MAT_IS_NOT_SPD );
00295 }
00296 if ( nnz != B_nel )
00297 return ( MAT_IS_NOT_SPD );
00298
00299 B->colptr[n] = nnz;
00300
00301 return ( A_PRIORI_OK );
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 int test_size_for_sparse(int pos, int m, int it, int nel, int * pl_miss)
00311 {
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 int lw = pos + Top - Rhs, il;
00323
00324 if (lw + 1 >= Bot) return FALSE;
00325
00326
00327
00328 il = iadr(*Lstk(lw )) + 5 + m + nel;
00329 *pl_miss = (sadr(il) + nel*(it+1)) - *Lstk(Bot);
00330
00331 if ( *pl_miss > 0 )
00332 return FALSE;
00333 else
00334 return TRUE;
00335 }
00336
00337 int test_size_for_mat(int pos, int m, int n, int it, int * pl_miss)
00338 {
00339
00340 int lw = pos + Top - Rhs, il;
00341
00342 if (lw + 1 >= Bot) return FALSE;
00343
00344
00345
00346
00347 il = iadr(*Lstk(lw )) + 4;
00348
00349 *pl_miss = (sadr(il) + m*n*(it+1)) - *Lstk(Bot);
00350
00351 if ( *pl_miss > 0 )
00352 return FALSE;
00353 else
00354 return TRUE;
00355 }
00356
00357
00358 void residu_with_prec(SciSparse *A, double x[], double b[], double r[], double *rn)
00359 {
00360
00361 int i, j, k, l;
00362 long double temp, norm2;
00363
00364 norm2 = 0.0;
00365 k = 0;
00366 for ( i = 0 ; i < A->m ; i++ )
00367 {
00368 temp = 0.0;
00369 for ( l = 0 ; l < A->mnel[i] ; l++ )
00370 {
00371 j = A->icol[k] - 1;
00372 temp += (long double) A->R[k] * (long double) x[j];
00373 k++;
00374 }
00375 temp -= (long double) b[i];
00376 r[i] = (double) temp;
00377 norm2 += temp*temp;
00378 }
00379 *rn = (double) sqrt(norm2);
00380 return;
00381 }
00382
00383 void residu_with_prec_for_chol(SciSparse *A, double x[], double b[], double r[],
00384 double *rn, int A_is_upper_triangular, long double wk[])
00385 {
00386
00387
00388 int i, j, k, l;
00389 long double norm2 = 0.0;
00390
00391 if ( ! A_is_upper_triangular )
00392 residu_with_prec(A, x, b, r, rn);
00393 else
00394 {
00395 for ( i = 0 ; i < A->m ; i++ )
00396 wk[i] = -(long double) b[i];
00397 k = 0;
00398 for ( i = 0 ; i < A->m ; i++ )
00399 {
00400 for ( l = 0 ; l < A->mnel[i] ; l++ )
00401 {
00402 j = A->icol[k] - 1;
00403 wk[i] += (long double) A->R[k] * (long double) x[j];
00404 if ( j != i )
00405 wk[j] += (long double) A->R[k] * (long double) x[i];
00406 k++;
00407 }
00408 }
00409 for ( i = 0 ; i < A->m ; i++ )
00410 {
00411 r[i] = (double) wk[i];
00412 norm2 += wk[i]*wk[i];
00413 }
00414 *rn = (double) sqrt(norm2);
00415 }
00416 return;
00417 }
00418
00419
00420 void cmplx_residu_with_prec(SciSparse *A,
00421 double xr[], double xi[],
00422 double br[], double bi[],
00423 double rr[], double ri[],
00424 double *rn)
00425 {
00426
00427 int i, j, k, l;
00428 long double tempr, tempi, norm2;
00429
00430 norm2 = 0.0;
00431 k = 0;
00432 for ( i = 0 ; i < A->m ; i++ )
00433 {
00434 tempr = 0.0; tempi = 0.0;
00435 for ( l = 0 ; l < A->mnel[i] ; l++ )
00436 {
00437 j = A->icol[k] - 1;
00438 tempr += (long double) A->R[k] * (long double) xr[j]
00439 - (long double) A->I[k] * (long double) xi[j];
00440 tempi += (long double) A->I[k] * (long double) xr[j]
00441 + (long double) A->R[k] * (long double) xi[j];
00442 k++;
00443 }
00444 tempr -= (long double) br[i];
00445 tempi -= (long double) bi[i];
00446 rr[i] = (double) tempr;
00447 ri[i] = (double) tempi;
00448 norm2 += tempr*tempr + tempi*tempi;
00449 }
00450 *rn = (double) sqrt(norm2);
00451 return;
00452 }