Vladimir has posted 1 posts at DZone. View Full User Profile

Introduction to la4j

11.21.2011
| 2114 views |
  • submit to reddit


Introduction

la4j - is 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 author in process of learning Calculation Math in one Russian university.


Features

There are following features of 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 bellow la4j core class diagram.


Figure 1. la4j core class diagram

la4j provides 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 popular calculation methods for solving linear systems. See bellow design of la4j.linear package.


Figure 2. la4j.linear package class diagram

As You can see la4j.linear package implement 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 la4j.decomposition package.


Figure 2. la4j.decomposition package class diagram

This package implemented in terms of Strategy design pattern.

Here is an example 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

Input/Output

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

In 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
0
1
0
0
2
0
0
3
0

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 ObjectInputStream and ObjectOutputStream in serialization algorithms. For example:


Matrix a = denseFactory.createMatrix(array);

ObjectOutput mmos = new MMOutputStream(new FileOutputStream("file.mm"));
mmos.writeObject(a);
mmos.close();
                
ObjectInput mmis = new MMInputStream(new FileInputStream("file.mm"));
Matrix b = (Matrix) mmis.readObject();
mis.close(); 

Resources

0
Published at DZone with permission of its author, Vladimir Kostyukov.

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