如何返回一个二维数组?

huangapple go评论69阅读模式
英文:

How do I return a 2-dimensional array?

问题

我正在制作一个用于反转一种类型矩阵的函数,但在返回它时遇到了问题。我查了一些相关的教程,但它们并没有成功,可能是因为它们涉及到不同的情况,所以我来到这个网站来寻求帮助。以下是代码(C++):

float inverse(float mat[4][4])
{
    float nmat[4][4];
    nmat[0][0] = mat[0][0]; nmat[0][1] = mat[0][1]; nmat[0][2] = mat[0][2]; nmat[0][3] = mat[0][3];
    nmat[1][0] = mat[1][0]; nmat[1][1] = mat[1][1]; nmat[1][2] = mat[1][2]; nmat[1][3] = mat[1][3];
    nmat[2][0] = mat[2][0]; nmat[2][1] = mat[2][1]; nmat[2][2] = mat[2][2]; nmat[2][3] = mat[2][3];

    nmat[3][0] = -(mat[3][0] * mat[0][0] + mat[3][1] * mat[1][0] + mat[3][2] * mat[2][0]);
    nmat[3][1] = -(mat[3][0] * mat[0][1] + mat[3][1] * mat[1][1] + mat[3][2] * mat[2][1]);
    nmat[3][2] = -(mat[3][0] * mat[0][2] + mat[3][1] * mat[1][2] + mat[3][2] * mat[2][2]);
    nmat[3][3] = 1.0f;

    return nmat;
}
英文:

I'm making a function to invert a type of matrix and I'm having trouble returning it. I've looked up some tutorials on it buy they didn't work probably because they had a different situation which is why it came to this site to answer my problem. Here's the code(C++):

   float inverse(float mat[4][4])
    {
    float nmat[4][4];
    nmat[0][0] = mat[0][0]; nmat[0][1] = mat[0][1]; nmat[0][2] = mat[0][2]; nmat[0][3] = mat[0][3];
    nmat[1][0] = mat[1][0]; nmat[0][1] = mat[1][1]; nmat[1][2] = mat[1][2]; nmat[1][3] = mat[1][3];
    nmat[2][0] = mat[2][0]; nmat[0][1] = mat[2][1]; nmat[2][2] = mat[2][2]; nmat[2][3] = mat[2][3];

    nmat[3][0] = -(mat[3][0] * mat[0][0] + mat[3][1] * mat[1][0] + mat[3][2] * mat[2][0]);
    nmat[3][1] = -(mat[3][0] * mat[0][1] + mat[3][1] * mat[1][1] + mat[3][2] * mat[2][1]);
    nmat[3][2] = -(mat[3][0] * mat[0][2] + mat[3][1] * mat[1][2] + mat[3][2] * mat[2][2]);
    nmat[3][3] = 1.0f;

    return mat;
    }

答案1

得分: 2

不要有别的内容,只返回翻译好的部分:

与低级数组不同,您可以使用 std::array,因为std::array可以在不引起任何问题的情况下进行复制、返回和传递:

#include <array>
using Float2D = std::array<std::array<float, 4>, 4>;

Float2D inverse(const Float2D& mat)
{
    Float2D nmat;
    nmat[0][0] = mat[0][0]; nmat[0][1] = mat[0][1]; nmat[0][2] = mat[0][2]; nmat[0][3] = mat[0][3];
    nmat[1][0] = mat[1][0]; nmat[0][1] = mat[1][1]; nmat[1][2] = mat[1][2]; nmat[1][3] = mat[1][3];
    nmat[2][0] = mat[2][0]; nmat[0][1] = mat[2][1]; nmat[2][2] = mat[2][2]; nmat[2][3] = mat[2][3];
    nmat[3][0] = -(mat[3][0] * mat[0][0] + mat[3][1] * mat[1][0] + mat[3][2] * mat[2][0]);
    nmat[3][1] = -(mat[3][0] * mat[0][1] + mat[3][1] * mat[1][1] + mat[3][2] * mat[2][1]);
    nmat[3][2] = -(mat[3][0] * mat[0][2] + mat[3][1] * mat[1][2] + mat[3][2] * mat[2][2]);
    nmat[3][3] = 1.0f;
    return mat;
}

int main()
{
    Float2D mat;
    // 填充 mat 的值
    //...
    // 获取逆矩阵
    auto invMat = inverse(mat);
}
英文:

Instead of low-level arrays, you can use std::array, since std::array can be copied, returned, and passed without any issues:

#include &lt;array&gt;
using Float2D = std::array&lt;std::array&lt;float, 4&gt;,4&gt;;

Float2D inverse(const Float2D&amp; mat)
{
    Float2D nmat;
    nmat[0][0] = mat[0][0]; nmat[0][1] = mat[0][1]; nmat[0][2] = mat[0][2]; nmat[0][3] = mat[0][3];
    nmat[1][0] = mat[1][0]; nmat[0][1] = mat[1][1]; nmat[1][2] = mat[1][2]; nmat[1][3] = mat[1][3];
    nmat[2][0] = mat[2][0]; nmat[0][1] = mat[2][1]; nmat[2][2] = mat[2][2]; nmat[2][3] = mat[2][3];
    nmat[3][0] = -(mat[3][0] * mat[0][0] + mat[3][1] * mat[1][0] + mat[3][2] * mat[2][0]);
    nmat[3][1] = -(mat[3][0] * mat[0][1] + mat[3][1] * mat[1][1] + mat[3][2] * mat[2][1]);
    nmat[3][2] = -(mat[3][0] * mat[0][2] + mat[3][1] * mat[1][2] + mat[3][2] * mat[2][2]);
    nmat[3][3] = 1.0f;
    return mat;
}

int main()
{
    Float2D mat;
    // fill in mat with values
    //...
    // Get inverse
    auto invMat = inverse(mat);
}

答案2

得分: 1

你想返回一个新数组还是更改你已有的数组?由于你将整个矩阵复制到 nmat 中,我想你想返回 nmat,而 nat 保持不变!正如 @273K 提到的,你的函数的返回类型不应该是一个单一的浮点数,而应该是一个数组的数组。最后一点:如果你使用循环来复制数组,会看起来更清晰。

英文:

Did you want to return a new array or change the array you had? As you copy the whole matrix into nmat, I suppose you wanted to return nmat as nat is unchanged! As @273K mentioned, the return type of your function should not be a single float but an array of arrays. Last point: It would look cleaner if you use a loop to copy the arrays.

huangapple
  • 本文由 发表于 2023年6月16日 10:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定