Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matrices are fundamental objects in computer science and mathematics alike, they

ID: 3851871 • 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 AT. 6. a method that verifies wheat her a given n time 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 anon-singular n times n matrix A and an n-dimensional column vector b solves the equation Ax = b using Cramer's rule. Example Define A = [1 4 2 5 3 6] B = [-1 9 4 8 7 2] C = [2 5 4 1 3 7] D = [1 3 2 4]

Explanation / Answer

import java.util.Scanner;
public class MatrixFull
{
public static int[][] transpose(int m[][])
{
int row=m.length;
int column=m[0].length;
int t[][] = new int[column][row];
for(int i = 0; i < column; i++)
{
for(int j = 0; j < row; j++)
{
t[i][j]=m[j][i];

}
  
}
  
return t;
}
public static int[][] add(int matrix1[][], int matrix2[][])
{
int sum[][]=new int [matrix1.length][matrix1[0].length];
      
       for(int i=0; i<matrix1.length; i++)
{
          
           for(int j=0; j<matrix1[0].length; j++) {
              
               sum[i][j] = matrix1[i][j] + matrix2[i][j];
           }
       }
  
return sum;
      
      
   }
public static int[][] subtract(int matrix1[][], int matrix2[][])
{
int diff[][]=new int [matrix1.length][matrix1[0].length];
      
       for(int i=0; i<matrix1.length; i++)
{
          
           for(int j=0; j<matrix1[0].length; j++) {
              
               diff[i][j] = matrix1[i][j] - matrix2[i][j];
           }
       }
  
return diff;
      
      
   }
public static int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}

public static int[][] scalarMul(int a[][], int p)
{
int f[][]=new int [a.length][a[0].length];
for(int i=0; i<a.length; i++)
{
          
           for(int j=0; j<a[0].length; j++) {
               f[i][j] = (int)p*a[i][j];
              
           }

       }  
  
return f;
  
}
public static int[][] power(int a[][], int p)
{
int f[][]=new int [a.length][a[0].length];
for(int i=0; i<a.length; i++)
{
          
           for(int j=0; j<a[0].length; j++) {
               f[i][j] = (int) Math.pow(a[i][j],p);
              
           }

       }  
  
return f;
  
}
public static double determinant(int A[][],int N)
{


int det=0;
int res;


if(N == 1)
res = A[0][0];

else if (N == 2)
{
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}

else
{
res=0;
int m[][];
for(int j1=0;j1<N;j1++)
{
m = new int[N-1][];
for(int k=0;k<(N-1);k++)
m[k] = new int[N-1];
for(int i=1;i<N;i++)
{
int j2=0;
for(int j=0;j<N;j++)
{
if(j == j1)
continue;
m[i-1][j2] = A[i][j];
j2++;
}
}
res += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);


}
}


return res;

}
public static void display(int m[][])
{
      
              
       for(int i=0; i<m.length; i++) {
          
           for(int j=0; j<m[0].length; j++) {
              
               System.out.print(" " + m[i][j]);
           }
           System.out.println();
       }  
      
      
   }
public static void main(String args[])
{
   int i, j;
   System.out.println("Enter total rows and columns: ");
   Scanner s = new Scanner(System.in);
   int row = s.nextInt();
   int column = s.nextInt();
   int array[][] = new int[row][column];
   System.out.println("Enter matrix:");
    for(i = 0; i < row; i++)
   {
    for(j = 0; j < column; j++)
    {
   array[i][j] = s.nextInt();
   System.out.print(" ");
}
   }
   System.out.println("The original matrix is ");
   display(array);
int t[][]=transpose(array);   
    System.out.println("The Transpose is ");
display(t);

int p[][]=power(array,3);   
    System.out.println("The Power of matrix is ");
display(p);

int scalar[][]=scalarMul(array,3);   
    System.out.println("The Sclar Mul of matrix is ");
display(scalar);

System.out.println("The determinant of matrix is "+determinant(array,2));
  
}
}