3

With the Apache common math library I get back a primitive double array.

  RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();

  double[][] temp = pInverse.getData();

I need to convert temp to a Double[][]

  Double[][] inverse = new Double[][]temp;
8
  • 5
    I'm pretty sure the only way to do this is to copy the arrays element by element, boxing as you go. It's a PITA but shouldn't be too hard to write a little method that does this. Commented Jul 10, 2018 at 19:50
  • "I need to convert temp to a Double[][]" So what is stopping you from doing exactly that? Commented Jul 10, 2018 at 19:50
  • Double[][] inverse = new Double[][]temp; I don't really get this... Is the Same!!! Commented Jul 10, 2018 at 19:51
  • @Andreas do you need to do this element by element or is there a better way? Commented Jul 10, 2018 at 19:52
  • @DCR See first comment: Element by element. --- Even if there was a potential better way, it shouldn't have stopped you from trying. All your question says: "I need ...", and implicitly "and I couldn't be bothered to try something on my own". Commented Jul 10, 2018 at 19:52

3 Answers 3

17

If you are using Java 8+ you can use :

Double[][] inverse = Arrays.stream(temp)
        .map(d -> Arrays.stream(d).boxed().toArray(Double[]::new))
        .toArray(Double[][]::new);
Sign up to request clarification or add additional context in comments.

3 Comments

can you break this syntax down and explain what is going on? Why can't we use the map function directly on temp and what does :: mean? also, why do you use to toArray method twice?
@DCR I would like to share some links to makes you understand what's going on, first take a look at this winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples and :: (double colon) operator in Java 8 Also I used two toArray, the first one to collect the result of the boxing of each single array, the second is to collect the result in an array of arrays hope this can gives you a global idea :)
Streams and Lambdas seem like the best way to deal with this question. The accepted answer isn't wrong, but this method is cleaner to read.
9

As you are already using Apache Commons, it might be worth pointing out ArrayUtils.toObject

Converts an array of primitive doubles to objects.

Using that, you could write Andreas first solution as

Double[][] inverse = new Double[temp.length][];
for (int i = 0; i < temp.length; i++) {
    inverse[i] =  ArrayUtils.toObject(temp[i]);
}

or YCF_L's solution as

Double[][] inverse = Arrays.stream(temp)
    .map(ArrayUtils::toObject)
    .toArray(Double[][]::new);

Comments

7

It's a simple set of nested loop:

Double[][] inverse = new Double[temp.length][];
for (int i = 0; i < temp.length; i++) {
    inverse[i] = new Double[temp[i].length];
    for (int j = 0; j < temp[i].length; j++)
        inverse[i][j] = temp[i][j];
}

It's even shorter if you know all the sub-arrays are the same size:

Double[][] inverse = new Double[temp.length][temp[0].length];
for (int i = 0; i < temp.length; i++)
    for (int j = 0; j < temp[0].length; j++)
        inverse[i][j] = temp[i][j];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.