Question

I need to implement a multiplication formula where an row matrix of size 'n' is to be multiplied by an n*n matrix..

I have used DenseMatrix class to create the n*n matrix from a 2D array...but my problem is how to create a row Vector...

I can use the CompRowMatrix class to create a row matrix...but for that, input must be of 'Matrix'..but Matrix is an interface..can't instantiate it..the first constructor of CompRowMatrix class states it requires a 'non-zero array of indices' as input..but i am unable to understand what is this non-zero array of indices??

also, I can create a vector with DenseVector or any other suitable class..but there seems to be no method to directly multiply a vector with a matrix..

plz help

Was it helpful?

Solution

The CompRowMatrix class is not really intended to be used as a row vector, rather it is used to represent sparse matricies in such a way that it is easy to iterate over the matrix elements row by row.

While it is possible to use CompRowMatrix as a vector by setting all rows other than the 1st to zero, this is more complicated for you as a programmer and less efficient for the code which has to assume that other rows could potentially become non-zero.

Instead, use a DenseVector object to hold your row vector and use the mult method from the Matrix interface. It accepts two Vector objects as arguments and produces a vector-matrix product. The method is called on the matrix object being multiplied with the following arguments:

  • 1st arg, x, is the vector you want to multiply with your matrix
  • 2nd arg, y, holds the result of the multiplication

So to produce the vector-matrix product y = x*A (where both x and y are 1xnrow vectors and A is an nxn matrix), you would do something like this:

// create matrix A
double[][] matValues = new double[n][n];
... // initialize values of the matrix
Matrix A = new DenseMatrix(matValues);

// create vector x
double[] vecValues = new double[n];
... // initialize values of the vector
Vector x = new DenseVector(vecValues);

// create vector y to store result of multiplication
Vector y = new DenseVector(n);

// perform multiplication
A.mult(x, y);

Now you can use y in the rest of your code as needed. It is important that you allocate y before the multiplication, but it is irrelevant what data it holds. The mult method will overwrite whatever is in y on exit.

Also note that the ways I chose to initialize x and A were not the only ways available. For instance, the above code automatically deep copies the arrays vecValues and matValues when constructing the corresponding Vector and Matrix objects. If you don't intend to use the arrays for any other purpose, then you should probably not perform this deep copy. You do this by passing an extra boolean paramter set to false in the constructor, e.g.

// create matrix A without deep copying matValues
Matrix A = new DenseMatrix(matValues, false);

You should refer to the javadoc both you and I linked to earlier for more constructor options. Be aware, however, that said javadoc is for a different version that the current release of MTJ (version 1.01 as of the time of this post). I don't know which version it is for nor have I been able to find javadoc for the current version, but I did spot a few differences between it and the current source code.

OTHER TIPS

If I understand your question, one solution would be to create a matrix with one row and n columns to premultiply the n x n matrix. There are routines for multiplying vectors, but I believe they all have the vector post-multiplying the matrix. If you'd like to use these routines instead, you'd have to do the appropriate transposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top