1
我正在寫c代碼來獲得使用R中的lapack庫獲得矩陣A的QR分解。 我的結果與使用R語言命令得到的結果不同。這是一個lapack問題,或者可能是我的代碼中的錯誤?
這是一個lapack的東西,或者可能是我的代碼中的錯誤?
矩陣(行主要):
1, 19.5, 43.1, 29.1,
1, 24.7, 49.8, 28.2,
1, 30.7, 51.9, 37.0,
1, 29.8, 54.3, 31.1,
1, 19.1, 42.2, 30.9,
1, 25.6, 53.9, 23.7,
1, 31.4, 58.5, 27.6,
1, 27.9, 52.1, 30.6,
1, 22.1, 49.9, 23.2,
1, 25.5, 53.5, 24.8,
1, 31.1, 56.6, 30.0,
1, 30.4, 56.7, 28.3,
1, 18.7, 46.5, 23.0,
1, 19.7, 44.2, 28.6,
1, 14.6, 42.7, 21.3,
1, 29.5, 54.4, 30.1,
1, 27.7, 55.3, 25.7,
1, 30.2, 58.6, 24.6,
1, 22.7, 48.2, 27.1,
1, 25.2, 51.0, 27.5
[R結果是:
V1 V2 V3 V4
[1,] -4.4721360 -113.16740034 -2.288392e+02 -123.52039508
[2,] 0.2236068 -21.89587861 -2.107945e+01 -7.27753395
[3,] 0.2236068 0.29484219 8.733781e+00 -14.04825478
[4,] 0.2236068 0.25373857 7.566965e-02 -1.55436071
[5,] 0.2236068 -0.23493787 2.999600e-01 0.26555995
[6,] 0.2236068 0.06192165 -3.343037e-01 0.12188660
[7,] 0.2236068 0.32681168 -2.315941e-01 0.40765540
[8,] 0.2236068 0.16696425 1.213823e-01 -0.18580207
[9,] 0.2236068 -0.09792578 -2.561224e-01 -0.16369010
[10,] 0.2236068 0.05735458 -2.993562e-01 0.52588892
[11,] 0.2236068 0.31311047 -4.660317e-02 0.29409317
[12,] 0.2236068 0.28114099 -1.340150e-01 0.16746961
[13,] 0.2236068 -0.25320615 -2.357881e-01 0.26072358
[14,] 0.2236068 -0.20753545 1.360745e-01 0.18135493
[15,] 0.2236068 -0.44045600 -2.456167e-01 0.15393180
[16,] 0.2236068 0.24003736 3.166468e-02 -0.02119950
[17,] 0.2236068 0.15783011 -2.667146e-01 0.32042553
[18,] 0.2236068 0.27200685 -3.732646e-01 0.05926317
[19,] 0.2236068 -0.07052337 3.634501e-03 -0.20518296
[20,] 0.2236068 0.04365337 -4.566657e-02 -0.03457051
我的結果:
-4.4721, -113.1674, -228.8392, -123.5204,
0.1827, -21.8959, -21.0794, -7.2775,
0.1827, 0.2888, 8.7338, -14.0483,
0.1827, 0.2486, 0.0523, -1.5544,
0.1827, -0.2301, 0.2071, 0.2316,
0.1827, 0.0607, -0.2309, 0.1063,
0.1827, 0.3201, -0.1599, 0.3555,
0.1827, 0.1636, 0.0838, -0.1620,
0.1827, -0.0959, -0.1769, -0.1427,
0.1827, 0.0562, -0.2067, 0.4586,
0.1827, 0.3067, -0.0322, 0.2565,
0.1827, 0.2754, -0.0925, 0.1460,
0.1827, -0.2480, -0.1628, 0.2274,
0.1827, -0.2033, 0.0940, 0.1581,
0.1827, -0.4315, -0.1696, 0.1342,
0.1827, 0.2351, 0.0219, -0.0185,
0.1827, 0.1546, -0.1842, 0.2794,
0.1827, 0.2665, -0.2578, 0.0517,
0.1827, -0.0691, 0.0025, -0.1789,
0.1827, 0.0428, -0.0315, -0.0301,
#include <stdio.h>
#include <R.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
int min(int x, int y) {
if (x <= y)
return x;
else
return y;
}
int max(int x, int y) {
if (x >= y)
return x;
else
return y;
}
void transpose(int* nrow, int* ncol, double* a) {
int i, j, index, k = 0;
double* atransp = malloc(*nrow**ncol * sizeof (double));
//compute transpose
for (i = 0; i<*ncol; i++) {
index = i;
atransp[k] = a[index];
k++;
for (j = 0; j<*nrow - 1; j++) {
index += *ncol;
atransp[k] = a[index];
k++;
}
}
//copy transpose in array a
for (i = 0; i<*nrow**ncol; i++)
a[i] = atransp[i];
//free memory
free(atransp);
}
void getQR(int* rowX, int* colX, double* X, double* Tau) {
const int m = *rowX;
const int n = *colX;
double* a = X;
const int lda = max(1, m);
double* tau = malloc(min(m, n) * sizeof (double));
const int lwork = max(1, n);
double* work = malloc(max(1, lwork) * sizeof (double));
int info;
F77_NAME(dgeqrf)(&m, &n, a, &lda, tau, work, &lwork, &info);
printf("\n dgeqrf() ended with : %d\n",info);
copyTo(min(m, n), tau, Tau);
free(work);
free(tau);
}
int main() {
int rX = 20, cX = 4;
double X[] = {
1, 19.5, 43.1, 29.1,
1, 24.7, 49.8, 28.2,
1, 30.7, 51.9, 37.0,
1, 29.8, 54.3, 31.1,
1, 19.1, 42.2, 30.9,
1, 25.6, 53.9, 23.7,
1, 31.4, 58.5, 27.6,
1, 27.9, 52.1, 30.6,
1, 22.1, 49.9, 23.2,
1, 25.5, 53.5, 24.8,
1, 31.1, 56.6, 30.0,
1, 30.4, 56.7, 28.3,
1, 18.7, 46.5, 23.0,
1, 19.7, 44.2, 28.6,
1, 14.6, 42.7, 21.3,
1, 29.5, 54.4, 30.1,
1, 27.7, 55.3, 25.7,
1, 30.2, 58.6, 24.6,
1, 22.7, 48.2, 27.1,
1, 25.2, 51.0, 27.5
};
//column major
transpose(&rX,&cX,X);
//tau is needed to extract Q later
double* tau = malloc(min(rX, cX) * sizeof (double));
double* QR = malloc(rX*cX*sizeof(double));
copyTo(rX*cX,X,QR);
getQR(&rX, &cX, QR, tau);
//printmat(cX,rX,QR,"QR");
return 0;
}
沒有看到您的代碼,這是幾乎不可能告訴。但這不太可能是Lapack中的一個bug。 – 2011-05-02 01:02:36
好吧,我會發布代碼 – cfort 2011-05-02 01:08:04
你已經接受沒有答案,並且還沒有投票在堆棧溢出。請儘量成爲社區中更多參與者,並將其傳遞給幫助你的人。 – 2011-05-02 01:30:23