0

I have a problem that I'm trying to solve for a couple a days now. The problem sounds like this:

I have a function in R :

f <- function(x) {
  x ^ 3 - x ^ 2 - 4 * x + 2;
}

but I want to use it in Java, for example as follows: double y = f(5).

How can I do that?


Now, I'm using rJava package in R to use java code. I need to send this function as a parameter to a java method and calculate the function there (of course I can call the function in R and just send the result, but this is not the case, I need the function in Java as a whole).

989
  • 12,579
  • 5
  • 31
  • 53
ashcrok
  • 234
  • 3
  • 16
  • `java.util.Math.pow(int a, int b)` returns the value of `a^b`. But for your small powers, you can use `x*x*x` and `x*x` if you want. If powers specifically aren't your issue, then what is? Do you want a tutorial on Java-programming? – RaminS Jun 13 '16 at 21:05
  • What did you not understand from "for example". I want to create an R project for a complicating multi-objective processing, that is too expensive in R and I build it in Java. I need to pass complex functions as parameters (from R to Java) so I can process those functions in Java. Those functions are Input values. – ashcrok Jun 13 '16 at 21:14
  • You might want to have a look at this question:http://stackoverflow.com/questions/7451716/java-r-integration There were a lot of solutions offered some of which might work I think. – Alos Jun 13 '16 at 21:30
  • None of the answers from your link answer my question. – ashcrok Jun 14 '16 at 07:28
  • Noone has any ideea how to do this? Too bad :(. – ashcrok Jun 14 '16 at 15:41

2 Answers2

1

Assuming you're using the REngine API you want to construct a call to the function an evaluate it:

// get a reference to the function
REXP fn = eng.parseAndEval("function(x) x ^ 3 - x ^ 2 - 4 * x + 2", null, false);

// create a call and evaluate it
REXP res = eng.eval(new REXPLanguage(new RList( new REXP[] {
             fn, new REXPInteger(5)
                    })), null, true);
System.out.println("Result: " + res.asDouble());

and you'll get

Result: 82.0

Obviously, you can use new REXPSymbol("f") instead of fn if you want to keep the function as f on the R side.

PS: if you want quick answers, consider using the rJava/JRI mailing list stats-rosuda-devel.

Simon Urbanek
  • 13,842
  • 45
  • 45
  • Nice! Thank you. This is a nice approach and I will try it later. For now I made the evaluation part in R, and called each iteration in my algorithm Java code. It's not that expensive because, after a few tests, rJava wastes around 0.0005 seconds more than Java itself. – ashcrok Jun 18 '16 at 09:36
0

You can also use FastR, which is GraalVM based R implementation. The equivalent with FastR would be:

Context ctx = Context.newBuilder("R").allowAllAccess(true).build();
Value fun = ctx.eval("R", "function(x) x ^ 3 - x ^ 2 - 4 * x + 2")
System.out.println(fun.execute(5);

More details here: https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb

Steves
  • 2,798
  • 21
  • 21