0

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
    You promise to return `float` but actually return `(float[4])*`. – 273K Jun 16 '23 at 02:17
  • could you tell me how to fix it please? –  Jun 16 '23 at 02:38
  • 2
    Arrays in C++ are dumb. They cannot be returned. Use `using Float2D=std::array,4>;` and pass/return `Float2D`. The rest of your code remains the same. – PaulMcKenzie Jun 16 '23 at 02:41
  • can you show me an edited version of the code because it don't how to implement this by my own –  Jun 16 '23 at 02:44
  • The dupes: https://stackoverflow.com/questions/8617683/return-a-2d-array-from-a-function, https://stackoverflow.com/questions/3473438/return-array-in-a-function – 273K Jun 16 '23 at 02:48
  • Raw arrays cannot be returned in C++. It is possible to return an object (of `struct`/`class` type) that *contains* an array [which, roughly, is what `std::array,4>` is]. – Peter Jun 16 '23 at 03:03
  • Can you explain why you use raw arrays like this? It can be you are learning C++ from an outdates source. – Pepijn Kramer Jun 16 '23 at 05:26

2 Answers2

2

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);
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • One more improvement you can: Make Float2D a class Matrix2D and you can start adding operations to it, and you've made it distinct from a 2D vector allowing you to have functions that really only accept Matrix instances (more clear intent) – Pepijn Kramer Jun 16 '23 at 05:29
1

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.

  • i wanted to return nmat, and is there away to change the function type to accommodate to the array? –  Jun 16 '23 at 02:36