Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast Java matrix library suitable for JOGL + generic matrix math?

Tags:

java

matrix

jogl

I'm interested in writing an OpenGL app in JOGL 2, using shaders instead of the fixed-function pipeline. I'll need to do a fair bit of 4x4 double-precision matrix math CPU-side, to replace the fixed function pipeline's push/pop/transform business. The same app is also going to include some machine learning code that will require operations on large matrices. I've looked at JBLAS for machine learning stuff (and since I'm already using JNI for JOGL, there're minimal downsides to depending on another native library)), but I'm not sure if it's the best choice for GL-related matrices. Thoughts?

like image 731
Alterscape Avatar asked Jun 10 '11 21:06

Alterscape


2 Answers

Do you only need to manipulate 4x4 matrices? Most general purpose linear algebra libraries have been highly optimized for large matrices with little attention put into smaller ones. Part of the reason I wrote EJML was to address this issue and to motivate other developers to optimize for small matrices. EJML is the fastest for small matrices, but it is possible to do better.

If you really need a lot of performance I would not use any of the usual suspects and instead roll your own highly specialized code. It should be possible to beat general purpose libraries by several times.

Simple example for 2x2 matrix:

public class Matrix2x2 {
  double a11,a12,a21,a22;
}

public static void mult( Matrix2x2 a , Matrix2x2 b , Matrix2x2 c ) {
  c.a11 = a.a11*b.a11 + a.12*b.a21;
  c.a12 = a.a11*b.a12 + a.12*b.a22;
  c.a21 = a.a21*b.a11 + a.22*b.a21;
  c.a22 = a.a21*b.a12 + a.22*b.a22;
}

Note I have not tried to compile this code, it is just an example.

like image 53
lessthanoptimal Avatar answered Sep 21 '22 01:09

lessthanoptimal


These benchmarks might help you choose something that meets your performance needs.

http://lessthanoptimal.github.io/Java-Matrix-Benchmark/

like image 41
Sean McCauliff Avatar answered Sep 18 '22 01:09

Sean McCauliff