Author: Peter Abeles [email protected]
Efficient Java Matrix Library (EJML) is a linear algebra library for manipulating real/complex/dense/sparse matrices. Its 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, clean API, and multiple interfaces. EJML is free, written in 100% Java and has been released under an Apache v2.0 license.
EJML has three distinct ways to interact with it: 1) Operations, 2) SimpleMatrix, and 3) Equations. Operations provides all capabilities of EJML and almost complete control over memory creation, speed, and specific algorithms with a procedural API. SimpleMatrix provides a simplified subset of the core capabilities in an easy to use flow styled object-oriented API, inspired by Jama. Equations is a symbolic interface, similar in spirit to Matlab and other CAS, that provides a compact way of writing equations. The following functionality is provided:
Unit tests are extensively used to ensure correctness of each algorithm's implementation. Internal benchmarks and Java Matrix Benchmark are both used to ensure the speed of this library.
For a more detailed explanation of how to use the library see:
The JavaDoc has also been posted online at:
EJML is in Maven central repository and can easily be added to Gradle, Maven, and similar project managers.
<groupId>org.ejml</groupId> <artifactId>ejml-all</artifactId> <version>0.41.1</version>
This will add the entire library. Alternatively, you can include the required modules individually:
|ejml-core||Contains core data structures and common code|
|ejml-fdense||Algorithms for dense real 32-bit floats|
|ejml-ddense||Algorithms for dense real 64-bit floats|
|ejml-cdense||Algorithms for dense complex 32-bit floats|
|ejml-zdense||Algorithms for dense complex 64-bit floats|
|ejml-fsparse||Algorithms for sparse real 32-bit floats|
|ejml-dsparse||Algorithms for sparse real 64-bit floats|
|ejml-simple||Object oriented SimpleMatrix and Equations interfaces|
Unless you need a bleeding edge new feature or are contributing to EJML you probably don't need to build it yourself since pre-build jars are readily available on Maven Central. Gradle is the official tool environment for EJML. Java 11 or higher is required to build EJML, but it will generate Java 8 (a.k.a. Java 1.8) byte code. This is because it uses a few recent language features.
To build EJML from the command line use the following commands. These will generate all the source code and install it in your local Maven repository.
cd ejml ./gradlew autogenerate ./gradlew publishToMavenLocal
Here are a few other useful Gradle commands:
EJML provides three different ways to access the library. This lets the user trade off ease of use for control/complexity. An example of each is shown below. All of which implement Kalman gain function:
mult(H,P,c); multTransB(c,H,S); addEquals(S,R); if( !invert(S,S_inv) ) throw new RuntimeException("Invert failed"); multTransA(H,S_inv,d); mult(P,d,K);
SimpleMatrix S = H.mult(P).mult(H.transpose()).plus(R); SimpleMatrix K = P.mult(H.transpose().mult(S.invert()));
eq.process("K = P*H'*inv( H*P*H' + R )");
EJML supports a variety of different matrix types and uses the following pattern for matrix class names:
Patterns: <data type>Matrix<structure> <data type>MatrixSparse<structure> Description: <data type> is a single character 'D' for real double 'F' for real float 'Z' for complex double 'C' for complex float 'B' for binary <structure> is the internal data structure's name, see below. Matrix Suffix Abbreviation Description ========================================================================= RMaj RM dense row-major RBlock RB dense block row-major NxN FN dense fixed sized matrix of size N N FN dense fixed sized vector of length N CSC CC compressed sparse column Triplet TR sparse triplet ========================================================================= Examples: DMatrixRMaj double real dense row-major matrix CMatrixRMaj float complex dense row-major matrix ZMatrixSparseCSC double complex sparse CSC matrix CommonOps_DDRM Operations on DMatrixRMaj CommonOps_DSCC Operations on DMatrixSparseCSC
Algorithms which operate on a specific matrix type have a suffix that's 5 characters, e.g. _DDRM. The first letter 'D' is the data type, the second letter 'D' is for dense (sparse is 'S'), and the last two letters are an abbreviation for the structure.
A public message board has been created for asking questions and making comments:
Bugs can either be posted on that message board or at:
I would like to thank all the people have made various comments, suggestions, and reported bugs. Also David Watkins for writing "Fundamentals of Matrix Computations", which clearly explains algorithms and yet addresses important implementation issues. Timothy A. Davis for his book "Direct Methods for Sparse Linear Systems" and for CSparse which provided the initial seed for the sparse algorithms.
EJML is released under the Apache v2.0 open source license