0

代码行: gsl_blas_daxpy(-a,&gsl_matrix_column(D, q).vector,y); 导致错误

错误 C2102:“&”需要左值

,现在的问题是我无法控制 GSL 功能,所以我不知道如何解决这个问题(删除“ &”不起作用)

之后我得到

错误 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;
}
4

4 回答 4

4

gsl_matrix_column(D, q).vector是一个 R 值。你不能拿它的地址。您需要一个 L 值,因此首先将其分配给命名变量,然后将该变量的地址传递给函数。

于 2013-03-04T15:01:26.537 回答
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)

有一些解释

向量视图可以传递给任何接受向量参数的子程序,就像直接分配的向量一样,使用 &view.vector。

和一个例子

   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);
     }
于 2013-03-04T15:05:02.857 回答
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;
}
于 2013-03-04T15:27:11.237 回答
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); c吗?它将设置x0

/* 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 个随机值,但可能是最少的)

于 2013-03-04T23:42:59.997 回答