Je suis en train de tester la bibliothèque scientifique GNU (GSL) et je veux en créer une matrice clairsemée. Ceci est mon code qui provient directement de https://www.gnu.org/software/gsl/manual/html_node/Sparse-Matrix-Examples.html:matrices creuses avec GSL - erreur de compilation
#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 *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");
for (i = 0; i < A->nz; ++i)
printf("(%zu, %zu, %.1f)\n", A->i[i], A->p[i], A->data[i]);
/* convert to compressed column format */
C = gsl_spmatrix_compcol(A);
printf("matrix in compressed column 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->size2 + 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(C);
return 0;
}
Je compilé ce code en utilisant la commande suivante:
gcc test1.c -o test -lgsl -lgslcblas -lm
Le résultat était:
/tmp/ccyBfp0p.o: In function `main':
test1.c:(.text+0x13): undefined reference to `gsl_spmatrix_alloc'
test1.c:(.text+0x40): undefined reference to `gsl_spmatrix_set'
test1.c:(.text+0x69): undefined reference to `gsl_spmatrix_set'
test1.c:(.text+0x87): undefined reference to `gsl_spmatrix_set'
test1.c:(.text+0xb0): undefined reference to `gsl_spmatrix_set'
test1.c:(.text+0xd9): undefined reference to `gsl_spmatrix_set'
/tmp/ccyBfp0p.o:test1.c:(.text+0x102): more undefined references to `gsl_spmatrix_set' follow
/tmp/ccyBfp0p.o: In function `main':
test1.c:(.text+0x189): undefined reference to `gsl_spmatrix_get'
test1.c:(.text+0x25d): undefined reference to `gsl_spmatrix_compcol'
test1.c:(.text+0x39b): undefined reference to `gsl_spmatrix_free'
test1.c:(.text+0x3a7): undefined reference to `gsl_spmatrix_free'
collect2: error: ld returned 1 exit status
J'essayé d'utiliser la code suivant à compiler:
gcc -I/usr/local/include/gsl -L/usr/local/lib -o test test1.c -lgsl -lgslcblas -lm
Cette compilé le code sans erreur mais quand j'ai essayé de l'exécuter l'erreur suivante:
./test: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory
Mais quand je fais:
ls /usr/local/lib
je peux voir le résultat:
libemon.a libgslcblas.so libgsl.so python2.7
libgsl.a libgslcblas.so.0 libgsl.so.19 python3.4
libgslcblas.a libgslcblas.so.0.0.0 libgsl.so.19.1.0 site_ruby
libgslcblas.la libgsl.la pkgconfig
Je pense avoir un problème avec mon installation GSL. Mais le problème est, tous les autres calculs fonctionnent bien! Seule la matrice clairsemée me donne ce problème.
Etes-vous sûr que votre version installée de GSL supporte des tableaux rares? Quelle version de GSL utilisez-vous? (utilisez les macros 'GSL_VERSION',' GSL_MAJOR_VERSION' et 'GSL_MINOR_VERSION') – alfC