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;
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
DCR
  • 14,737
  • 12
  • 52
  • 115
  • 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. – markspace Jul 10 '18 at 19:50
  • *"I need to convert temp to a Double[][]"* So what is stopping you from doing exactly that? – Andreas Jul 10 '18 at 19:50
  • `Double[][] inverse = new Double[][]temp;` I don't really get this... Is the Same!!! – Gatusko Jul 10 '18 at 19:51
  • @Andreas do you need to do this element by element or is there a better way? – DCR Jul 10 '18 at 19:52
  • @DCR See [first comment](https://stackoverflow.com/questions/51272989/convert-double-array-to-double-array#comment89524058_51272989): 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". – Andreas Jul 10 '18 at 19:52
  • @Andreas I don't see how this question implicitly says that at all. It's a lot more work to ask a full question on stackoverflow, (copying the code in your problem, formatting it, etc), than it is to try something on your own for this problem, IMO. I suppose that is just my own opinion, but perhaps the OP didn't know *what* to try, and needed some kind of lead for his specific problem. Maybe the OP did try something (e.g. `Double[][] inverse = new Double[][]temp;`). – Kröw Jul 10 '18 at 21:19
  • very closely related, but the opposite direction: https://stackoverflow.com/questions/37714550/how-do-you-convert-a-two-dimensional-array-of-a-boxed-type-to-a-two-dimensional – Hulk Jul 11 '18 at 06:51
  • @Kröw thank you. I did in fact try Double[][] inverse = new Double[][]temp; and of course it didn't work. It just seemed odd to me that you need to do this element by element. – DCR Jul 12 '18 at 01:44

3 Answers3

16

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);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 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 Jul 10 '18 at 23:27
  • @DCR I would like to share some links to makes you understand what's going on, first take a look at this http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ and [:: (double colon) operator in Java 8](https://stackoverflow.com/questions/20001427/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 :) – Youcef LAIDANI Jul 11 '18 at 06:48
  • 1
    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. – ViaTech Jul 11 '18 at 11:12
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);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Hulk
  • 6,399
  • 1
  • 30
  • 52
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];
Andreas
  • 154,647
  • 11
  • 152
  • 247