Domanda

Ho bisogno di attuare una formula moltiplicazione in cui una matrice di dimensioni fila 'n' deve essere moltiplicato per un n * n matrice ..

Ho usato classe DenseMatrix per creare il n * n matrice da una matrice 2D ... ma il mio problema è come creare un vettore riga ...

Posso utilizzare la classe CompRowMatrix per creare una matrice di fila ... ma per questo, di ingresso devono essere di 'Matrix' .. ma Matrix è un instantiate interface..can't it..the primo costruttore di class CompRowMatrix stati richiede un ' non zero array di indici come input..but io sono in grado di capire che cosa è questo non-zero array di indici ??

Inoltre, posso creare un vettore con DenseVector o qualsiasi altro adatto class..but sembra esserci alcun metodo per moltiplicare direttamente un vettore con una matrice ..

plz help

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top