0

This is my method to calculate the inverse of a matrix (key). I used the JAMA lib for the calculation.

public static double[][] inverseKey(int[][] key) { 
    int n = key.length;
    double[][] keyAsDoubleArray = new double[n][n];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            keyAsDoubleArray[i][j] = key[i][j];
        }
    }

    
    Matrix matrix = new Matrix(keyAsDoubleArray);

    // Berechne die Inverse der Matrix
    Matrix inverseMatrix = matrix.inverse();
    double[][] inverseArray = inverseMatrix.getArray();

    return inverseArray;
}

There is no error but the result of the calculation is wrong. Result: 0.1587301587301587 0.011337868480725636 -0.2244897959183673 -0.7777777777777777 0.15873015873015872 0.857142857142857 0.5079365079365079 -0.10657596371882086 -0.48979591836734687

This is the matrix I used: int[][] key = {{6,13,20},{24,16, 17}, {1,10,15}};

I think there is maybe a problem in converting the int[][] in a double[][], but it is necessary in my specific case that the parameter of the method is a Integer Array. Have you any ideas to solve this problem?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
KayraOz
  • 31
  • 5
  • have you tried to directly create a new instance of `Matrix` passing a `double[][]` as constructor parameter without doing the conversion? if that also fails, I guess the lib could have a bug – Gastón Schabas May 15 '23 at 21:17

1 Answers1

1

According to this site the inverse of your matrix is:

0.15873015873015873015  0.011337868480725623586 -0.22448979591836734692
-0.77777777777777777776 0.15873015873015873014  0.85714285714285714285
0.5079365079365079365   -0.10657596371882086167 -0.48979591836734693897

which is identical to the inverse calculated by JAMA, so I'm inclined to think it's actually correct. What makes you think it is incorrect? You can verify it by multiplying the two matrices together - it should result in the identity matrix.

jon hanson
  • 8,722
  • 2
  • 37
  • 61