- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写 C 代码以使用 R 中的 lapack 库获取矩阵 A 的 QR 分解。我的结果与使用 R 语言命令得到的结果不同。
这是 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;
}
最佳答案
首先,R 默认使用 LINPACK 例程 DQRDC2。您可以在 R 中使用 qr()
命令和 LAPACK=TRUE
选项来使用 LAPACK 例程:
> QR <- qr(Mat,LAPACK=TRUE)
> QR
$qr
V3 V4 V2 V1
[1,] -229.9739116 -123.04447843 -114.61600066 -4.45006998
[2,] 0.1823682 -19.23736803 -1.81750327 -0.25177355
[3,] 0.1900584 0.41052447 -12.08962672 0.36451274
[4,] 0.1988473 0.04298840 0.18492760 0.02485389
[5,] 0.1545369 0.37519893 -0.14576039 0.48992952
[6,] 0.1973825 -0.32149888 -0.01277324 -0.02631498
[7,] 0.2142277 -0.25359559 0.19391630 0.15368409
[8,] 0.1907908 0.07984478 0.13051129 -0.21692785
[9,] 0.1827344 -0.23371181 -0.11707414 -0.19513972
[10,] 0.1959177 -0.25431801 -0.01531797 0.38150533
[11,] 0.2072699 -0.07795264 0.21041668 0.11596815
[12,] 0.2076361 -0.16711575 0.17604756 -0.02208373
[13,] 0.1702836 -0.14766629 -0.23298857 0.30390902
[14,] 0.1618609 0.20180495 -0.14729488 0.33577876
[15,] 0.1563679 -0.12647957 -0.37138938 0.29164143
[16,] 0.1992135 -0.01062557 0.17041958 -0.12582222
[17,] 0.2025093 -0.25954266 0.06531149 0.14217017
[18,] 0.2145939 -0.40877853 0.13742162 -0.20783552
[19,] 0.1765090 0.01244890 -0.06067115 -0.15702073
[20,] 0.1867626 -0.04646282 0.01506042 -0.06657167
但是,您应该知道,函数 qr()
在本例中使用了 LAPACK 例程 DGEQP3。与您使用的 DGEQRF 例程相反,DGEQP3 计算具有列旋转的矩阵的 QR 分解。
很正常,您会得到不同的结果,因为您没有使用相同的方法。您应该记住,QR 分解不是唯一的解决方案。要知道你的 QR 分解是否正确,你可以简单地检查 Q 和 R 矩阵是否满足要求。例如在 R 中:
> Q <- qr.Q(QR)
> round( t(Q) %*% Q , 10 )
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
> all.equal(Q %*% qr.R(QR),Mat)
[1] TRUE
另见 ?qr
。
关于c - 这是 lapack 问题还是我的代码中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5852598/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!