Peter has posted 10 posts at DZone. View Full User Profile

Introduction to Efficient Java Matrix Library (EJML)

  • submit to reddit

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
EJML can be downloaded from its website at:

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:


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


if( !invert(S,S_inv) ) 
throw new RuntimeException("Invert failed");


if( !solver.setA(S) )
throw new RuntimeException("Invert failed");

The full examples are available at:

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:

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

Performance on Corei7M620

Memory Benchmark

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


Published at DZone with permission of its author, Peter Abeles.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


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.


Why String is immutable in Java

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

p { margin-bottom: 0.08in; }a:link { }

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:

- Peter

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.