×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

# How to use Java for performing Matrix Operations

In this article, we will be working on JAVA to perform various Matrix operations. These include operations such as transpose of matrix, cofactor of matrix, inverse of matrix and determinant of square matrix.

0

Note: Before performing these operations using JAVA, the most important thing is to have a better understanding of matrix. So, first we will be discussing matrices in detail. A Matrix is defined as a collection of numbers which are arranged into a fixed number of rows and columns. Usually the numbers used in these matrices are real numbers.

Figure 1: Shows the image of a matrix

The image shown above is a 3x3 matrix because it has three rows and three columns. The first 3 denotes the rows while the other 3 denotes the column. So, in simple terms the format for defining a matrix is “rows X columns”. Now each number that makes up a matrix is called an element of a matrix. All the elements in a matrix have specific locations.

Now, in this article for better understanding of the users I will be defining the matrices using three parameters. They are as follows:

• Number of rows
• Number of columns
• Data as an arrangement of doubles

Listing 1: Shows the code for defining a matrix

public class Matrix {

private int nrows;
private int ncols;
private double[][] data;

public Matrix(double[][] dat) {
this.data = dat;
this.nrows = dat.length;
this.ncols = dat[0].length;
}

public Matrix(int nrow, int ncol) {
this.nrows = nrow;
this.ncols = ncol;
data = new double[nrow][ncol];
}

After defining the matrices, the next thing is to perform the specific operations. The first thing is to perform the transpose of the matrix. Before performing the operation it is important to understand what is transpose? Transpose of a matrix is another matrix in which rows and columns are swapped.

Listing 2: Shows the code to transpose a matrix

public static Matrix transpose(Matrix matrix) {
Matrix transposedMatrix = new Matrix(matrix.getNcols(), matrix.getNrows());
for (int i=0;i<matrix.getNrows();i++) {
for (int j=0;j<matrix.getNcols();j++) {
transposedMatrix.setValueAt(j, i, matrix.getValueAt(i, j));
}
}
return transposedMatrix;
}

The second operation is to find the determinant of a square matrix. The important thing that needs to be noted here is that determinant is always found out for square matrix i.e., the matrix which has equal number of rows and columns.

Listing 3: Shows the code for finding the determinant of a square matrix

public static double determinant(Matrix matrix) throws NoSquareException {
if (!matrix.isSquare())
throw new NoSquareException("matrix need to be square.");

if (matrix.size()==2) {
return (matrix.getValueAt(0, 0) * matrix.getValueAt(1, 1)) - ( matrix.getValueAt(0, 1) * matrix.getValueAt(1, 0));
}
double sum = 0.0;
for (int i=0; i<matrix.getNcols(); i++) {
sum += changeSign(i) * matrix.getValueAt(0, i) * determinant(createSubMatrix(matrix, 0, i));
}
return sum;
}

Here change sign method is used according to which 1is returned if i is even and -1 is returned is i is odd. The above method used is a recursive function that breaks the larger matrix into smaller ones using the createSubMatrix method.

Listing 4: Shows the code to creating a SubMatrix

public static Matrix createSubMatrix(Matrix matrix, int excluding_row, int excluding_col) {
Matrix mat = new Matrix(matrix.getNrows()-1, matrix.getNcols()-1);
int r = -1;
for (int i=0;i<matrix.getNrows();i++) {
if (i==excluding_row)
continue;
r++;
int c = -1;
for (int j=0;j<matrix.getNcols();j++) {
if (j==excluding_col)
continue;
mat.setValueAt(r, ++c, matrix.getValueAt(i, j));
}
}
return mat;
}

In this method, the input parameters are the original matrix and the row and column index numbers that need to be deleted from the original matrix to create the sub-matrix.

The next operation that we will be performing is to find the cofactor of a matrix. Cofactor of a matrix Z is another matrix X that the value of element Xij equals the determinant of a matrix created by removing row i and column j from matrix Z.

Listing 5: Shows the code for finding the cofactor of a matrix

public static Matrix cofactor(Matrix matrix) throws NoSquareException {
Matrix mat = new Matrix(matrix.getNrows(), matrix.getNcols());
for (int i=0;i<matrix.getNrows();i++) {
for (int j=0; j<matrix.getNcols();j++) {
mat.setValueAt(i, j, sign(i) * changeSign(j) * determinant(createSubMatrix(matrix, i, j)));
}
}

return mat;
}

This method is very important for calculating the inverse of a matrix. The last operation that we will be performing is to find the inverse of the matrix. The inverse of a matrix is the hardest operation among others to understand and implement. Inverse of the matrix Z is another matrix which is denoted by Z-1.

The multiplication of the both the matrix i.e., Z and Z-1 is an identity matric which is denoted by I. Identity matrix is a matrix in which only the diagonal elements are 1while the rest of the elements are zero. In the case of a square matrix, the main or principal diagonal is the diagonal line of entries running from the top-left corner to the bottom-right corner. For a matrix A with row index specified by i and column index specified by j, these would be entries Aij with i=j.

Listing 6: Shows the code for finding the inverse of a matrix

public static Matrix inverse(Matrix matrix) throws NoSquareException {
return (transpose(cofactor(matrix)).multiplyByConstant(1.0/determinant(matrix)));
}

In this method the inverse of a matrix is calculated by finding the transpose of the cofactor of that matrix divided by the determinant of that matrix. Not all of square matrices have inverse. If the matrix is not invertible (a singular matrix), the value of the matrix coming out of the above method will be NAN (stands for not a number) or Infinity.

### Conclusion

In this article, we have learned about matrix and various operations that are performed on them. For performing these operations, we will be using JAVA.

I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

What did you think of this post?
Services

Related Posts

[Close]

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US\$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!