【问题标题】:QR decomposition in C++ by LAPACKLAPACK 在 C++ 中的 QR 分解
【发布时间】:2017-04-27 22:46:05
【问题描述】:

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

#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 矩阵)保存到另一个矩阵中并在我的计算中使用。

有人知道吗?

【问题讨论】:

  • tauwork 参数应该是预分配的数组。有关更多信息(尤其是有关所述数组所需长度的信息),请参阅文档。
  • @BaummitAugen,但我找不到有关如何在文档甚至 Internet 中的“dgesqr”函数的简单 c++ 示例中确定参数的信息。
  • 好吧,好吧,我去找一个骗子,如果找不到,我会写一个答案。给我几分钟。

标签: c++ matrix linear-algebra lapack


【解决方案1】:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多