数组在通过void函数传递后不会改变

时间:2015-03-07 21:13:27

标签: c arrays matrix linear-algebra numerical-methods

我用C编写了一个程序来计算矩阵的SVD,使用Jacobi算法。我在程序中有3个函数:一个名为jacobi_helper的函数,它将执行SVD,一个名为jacobi的函数,在初始化一些数组后调用jacobi_helper,以及调用jacobi的main函数。

我的问题在于,在jacobi_helper中SVD似乎正确执行但是这些值似乎没有返回到jacobi,即使它们是在void函数中传递的。最后,我试图打印SVD的矩阵U和V以及奇异值的向量,我的混淆源于奇异值的向量能够很好地打印但矩阵不能。

这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    double dot_product(double *a, double *b, int n){
        double sum = 0;
        for(int i = 0; i < n; i++){
            sum = sum + a[i]*b[i];
        }
        return sum;
    }

    //Right matrix must be transpose of the matrix you want to multiply
    void matrix_multiplication(left, right, output, n)
    int n;
    double left[n][n];
    double right[n][n];
    double output[n][n];
    {
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                output[i][j] = dot_product(left[i], right[j], n);
            }
        }
    }

    void transpose(a, n, a_transpose)
    int n;
    double a[n][n];
    double a_transpose[n][n];
    {

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                a_transpose[i][j] = a[j][i];
            }
        }
    }


    void jacobi_helper(a, n, givens, givens_transpose, s, s_matrix, u, temp_u, u_output, v, temp_v, v_output)
    int n; //Dimension of Matrix
    double a[n][n]; //Input Matrix
    double givens[n][n]; //Givens matrix to be formed in each step
    double givens_transpose[n][n]; //Transpose of Givens Matrix
    double s[n]; //Vector of singular values
    double s_matrix[n][n]; //Matrix containing the singular values.
    double u[n][n]; //U Matrix in SVD. (A = USV)
    double temp_u[n][n]; //Temporary Matrix
    double u_output[n][n];
    double v[n][n];
    double temp_v[n][n];
    double v_output[n][n];
    {
        u = a;


        //Set V to Identity Matrix
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(i == j){
                    v[i][j] = 1;
                }
                else{
                    v[i][j] = 0;
                }
            }
        }

        double n_squared = 0;
        double value = 0;
        double epsilon = 0.1;

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                n_squared = n_squared + (u[i][j]*u[i][j]);
            }
        }

        do{
            value = 0;
            for(int i = 0; i < (n-1); i++){
                for(int j = (i+1); j < n; j++){

                    double tmp1 = 0, tmp2 = 0, tmp4 = 0;
                    for(int k = 0; k < n; k++){
                        tmp1 = tmp1 + (u[k][i]*u[k][i]);
                        tmp2 = tmp2 + (u[k][i]*u[k][j]);
                        tmp4 = tmp4 + (u[k][j]*u[k][j]);
                    }

                    double tau = (tmp4 - tmp1)/(2*tmp2);

                    double t;
                    double t1;
                    double t2;

                    t1 = -tau + sqrt(1 + (tau*tau));
                    t2 = -tau - sqrt(1 + (tau*tau));

                    if(fabs(t1) < fabs(t2)){
                        t = t1;
                    }else{
                        t = t2;
                    }

                    double cos = 1/(sqrt(1+(t*t)));
                    double sin = cos*t;


                    //Make Givens Matrix
                    for(int k = 0; k < n; k++){
                        for(int l = 0; l < n; l++){
                            if(k == i && l == i){
                                givens[k][l] = cos;
                            }
                            else if (k == j && l == j){
                                givens[k][l] = cos;
                            }
                            else if ((k == i && l == j) && i < j){
                                givens[k][l] = -sin;
                            }
                            else if ((k == i && l == j) && i > j){
                                givens[k][l] = sin;
                            }
                            else if ((k == j && l == i) && i < j){
                                givens[k][l] = sin;
                            }
                            else if ((k == j && l == i) && i > j){
                                givens[k][l] = -sin;
                            }
                            else if(k == l){
                                givens[k][l] = 1;
                            }
                            else{
                                givens[k][l] = 0;
                            }
                        }
                    }


                    //Set U <- U*Givens
                    matrix_multiplication(u, givens, temp_u, n);
                    u = temp_u;

                    //Set V <- V*Givens
                    matrix_multiplication(v, givens, temp_v, n);
                    v = temp_v;

                    double temp = 0;
                    for(int k = 0; k < n; k++){
                        temp = temp + (u[k][i]*u[k][j]);
                    }
                    value = value + temp*temp;
                }
            }

        }while(sqrt(value) >= (epsilon*epsilon)*(n_squared));

        for(int i = 0; i < n; i++){
            double sing_value = 0;
            for(int k = 0; k < n; k++){
                sing_value = sing_value + (u[k][i]*u[k][i]);
            }
            s[i] = sqrt(sing_value);
            s_matrix[i][i] = 1/sqrt(sing_value);
        }

        matrix_multiplication(u, s_matrix, temp_u, n);
        u = temp_u;

        printf("%.16f  %.16f  %.16f  %.16f\n",u[0][0],u[0][1],u[1][0],u[1][1]);
        printf("%.16f  %.16f  %.16f  %.16f\n",v[0][0],v[0][1],v[1][0],v[1][1]);
        printf("%.16f  %.16f\n\n", s[0], s[1]);
    }


    void jacobi(double *a, int n, double *s, double *u, double *v){
        static double s_matrix[5000000];
        static double givens[5000000];
        static double givens_transpose[5000000];
        static double temp_u[5000000];
        static double temp_v[5000000];
        static double u_output[5000000];
        static double v_output[5000000];


        jacobi_helper(a, n, givens, givens_transpose, s, s_matrix, u, temp_u, u_output, v, temp_v, v_output);
    }

    int main(int argc, const char * argv[]) {
        static double a[4] = {2,2,-1,1};
        int n = 2;
        static double s[2];
        static double u[4];
        static double v[4];


        jacobi(a, n, s, u, v);
        printf("%.16f  %.16f  %.16f  %.16f\n",u[0],u[1],u[2],u[3]);
        printf("%.16f  %.16f  %.16f  %.16f\n",v[0],v[1],v[2],v[3]);
        printf("%.16f  %.16f\n", s[0], s[1]);
        return 0;
    }

所以问题是当我在结尾处有printf语句时,s [0]和s [1]在两种情况下是相同的,但u [0]到u [3]和v [0]到v [3]不一样。

从jacobi_helper函数中的print语句中得到(正确的值):

    u = 1.0000000000000000  0.0000000000000000  0.0000000000000000  1.0000000000000000
    v = 0.7071067811865475  -0.7071067811865475  0.7071067811865475  0.7071067811865475
    s = 2.8284271247461898  1.4142135623730949

但是从我得到的主要功能:

    u = 0.0000000000000000  0.0000000000000000  0.0000000000000000  0.0000000000000000
    v = 1.0000000000000000  0.0000000000000000  0.0000000000000000  1.0000000000000000
    s = 2.8284271247461898  1.4142135623730949

任何人都可以帮忙吗?如果这很有用,我使用的算法是来自这个网站的算法6:http://www.cs.utexas.edu/users/inderjit/public_papers/HLA_SVD.pdf

1 个答案:

答案 0 :(得分:1)

使用参数double *u调用jacobi_helper,该参数是指向可以存储结果的内存地址的指针,但jacobi_helper执行的第一件事是u = a,这意味着:忘记了内存地址u并改为使用a您在数组中设置了一些指向参数v的值,但稍后您继续使用v = temp_v

数组变量只是指向内存地址的指针,为该数组变量赋值将用另一个地址替换该内存地址。如果要更改数组变量指向的内存,则必须执行取消引用地址。

代码

u[0] = 123;
u[1] = 345;
u[2] = 678;

更改由u指向的内存保留的数据。另一方面,

u = a;

只会更改你,而u[0]的所有后续访问权限实际上都会访问与a[0]相同的内存。

参数double *u的类型和参数double u[n][n]也不匹配。