## JAMA Shortcoming

Recently I was involved in a task of building some real dense matrix (basically Term-document-Matrix )  based on the  requirement my matrix size will vary accordingly. initially my matrix row size was 2514 and  column was 1000 , so direct manipulation on this matrix is not  a good idea to practise. I used Single value decomposition  technique to reduce the dimension of any dense matrix  then manipulation is carried out on  the reduced dimension without compromising the result.

As a java programmer I had first choice to use JAMA library. JAMA is a basic linear algebra package for Java. It has six basic java classes defined to carry out the algebra problems. One of its class is SingularValueDecomposition . I used SVD class to get U , V , S matrix (reduced dimension )of my matrix ( size 2514 X 1000) , this works good .I was able to go for  further manipulations and got my results.

singular value decomposition

In my next matrix of size  29 X 1000 ( row length is 29 and column length is 1000)  I was thrown with  ArrayIndexOutOfBounds  exception from SVD class , on the close investigation on the issues , exception was thrown when ever S matrix is asked to compute. I reported the issue my immediate senior Raghavendran and asked his help to resolve the problem. He cross verified my program code and gave me confidence that code is error free and he also cracked joke that “there could be error on library ”. Thanks to his words what he said is true . While googling the issue it was found that JAMA library doest work properly when row length is less that column length.

Simple i can say for

3 X 3  Matrix – JAMA will work fine

3 X 2 Matrix  – JAMA will work fine

2 X 3  Matrix – JAMA will thow exception.

How I over come this:

One solution recommend by JAMA developer is to take transpose of Matrix then work for manipulation . That is make row as column and make column as row. If I take transpose of matrix then I have change my code , so I was not interested in taking transpose (this is mean that I cannot use JAMA lib for this instance).

Efficient Java Matrix Library (EJML)

I had a backup plan to go for Efficient Java Matrix Library (EJML) EJML is an another linear algebra package for Java.In EJML there is no restriction for matrix such as row length should be less than column length. Another  good thing about EJML is , it dynamically selects the best algorithm to be followed depending on whether the matrix is small or big. Depending on the dense of your matrix computation time varies  and It has got all functionally that JAMA has got (but nomenclature is different from JAMA). I know this library is good but never used to it  any how I decided my self that this is a chance where I can experiment this new library and  finally enjoyed its out come and benefits .

1. Chinni wrote
at 11:10 AM - 13th January 2011 Permalink

I am very curious why “JAMA’ throwing exception for 2 x 3 Matrix AND NOT FOR 3 X 2 Matrix.

2. shakthydoss wrote
at 12:40 PM - 13th January 2011 Permalink

Dear Chinna ,

JAMA lib works with an assumption that row length should be greater than column (e.g. 3 X 2) while using SVD. In case out matrix is TDM then U matrix from SVD correspond to Terms and V matrix from SVD corresponds to Documents.

Some times row length of TDM is less than column (2 x 3) , then JAMA developer ask us take transpose i.e make column as rows and make row as column (3 X 2) for using SVD. Then U matrix from SVD correspond to Documents and V matrix from SVD corresponds to Term.

This transpose of row as columns and column as row is not dynamically handled in JAMA lib as it was handled in EJML.
Now i hope would have understand why JAMA throws an exception 🙂