views:

293

answers:

4

Hi, I wanted to do matrix multiplication in Java, and the speed needs to be very good.

I was thinking of calling R through java to achieve this.

I had a couple of Qs though:

  1. Is calling R using Java a good idea? If yes, are there any code samples that can be shared?

  2. What are the other ways that can be considered to do matrix multiplication in Java?

Many thanks.

--Chapax

+3  A: 

You could use a matrix package such as JAMA.

Sebastian P.
+2  A: 

There are several stackoverflow questions on using R with Java. This is simple with JRI. See this question for an example: http://stackoverflow.com/questions/2180235/r-from-within-java. Once you have integrated your code, doing the matrix multiplication in R is trivial. If you have two matrices, a and b, you would simply call: a %*% b.

If you want to stay in pure Java and work with a mathematics library, you can also look into using Colt (which is adapted from JAMA), although you could be better off just using JAMA directly.

Another option would be to use Incanter from Clojure (which provides a wrapper around Parallel Colt, amongst other things), and then call it as a Jar from Java. It's trivial to integrate Clojure into Java, and if all you want is matrix multiplication, that will be easier for you than using R.

Shane
A: 

I have more details about the issue now, and I certainly wanted the experts' inputs on this:

My colleague who quit the firm was a C# programmer, who was forced to write Java code that involved matrix multiplication.

-- He has written his own DataTable class in Java, in order to be able to

a) create indexes to sort and join with other DataTables

b) matrix multiplication.

So, I want to essentially clean up the code, and thought using something like R within Java will help me focus on business logic rather than sorting, joining, matrix multiplication, etc.

Please let me know what you think.

Many thanks. --Chapax

Chapax
What I think is that you still haven't elucidated a need. Why do you want to clean it up? Is it wrong? Is it too slow? Is it unmaintainable? Is it failing to meet business requirements?
CPerkins
(1) Please edit your existing question rather than providing comments in an "answer", (2) you should create a user account so you don't have multiple different ones floating around, and (3) I updated my answer to address some of your points.
Shane
A: 

Thanks for your responses .. apologies -- I should have edited my existing question rather than post as an answer.

CPerkins -- the code, as I mentioned, is a mash of Java & C#!! It's correct, but not maintainable/extensible. I'm still figuring out whether it is slow ... from first glances, it looks like it cane be made faster just because it's been coded very poorly in Java. Also, my understanding of DataTables is pretty poor.

Chapax