# include # include # include "ppc_array.h" # include "ppc_sparse_matrix.h" # include "ppc_xmalloc.h" /******************************************************************************/ void ccs_pack ( double **a, int m, int n, int *Ap, int *Ai, double *Ax ) /******************************************************************************/ /* Purpose: ccs_pack() packs an MxN sparse matrix to CCS format. Licensing: This code is distributed under the MIT license. Modified: 09 September 2023 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: double **a: the MxN matrix, which has already been allocated by the user. int m, n: the number of rows and columns of the matrix. Output: int *Ap[n+1]: int *Ai[nz]: double *Ax[nz]: */ { int i; int j; int nz; /* Scan the MxN matrix. */ nz = 0; Ap[nz] = 0; for ( j = 0; j < n; j++ ) { for ( i = 0; i < m; i++ ) { if ( a[i][j] != 0.0 ) { Ap[j+1] = nz + 1; Ai[nz] = i; Ax[nz] = a[i][j]; nz = nz + 1; } } } return; } /******************************************************************************/ void ccs_unpack ( double **a, int m, int n, int *Ap, int *Ai, double *Ax ) /******************************************************************************/ /* Purpose: ccs_unpack() unpacks a CCS sparse matrix to MxN format. Licensing: This code is distributed under the MIT license. Modified: 09 September 2023 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: int m, n: the number of rows and columns of the matrix. int *Ap: int *Ai: double *Ax: Output: double **a: the MxN matrix, which has already been allocated by the user. */ { int i; int j; int k; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a[i][j] = 0.0; } } for ( j = 0; j < n; j++ ) { for ( k = Ap[j]; k < Ap[j+1]; k++ ) { i = Ai[k]; a[i][j] = Ax[k]; } } return; }