Sunday, March 22, 2015

Porting Matrix and Vector Algorithms using Java-OO and CFR

Porting algorithms between Java and other languages can be quite cumbersome. Different languages have different features, some of which may be difficult to port. When dealing with vectors and matrices, in my experience important differences in languages are:
  • Language support for matrices/vectors
  • Operator-overloading
  • Mutable or immutable matrices/vectors
  • Zero or one based indices
In this post I will discuss operator-overloading when porting algorithms to Java.
Operator overloading is not a feature supported by the Java language. Java-OO is a project that provides basic operator overloading support to the Java language. In this post I will discuss the use of this software in combination with the Matrix and  Vector classes in the thorwin-math library.

Before you can execute the example, set-up your IDE:
  • Download thorwin-math.*.jar and add it as library to your project. This file can be downloaded from
  • Download and install Java-OO for your compiler/IDE of choice. The Java-OO site is located here. It may be a good idea to check over the features of Java-OO before continuing.


Ok, lets take a look at constructors first. As thorwin-math is designed to be used with Java-OO, the Matrix and Vector classes provide static valueOf methods for constructing instances. Using Java-OO, these constructors are automatically found and used when assigning arrays or strings. Take a look at the following code:

double[] vectorArray = {1, 2, 3};
Vector v1 = vectorArray;
Vector v2 = "4 5 6";

double[][] matrixArray = {{1, 2, 3}, {4, 5, 6}};
Matrix m1 = matrixArray;
Matrix m2 = "1 2; 3 4; 5 6";

The first line contains the conversion of an array of doubles to a Vector. Although the valueOf method itself is very clear, this automatic conversion is quite nice when mixing the Vector class with existing code that uses arrays, when porting C code to Java for instance.

The conversion from a string is not the fastest way to construct a vector, but it is very clear and consise. When writing unit tests this notation is very nice, as I can cut and paste values between MATLAB and Java.

The conversion for the Matrix class are very similar. Again, the main use here is conversion of 2D arrays to matrices and interoperability with MATLAB.


The following code shows some examples of vector and matrix calculations, when using Java-OO.

Vector v3 = v1 + v2;
Vector v4 = v1 - v2;
Vector v5 = -v1;

Matrix m3 = m1 + m1;
Matrix m4 = m1 - m1;
Matrix m5 = -m1;
Matrix m6 = m1 * m2;
Vector v6 = m1 * v1;

double dot = v1 * v2;

This is all pretty much what you expect when using operator overloading. Java-OO really helps to implement algorithms from pseudo-code or porting from languages such as C#, C++ or MATLAB.

Element Access

Java-OO even provides array-like element access, which is really helpful to port code that uses arrays for vector values (C) or code that also has support for this (C++).

double element = v1[2];

Array-like assignment is also possible in Java-OO, but not with thorwin-math vectors, as these are immutable.

Note that Java code uses zero based indices. Special care must be taken when porting code that is has 1-based indices (MATLAB).

Removing Java-OO

Java-OO is very helpful for creating and maintaining math code.  Current support for compilers and integrated development environments is very good. There may still, however, be a good reason for not wanting a build dependency to Java-OO.

This is where a good Java decompiler is a valuable tool. I currently use CFR, which is a nice decompiler with Java 8 support. You can find it here.

Take, for example, the following piece of Java code that uses Java-OO:

Matrix m = (a + b) * (c - d);

This code can be compiled as part of some Java class. The resulting class file can be decompiled to regular Java code using CFR. The code snippet above is decompiled to:

Matrix m = a.add(b).multiply(c.subtract(d));

We end up with regular Java code.

No comments: