# include # include # include # include "ppc_array.h" # include "ppc_sparse_matrix.h" int main ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: ppc_sparse_matrix_test() tests ppc_sparse_matrix(). Licensing: This code is distributed under the MIT license. Modified: 10 September 2023 Author: John Burkardt */ { double **a; double **a2; int *Ai; int *Ap; double *Ax; int i; int j; int m = 4; int n = 5; int nz; timestamp ( ); printf ( "\n" ); printf ( "ppc_sparse_matrix_test():\n" ); printf ( " C version\n" ); printf ( " Test ppc_sparse_matrix().\n" ); /* Define an MxN sparse matrix. */ make_matrix ( a, m, n ); a[0][0] = 0; a[0][1] = 7; a[0][2] = 0; a[0][3] = 0; a[0][4] = 1; a[1][0] = 0; a[1][1] = 4; a[1][2] = 0; a[1][3] = 3; a[1][4] = 0; a[2][0] = 6; a[2][1] = 6; a[2][2] = 5; a[2][3] = 1; a[2][4] = 4; a[3][0] = 5; a[3][1] = 5; a[3][2] = 0; a[3][3] = 0; a[3][4] = 0; printf ( "\n" ); printf ( " The MxN matrix:\n" ); printf ( "\n" ); print_matrix ( " %f", a, m, n ); /* Count nonzeros. */ nz = 0; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { if ( a[i][j] != 0.0 ) { nz = nz + 1; } } } printf ( " Number of nonzero matrix entries = %d\n", nz ); /* Allocate sparse arrays. */ make_vector ( Ai, nz ); make_vector ( Ap, n + 1 ); make_vector ( Ax, nz ); /* Convert matrix to sparse form. */ ccs_pack ( a, m, n, Ap, Ai, Ax ); /* Report results. */ printf ( "\n" ); printf ( " Ai:" ); printf ( "\n" ); print_vector ( " %d", Ai, nz ); printf ( "\n" ); printf ( " Ap:" ); printf ( "\n" ); print_vector ( " %d", Ap, n + 1 ); printf ( "\n" ); printf ( " Ax:" ); printf ( "\n" ); print_vector ( " %g", Ax, nz ); /* Reconstruct matrix from sparse information. */ make_matrix ( a2, m, n ); ccs_unpack ( a2, m, n, Ap, Ai, Ax ); printf ( "\n" ); printf ( " The reconstructed MxN matrix:\n" ); printf ( "\n" ); print_matrix ( " %f", a2, m, n ); /* Free memory. */ free_matrix ( a ); free_matrix ( a2 ); free_vector ( Ai ); free_vector ( Ap ); free_vector ( Ax ); /* Terminate. */ printf ( "\n" ); printf ( "ppc_sparse_matrix_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 17 June 2014 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }