Matrices are fundamental objects in computer science and mathematics alike, they
ID: 3851273 • Letter: M
Question
Matrices are fundamental objects in computer science and mathematics alike, they are used extensively in computer graphics, error correcting codes, Google's pageRank algorithms, differential equations and many more. In this assignment you are asked to create the following set of methods dealing with matrix manipulation: 1. a method that given two m times n matrices A and B returns their sum A + B. 2. a method that given two m times n matrices A and B returns their difference A - B. 3. a method that given an m times n matrix A and a scalar r returns their product rA. 4. a method that given an m times n matrix A and an n times p matrix B returns their product AB. 5. a method that given an m times n matrix A returns its transpose A^T. 6. a method that verifies whether a given n times n matrix A is symmetric. 7. a method that given an n times n matrix A and a positive integer k computes A^k. 8. (HONORS) a method that given a nilpotent matrix N computes e^N. 9. (HONORS) a method that given an n times n A computes its determinant. 10. (HONORS) a method that given a non-singular n times n matrix A and an n-dimensional column vector b solves the equation Ax = b using Cramer's rule.Explanation / Answer
Solution:
Note: Here I answered first 5 methods; please post the rest problems separately
1.
//1.Method to perform the addition
public static int[][] add(int[][] A, int[][] B) {
//find the dimension of the matrix
int m = A.length;
int n = A[0].length;
//create the resukltant matrix
int[][] sum = new int[m][n];
//The for loop perform the addition
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//set the resulat matrix
sum[i][j] = A[i][j] + B[i][j];
}
}
//return the sum
return sum;
}
2.
//2.method to perform the subtration
public static int[][] subtract(int[][] A, int[][] B) {
//find the dimension of the matrix
int m = A.length;
int n = A[0].length;
//create the difference matrix
int[][] difference = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//compute the difference
difference[i][j] = A[i][j] - B[i][j];
}
}
//resturn the difference
return difference;
}
3.
//3.method to compute the scalare multiplication
public static int[][] scalarMulti(int [][]A, int r) {
//find the dimension of the matrix
int m = A.length;
int n = A[0].length;
//crate the resultant matrix
int[][] matrixResult = new int[m][n];
//compute the elemts of the resulat matrix
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrixResult[i][j] = r*A[i][j];
}
}
//return the result
return matrixResult;
}
4.
//4.method to find the produce AB
public static int[][] matrixMulti(int[][] A, int[][] B) {
//compute m.n and p
int m = A.length;
int n = A[0].length;
int p = B[0].length;
//the for loop to perform the multiplication
int[][] AB = new int[m][p];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; p < n; k++)
{
//find the produce term
AB[i][j] = AB[i][j] + A[i][k] * B[k][j];
}
}
}
//return the produce
return AB;
}
5.
//5.method to find the traspose of A
public static int[][] transpose(int[][] A) {
//find the dimension of the matrix
int m = A.length;
int n = A[0].length;
//initialize the transpose matrix
int[][] trans = new int[n][m];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
//assign the tranpose values
trans[j][i]=A[i][j];
}
}
//return the result
return trans;
}