Return multiple matrices in C ++ (Armadillo library)

I am working with the Armadillo library in C ++. First I compute a special matrix (in my code: P), then I compute a QR decomposition (in my code: Q). In the end, I need to return both P and Q, as well as another matrix T to my main function.

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

I am looking for a method to return these matrices to armadillo without using pointers and references. My matrices are huge and usually 500 * 500 or 1000 * 1000. Does anyone have a solution? thanks in advance

+5
source share
1 answer

Here is an example using 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); ... } 

With C ++ 17 (and structured binding ) you can do it like this:

 #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); ... } 
+7
source

Source: https://habr.com/ru/post/1262957/


All Articles