简体   繁体   中英

How can I put pointer to function? (SVD example)

I found an example code that solve SVD. And It has this function :

int dsvd(float **a, int m, int n, float *w, float **v)

with description : * Input to dsvd is as follows:

  • a = mxn matrix to be decomposed, gets overwritten with u

  • m = row dimension of a

  • n = column dimension of a

  • w = returns the vector of singular values of a

  • v = returns the right orthogonal transformation matrix

And let's assuem I want to solve SVD with matrix (=a) is = {1,0,0,0,2, 0,0,3,0,0, 0,0,0,0,0, 0,4,0,0,0};

then what should I have to put into a, m, n, w, v????

Do I have to put values like

int a1 = 5;
int b1 = 4;
float **a = (float **)malloc(a1*sizeof(float*))
a[0] = (float*)malloc(b1*sizeof(float))
a[1] = (float*)malloc(b1*sizeof(float))
a[2] = (float*)malloc(b1*sizeof(float))
a[3] = (float*)malloc(b1*sizeof(float))
a[0][0] = 1, a[0][4] = 2  ...... 

?????????

Even though it is right (actually I do not think so..), I don't know what kinds of values do I have to put into *w and **v.

I think you do not need to put value in w and v. They are used to store return value. Hope the following solution will help you.

int main(){
int row=3;
int col=2;
float **a = (float **)malloc(row*sizeof(float*));
float *m = (float *)malloc(row*sizeof(float));     /* m is an array of size row*/
float **v = (float **)malloc(row*sizeof(float*));
int i, j;

for (i=0; i < row; i++) {
        a[i] = (float*)malloc(col*sizeof(float));  /* a is row*col */
}   

for (i=0; i < row; i++) {
        v[i] = (float*)malloc(row*sizeof(float));  /* v is a row*row */
}   

for (i=0; i < row; i++) {
    for (j=0; j < col; j++) {
        a[i][j] = i;                               /* you can assign the value you want to a */
    }   
}   
dsvd(a, m, n, w, v); 

}

Better something like this,

float a[][4] = {{1,0,0,0},{2,0,0,3},{0,0,0,0},{0,0,0,0},{4,0,0,0}};
int m = 5, n = 4;
float *w = malloc(4*sizeof(float));
float **v = malloc (4 * sizeof(float *) + 16 * sizeof(float));
dsvd(a,m,n,w,v);

If you need svd for some analysis (ie not study), it's probably better to use one of the standard LAPACK libraries or maybe NR code ( http://3map.snu.ac.kr/courses/2005/advance/c2-6.pdf )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM