如何从犰狳中的函数返回多个值?

发布于 2025-01-16 14:23:24 字数 906 浏览 3 评论 0原文

我是 c++ 和犰狳的新手。我编写了一个函数,如下所示:

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

我想返回多个矩阵,例如 green_func_expand 和 temp_8。如何实现这一目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2025-01-23 14:23:24

在接收这个时使用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 ) ;
}

,尝试

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

然后你可以直接使用ret1和ret2

或者,你可以使用一个类并添加你想要返回的任何内容作为类变量,在你的函数中创建类的对象,填充变量并返回对象

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文