【发布时间】:2016-11-28 16:23:46
【问题描述】:
我正在尝试使用 GSL 来计算两个向量之间的点积。向量是矩阵列的视图。我将函数称为gsl_blas_dsdot( &view1.vector, &view2.vector, &val),但在编译时我收到警告,指出函数预期参数类型const gsl_vector_float *,我得到一个无意义的结果。下面是一段代码来演示:
#include<stdio.h>
#include<gsl/gsl_matrix.h>
#include<gsl/gsl_vector.h>
#include<gsl/gsl_blas.h>
void main(void){
int i;
double val = 111.111; // Initialize to something
gsl_matrix *A = gsl_matrix_alloc(3,3); //Initialize mx
gsl_matrix_set_identity(A); // Set mx to identity
gsl_matrix_set(A,0,1,3.14);
gsl_vector_view a1 = gsl_matrix_column(A,0); // Vector allocations
gsl_vector_view a2 = gsl_matrix_column(A,1);
/* Print the vectors */
printf("a1 = ");
for(i=0; i<3; i++){
printf("%g ", gsl_vector_get(&a1.vector,i));}
printf("\na2 = ");
for(i=0; i<3; i++){
printf("%g ", gsl_vector_get(&a2.vector,i));}
printf("\n");
gsl_blas_dsdot(&a1.vector, &a2.vector, &val); // Dot product
printf("a1.a2 = %.2f\n", val); // Print result
}
我使用 gcc 版本 5.4.0、GSL 版本 2.2.1 进行编译,具有以下内容:
gcc example.c -o example -lgsl -lgslcblas
我收到以下警告,虽然程序执行,但结果是荒谬的:
gsl_dot.c: In function ‘main’:
gsl_dot.c:22:18: warning: passing argument 1 of ‘gsl_blas_dsdot’ from incompatible pointer type [-Wincompatible-pointer-types]
gsl_blas_dsdot(&a1.vector, &a2.vector, &val); // Dot product
^
In file included from gsl_dot.c:4:0:
/usr/local/include/gsl/gsl_blas.h:56:5: note: expected ‘const gsl_vector_float * {aka const struct <anonymous> *}’ but argument is of type ‘gsl_vector * {aka struct <anonymous> *}’
int gsl_blas_dsdot (const gsl_vector_float * X,
^
gsl_dot.c:22:30: warning: passing argument 2 of ‘gsl_blas_dsdot’ from incompatible pointer type [-Wincompatible-pointer-types]
gsl_blas_dsdot(&a1.vector, &a2.vector, &val); // Dot product
^
In file included from gsl_dot.c:4:0:
/usr/local/include/gsl/gsl_blas.h:56:5: note: expected ‘const gsl_vector_float * {aka const struct <anonymous> *}’ but argument is of type ‘gsl_vector * {aka struct <anonymous> *}’
int gsl_blas_dsdot (const gsl_vector_float *
另请注意,使用 get_matrix_get_col() 将矩阵列复制到 gsl_vector 类型会导致相同的警告。
有人可以帮忙吗?这些 gsl 向量和向量视图是什么使它们成为不兼容的类型?这些const gsl_vector_float 类型是什么?
【问题讨论】: