## File: Sparse-Matrices-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (209 lines) | stat: -rw-r--r-- 8,069 bytes parent folder | download
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209` `````` GNU Scientific Library – Reference Manual: Sparse Matrices Examples

41.13 Examples

The following example program builds a 5-by-4 sparse matrix and prints it in triplet, compressed column, and compressed row format. The matrix which is constructed is The output of the program is

printing all matrix elements: A(0,0) = 0 A(0,1) = 0 A(0,2) = 3.1 A(0,3) = 4.6 A(1,0) = 1 . . . A(4,0) = 4.1 A(4,1) = 0 A(4,2) = 0 A(4,3) = 0 matrix in triplet format (i,j,Aij): (0, 2, 3.1) (0, 3, 4.6) (1, 0, 1.0) (1, 2, 7.2) (3, 0, 2.1) (3, 1, 2.9) (3, 3, 8.5) (4, 0, 4.1) matrix in compressed column format: i = [ 1, 3, 4, 3, 0, 1, 0, 3, ] p = [ 0, 3, 4, 6, 8, ] d = [ 1, 2.1, 4.1, 2.9, 3.1, 7.2, 4.6, 8.5, ] matrix in compressed row format: i = [ 2, 3, 0, 2, 0, 1, 3, 0, ] p = [ 0, 2, 4, 4, 7, 8, ] d = [ 3.1, 4.6, 1, 7.2, 2.1, 2.9, 8.5, 4.1, ]

We see in the compressed column output, the data array stores each column contiguously, the array i stores the row index of the corresponding data element, and the array p stores the index of the start of each column in the data array. Similarly, for the compressed row output, the data array stores each row contiguously, the array i stores the column index of the corresponding data element, and the p array stores the index of the start of each row in the data array.

#include <stdio.h> #include <stdlib.h>  #include <gsl/gsl_spmatrix.h>  int main() {   gsl_spmatrix *A = gsl_spmatrix_alloc(5, 4); /* triplet format */   gsl_spmatrix *B, *C;   size_t i, j;    /* build the sparse matrix */   gsl_spmatrix_set(A, 0, 2, 3.1);   gsl_spmatrix_set(A, 0, 3, 4.6);   gsl_spmatrix_set(A, 1, 0, 1.0);   gsl_spmatrix_set(A, 1, 2, 7.2);   gsl_spmatrix_set(A, 3, 0, 2.1);   gsl_spmatrix_set(A, 3, 1, 2.9);   gsl_spmatrix_set(A, 3, 3, 8.5);   gsl_spmatrix_set(A, 4, 0, 4.1);    printf("printing all matrix elements:\n");   for (i = 0; i < 5; ++i)     for (j = 0; j < 4; ++j)       printf("A(%zu,%zu) = %g\n", i, j,              gsl_spmatrix_get(A, i, j));    /* print out elements in triplet format */   printf("matrix in triplet format (i,j,Aij):\n");   gsl_spmatrix_fprintf(stdout, A, "%.1f");    /* convert to compressed column format */   B = gsl_spmatrix_ccs(A);    printf("matrix in compressed column format:\n");   printf("i = [ ");   for (i = 0; i < B->nz; ++i)     printf("%zu, ", B->i[i]);   printf("]\n");    printf("p = [ ");   for (i = 0; i < B->size2 + 1; ++i)     printf("%zu, ", B->p[i]);   printf("]\n");    printf("d = [ ");   for (i = 0; i < B->nz; ++i)     printf("%g, ", B->data[i]);   printf("]\n");    /* convert to compressed row format */   C = gsl_spmatrix_crs(A);    printf("matrix in compressed row format:\n");   printf("i = [ ");   for (i = 0; i < C->nz; ++i)     printf("%zu, ", C->i[i]);   printf("]\n");    printf("p = [ ");   for (i = 0; i < C->size1 + 1; ++i)     printf("%zu, ", C->p[i]);   printf("]\n");    printf("d = [ ");   for (i = 0; i < C->nz; ++i)     printf("%g, ", C->data[i]);   printf("]\n");    gsl_spmatrix_free(A);   gsl_spmatrix_free(B);   gsl_spmatrix_free(C);    return 0; }

``````