/*////////////////////////////////////////////////////////////////////////////// // // // // Example to demonstrate how to use wn_matrix routines to read in // a matrix from an ascii file, get a copy of the matrix and the // transpose, output the elements to a file, and then free all // matrices and their allocated memory. // // // // Copyright (c) 2006-2007 Clemson University. // // This file is part of the wn_matrix module, originally developed // by David C. Adams and Bradley S. Meyer. For more information, please see // http://www.webnucleo.org. // // The wn_matrix module is // free software. You can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // The wn_matrix module is distributed in the hope that // it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with the wn_matrix module; // if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, // Boston, MA 02111-1307 USA // // //////////////////////////////////////////////////////////////////////////////*/ #include "WnMatrix.h" int main( int argc, char *argv[] ) { unsigned int i_rows, i_cols; unsigned int i_row, i_col; double d_val; WnMatrix *p_matrix, *p_copy, *p_transpose; FILE *p_in, *p_out; /*============================================================================ // Check arguments. //==========================================================================*/ if ( argc != 3 ) { fprintf( stderr, "\nUsage: %s in_file out_file\n\n", argv[0] ); fprintf( stderr, " in_file = data input file\n\n" ); fprintf( stderr, " out_file = data output file\n\n" ); return EXIT_FAILURE; } /*============================================================================ // Open input and output files. //==========================================================================*/ if( ( p_in = fopen( argv[1], "r" ) ) == NULL ) { printf("\nCannot open input matrix file!\n"); return EXIT_FAILURE; } if( ( p_out = fopen( argv[2], "w" ) ) == NULL ) { printf("\nCannot open output matrix file!\n"); return EXIT_FAILURE; } /*============================================================================ // Determine the number of rows and columns. //==========================================================================*/ i_rows = 0; i_cols = 0; while( !feof( p_in ) ) { fscanf( p_in, "%u %u %lf\n", &i_row, &i_col, &d_val ); if( i_row > i_rows ){ i_rows = i_row; } if( i_col > i_cols ){ i_cols = i_col; } } /*============================================================================ // Close input file. //==========================================================================*/ fclose( p_in ); /*============================================================================ // Create matrix. //==========================================================================*/ p_matrix = WnMatrix__new( i_rows, i_cols ); /*============================================================================ // Reopen input file and store elements. //==========================================================================*/ p_in = fopen( argv[1], "r" ); while( !feof( p_in ) ) { fscanf( p_in, "%u %u %lf\n", &i_row, &i_col, &d_val ); WnMatrix__assignElement( p_matrix, i_row, i_col, d_val ); } /*============================================================================ // Close input file. //==========================================================================*/ fclose( p_in ); /*============================================================================ // Get copy of matrix. //==========================================================================*/ p_copy = WnMatrix__getCopy( p_matrix ); /*============================================================================ // Get transpose of matrix. //==========================================================================*/ p_transpose = WnMatrix__getTranspose( p_matrix ); /*============================================================================ // Print output to file. //==========================================================================*/ fprintf( p_out, "The elements of the matrices are:\n\n" ); fprintf( p_out, "Row Column Matrix Copy Transpose\n" ); fprintf( p_out, "--- ------ ------ ------- ---------\n" ); for ( i_row = 1; i_row <= WnMatrix__getNumberOfRows( p_copy ); i_row++ ) { for ( i_col = 1; i_col <= WnMatrix__getNumberOfColumns( p_copy ); i_col++ ) { fprintf( p_out, "%3d %6d %7.3f %7.3f %7.3f\n", i_row, i_col, WnMatrix__getElement( p_matrix, i_row, i_col ), WnMatrix__getElement( p_copy, i_row, i_col ), WnMatrix__getElement( p_transpose, i_row, i_col ) ); } } /*============================================================================ // Close file, clean up, and exit. //==========================================================================*/ fclose( p_out ); WnMatrix__free( p_matrix ); WnMatrix__free( p_copy ); WnMatrix__free( p_transpose ); return EXIT_SUCCESS; }