Simple 3x3 matrix inverse code (C++)

后端 未结 13 2078
花落未央
花落未央 2020-12-04 19:35

What\'s the easiest way to compute a 3x3 matrix inverse?

I\'m just looking for a short code snippet that\'ll do the trick for non-singular matrices, possibly using C

13条回答
  •  一整个雨季
    2020-12-04 20:17

    //Function for inverse of the input square matrix 'J' of dimension 'dim':
    
    vector > inverseVec33(vector > J, int dim)
    {
    //Matrix of Minors
     vector > invJ(dim,vector (dim));
    for(int i=0; i > invJT(dim,vector (dim));
     for(int i=0; i > Jac(3,vector (3));
    Jac[0][0] = 1; Jac[0][1] = 2;  Jac[0][2] = 6;
    Jac[1][0] = -3; Jac[1][1] = 4;  Jac[1][2] = 3;
    Jac[2][0] = 5; Jac[2][1] = 1;  Jac[2][2] = -4;`
    
    //Inverse of the matrix Jac:
    vector > JacI(3,vector (3));
        //call function and store inverse of J as JacI:
    JacI = inverseVec33(Jac,3);
    }
    

提交回复
热议问题