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 #include <stdio.h>
00038 #include "spmalloc.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #define spINSIDE_SPARSE
00053 #include "spConfig.h"
00054 #include "spmatrix.h"
00055 #include "spDefs.h"
00056
00057
00058
00059
00060
00061 #if DOCUMENTATION
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 void spPrint( eMatrix, PrintReordered, Data, Header )
00134
00135 char *eMatrix;
00136 int PrintReordered, Data, Header;
00137 {
00138 register int J = 0;
00139 int I, Row, Col, Size, Top_, StartCol = 1, StopCol, Columns, ElementCount = 0;
00140 double Magnitude, SmallestDiag, SmallestElement;
00141 double LargestElement = 0.0, LargestDiag = 0.0;
00142 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00143 ElementPtr pElement, pImagElements[PRINTER_WIDTH/10+1];
00144 int *PrintOrdToIntRowMap, *PrintOrdToIntColMap;
00145
00146
00147 ASSERT( IS_SPARSE( Matrix ) );
00148 Size = Matrix->Size;
00149
00150
00151 # if TRANSLATE
00152 Top_ = Matrix->AllocatedExtSize;
00153 #else
00154 Top_ = Matrix->AllocatedSize;
00155 #endif
00156 SPCALLOC( PrintOrdToIntRowMap, int, Top_ + 1 );
00157 SPCALLOC( PrintOrdToIntColMap, int, Top_ + 1 );
00158 if ( PrintOrdToIntRowMap == NULL OR PrintOrdToIntColMap == NULL)
00159 { Matrix->Error = spNO_MEMORY;
00160 return;
00161 }
00162 for (I = 1; I <= Size; I++)
00163 { PrintOrdToIntRowMap[ Matrix->IntToExtRowMap[I] ] = I;
00164 PrintOrdToIntColMap[ Matrix->IntToExtColMap[I] ] = I;
00165 }
00166
00167
00168 for (J = 1, I = 1; I <= Top_; I++)
00169 { if (PrintOrdToIntRowMap[I] != 0)
00170 PrintOrdToIntRowMap[ J++ ] = PrintOrdToIntRowMap[ I ];
00171 }
00172 for (J = 1, I = 1; I <= Top_; I++)
00173 { if (PrintOrdToIntColMap[I] != 0)
00174 PrintOrdToIntColMap[ J++ ] = PrintOrdToIntColMap[ I ];
00175 }
00176
00177
00178 if (Header)
00179 { printf("MATRIX SUMMARY\n\n");
00180 printf("Size of matrix = %1u x %1u.\n", Size, Size);
00181 if ( Matrix->Reordered AND PrintReordered )
00182 printf("Matrix has been reordered.\n");
00183 putchar('\n');
00184
00185 if ( Matrix->Factored )
00186 printf("Matrix after factorization:\n");
00187 else
00188 printf("Matrix before factorization:\n");
00189
00190 SmallestElement = LARGEST_REAL;
00191 SmallestDiag = SmallestElement;
00192 }
00193
00194
00195 Columns = PRINTER_WIDTH;
00196 if (Header) Columns -= 5;
00197 if (Data) Columns = (Columns+1) / 10;
00198
00199
00200
00201
00202
00203 J = 0;
00204 while ( J <= Size )
00205
00206
00207 { StopCol = StartCol + Columns - 1;
00208 if (StopCol > Size)
00209 StopCol = Size;
00210
00211
00212 if (Header)
00213 { if (Data)
00214 { printf(" ");
00215 for (I = StartCol; I <= StopCol; I++)
00216 { if (PrintReordered)
00217 Col = I;
00218 else
00219 Col = PrintOrdToIntColMap[I];
00220 printf(" %9d", Matrix->IntToExtColMap[ Col ]);
00221 }
00222 printf("\n\n");
00223 }
00224 else
00225 { if (PrintReordered)
00226 printf("Columns %1d to %1d.\n",StartCol,StopCol);
00227 else
00228 { printf("Columns %1d to %1d.\n",
00229 Matrix->IntToExtColMap[ PrintOrdToIntColMap[StartCol] ],
00230 Matrix->IntToExtColMap[ PrintOrdToIntColMap[StopCol] ]);
00231 }
00232 }
00233 }
00234
00235
00236 for (I = 1; I <= Size; I++)
00237 { if (PrintReordered)
00238 Row = I;
00239 else
00240 Row = PrintOrdToIntRowMap[I];
00241
00242 if (Header)
00243 { if (PrintReordered AND NOT Data)
00244 printf("%4d", I);
00245 else
00246 printf("%4d", Matrix->IntToExtRowMap[ Row ]);
00247 if (NOT Data) putchar(' ');
00248 }
00249
00250
00251 for (J = StartCol; J <= StopCol; J++)
00252 { if (PrintReordered)
00253 Col = J;
00254 else
00255 Col = PrintOrdToIntColMap[J];
00256
00257 pElement = Matrix->FirstInCol[Col];
00258 while(pElement != NULL AND pElement->Row != Row)
00259 pElement = pElement->NextInCol;
00260
00261 if (Data)
00262 pImagElements[J - StartCol] = pElement;
00263
00264 if (pElement != NULL)
00265
00266
00267 { if (Data)
00268 printf(" %9.3lg", (double)pElement->Real);
00269 else
00270 putchar('x');
00271
00272
00273 if ( (Magnitude = ELEMENT_MAG(pElement)) > LargestElement )
00274 LargestElement = Magnitude;
00275 if ((Magnitude < SmallestElement) AND (Magnitude != 0.0))
00276 SmallestElement = Magnitude;
00277 ElementCount++;
00278 }
00279
00280
00281 else
00282 { if (Data)
00283 printf(" ...");
00284 else
00285 putchar('.');
00286 }
00287 }
00288 putchar('\n');
00289
00290 #if spCOMPLEX
00291 if (Matrix->Complex AND Data)
00292 { printf(" ");
00293 for (J = StartCol; J <= StopCol; J++)
00294 { if (pImagElements[J - StartCol] != NULL)
00295 { printf(" %8.2lgj",
00296 (double)pImagElements[J-StartCol]->Imag);
00297 }
00298 else printf(" ");
00299 }
00300 putchar('\n');
00301 }
00302 #endif
00303 }
00304
00305
00306 StartCol = StopCol;
00307 StartCol++;
00308 putchar('\n');
00309 }
00310 if (Header)
00311 { printf("\nLargest element in matrix = %-1.4lg.\n", LargestElement);
00312 printf("Smallest element in matrix = %-1.4lg.\n", SmallestElement);
00313
00314
00315 for (I = 1; I <= Size; I++)
00316 { if (Matrix->Diag[I] != NULL)
00317 { Magnitude = ELEMENT_MAG( Matrix->Diag[I] );
00318 if ( Magnitude > LargestDiag ) LargestDiag = Magnitude;
00319 if ( Magnitude < SmallestDiag ) SmallestDiag = Magnitude;
00320 }
00321 }
00322
00323
00324 if ( Matrix->Factored )
00325 { printf("\nLargest diagonal element = %-1.4lg.\n", LargestDiag);
00326 printf("Smallest diagonal element = %-1.4lg.\n", SmallestDiag);
00327 }
00328 else
00329 { printf("\nLargest pivot element = %-1.4lg.\n", LargestDiag);
00330 printf("Smallest pivot element = %-1.4lg.\n", SmallestDiag);
00331 }
00332
00333
00334 printf("\nDensity = %2.2lf%%.\n", ((double)(ElementCount * 100)) /
00335 ((double)(Size * Size)));
00336 if (NOT Matrix->NeedsOrdering)
00337 printf("Number of fill-ins = %1d.\n", Matrix->Fillins);
00338 }
00339 putchar('\n');
00340 (void)fflush(stdout);
00341
00342 SPFREE(PrintOrdToIntColMap);
00343 SPFREE(PrintOrdToIntRowMap);
00344 return;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 int
00400 spFileMatrix( eMatrix, File, Label, Reordered, Data, Header )
00401
00402 char *eMatrix, *Label, *File;
00403 int Reordered, Data, Header;
00404 {
00405 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00406 register int I, Size;
00407 register ElementPtr pElement;
00408 int Row, Col, Err;
00409 FILE *pMatrixFile, *fopen();
00410
00411
00412 ASSERT( IS_SPARSE( Matrix ) );
00413
00414
00415 if ((pMatrixFile = fopen(File, "w")) == NULL)
00416 return 0;
00417
00418
00419 Size = Matrix->Size;
00420 if (Header)
00421 { if (Matrix->Factored AND Data)
00422 { Err = fprintf
00423 ( pMatrixFile,
00424 "Warning : The following matrix is factored in to LU form.\n"
00425 );
00426 }
00427 if (Err < 0) return 0;
00428 if (fprintf(pMatrixFile, "%s\n", Label) < 0) return 0;
00429 Err = fprintf( pMatrixFile, "%d\t%s\n", Size,
00430 (Matrix->Complex ? "complex" : "real"));
00431 if (Err < 0) return 0;
00432 }
00433
00434
00435 if (NOT Data)
00436 { for (I = 1; I <= Size; I++)
00437 { pElement = Matrix->FirstInCol[I];
00438 while (pElement != NULL)
00439 { if (Reordered)
00440 { Row = pElement->Row;
00441 Col = I;
00442 }
00443 else
00444 { Row = Matrix->IntToExtRowMap[pElement->Row];
00445 Col = Matrix->IntToExtColMap[I];
00446 }
00447 pElement = pElement->NextInCol;
00448 if (fprintf(pMatrixFile, "%d\t%d\n", Row, Col) < 0) return 0;
00449 }
00450 }
00451
00452 if (Header)
00453 if (fprintf(pMatrixFile, "0\t0\n") < 0) return 0;
00454 }
00455
00456 #if spCOMPLEX
00457 if (Data AND Matrix->Complex)
00458 { for (I = 1; I <= Size; I++)
00459 { pElement = Matrix->FirstInCol[I];
00460 while (pElement != NULL)
00461 { if (Reordered)
00462 { Row = pElement->Row;
00463 Col = I;
00464 }
00465 else
00466 { Row = Matrix->IntToExtRowMap[pElement->Row];
00467 Col = Matrix->IntToExtColMap[I];
00468 }
00469 Err = fprintf
00470 ( pMatrixFile,"%d\t%d\t%-.15lg\t%-.15lg\n",
00471 Row, Col, (double)pElement->Real, (double)pElement->Imag
00472 );
00473 if (Err < 0) return 0;
00474 pElement = pElement->NextInCol;
00475 }
00476 }
00477
00478 if (Header)
00479 if (fprintf(pMatrixFile,"0\t0\t0.0\t0.0\n") < 0) return 0;
00480
00481 }
00482 #endif
00483
00484 #if REAL
00485 if (Data AND NOT Matrix->Complex)
00486 { for (I = 1; I <= Size; I++)
00487 { pElement = Matrix->FirstInCol[I];
00488 while (pElement != NULL)
00489 { Row = Matrix->IntToExtRowMap[pElement->Row];
00490 Col = Matrix->IntToExtColMap[I];
00491 Err = fprintf
00492 ( pMatrixFile,"%d\t%d\t%-.15lg\n",
00493 Row, Col, (double)pElement->Real
00494 );
00495 if (Err < 0) return 0;
00496 pElement = pElement->NextInCol;
00497 }
00498 }
00499
00500 if (Header)
00501 if (fprintf(pMatrixFile,"0\t0\t0.0\n") < 0) return 0;
00502
00503 }
00504 #endif
00505
00506
00507 if (fclose(pMatrixFile) < 0) return 0;
00508 return 1;
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 int
00555 spFileVector( eMatrix, File, RHS IMAG_RHS )
00556
00557 char *eMatrix, *File;
00558 RealVector RHS IMAG_RHS;
00559 {
00560 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00561 register int I, Size, Err;
00562 FILE *pMatrixFile;
00563 FILE *fopen();
00564
00565
00566 ASSERT( IS_SPARSE( Matrix ) AND RHS != NULL)
00567
00568
00569 if ((pMatrixFile = fopen(File,"a")) == NULL)
00570 return 0;
00571
00572
00573 #if NOT ARRAY_OFFSET
00574 #if spCOMPLEX
00575 if (Matrix->Complex)
00576 {
00577 #if spSEPARATED_COMPLEX_VECTORS
00578 ASSERT(iRHS != NULL)
00579 --RHS;
00580 --iRHS;
00581 #else
00582 RHS -= 2;
00583 #endif
00584 }
00585 else
00586 #endif
00587 --RHS;
00588 #endif
00589
00590
00591
00592 Size = Matrix->Size;
00593 #if spCOMPLEX
00594 if (Matrix->Complex)
00595 {
00596 #if spSEPARATED_COMPLEX_VECTORS
00597 for (I = 1; I <= Size; I++)
00598 { Err = fprintf
00599 ( pMatrixFile, "%-.15lg\t%-.15lg\n",
00600 (double)RHS[I], (double)iRHS[I]
00601 );
00602 if (Err < 0) return 0;
00603 }
00604 #else
00605 for (I = 1; I <= Size; I++)
00606 { Err = fprintf
00607 ( pMatrixFile, "%-.15lg\t%-.15lg\n",
00608 (double)RHS[2*I], (double)RHS[2*I+1]
00609 );
00610 if (Err < 0) return 0;
00611 }
00612 #endif
00613 }
00614 #endif
00615 #if REAL AND spCOMPLEX
00616 else
00617 #endif
00618 #if REAL
00619 { for (I = 1; I <= Size; I++)
00620 { if (fprintf(pMatrixFile, "%-.15lg\n", (double)RHS[I]) < 0)
00621 return 0;
00622 }
00623 }
00624 #endif
00625
00626
00627 if (fclose(pMatrixFile) < 0) return 0;
00628 return 1;
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 int
00676 spFileStats( eMatrix, File, Label )
00677
00678 char *eMatrix, *File, *Label;
00679 {
00680 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00681 register int Size, I;
00682 register ElementPtr pElement;
00683 int NumberOfElements;
00684 RealNumber Data, LargestElement, SmallestElement;
00685 FILE *pStatsFile, *fopen();
00686
00687
00688 ASSERT( IS_SPARSE( Matrix ) );
00689
00690
00691 if ((pStatsFile = fopen(File, "a")) == NULL)
00692 return 0;
00693
00694
00695 Size = Matrix->Size;
00696 if (NOT Matrix->Factored)
00697 fprintf(pStatsFile, "Matrix has not been factored.\n");
00698 fprintf(pStatsFile, "||| Starting new matrix |||\n");
00699 fprintf(pStatsFile, "%s\n", Label);
00700 if (Matrix->Complex)
00701 fprintf(pStatsFile, "Matrix is complex.\n");
00702 else
00703 fprintf(pStatsFile, "Matrix is real.\n");
00704 fprintf(pStatsFile," Size = %d\n",Size);
00705
00706
00707 NumberOfElements = 0;
00708 LargestElement = 0.0;
00709 SmallestElement = LARGEST_REAL;
00710
00711 for (I = 1; I <= Size; I++)
00712 { pElement = Matrix->FirstInCol[I];
00713 while (pElement != NULL)
00714 { NumberOfElements++;
00715 Data = ELEMENT_MAG(pElement);
00716 if (Data > LargestElement)
00717 LargestElement = Data;
00718 if (Data < SmallestElement AND Data != 0.0)
00719 SmallestElement = Data;
00720 pElement = pElement->NextInCol;
00721 }
00722 }
00723
00724 SmallestElement = MIN( SmallestElement, LargestElement );
00725
00726
00727 fprintf(pStatsFile, " Initial number of elements = %d\n",
00728 NumberOfElements - Matrix->Fillins);
00729 fprintf(pStatsFile,
00730 " Initial average number of elements per row = %lf\n",
00731 (double)(NumberOfElements - Matrix->Fillins) / (double)Size);
00732 fprintf(pStatsFile, " Fill-ins = %d\n",Matrix->Fillins);
00733 fprintf(pStatsFile, " Average number of fill-ins per row = %lf%%\n",
00734 (double)Matrix->Fillins / (double)Size);
00735 fprintf(pStatsFile, " Total number of elements = %d\n",
00736 NumberOfElements);
00737 fprintf(pStatsFile, " Average number of elements per row = %lf\n",
00738 (double)NumberOfElements / (double)Size);
00739 fprintf(pStatsFile," Density = %lf%%\n",
00740 (double)(100.0*NumberOfElements)/(double)(Size*Size));
00741 fprintf(pStatsFile," Relative Threshold = %e\n", Matrix->RelThreshold);
00742 fprintf(pStatsFile," Absolute Threshold = %e\n", Matrix->AbsThreshold);
00743 fprintf(pStatsFile," Largest Element = %e\n", LargestElement);
00744 fprintf(pStatsFile," Smallest Element = %e\n\n\n", SmallestElement);
00745
00746
00747 (void)fclose(pStatsFile);
00748 return 1;
00749 }
00750 #endif