Click here to Skip to main content
14,969,782 members
Articles / Programming Languages / Java
Posted 14 Nov 2011

Tagged as


8 bookmarked

la4j - Linear Algebra for Java

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Nov 2011CPOL2 min read
Elegant and pure Java matrix library


la4j is an open source, single-threaded and 100% Java library for solving problems of linear algebra. It supports sparse and dense matrices and covers almost all of the linear algebra tasks.

la4j was written by the author in the process of learning Calculation Math in one Russian university.


Following are the features of the current version of la4j:

  • Uniform interpretation of vectors and matrices
  • Sparse (CSR) and dense (2d array) matrices and vectors support
  • Basic vectors and matrices operations (addition, multiplying, transposing and other)
  • Linear systems solving (Gaussian Elimination, Jacobi, Gauss-Seidel and other)
  • Matrices decomposition (SVD, LU, Cholesky and other)
  • Inverted matrix foundation
  • Matrices and vectors serialization
  • I/O for vectors and matrices support (MatrixMarket format)

In addition, la4j now is 55 classes, 6700 loc, 90 tests, 50 kb (in jar).

Matrices and Vectors

See the below la4j core class diagram:

Image 1

la4j provides a flexible API for working with matrices and vectors through factories - DenseFactory and SparseFactory. Here is an example:

Factory denseFactory = new DenseFactory();
Factory sparseFactory = new SparseFactory();

double array[][] = new double[][] { 
    {1.0, 0.0, 0.0}, 
    {0.0, 5.0, 0.0}, 
    {0.0, 0.0, 9.0} 
Matrix a = sparseFactory.createMatrix(array);
Matrix b = denseFactory.createMatrix(array);

Matrix c = a.copy(denseFactory); // convert sparse to dense
Matrix d = b.copy(sparseFactory); // convert dense to sparse  

Here is an example of basic operations:

Matrix a = sparseFactory.createMatrix(array);
Matrix b = denseFactory.createMatrix(array);
Matrix c = a.multiply(b); // c - is sparse matrix
Matrix d = a.multiply(b, denseFactory); // d - is dense matrix

Matrix e = c.add(d).subtract(a).multiply(100); // c + d - a * 100

Matrix f = a.transpose(); // f - is sparse matrix
Matrix g = a.transpose(denseFactory); // g - is dense matrix 

Linear Systems

la4j supports most of the popular calculation methods for solving linear systems. See the below design of la4j.linear package:

Image 2

As you can see, la4j.linear package implements the Strategy design pattern.

Here is an example of solving linear systems in la4j:

Matrix a = denseFactory.createMatrix(array);
Vector b = sparseFactory.createVector(array[0]);

LinearSystem system = new LinearSystem(a, b);

Vector x = system.solve(new GaussianSolver()); // x - is dense vector

Vector y = system.solve(new JacobiSolver(), sparseFactory); // y - is sparse vector 

Matrix Decomposition

There are a lot of matrix decomposition methods available in the la4j.decomposition package.

Image 3

This package is implemented in terms of Strategy design pattern.

Here is an example of how to use la4j for matrix decomposition:

Matrix a = denseFactory.createMatrix(array);

Matrix[] qr = a.decompose(new QRDecompositor());     // qr[0] = Q, qr[1] = R; 
                        // Q,R - dense matrices

Matrix[] lu = a.decompose(new LUDecompositor(), sparseFactory); // lu[0] = L, lu[1] = U; 
                            // L,U - sparse matrices 


la4j supports I/O operations through package. It implements Bridge Design pattern.

The current implementation supports MatrixMarket format. Here is an example of output for matrix:

0 1 0
0 2 0
0 3 0 

For dense matrix, it will be:

%%MatrixMarket matrix array real general
3 3

For sparse matrix, it will be:

%%MatrixMarket matrix coordinate real general
3 3 3
0 1 1
1 1 2
2 1 3

la4j provides two classes: MMInputStream and MMInputStream, which can be used instead of ObjectInputStream and ObjectOutputStream in serialization algorithms. For example:

Matrix a = denseFactory.createMatrix(array);

ObjectOutput mmos = new MMOutputStream(new FileOutputStream(""));
ObjectInput mmis = new MMInputStream(new FileInputStream(""));
Matrix b = (Matrix) mmis.readObject();


You can find the la4j project at Google Code. Also you can visit la4j development blog at Blogger.


  • 14th November, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vladimir Kostyukov
Software Developer Intel
Russian Federation Russian Federation
I've received a Master's Degree in Computer Science at the IT Faculty of Altai State Technical University, Russia.

Currently, I am Software Engineer at Intel Corporation.

Comments and Discussions

QuestionIs there a library for Linear algebra matrix handling in Java? Pin
jackmay221-Dec-14 15:15
Memberjackmay221-Dec-14 15:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.