Linear algebra is a commonly used area of mathematics with a wide range of applications in various engineering and scientific fields. Examples of applications include line fitting, Kalman filters, face recognition, financial software, and numerical optimization to name a few. Many computer libraries have been developed for linear algebra. One of the better known would be LAPACK, which was originally programmed in Fortran.

## Introducing Efficient Java Matrix Library

The following article provides a brief introduction to one of the newer linear algebra libraries for Java, Efficient Java Matrix Library (EJML), a free open source library. EJML is a linear algebra library for dense real matrices. EJML's design goals are; 1) to be as computationally and memory efficient as possible for both small and large matrices, and 2) to be accessible to both novices and experts. These goals are accomplished by dynamically selecting the best algorithms to use at runtime and through a thoughtfully designed clean API. Providing good documentation, code examples, and constant benchmarking for speed, memory, and stability are also priorities.

The following functionality is provided:

- Basic Operators (addition, multiplication, ... )
- Matrix Manipulation (extract, insert, combine, ... )
- Linear Solvers (linear, least squares, incremental, ... )
- Decompositions (LU, QR, Cholesky, SVD, Eigenvalue, ...)
- Matrix Features (rank, symmetric, definitiveness, ... )
- Random Matrices (covariance, orthogonal, symmetric, ... )
- Different Internal Formats (row-major, block)
- Unit Testing

## Application Programming Interface

There are three primary ways to interact with EJML, 1) a simple to use object oriented interface, 2) procedural interface that provides greater control over memory and algorithms, 3) an expert interface that directly access specialized algorithms that is abstracted away using the first two. To see a practical comparison of these three interfaces take a look at the Kalman filter example provided at EJML's website. There each interface is used to code up a functionally identical Kalman filter:

Here are three code sniplets showing the Kalman gain being computed:

Simple:

SimpleMatrix K = P.mult(H.transpose().mult(S.invert()));

Procedural:

if( !invert(S,S_inv) )

throw new RuntimeException("Invert failed");

multTransA(H,S_inv,d);

mult(P,d,K);

Specialized:

if( !solver.setA(S) )

throw new RuntimeException("Invert failed");

solver.invert(S_inv);

MatrixMatrixMult.multTransA_small(H,S_inv,d);

MatrixMatrixMult.mult_small(P,d,K);

The full examples are available at: http://code.google.com/p/efficient-java-matrix-library/wiki/KalmanFilterExamples

Going beyond the basics, there are easy to use yet powerful Java interfaces for solving linear systems and matrix decompositions. These provide much more control over the type of algorithm used, what it computes, how much memory is used, and remove as much of the drudgery as possible. DecompositionFactory and LinearSolverFactory are provided for creating these solvers and decompositions. By using the factory the best most update algorithm will be automatically selected. EJML offer many different decomposition algorithms, even within the same family. For instance, there are four QR decompositions provided, each of which is catered towards a different sized matrix. Different factories hide much of these detail from the end user.

## Who is using EJML?

Even though EJML is a fairly new library it has already been picked up by several opensource projects:

- goGPS: http://code.google.com/p/gogps/
- Set Visualiser: http://www-edc.eng.cam.ac.uk/tools/set_visualiser/
- Universal Java Matrix Library (UJML): http://www.ujmp.org/
- Scalalab: http://code.google.com/p/scalalab/
- Java Content Based Image Retrieval (JCBIR): http://code.google.com/p/jcbir/
- JquantLib (Will be added): http://www.jquantlib.org/

## Performance and Benchmarks

What about speed, stability, and correctness? Constant benchmarking for speed and stability is a core part of EJML's development. It has many internal benchmarks and also uses Java Matrix Benchmark (JMatBench)[1] (http://code.google.com/p/java-matrix-benchmark/) to compare its performance against other libraries. At the time of this writing there are a total of 551 unit tests that test basic correctness of almost all the functions in EJML.

For runtime performance EJML is one of
the fastest **single** threaded
libraries. When compared against multi-threaded libraries on systems
with several cores/CPU's it still competitive for many operations,
despite its disadvantage of being single threaded. It
has one of the lowest memory footprints [2]. A
brief summary of EJML's performance relative to other libraries is
shown below.

- Runtime performance is measured on a Core i7M620 system. (2 cores with 4 threads)
- Performance varies significantly by system.
- This processor was choosen because it is the most modern system benchmarked.
- Click here to get a explaination of the plots shown below.

**[1]** Both EJML and JMatBench are
developed by the same author.

**[2]** Memory usage is highly dependent on
the operation being used and the parameters passed to it. JMatBench
only tests a few operations, but at least it shows attention is being
paid to memory usage.

## Comments

## Javin Paul replied on Mon, 2011/04/25 - 8:00am

Thanks a lot for making us aware of this library. A good matrix library can be quite useful for matrix related computation but as with other open source library how much testing has been done on this we are not sure and how good it is to use for production is still a question. I am sure it would get mature and accepted widely by other open source projects but it has long way to go.

Javin

Why String is immutable in Java

## Peter Abeles replied on Mon, 2011/04/25 - 12:05pm in response to: Javin Paul

Hi Javin,

Glad you mentioned testing, a fair amount of internal testing is done inside of EJML. There are hundreds of unit tests and as mentioned in the description above, constant testing for runtime performance, numerical stability, and memory usage are part of the development cycle. So far I have found numerical stability is the hardest area to compete against mature libraries such as LAPACK because of how subtle it is. Based on tests results in Java Matrix Benchmark is very close to LAPACK in terms of stability. In fact each time I update that benchmark I often send in at least a couple of bug reports to other library developers.

If you have any specific criticisms I would be glad to hear it, but please post it on the EJML message board at: http://groups.google.com/group/efficient-java-matrix-library-discuss

- Peter