简体   繁体   中英

Matrix operations in Java

I looked for this everywhere, but I am wrong somewhere. In my Java program, I created few 2D Arrays. Now, I need to form new 2D arrays with previous, like, inverse, transpose, multiplying , LEFT DIVISION and maybe even more. Some of them (simple) I created myself, but left division, right division, inverse I didn't.

By using libs like Jama, some problem appears.

java.lang.RuntimeException: Matrix is rank deficient.

And I code it like this:

Matrix Am=new Matrix(A);
Am=Am.inverse();

A is 2D Array (mxn), and Am is new matrix created from 2DArray A.

I tried to do this to get left division, but I cannot solve matrix inverse first.

Where is my mistake? Does someone know another library to convert from 2DArray to Matrix, and then do harder matrix operations (left div, inv...) with it?

EDIT

I use inverse on this matrix to get A\\P (can be computed as inverse(A)*P as I saw somewhere). Do you know how to get A\\P with Jama? This is my primary probleb, left division.

For matrix libraries, you can check this question :

But for code you wrote, no one can say anything, you have to include the code itself to see where the problem is.

As your exception says, your matrix is rank deficient, it is mathematically impossible to compute an inverse matrix. The computer is not above mathematics in these affairs.

This can have multiple reasons. First of all, inverse matrices only exist for square inputs, ie, format nx n. You did not indicate that this is the case. There exists the idea of the pseudo-inverse that can be constructed for all matrices.

But even for square matrices, the rank can be deficient, ie, the matrix can be singular. In exact terms, this happens if the determinant is zero. In computational terms, this can also happen for ill-conditioned matrices with a wide magnitude range in their singular values.

Can you provide the matrix where that problem occurs?

如果矩阵的秩小于等于min(numOfRows,numOfColumns),则表示矩阵秩不足,并且无法计算此类矩阵的逆矩阵。

A is 2D Array (mxn), and Am is new matrix created from 2DArray A.

Make sure that your matrix is quadratic (nxn), otherwise the inverse is not defined.

If you can't avoid that the matrix is not full-ranked or quadratic, you might consider using the pseudo-inverse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM