1

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?

Update:

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.

Community
  • 1
  • 1
Chapax
  • 21
  • 1
  • 2
  • 3
    Heaving large amounts of data in and out of a JVM can be rather resource consuming. Profile first to see if the pure-Java results are fast enough. – Thorbjørn Ravn Andersen Apr 17 '10 at 15:05
  • 1
    Don't reinvent the wheel. For almost any math question of the form "I want to do X in Y", there's always an optimized package out there. Search for "lapack java" or "blas java" or even "matrix multiplication java". – William Doane Apr 17 '10 at 16:01

5 Answers5

4

You could use a matrix package such as JAMA.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
4

There are several stackoverflow questions on using R with Java. This is simple with JRI. See this question for an example: 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.

Community
  • 1
  • 1
Shane
  • 98,550
  • 35
  • 224
  • 217
0

Another vote for jblas. all the methods are like you'd expect them to be.

Amir
  • 888
  • 9
  • 18
0

EJML seems to be a promising new one for fast multiplication. They have benchmarks on their site to show what they claim to be fast times for matrix multiplication.

demongolem
  • 9,474
  • 36
  • 90
  • 105
0

Parallel colt is an effective library to perform matrix operations .

Other good matrix libraries would include jblas and ujmp

All of these packages are effective. jblas is my personal favourite . It has good documentation and straight forward unlike ujmp

CTsiddharth
  • 907
  • 12
  • 21