'how to return multi values from function in Armadillo?

I'm new to c++ and Armadillo. I've written a function as follows:

mat solver(mat charge)
{
    mat x = regspace(0, mesh_num - 1);
    mat xx = reshape(x, 1, mesh_num);
    mat xxx = repmat(xx, mesh_num, 1);
    mat y = regspace(0, mesh_num - 1);
    mat yy = reshape(y, mesh_num, 1);
    mat yyy = repmat(yy, 1, mesh_num);
    mat green_func = -0.5 * log(square(step_xc * xxx) + square(step_yc * yyy));
    green_func(0, 0) = 0;
    mat temp_1 = fliplr(green_func);
    mat temp_2 = temp_1.cols(0, mesh_num - 2);
    mat temp_3 = flipud(green_func);
    mat temp_4 = temp_3.rows(0, mesh_num - 2);
    mat temp_5 = fliplr(temp_4);
    mat temp_6 = temp_5.cols(0, mesh_num - 2);
    mat temp_7 = join_horiz(temp_6, temp_4);
    mat temp_8 = join_horiz(temp_2, green_func);
    mat green_fun_expand = join_vert(temp_7, temp_8);
    return green_fun_expand;
}

I'd like to return more-than-one matrix, e.g. green_func_expand and temp_8. How to achieve this?



Solution 1:[1]

use a Tuple STL

tuple <mat, mat>  solver(mat charge)
{
    mat x = regspace(0, mesh_num - 1);
    mat xx = reshape(x, 1, mesh_num);
    mat xxx = repmat(xx, mesh_num, 1);
    mat y = regspace(0, mesh_num - 1);
    mat yy = reshape(y, mesh_num, 1);
    mat yyy = repmat(yy, 1, mesh_num);
    mat green_func = -0.5 * log(square(step_xc * xxx) + square(step_yc * yyy));
    green_func(0, 0) = 0;
    mat temp_1 = fliplr(green_func);
    mat temp_2 = temp_1.cols(0, mesh_num - 2);
    mat temp_3 = flipud(green_func);
    mat temp_4 = temp_3.rows(0, mesh_num - 2);
    mat temp_5 = fliplr(temp_4);
    mat temp_6 = temp_5.cols(0, mesh_num - 2);
    mat temp_7 = join_horiz(temp_6, temp_4);
    mat temp_8 = join_horiz(temp_2, green_func);
    mat green_fun_expand = join_vert(temp_7, temp_8);
    return make_tuple( green_fun_expand,temp_8 ) ;
}

while recieving this , try

  mat ret1, ret2;
    tie(ret1, ret2) = solver(mat, change);

then you can directly use ret1 and ret2

Alternatively , you can use a class and add whatever you want to return as class variables, create object of class inside your function, populate the variables and return the object

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vineesh Vijayan