简体   繁体   English

无法理解以下代码。 以下代码如何创建正定矩阵

[英]Unable to understand the following code. How does the following code create a positive definite matrix

//Cannot understand use of this function

    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            double sum = 0;
            for(int k=0; k<n; k++) {
                //Why is i*n+k used here?
                sum += A[i*n+k]*A[j*n+k];
            }
            C[i*n+j] = sum;

int main() {
    

    double *m4 = (double*)malloc(sizeof(double)*n*n);
//Why was gemm_ATA function used here?
    gemm_ATA(m3, m4, n); //make a positive-definite matrix
    printf("\n");
    //show_matrix(m4,n);

    
    
}

I am making a project for parallelizing Cholesky method and found a useful code.我正在制作一个并行化 Cholesky 方法的项目,并找到了一个有用的代码。 In the given, project this function is used and I have no idea why is it used.在给定的情况下,使用了这个 function 项目,我不知道为什么要使用它。 Also, can someone help me understand the code and its function used in the code given in the link:- http://coliru.stacked-crooked.com/a/6f5750c20d456da9另外,有人可以帮助我理解链接中给出的代码中使用的代码及其 function 吗:- http://coliru.stacked-crooked.com/a/6f5750c20d456da9

The function gemm_ATA takes an input matrix A and calculates C = A^T * A , which is positive semi-definite by definition (note the semi-definiteness, depending on the properties of the input matrix). function gemm_ATA采用输入矩阵A并计算C = A^T * A ,根据定义这是半正定的(注意半定性,取决于输入矩阵的属性)。

Mathematically, calculating this matrix would be:在数学上,计算这个矩阵是:

c_i,j = sum_k  a_k,i * a_k,j

c_i,j is the entry of C in the i -th row and j -th column. c_i,jC在第i行第j列中的条目。 The expressions i*n+k and j*n+k transform these 2D indices (row and column) to a 1D index of the underlying array.表达式i*n+kj*n+k将这些二维索引(行和列)转换为基础数组的一维索引。

gemm_ATA calculate AA^T and stores it in C . gemm_ATA计算AA^T并将其存储在C中。 A^T is A but flipped over its diagonal. A^TA但翻转了它的对角线。 So A*A^T multiply each row of matrix A (call A[-,i] ) with each column of the matrix A (call A^T[j,-] ) which is also the row of A ( A[-,j] ).所以A*A^T将矩阵 A 的每一行(调用A[-,i] )与矩阵 A 的每一列(调用A^T[j,-] )相乘,这也是 A 的行( A[-,j] )。
Also, if you flatten an n*n 2D matrix to 1D matrix, you can access the first element of each i-th row by i*n+0 .此外,如果将 n*n 二维矩阵展平为一维矩阵,则可以通过i*n+0访问每个第 i行的第一个元素。 So if you want k-th column of ith row you have it with A[i*n+k] .因此,如果您想要第 i 行的第k列,您可以使用A[i*n+k]
Note that since you pass C by reference to your function, after calling the function, m4 is your positive definite matrix created from m3 .请注意,由于您通过引用 function 传递C ,因此在调用 function 之后, m4是从m3创建的正定矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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