return c++ 中的多个矩阵(Armadillo 库)

return multiple matrices in c++ (Armadillo library)

我正在使用 C++ 中的 Armadillo 库。首先我计算一个特殊矩阵(在我的代码中:P),然后我计算 QR 分解(在我的代码中:Q)。最后,我需要 return P 和 Q 以及另一个矩阵 T 到我的主要函数。

#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
double phi(int n, int q){
...
  mat P(n,n);
  P=...
   mat Q,R;
   qr(Q,R,P);
return P:
return Q;
return Q;
...
 }

int main() {
    ...
    int n,q;
    cout<<"Enter the value of parameters n and q respectively:"<<endl;
    cin>> n>>q;
    phi(n,q);
...
}

我正在寻找一种方法 return 犰狳中的这些矩阵而不使用指针 references.Here 我的矩阵很大,通常是 500*500 或 1000*1000。有人有解决办法吗?预先感谢

这是一个使用 std::tuple along with std::tie

的例子
#include <iostream>
#include <armadillo>

using namespace arma;

std::tuple<mat, mat> phi(int const n, int const q)
{
    ...
    mat P(n, n);
    P = ...
    mat Q, R;
    qr(Q, R, P);
    return std::make_tuple(P, Q);
}

int main()
{
    ...
    int n, q;
    std::cout << "Enter the value of parameters n and q respectively:" << std::endl;
    std::cin >> n >> q;
    mat P, Q;
    std::tie(P, Q) = phi(n,q);
    ...
}

使用 C++17(和 structured binding 声明)你可以这样做:

#include <iostream>
#include <armadillo>

using namespace arma;

std::tuple<mat, mat> phi(int const n, int const q)
{
    ...
    mat P(n, n);
    P = ...
    mat Q, R;
    qr(Q, R, P);
    return {P, Q};
}

int main()
{
    ...
    int n,q;
    std::cout << "Enter the value of parameters n and q respectively:" << std::endl;
    std::cin >> n >> q;
    auto [P, Q] = phi(n,q);
    ...
}