質問

I want to perform matrix operation (for example find the transpose matrix of a given matrix A)

I found some libraries to do so, for example Colt:

http://acs.lbl.gov/software/colt/api/index.html

http://acs.lbl.gov/software/colt/api/cern/colt/matrix/package-summary.html

In the second link is mentioned that if you want to print the transpose, you type:

System.out.println(matrix.viewDice());

However, I don't want to print the transposed matrix. I want to store it in a second matrix, for example B, that would have the appropriate dimensions.

Any ideas?

役に立ちましたか?

解決

Have you seen the library Apache Commons Math? e.g.:

public static void main(String[] args) {
    double[][] data = { { 1, 2, 3 }, { 4, 5, 6 } };

    RealMatrix matrix = MatrixUtils.createRealMatrix(data);

    RealMatrix transpose = matrix.transpose();
}

他のヒント

Here is another example of transposing matrices in la4j (Linear Algebra for Java):

// create simple 2D array matrix of double[][] array
Matrix a = new Basic2DMatrix(new double[] {
  { 1.0, 2.0, 3.0 },
  { 4.0, 5.0, 6.0 },
  { 7.0, 8.0, 9.0 }
});

// transpose out-of-place
Matrix b = a.transpose();

// output matrices  
System.out.println(a);
System.out.println(b);

With colt library, you can simply do

DoubleMatrix2D transposed = matrix.viewDice();

viewDice() method returns a DoubleMatrix2D.

Just to add my 2 cents because I was thinking of implementing a matrix transpose by myself.

Could we add an wrapper to the original java 2D array?

When you transpose a matrix, you don't transpose the actual array at all, instead simply flag it as transpose=true in the wrapper.

When you want to access value at (3,2) via the wrapper, the wrapper swaps the row and column indices and return the value at (2,3)

You can do the same to other operations until an actual transpose is "really" necessary.

By "really" I mean the actual transposed matrix has computation performance advantages than using the original matrix, which mainly depends on what operations you will perform on the matrix.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top