Homework Solution: Write a Java program to find the inverse of a square matrix. Let the user ente…

    Write a Java program to find the inverse of a square matrix. Let the user enter the size of the matrix up to size 21, and enter the matrix values from the keyboard. Check to make sure if the matrix is singular. Output the original matrix and its inverse. Finally, ask the user if he/she wishes to run the program again. Error check input and format the output. Use a method calculate the inverse. Refer to the sample output below. Sample Run: Enter the size of the Matrix (<=21) : 3 Enter the matrix values: 1 4 2 2 5 8 2 1 8 Original matrix: 1 4 2 2 5 8 2 1 8 Inverse matrix: 2       -1.875       1.375

    Expert Answer

     
    import java.util.Scanner; public class Inverse

    Write a Java program to discover the inverse of a clear matrix. Let the user penetrate the extent of the matrix up to extent 21, and penetrate the matrix values from the keyboard. Inhibit to mould secure if the matrix is unexampled. Quenchedput the peculiar matrix and its inverse. Finally, ask the user if he/she wishes to pass the program frequently. Error inhibit input and format the quenchedput. Use a system proportion the inverse. Refer to the pattern quenchedput underneath.

    Pattern Pass:

    Penetrate the extent of the Matrix (<=21) : 3

    Penetrate the matrix values: 1 4 2 2 5 8 2 1 8

    Peculiar matrix:

    1 4 2

    2 5 8

    2 1 8

    Inverse matrix:

    2       -1.875       1.375

    Expert Retort

     

    import java.util.Scanner;

    public tabulate Inverse

    {

    public static empty main(String argv[])

    {

    Scanner input = fantastic Scanner(System.in);

    System.out.println(“Penetrate the extent of the Matrix (<=21) : “);

    int n = input.nextInt();

    envelop a[][]= fantastic envelop[n][n];

    System.out.println(“Penetrate the matrix values: “);

    for(int i=0; i<n; i++)

    for(int j=0; j<n; j++)

    a[i][j] = input.nextDouble();

     

    System.out.println(“Original: “);

    for(int i=0; i<n; i++)

    {

    for(int j=0;j<n; j++)

    {

    System.out.print(a[i][j]+” “);

    }

    System.out.println(“n”);

    }

     

     

    envelop d[][] = inverse(a);

    System.out.println(“The inverse is: “);

    for (int i=0; i<n; ++i)

    {

    if(i>0)

    {

    break;

    }

    else

    {

    for (int j=0; j<n; ++j)

    {

    System.out.print(d[i][j]+” “);

    }

    System.out.println();

    }

    }

    input.close();

    }

    public static envelop[][] inverse(envelop a[][])

    {

    int n = a.length;

    envelop x[][] = fantastic envelop[n][n];

    envelop b[][] = fantastic envelop[n][n];

    int protest[] = fantastic int[n];

    for (int i=0; i<n; ++i)

    b[i][i] = 1;

    // Transform the matrix into an surpassing triangle

    gaussian(a, protest);

    // Update the matrix b[i][j] with the ratios stored

    for (int i=0; i<n-1; ++i)

    for (int j=i+1; j<n; ++j)

    for (int k=0; k<n; ++k)

    b[index[j]][k]

    -= a[index[j]][i]*b[index[i]][k];

    // Perform unskilled substitutions

    for (int i=0; i<n; ++i)

    {

    x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];

    for (int j=n-2; j>=0; –j)

    {

    x[j][i] = b[index[j]][i];

    for (int k=j+1; k<n; ++k)

    {

    x[j][i] -= a[index[j]][k]*x[k][i];

    }

    x[j][i] /= a[index[j]][j];

    }

    }

    return x;

    }

    // System to propel quenched the partial-pivoting Gaussian

    // elimination. Here protest[] stores pivoting classify.

    public static empty gaussian(envelop a[][], int protest[])

    {

    int n = protest.length;

    envelop c[] = fantastic envelop[n];

    // Initialize the protest

    for (int i=0; i<n; ++i)

    index[i] = i;

    // Discover the rescaling factors, undivided from each row

    for (int i=0; i<n; ++i)

    {

    envelop c1 = 0;

    for (int j=0; j<n; ++j)

    {

    envelop c0 = Math.abs(a[i][j]);

    if (c0 > c1) c1 = c0;

    }

    c[i] = c1;

    }

    // Search the pivoting atom from each column

    int k = 0;

    for (int j=0; j<n-1; ++j)

    {

    envelop pi1 = 0;

    for (int i=j; i<n; ++i)

    {

    envelop pi0 = Math.abs(a[index[i]][j]);

    pi0 /= c[index[i]];

    if (pi0 > pi1)

    {

    pi1 = pi0;

    k = i;

    }

    }

    // Interchange rows according to the pivoting classify

    int itmp = protest[j];

    index[j] = protest[k];

    index[k] = itmp;

    for (int i=j+1; i<n; ++i)

    {

    envelop pj = a[index[i]][j]/a[index[j]][j];

    // Record pivoting ratios underneath the diagonal

    a[index[i]][j] = pj;

    // Modify other atoms accordingly

    for (int l=j+1; l<n; ++l)

    a[index[i]][l] -= pj*a[index[j]][l];

    }

    }

    }

    }

    Output: