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://ejml.org/wiki/index.php?title=Example_Kalman_Filter

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.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}