14,969,782 members
Articles / Programming Languages / Java
Article
Posted 14 Nov 2011

27.8K views
8 bookmarked

# la4j - Linear Algebra for Java

Rate me:
Elegant and pure Java matrix library

## Introduction

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.

## Features

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:

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

Java
```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:

Java
```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:

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

Here is an example of solving linear systems in la4j:

Java
```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.

This package is implemented in terms of Strategy design pattern.

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

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

Java
```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"));
mis.close(); ```

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

## History

• 14th November, 2011: Initial post

## Share

 Software Developer Intel 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.