我正在嘗試使用GSL來計算兩個向量之間的點積。矢量是矩陣列的視圖。我調用gsl_blas_dsdot(&view1.vector, &view2.vector, &val)
這樣的函數,但在編譯時,我收到警告,說明函數預期的參數類型const gsl_vector_float *
,並且我得到一個無意義的結果。這裏是一個代碼來說明:什麼是GSL BLAS函數所需的「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 *
還要注意的是複製矩陣列到gsl_vector
類型使用get_matrix_get_col()
會導致相同的警告。
任何人都可以請協助嗎?這些gsl矢量和矢量視圖是什麼使它們成爲不兼容的類型?這些const gsl_vector_float
類型是什麼?