Thursday, November 26, 2009

The Pain of Java Matrix Libraries

Looking for a good Java Matrix (and actually also math) library, I was a bit surprised to find out there does not seem to be any really serious one still maintained.

Sure, there is Apache Commons Math, but it is still changing a lot, and it is not very performance optimized yet, while it has been active for several years already. There is also Java3D, it does Matrix through GMatrix, but not much linear algebra and if you look at their implementation, it is very basic, not performance oriented.

The other candidates seem to be 1-man projects that can disappear any other day (some of them look quite good like ojalgo, most of them are not interesting). Then you also have the serious but not maintained anymore Cern Colt library.

Compared to C/C++, the Java world is worrying if you want to do maths.

In those libraries, a dense matrix of double can be implemented two ways:
  1. by maintaining internally a double[][]. Usually those libraries allow for not copying the array, so it can be neat if your interfaces have this kind of arrays.
  2. by maintaining internally a double[]. The reason is for performance, but then each time you build a matrix from a double[][], an expensive copy will happen. So you need to use the Matrix object in your interfaces instead of double[][].
This is a pain because you can be very quickly stuck in one or the other Matrix library. A "solution" is to have your own interface, but that is a pain to write. There is UJMP, but it can hide some important methods (like transpose & multiply in one go from Colt or the ability to reuse an existing matrix in various operations to avoid allocating new memory), it is a students project (like parallel colt), but if it was a standard, it could be much more interesting.

In summary it does really look like scientific people, universities don't use Java for computation otherwise Colt surely would have been maintained.


  1. What do you think of Jama ( I'm not a matrix expert, but a friend/co-author of a project chose it a few years ago for a project involving matrix computation.

  2. Jama is good but is also not very active (1.0 in 1998, minor fixes in 2003 and 2005, current release is 1.02). It was also not intended for performance (although they did a tiny bit of optimization here and there).

  3. ojAlgo has been on SourceForge since early 2003 and has been continuously worked on since then.

    Even if I, the 1-man, abandon the project the (open) source code is still there.

    Other people are welcome to contribute to the project, but so far no one has. It seems people would rather build their own. I believe I can find around 30 java linear algebra libraries (most of which are incomplete, buggy and/or slow).

  4. Parallel Colt ( is being actively maintained, even tho the original, non-multithread oriented Colt no longer is.

  5. I'm the author of la4j library (Linear Algebra for Java). Yesterday (Dec 2012) I've released version 0.3.0. So, it's mainteined quite active. Try to look at it.

    BTW, It supports 2D-array based dense matrices as well as 1D-array based (and also sparse implemnetions).