Thursday, June 27, 2013

Vector Algebra in la4j

All Vector Algebra methods in la4j were completely messed up. That was my fault. I've spent loads of time improving core la4j's primitive - it's matrices. But, good news here, that Daniel Renshaw caught all this mess and proposed a pull-request with changes. Thanks Daniel! This is such a great example of how do the open source world live. So, I'll try to introduce you new features of la4j's Vector class.

The la4j now supports Inner Product (Dot Product). It was previously implemented by Vector.product(Vector) method:
Vector a = new BasicVector(new double[]{ 1.0, 2.0, 3.0 });
Vector b = new BasicVector(new double[]{ 4.0, 5.0, 6.0 });

double d = a.innerProduct(b); // 1.0*4.0 + 2.0*5.0 + 3.0*6.0 = 32

There is also a Hadamard Product (element-wise product), which was implemented in Vector.multiply(Vector).
Vector a = new BasicVector(new double[]{ 1.0, 2.0, 3.0 });
Vector b = new BasicVector(new double[]{ 4.0, 5.0, 6.0 });

Vector c = a.hadamardProduct(b); // Vector { 4.0, 10.0, 18.0 }

The la4j also supports Outer Product, which produces a matrix as result (was not supported previously).
Vector a = new BasicVector(new double[]{ 1.0, 2.0, 3.0 });
Vector b = new BasicVector(new double[]{ 4.0, 5.0, 6.0 });

/* 
Matrix {
  4.0,  5.0,  6.0
  8.0, 10.0, 12.0
 12.0, 15.0, 18.0
}
*/
Matrix c = a.outerProduct(b);

So, all these things allow do not care about vector's orientation (row-vector/column-vector). Just use vector class when you need it. The la4j does all the work for you. It automatically determine by context whether the vector is a row-vector or a column-vector.

No comments:

Post a Comment