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).

5
  • 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? Commented Jun 13, 2016 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. Commented Jun 13, 2016 at 21:14
  • You might want to have a look at this question:stackoverflow.com/questions/7451716/java-r-integration There were a lot of solutions offered some of which might work I think. Commented Jun 13, 2016 at 21:30
  • None of the answers from your link answer my question. Commented Jun 14, 2016 at 7:28
  • Noone has any ideea how to do this? Too bad :(. Commented Jun 14, 2016 at 15:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

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.