2013-03-04 40 views
0

的代碼行:gsl_blas_daxpy(-a,&gsl_matrix_column(D, q).vector,y); 原因錯誤錯誤C2102: '&' 需要-1-值

錯誤C2102: '&' 需要-1-值

,現在的問題是我沒有控制的GSL功能,所以我不知道如何解決這個問題(刪除「&」沒有工作)

之後我ge牛逼

錯誤C2198: 'gsl_blas_daxpy':呼叫

參數太少我使用Visual Studio 2010中

GSL_EXPORT int gsl_blas_daxpy (double alpha, 
           const gsl_vector * X, 
           gsl_vector * Y); 


#include <stdio.h> 
#include <math.h> 
#include <time.h> 
#include <gsl/gsl_vector.h> 
#include <gsl/gsl_matrix.h> 
#include <gsl/gsl_blas.h> 

#define M (10) // Number of columns in dictionary */ 
#define N ((int)(M/2)) // Number of rows in dictionary */ 
int K = 0.07*M; //Number of non-zero elements in signal - the sparsity 
int P=1; //number of signals 
double epsilon = 1.0e-7; // Residual error 
int numOfIterations = N; /* Max num of iterations - same as num of elements in signal */ 

double sign(double x){return (x>=0) - (x<0);} // Sign function 

int main(int argc, char** argv) 
{ 
int n, m, k, iter, q; 
double normi, normf, tmp , norm=sqrt(N), htime; 
gsl_matrix *D; // A random dictionary used for encoding the sparse signal NxM 
gsl_vector *x; // Sparse info signal (encoder input) MxP 
gsl_vector *z; // Evaluated Sparse info signal (decoder output) MxP 
gsl_vector *r; // Residual error vector MxP 
gsl_vector *y; // Sparse representation of signal (encoder output) NxP 
gsl_vector_view v; 
clock_t start; //for measuring performance 

printf("\nDictionary is:NxM=%dx%d,and the signal sparsity is K=%d", N, M, K); 


srand(time(NULL)); //Initialize srand 
start =clock(); //Initialize clock 

/* Initiallize D as a Bernoulli random dictionary */ 
D = gsl_matrix_alloc (N, M); 
for(m=0; m<M; m++) 
    { 
    for(n=0; n<N; n++) 
    { 
     tmp=sign(2.0*rand()/(double)RAND_MAX-1.0)/norm; 
     gsl_matrix_set (D, n, m, tmp); //D[n,m]=tmp 
    } 
    } 

/* Create a random K-sparse info signal */ 
x = gsl_vector_alloc(M); 
for(k=0; k<K; k++) 
{ 
    gsl_vector_set(x, rand()%M, 2.0*rand()/(float)RAND_MAX - 1.0); //put random values at k random positions 
} 

/* Allocate memory for solution (evaluated signal) */ 
z = gsl_vector_calloc(M); 

/* Allocate memory for residual vector */ 
r = gsl_vector_calloc(M); 

/* Allocate memory for the encoded signal vector (its representation) */ 
y = gsl_vector_alloc(N); 

htime=((double)clock()-start)/CLOCKS_PER_SEC; 
printf("\nTime data allocation: %f", htime); 

/* Encoding the signal (x to y) */ 
start = clock(); 
gsl_blas_dgemv(CblasNoTrans, 1, D, x, 0, y); // y = Dx 
htime=((double)clock()-start)/CLOCKS_PER_SEC; 
printf("\nTime for encoding: %f", htime); 

/* Decoding the signal */ 
start = clock(); 
normi = gsl_blas_dnrm2(y); // ||y|| (L2 norm) 
epsilon = sqrt(epsilon * normi); 
normf = normi; 
iter = 0; 

/*iterate till the computational error is small enough*/ 
while(normf > epsilon && iter < numOfIterations) 
    { 
     gsl_blas_dgemv(CblasTrans, 1, D, y, 0, r); // r=D'*y 
     q = gsl_blas_idamax(r); //index of max element in residual vector 
     tmp = gsl_vector_get(r, q); //the max element in r 
     gsl_vector_set(z, q, gsl_vector_get(z, q)+tmp); // z[q]=z[q]+ tmp 
     v=gsl_matrix_column(D, q); // choose the dictrionary's atom (coloum) with the index of largest element in r 
     gsl_blas_daxpy(-tmp,&v.vector,y); // y = y-tmp*v 
     normf = gsl_blas_dnrm2(y); // ||y|| (L2 norm) 
     iter++; 
    } 

htime = ((double)clock()-start)/CLOCKS_PER_SEC; 
printf("\nTime for decoding: %f", htime); 
tmp = 100.0*(normf*normf)/(normi*normi); // the error at end of algorithm 
printf("\nComputation residual error: %f",tmp); 

/* Check the solution (evaluated signal) against the original signal */ 
printf("\nSolution (first column),Reference (second column):"); 
getchar(); // wait for pressing a key 
for(m=0; m<M; m++) 
    { 
     printf("\n%.3f\t%.3f", gsl_vector_get(x, m),gsl_vector_get(z, m)); 
    } 

normi = gsl_blas_dnrm2(x); 
gsl_blas_daxpy(-1.0, x, z); // z = z-x 
normf = gsl_blas_dnrm2(z); // ||z|| (L2 norm) 
tmp = 100.0*(normf*normf)/(normi*normi); //final error 
printf("\nSolution residual error: %f\n",tmp); 

/* Memory clean up and shutdown*/ 
gsl_vector_free(y); gsl_vector_free(r); 
gsl_vector_free(z); gsl_vector_free(x); 
gsl_matrix_free(D); 
getchar(); 
return EXIT_SUCCESS; 
} 
+7

'#定義:N((INT)(M/2))'號請,** **號。另外,你是在同一時間用C和C++編寫的嗎? – 2013-03-04 14:44:47

+0

如何聲明'gsl_blas_daxpy'?什麼是參數類型?什麼是'gsl_matrix_column(D,q).vector'? – 2013-03-04 14:45:51

+2

你的編譯器告訴你到底是什麼問題。 'gsl_matrix_column(D,q).vector'不是內存中的可尋址插槽。 – SomeWittyUsername 2013-03-04 14:46:46

回答

1

如果你做出更永久的家的gsl_matrix_column的返回值,(這個特殊的)的問題就會迎刃而解。

下面是一些簡單的代碼,說明一個人如何可以在可尋址插槽捕獲返回值:

struct _foo { 
    int i; 
}; 

struct _foo bar() { 
    struct _foo result = { 5 }; 
    return result; 
} 

/* won't compile; 'lvalue required as unary & operand */ 
void qux() { 
    int *j = &bar().i; 
} 

/* compiles OK */ 
void qal() { 
    struct _foo result = bar(); 
    int* j = &result.i; 
} 
3

gsl_matrix_column(D, q).vector是R值。你不能接受它的地址。您需要一個L值,因此首先將其分配給一個命名變量,然後將該變量的地址傳遞給該函數。

+0

謝謝你的解決方案 – 2013-03-04 15:12:31

1
gsl_vector_view c=gsl_matrix_column(D, q); 
    gsl_blas_daxpy(-a,&c.vector,y); 

我想,引入一個時間變量導致您將一個指針傳遞給函數。

編輯:嗯,試圖瞭解這個問題,我想知道的功能期待:

int gsl_blas_daxpy (double alpha, const gsl_vector * x, gsl_vector * y)

gsl_vector_view gsl_matrix_column (gsl_matrix * m, size_t j)

witj一些explanation

矢量視圖可以被傳遞到這需要一個矢量 參數只是作爲一個直接分配矢量將是,使用 & view.vector任何子程序。

example

for (j = 0; j < 10; j++) 
    { 
     gsl_vector_view column = gsl_matrix_column (m, j); 
     double d; 

     d = gsl_blas_dnrm2 (&column.vector); 

     printf ("matrix column %d, norm = %g\n", j, d); 
    } 
+0

爲什麼gsl_vector_view並不僅僅是gsl_vector? – 2013-03-04 15:21:43

+1

@Ian Banani。請參閱編輯:和鏈接。希望幫助。 – qPCR4vir 2013-03-04 20:14:54

1

現在我們有另外一個問題:

這裏另一個答案:

你知道int K= 0.7K=0 ??

#define M (10) // Number of columns in dictionary */ 
int K = 0.07*M; //Number of non-zero elements in signal - the sparsity 

alloc不初始化矢量xx將含有垃圾值,而不是0。你的意思是x = gsl_vector_calloc(M);with c?它會將x設置爲0

/* Create a random K-sparse info signal */ 
x = gsl_vector_alloc(M); 
for(k=0; k<K; k++)  // K=0, for get skiped and x not modified. 
{ 
    gsl_vector_set(x, rand()%M, 2.0*rand()/(float)RAND_MAX - 1.0); //put random values at k random positions 
} 

(在這裏,你將有至多爲k的隨機值,但有可能免得)

+0

謝謝,你是對的.... – 2013-03-05 07:59:22

+0

你有什麼建議來解決鑄造/倒圓問題?我無法通過生成錯誤...#定義M(10) 的#define N((int)的(M/2)) 雙稀疏= 0.07 * M; int K =(int)floor(sparsity); – 2013-03-05 08:22:32

+0

@IdanBanani你沒有鑄造/四捨五入的問題。很簡單,10%的7%非常小。使用更大的M(100?)或更多的非零元素30%??例如M = 10,K = 0.3 * M = 3。 – qPCR4vir 2013-03-05 08:58:58

相關問題