## Efficient Java Matrix Library

This tip discusses recent changes to Efficient Java Matrix Library (EJML) with the release of v0.24. In v0.24, the license of EJML was changed to Apache 2.0 and fixed sized matrices were introduced.

EJML is an open source linear algebra library for manipulating dense matrices. Its design goals are:

- to be as computationally and memory efficient as possible for both small and large matrices, and
- to be accessible to both novices and experts.

These goals are accomplished by dynamically selecting the best algorithms to use at runtime and by designing a clean API.

Website: http://ejml.org

To see how fast EJML is to other libraries, take a look at Java Matrix Benchmark at http://code.google.com/p/java-matrix-benchmark/.

## License Change

EJML has switched from LGPL to Apache 2.0 licenses. Apache 2.0 is a more permissive license than LGPL and there is also confusion over exactly how LGPL applies to a Java library. See links below for information on this topic:

- http://www.apache.org/licenses/LICENSE-2.0.html
- http://www.gnu.org/copyleft/lesser.html
- http://www.gnu.org/licenses/lgpl-java.html

## New Features

Fixed sized matrices are now supported. A fixed sized matrix is just what it sounds like, a matrix whose shape cannot change. Only a limited number of very small matrices are supported, from 2x2 to 6x6. Matrices of that size are often found in computer vision, computer graphics, and rigid body motion. The advantage of fixed sized matrices is that they can run much faster, up to 8 times faster in-fact, when compared to general purpose matrices. The reason for this large speed improvement is because they avoid array access overhead and are processed without `for`

-loops. However, as their size increases, the performance gain decreases and they eventually become slower.

Most standard linear algebra operations are supported and tools are provided for converting to other matrix types. An example of how to use them and convert between different types of matrices is provided at the project website, but has been pasted below for convenience.

http://ejml.org/wiki/index.php?title=Example_Fixed_Sized_Matrices

```
public class ExampleFixedSizedMatrix {
public static void main( String args[] ) {
// declare the matrix
FixedMatrix3x3_64F a = new FixedMatrix3x3_64F();
FixedMatrix3x3_64F b = new FixedMatrix3x3_64F();
// Can assign values the usual way
for( int i = 0; i < 3; i++ ) {
for( int j = 0; j < 3; j++ ) {
a.set(i,j,i+j+1);
}
}
// Direct manipulation of each value is the fastest way to assign/read values
a.a11 = 12;
a.a23 = 64;
// can print the usual way too
a.print();
// most of the standard operations are support
FixedOps3.transpose(a,b);
b.print();
System.out.println("Determinant = "+FixedOps3.det(a));
// matrix-vector operations are also supported
// Constructors for vectors and matrices can be used to initialize its value
FixedMatrix3_64F v = new FixedMatrix3_64F(1,2,3);
FixedMatrix3_64F result = new FixedMatrix3_64F();
FixedOps3.mult(a,v,result);
// Conversion into DenseMatrix64F can also be done
DenseMatrix64F dm = ConvertMatrixType.convert(a,null);
dm.print();
// This can be useful if you need do more advanced operations
SimpleMatrix sv = SimpleMatrix.wrap(dm).svd().getV();
// can then convert it back into a fixed matrix
FixedMatrix3x3_64F fv = ConvertMatrixType.convert(sv.getMatrix(),(FixedMatrix3x3_64F)null);
System.out.println("Original simple matrix and converted fixed matrix");
sv.print();
fv.print();
}
}
```