Skip to content
Prev 38756 / 63424 Next

rJava question

Dominick,
On Dec 27, 2010, at 11:56 PM, Dominick Samperi wrote:

            
stats-rosuda-devel is the rJava list you want to use.
The above is plain wrong (well, that's actually a guess since you didn't provide even the signature of the method) - matrices in R are vectors, so the type is double[]. In Java there is no matrix type, so it's up to the application to represent matrices and there are many ways - some more efficient than others. From your example above it seems that you are using double[][] -- so you have to allocate the extra objects one way or another (that's why it's inefficient to represent matrices that way).

But from your e-mail I have the feeling that your questions is rather about calling a method that returns double[][], so for example:
public static double[][] pass();
where double[][] is a rectangular array (i.e. length of all inner arrays is equal).

Unfortunately .jcall is a bit inconsistent due to bug that was introduced with the new J() API. So the intended behavior is (using class Test with the method above):
[,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16

The $ operator makes sure that the result is converted to a native R form where possible, including recursive structures like double[][].

However, the intention of .jcall() was to never perform recursive evaluation, so this is intended:
[[1]]
[1] "Java-Array-Object[D:[D at 11ddcde"

[[2]]
[1] "Java-Array-Object[D:[D at 18fb1f7"

[[3]]
[1] "Java-Array-Object[D:[D at ed0338"

[[4]]
[1] "Java-Array-Object[D:[D at 6e70c7"


The intended behavior (and true in older version of rJava) was for .jcall with evalArray=FALSE to return the reference:
[1] "Java-Array-Object[[D:[[D at 8f4fb3"

Unfortunately, the bug is that .jcall uses rJava:::newArray() to create the reference which defaults to simplify=TRUE so the current (buggy) behavior is:
[,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16


The real issue is that:
a) if the bug is fixed, .jcall() can return just the reference which is intended
but
b) there is currently no exposed API for rJava:::newArray so the simplification is not available in any form for references if the bug is fixed (other than using the J/$ API).

That's why I was hesitant so far to fix the bug, but I really should -- which is why the behavior your discovered will change. However, then I need to add some API to be able to convert a reference in the same way that rJava:::newArray provides and expose it - I didn't think through that part, so that's why I got stuck.

I hope it helps...
Cheers,
Simon

(PS: please continue any discussion on stats-rosuda-devel)