c++ - LAPACK 在 C++ 中的 QR 分解

标签 c++ matrix linear-algebra lapack

我最近安装了一个新的 C++ 矩阵计算库,名为 LAPACK .我是这个领域的初学者,想通过使用 dgeqrf 函数测试它在 QR 分解中的应用。我在下面准备了这个简单的代码:

#include <iostream>
#include <lapacke.h>

using namespace std;

int main()
{
    double a[4] = {0, 2, 2, -1};

    int m=2;
    int n=2;
    int info = 0;
    int lda = m;
    int lwork = n;
    double *work;
    double *tau;

    dgeqrfp_(&m, &n, a, &lda, tau, work, &lwork, &info);
}

我构建它没有错误,但是当我尝试运行它时,它没有工作。我收到了这些警告消息:

D:\c++ code\lllll\main.cpp|15|warning: 'tau' is used uninitialized in this function [-Wuninitialized]|
D:\c++ code\lllll\main.cpp|15|warning: 'work' is used uninitialized in this function [-Wuninitialized]|

我不知道问题出在哪里,但我认为我对 dgeqrf 函数的定义是错误的。

此外,dgeqrf 是一个void 函数。我需要将其结果(Q 矩阵)保存到另一个矩阵中并在我的计算中使用它。

有人知道吗?

最佳答案

the docs 中所述, TAUWORK 应该是函数可以处理的数组。

特别地,WORK 应该是一个double 的数组并且具有(至少)长度LWORK,它被用作内部临时存储器.

TAU 是一个数组,用于输出 QR 分解的基本反射器,长度(至少)min(n,m)

所以你的完整调用看起来像这样:

#include <iostream>
#include <lapacke.h>
using namespace std;
int main()
{
    double a[4] = {0,2,2,-1};
    int m=2;
    int n=2;
    int info = 0;
    int lda = m;
    int lwork = n;
    double work[2];
    double tau[2];
    dgeqrfp_(&m, &n, a, &lda, tau, work, &lwork, &info);
}

关于c++ - LAPACK 在 C++ 中的 QR 分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41111521/

相关文章:

r - 如何选择 R 矩阵中的唯一列

python - SymPy 秩与 NumPy 矩阵秩不同

python 沿角度而非轴求numpy数组中的元素

c++ - boost::program_options 是否支持要求一系列替代方案中的一个?

c++ - glfw 和 MSVS 2012 的链接器错误

C++ vector 矩阵的 Boost 序列化

Python - 具有稀疏结果的矩阵乘法

python - 选择 Numpy 数组的最后一个值

c++ - QGL小部件: Using setFormat() To Enable Multisampling = The Window Doesn't Close

c++ - 在 C++ 中,我必须为 'ctz' 命令使用什么库?