【发布时间】:2017-09-07 04:05:41
【问题描述】:
我正在测试 GNU Scientific Library (GSL),我想用它创建一个稀疏矩阵。这是我的代码,直接来自https://www.gnu.org/software/gsl/manual/html_node/Sparse-Matrix-Examples.html:
#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;
}
我使用以下命令编译了这段代码:
gcc test1.c -o test -lgsl -lgslcblas -lm
结果是:
/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
然后我尝试使用以下代码进行编译:
gcc -I/usr/local/include/gsl -L/usr/local/lib -o test test1.c -lgsl -lgslcblas -lm
这编译代码没有错误,但是当我尝试运行它时出现以下错误:
./test: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory
但是当我这样做时:
ls /usr/local/lib
我可以看到结果:
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
我认为我的 GSL 安装有问题。但问题是,所有其他计算都可以正常工作!只有稀疏矩阵给了我这个问题。
【问题讨论】:
-
您确定您安装的 GSL 版本支持稀疏数组吗?您使用的是什么版本的 GSL? (使用宏
GSL_VERSION、GSL_MAJOR_VERSION和GSL_MINOR_VERSION)
标签: compilation sparse-matrix gsl