spOutput.c

Go to the documentation of this file.
00001 /*
00002  *  MATRIX OUTPUT MODULE
00003  *
00004  *  Author:                     Advisor:
00005  *      Kenneth S. Kundert          Alberto Sangiovanni-Vincentelli
00006  *      UC Berkeley
00007  *
00008  *  This file contains the output-to-file and output-to-screen routines for
00009  *  the matrix package.
00010  *
00011  *  >>> User accessible functions contained in this file:
00012  *  spPrint
00013  *  spFileMatrix
00014  *  spFileVector
00015  *  spFileStats
00016  *
00017  *  >>> Other functions contained in this file:
00018  */
00019 
00020 
00021 /*
00022  *  Revision and copyright information.
00023  *
00024  *  Copyright (c) 1985,86,87,88
00025  *  by Kenneth S. Kundert and the University of California.
00026  *
00027  *  Permission to use, copy, modify, and distribute this software and
00028  *  its documentation for any purpose and without fee is hereby granted,
00029  *  provided that the copyright notices appear in all copies and
00030  *  supporting documentation and that the authors and the University of
00031  *  California are properly credited.  The authors and the University of
00032  *  California make no representations as to the suitability of this
00033  *  software for any purpose.  It is provided `as is', without express
00034  *  or implied warranty.
00035  */
00036 
00037 #include <stdio.h>
00038 #include "spmalloc.h"
00039 
00040 /*
00041  *  IMPORTS
00042  *
00043  *  >>> Import descriptions:
00044  *  spConfig.h
00045  *     Macros that customize the sparse matrix routines.
00046  *  spmatrix.h
00047  *     Macros and declarations to be imported by the user.
00048  *  spDefs.h
00049  *     Matrix type and macro definitions for the sparse matrix routines.
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  *  PRINT MATRIX
00065  *
00066  *  Formats and send the matrix to standard output.  Some elementary
00067  *  statistics are also output.  The matrix is output in a format that is
00068  *  readable by people.
00069  *
00070  *  >>> Arguments:
00071  *  Matrix  <input>  (char *)
00072  *      Pointer to matrix.
00073  *  PrintReordered  <input>  (int)
00074  *      Indicates whether the matrix should be printed out in its original
00075  *      form, as input by the user, or whether it should be printed in its
00076  *      reordered form, as used by the matrix routines.  A zero indicates that
00077  *      the matrix should be printed as inputed, a one indicates that it
00078  *      should be printed reordered.
00079  *  Data  <input>  (int)
00080  *      Boolean flag that when false indicates that output should be
00081  *      compressed such that only the existence of an element should be
00082  *      indicated rather than giving the actual value.  Thus 11 times as
00083  *      many can be printed on a row.  A zero signifies that the matrix
00084  *      should be printed compressed. A one indicates that the matrix
00085  *      should be printed in all its glory.
00086  *  Header  <input>  (int)
00087  *      Flag indicating that extra information should be given, such as row
00088  *      and column numbers.
00089  *
00090  *  >>> Local variables:
00091  *  Col  (int)
00092  *      Column being printed.
00093  *  ElementCount  (int)
00094  *      Variable used to count the number of nonzero elements in the matrix.
00095  *  LargestElement  (RealNumber)
00096  *      The magnitude of the largest element in the matrix.
00097  *  LargestDiag  (RealNumber)
00098  *      The magnitude of the largest diagonal in the matrix.
00099  *  Magnitude  (RealNumber)
00100  *      The absolute value of the matrix element being printed.
00101  *  PrintOrdToIntColMap  (int [])
00102  *      A translation array that maps the order that columns will be
00103  *      printed in (if not PrintReordered) to the internal column numbers.
00104  *  PrintOrdToIntRowMap  (int [])
00105  *      A translation array that maps the order that rows will be
00106  *      printed in (if not PrintReordered) to the internal row numbers.
00107  *  pElement  (ElementPtr)
00108  *      Pointer to the element in the matrix that is to be printed.
00109  *  pImagElements  (ElementPtr [ ])
00110  *      Array of pointers to elements in the matrix.  These pointers point
00111  *      to the elements whose real values have just been printed.  They are
00112  *      used to quickly access those same elements so their imaginary values
00113  *      can be printed.
00114  *  Row  (int)
00115  *      Row being printed.
00116  *  Size  (int)
00117  *      The size of the matrix.
00118  *  SmallestDiag  (RealNumber)
00119  *      The magnitude of the smallest diagonal in the matrix.
00120  *  SmallestElement  (RealNumber)
00121  *      The magnitude of the smallest element in the matrix excluding zero
00122  *      elements.
00123  *  StartCol  (int)
00124  *      The column number of the first column to be printed in the group of
00125  *      columns currently being printed.
00126  *  StopCol  (int)
00127  *      The column number of the last column to be printed in the group of
00128  *      columns currently being printed.
00129  *  Top  (int)
00130  *      The largest expected external row or column number.
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 /* Begin `spPrint'. */
00147     ASSERT( IS_SPARSE( Matrix ) );
00148     Size = Matrix->Size;
00149 
00150 /* Create a packed external to internal row and column translation array. */
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 /* Pack the arrays. */
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 /* Print header. */
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 /* Determine how many columns to use. */
00195     Columns = PRINTER_WIDTH;
00196     if (Header) Columns -= 5;
00197     if (Data) Columns = (Columns+1) / 10;
00198 
00199 /*
00200  * Print matrix by printing groups of complete columns until all the columns
00201  * are printed.
00202  */
00203     J = 0;
00204     while ( J <= Size )
00205 
00206 /* Calculate index of last column to printed in this group. */
00207     {   StopCol = StartCol + Columns - 1;
00208         if (StopCol > Size)
00209             StopCol = Size;
00210 
00211 /* Label the columns. */
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 /* Print every row ...  */
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 /* ... in each column of the group. */
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 /* Case where element exists */
00267                 {   if (Data)
00268                         printf(" %9.3lg", (double)pElement->Real);
00269                     else
00270                         putchar('x');
00271 
00272 /* Update status variables */
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 /* Case where element is structurally zero */
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 /* spCOMPLEX */
00303         }
00304 
00305 /* Calculate index of first column in next group. */
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 /* Search for largest and smallest diagonal values */
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     /* Print the largest and smallest diagonal values */
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     /* Calculate and print sparsity and number of fill-ins created. */
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  *  OUTPUT MATRIX TO FILE
00359  *
00360  *  Writes matrix to file in format suitable to be read back in by the
00361  *  matrix test program.
00362  *
00363  *  >>> Returns:
00364  *  One is returned if routine was successful, otherwise zero is returned.
00365  *  The calling function can query errno (the system global error variable)
00366  *  as to the reason why this routine failed.
00367  *
00368  *  >>> Arguments:
00369  *  Matrix  <input>  (char *)
00370  *      Pointer to matrix.
00371  *  File  <input>  (char *)
00372  *      Name of file into which matrix is to be written.
00373  *  Label  <input>  (char *)
00374  *      String that is transferred to file and is used as a label.
00375  *  Reordered  <input> (SPBOOLEAN)
00376  *      Specifies whether matrix should be output in reordered form,
00377  *      or in original order.
00378  *  Data  <input> (SPBOOLEAN)
00379  *      Indicates that the element values should be output along with
00380  *      the indices for each element.  This parameter must be true if
00381  *      matrix is to be read by the sparse test program.
00382  *  Header  <input> (SPBOOLEAN)
00383  *      Indicates that header is desired.  This parameter must be true if
00384  *      matrix is to be read by the sparse test program.
00385  *
00386  *  >>> Local variables:
00387  *  Col  (int)
00388  *      The original column number of the element being output.
00389  *  pElement  (ElementPtr)
00390  *      Pointer to an element in the matrix.
00391  *  pMatrixFile  (FILE *)
00392  *      File pointer to the matrix file.
00393  *  Row  (int)
00394  *      The original row number of the element being output.
00395  *  Size  (int)
00396  *      The size of the matrix.
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 /* Begin `spFileMatrix'. */
00412     ASSERT( IS_SPARSE( Matrix ) );
00413 
00414 /* Open file matrix file in write mode. */
00415     if ((pMatrixFile = fopen(File, "w")) == NULL)
00416         return 0;
00417 
00418 /* Output header. */
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 /* Output matrix. */
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 /* Output terminator, a line of zeros. */
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 /* Output terminator, a line of zeros. */
00478         if (Header)
00479             if (fprintf(pMatrixFile,"0\t0\t0.0\t0.0\n") < 0) return 0;
00480 
00481     }
00482 #endif /* spCOMPLEX */
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 /* Output terminator, a line of zeros. */
00500         if (Header)
00501             if (fprintf(pMatrixFile,"0\t0\t0.0\n") < 0) return 0;
00502 
00503     }
00504 #endif /* REAL */
00505 
00506 /* Close file. */
00507     if (fclose(pMatrixFile) < 0) return 0;
00508     return 1;
00509 }
00510 
00511 
00512 
00513 
00514 
00515 
00516 
00517 /*
00518  *  OUTPUT SOURCE VECTOR TO FILE
00519  *
00520  *  Writes vector to file in format suitable to be read back in by the
00521  *  matrix test program.  This routine should be executed after the function
00522  *  spFileMatrix.
00523  *
00524  *  >>> Returns:
00525  *  One is returned if routine was successful, otherwise zero is returned.
00526  *  The calling function can query errno (the system global error variable)
00527  *  as to the reason why this routine failed.
00528  *
00529  *  >>> Arguments:
00530  *  Matrix  <input>  (char *)
00531  *      Pointer to matrix.
00532  *  File  <input>  (char *)
00533  *      Name of file into which matrix is to be written.
00534  *  RHS  <input>  (RealNumber [])
00535  *      Right-hand side vector. This is only the real portion if
00536  *      spSEPARATED_COMPLEX_VECTORS is true.
00537  *  iRHS  <input>  (RealNumber [])
00538  *      Right-hand side vector, imaginary portion.  Not necessary if matrix
00539  *      is real or if spSEPARATED_COMPLEX_VECTORS is set false.
00540  *
00541  *  >>> Local variables:
00542  *  pMatrixFile  (FILE *)
00543  *      File pointer to the matrix file.
00544  *  Size  (int)
00545  *      The size of the matrix.
00546  *
00547  *  >>> Obscure Macros
00548  *  IMAG_RHS
00549  *      Replaces itself with `, iRHS' if the options spCOMPLEX and
00550  *      spSEPARATED_COMPLEX_VECTORS are set, otherwise it disappears
00551  *      without a trace.
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 /* Begin `spFileVector'. */
00566     ASSERT( IS_SPARSE( Matrix ) AND RHS != NULL)
00567 
00568 /* Open File in append mode. */
00569     if ((pMatrixFile = fopen(File,"a")) == NULL)
00570         return 0;
00571 
00572 /* Correct array pointers for ARRAY_OFFSET. */
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 /* spCOMPLEX */
00587         --RHS;
00588 #endif /* NOT ARRAY_OFFSET */
00589 
00590 
00591 /* Output vector. */
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 /* spCOMPLEX */
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 /* REAL */
00625 
00626 /* Close file. */
00627     if (fclose(pMatrixFile) < 0) return 0;
00628     return 1;
00629 }
00630 
00631 
00632 
00633 
00634 
00635 
00636 
00637 
00638 
00639 /*
00640  *  OUTPUT STATISTICS TO FILE
00641  *
00642  *  Writes useful information concerning the matrix to a file.  Should be
00643  *  executed after the matrix is factored.
00644  * 
00645  *  >>> Returns:
00646  *  One is returned if routine was successful, otherwise zero is returned.
00647  *  The calling function can query errno (the system global error variable)
00648  *  as to the reason why this routine failed.
00649  *
00650  *  >>> Arguments:
00651  *  Matrix  <input>  (char *)
00652  *      Pointer to matrix.
00653  *  File  <input>  (char *)
00654  *      Name of file into which matrix is to be written.
00655  *  Label  <input>  (char *)
00656  *      String that is transferred to file and is used as a label.
00657  *
00658  *  >>> Local variables:
00659  *  Data  (RealNumber)
00660  *      The value of the matrix element being output.
00661  *  LargestElement  (RealNumber)
00662  *      The largest element in the matrix.
00663  *  NumberOfElements  (int)
00664  *      Number of nonzero elements in the matrix.
00665  *  pElement  (ElementPtr)
00666  *      Pointer to an element in the matrix.
00667  *  pStatsFile  (FILE *)
00668  *      File pointer to the statistics file.
00669  *  Size  (int)
00670  *      The size of the matrix.
00671  *  SmallestElement  (RealNumber)
00672  *      The smallest element in the matrix excluding zero elements.
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 /* Begin `spFileStats'. */
00688     ASSERT( IS_SPARSE( Matrix ) );
00689 
00690 /* Open File in append mode. */
00691     if ((pStatsFile = fopen(File, "a")) == NULL)
00692         return 0;
00693 
00694 /* Output statistics. */
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 /* Search matrix. */
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 /* Output remaining statistics. */
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 /* Close file. */
00747     (void)fclose(pStatsFile);
00748     return 1;
00749 }
00750 #endif /* DOCUMENTATION */

Generated on Sun Mar 4 15:04:00 2007 for Scilab [trunk] by  doxygen 1.5.1