英文:
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 <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;
// 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论