/* * ========================================================================== * Program to Allocate and Print Small Matrix * ========================================================================== * Copyright (C) 1993-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By: Mark Austin January 1993 * ========================================================================== */ #include #include "miscellaneous.h" #define MIN(a,b) ( a < b ? a : b) /* function declarations */ void matPrint( char *, double **, int , int ); void matPrintIndirectDouble( char *, double **, int, int ); double ** matAllocSequential( int , int ); double ** matAllocIndirect( int , int ); void matFreeSequential( double ** ); void matFreeIndirect( double ** , int ); int main( void ) { enum { NoRows = 4 , NoColumns = 14 }; double **dppA; double **dppB; int iRow, iCol; /* [a] : Allocate memory for small 4 x 4 matrices */ dppA = matAllocSequential( NoRows , NoColumns ); dppB = matAllocIndirect( NoRows , NoColumns ); /* [b] : Initialize Values in Matrix */ for( iCol = 1; iCol <= NoColumns; iCol++ ) for( iRow = 1; iRow <= NoRows; iRow++ ) { dppA [ iRow - 1 ][ iCol - 1 ] = iRow - iCol; dppB [ iRow - 1 ][ iCol - 1 ] = iRow - iCol; } /* [c] : Print matrix */ matPrint( "B", dppB, NoRows, NoColumns); matPrintIndirectDouble( "B", dppB, NoRows, NoColumns ); /* [d] : Free Matrix Memory */ matFreeSequential( dppA ); matFreeIndirect( dppB , NoRows ); } /* * ================================================================ * matAllocSequential() : Memory allocation of Matrices with * Sequential Storage Pattern. * * matAllocIndirect() : Memory allocation of Matrices with * Indirect Storage Pattern. * * Input : int iNoRows -- Number of Rows in Matrix. * int iNoColumns -- Number of Columns in Matrix. * Output : double **Matrix -- Pointer to Allocated Matrix. * ================================================================ */ double ** matAllocSequential( int iNoRows , int iNoColumns ) { double ** dppM; int iRow; dppM = (double **) safeMalloc( iNoRows * sizeof(double *), __FILE__, __LINE__ ); dppM [0] = (double *) safeMalloc( iNoRows * iNoColumns * sizeof(double), __FILE__, __LINE__ ); for( iRow = 1; iRow < iNoRows; iRow++ ) dppM [ iRow ] = dppM [ 0 ] + (iRow * iNoColumns); return ( dppM ); } double ** matAllocIndirect( int iNoRows , int iNoColumns ) { double ** dppM; int iRow; dppM = (double **) safeMalloc( iNoRows * sizeof(double *), __FILE__, __LINE__); for( iRow = 0; iRow < iNoRows; iRow++ ) dppM [ iRow ] = (double *) safeMalloc( iNoColumns * sizeof(double), __FILE__, __LINE__); return ( dppM ); } /* * ================================================================ * matFreeSequential() : Deallocate Memory for Matrices with * Sequential Storage Pattern. * * matFreeIndirect() : Deallocate Memory for Matrices with * Indirect Storage Pattern. * * Input : double **Matrix -- Pointer to Allocated Matrix. * : int iNoRows -- Number of Rows in Matrix. * Output : void * ================================================================ */ void matFreeSequential( double **dppM ) { free ((char *) dppM [0] ); free ((char *) dppM ); } void matFreeIndirect( double **dppM, int iNoRows ) { int iRow; for( iRow = 0; iRow < iNoRows; iRow++ ) free ((char *) dppM [ iRow ]); free ((char *) dppM ); } /* * ================================================================ * matPrint() : Naive Implementation of Matrix Print * * Input : char *cpMatrixName -- Pointer to matrix name. * double **Matrix -- Pointer to Allocated Matrix. * int iNoRows -- Number of Rows in Matrix. * int iNoColumns -- Number of Columns in Matrix. * ================================================================ */ void matPrint( char *cpMatrixName , double **dppM , int iNoRows , int iNoColumns ) { int iRow, iCol; printf("\n\"MATRIX %s\"\n\n", cpMatrixName); for( iRow = 1; iRow <= iNoRows; iRow++ ) { for( iCol = 1; iCol <= iNoColumns; iCol++ ) printf(" %12.5f ", dppM [ iRow - 1 ][ iCol - 1 ] ); printf("\n"); } } /* * ======================================================================= * matPrintIndirectDouble() : Print a Matrix [iNoRows][iNoColumns] of * data type double * * Where -- COLUMNS_ACROSS_PAGE = Number of columns printed across page. * iFirstColumn = Number of first column in block * iLastColumn = Number of last column in block * * Input : char *cpMatrixName -- Pointer to matrix name. * double **dppMatrix -- Pointer to Allocated Matrix. * int iNoRows -- Number of Rows in Matrix. * int iNoColumns -- Number of Columns in Matrix. * Output : void * ======================================================================= */ void matPrintIndirectDouble( char *cpName, double **dppMatrix, int iNoRows, int iNoColumns) { enum { ColumnsAcrossPage = 6 }; int iFirstColumn, iLastColumn; int iNoBlocks; int iRow, iCol, iBlock; /* [a] : Compute no of blocks of columns to be printed */ if( iNoColumns % ColumnsAcrossPage == 0 ) iNoBlocks = iNoColumns / ColumnsAcrossPage; else iNoBlocks = iNoColumns / ColumnsAcrossPage + 1; /* [b] : Walk along blocks of columns */ for( iBlock = 1; iBlock <= iNoBlocks; iBlock++ ) { /* [b.1] : Get first and last column nos in block */ iFirstColumn = (iBlock-1)*ColumnsAcrossPage + 1; iLastColumn = MIN( iBlock*ColumnsAcrossPage , iNoColumns ); /* [b.2] : Print title of matrix at top of each block */ if( cpName != NULL ) printf("\nMATRIX : \"%s\"\n\n", cpName); else printf("\nMATRIX : \"UNTITLED\" cpName \n\n"); /* [b.3] : Label row and column nos */ printf ("row/col "); for( iRow = iFirstColumn; iRow <= iLastColumn; iRow++ ) printf("%3d ", iRow ); printf("\n"); /* [b.4] : Print Contents of Matrix */ for( iRow = 1; iRow <= iNoRows; iRow++ ) { printf(" %3d ", iRow); for( iCol = iFirstColumn; iCol <= iLastColumn; iCol++) printf(" %12.5e", dppMatrix [ iRow-1 ][ iCol-1 ] ); printf("\n"); } } }