Search code examples
javaarraysmatrixinverse

How to convert from Jama Matrix object to double[][] - Java


I have an object Matrix from the package JAMA, where I stored a double[][] array. I want to return the values of the method Matrix.inverse().

How can I pass values from a Matrix object to a double[][] array?

double K[][] = {{1,5,3,4} , {1,10,4,3} , {1,40,232,4} , {32,434,23,5}};
Matrix GetInverse = new Matrix(K);

double returnValues[][] = GetInverse.inverse(); // ---?----

Solution

  • It's hard to know how to answer this without knowing more about the Matrix class itself. The best approach is likely to be to write a toDoubleArray() method in the Matrix class, which would operate in a way similar to how the toArray() method words for the classes that implement List (which return an Object[]).

    It might be as simple as returning the internal double[][] stored in the Matrix instance.

    Since your edit clarified that you are using the JAMA class, you can simply do this:

    double returnValues[][] = GetInverse.inverse().getArray();
    

    Since you are not keeping a reference to the inverse Matrix object, this is safe. In general, though, to be safe you should not access the internal array directly and instead use the getArrayCopy() instead of getArray().