Question

11. matrix inversion puter program to find the inverse of a specified square matrix. Some nice features in such a program wou

C++ Language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to find adjoint and inverse of a matrix
#include<bits/stdc++.h>
using namespace std;
#define N 4

// Function to get cofactor of A[p][q] in temp[][]. n is current
// dimension of A[][]
void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)
{
   int i = 0, j = 0;

   // Looping for each element of the matrix
   for (int row = 0; row < n; row++)
   {
       for (int col = 0; col < n; col++)
       {
           // Copying into temporary matrix only those element
           // which are not in given row and column
           if (row != p && col != q)
           {
               temp[i][j++] = A[row][col];

               // Row is filled, so increase row index and
               // reset col index
               if (j == n - 1)
               {
                   j = 0;
                   i++;
               }
           }
       }
   }
}

/* Recursive function for finding determinant of matrix.
n is current dimension of A[][]. */
int determinant(int A[N][N], int n)
{
   int D = 0; // Initialize result

   // Base case : if matrix contains single element
   if (n == 1)
       return A[0][0];

   int temp[N][N]; // To store cofactors

   int sign = 1; // To store sign multiplier

   // Iterate for each element of first row
   for (int f = 0; f < n; f++)
   {
       // Getting Cofactor of A[0][f]
       getCofactor(A, temp, 0, f, n);
       D += sign * A[0][f] * determinant(temp, n - 1);

       // terms are to be added with alternate sign
       sign = -sign;
   }

   return D;
}

// Function to get adjoint of A[N][N] in adj[N][N].
void adjoint(int A[N][N],int adj[N][N])
{
   if (N == 1)
   {
       adj[0][0] = 1;
       return;
   }

   // temp is used to store cofactors of A[][]
   int sign = 1, temp[N][N];

   for (int i=0; i<N; i++)
   {
       for (int j=0; j<N; j++)
       {
           // Get cofactor of A[i][j]
           getCofactor(A, temp, i, j, N);

           // sign of adj[j][i] positive if sum of row
           // and column indexes is even.
           sign = ((i+j)%2==0)? 1: -1;

           // Interchanging rows and columns to get the
           // transpose of the cofactor matrix
           adj[j][i] = (sign)*(determinant(temp, N-1));
       }
   }
}

// Function to calculate and store inverse, returns false if
// matrix is singular
bool inverse(int A[N][N], float inverse[N][N])
{
   // Find determinant of A[][]
   int det = determinant(A, N);
   if (det == 0)
   {
       cout << "Singular matrix, can't find its inverse";
       return false;
   }

   // Find adjoint
   int adj[N][N];
   adjoint(A, adj);

   // Find Inverse using formula "inverse(A) = adj(A)/det(A)"
   for (int i=0; i<N; i++)
       for (int j=0; j<N; j++)
           inverse[i][j] = adj[i][j]/float(det);

   return true;
}

// Generic function to display the matrix. We use it to display
// both adjoin and inverse. adjoin is integer matrix and inverse
// is a float.
template<class T>
void display(T A[N][N])
{
   for (int i=0; i<N; i++)
   {
       for (int j=0; j<N; j++)
           cout << A[i][j] << " ";
       cout << endl;
   }
}

// Driver program
int main()
{
   int A[N][N] = { {5, -2, 2, 7},
                   {1, 0, 0, 3},
                   {-3, 1, 5, 0},
                   {3, -1, -9, 4}};

   int adj[N][N]; // To store adjoint of A[][]

   float inv[N][N]; // To store inverse of A[][]

   cout << "Input matrix is :\n";
   display(A);

   cout << "\nThe Adjoint is :\n";
   adjoint(A, adj);
   display(adj);

   cout << "\nThe Inverse is :\n";
   if (inverse(A, inv))
       display(inv);

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Language. 11. matrix inversion puter program to find the inverse of a specified square matrix....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Wondering if I could get some guidance on setting up newMatrix(). Language is C Matrix ADT...

    Wondering if I could get some guidance on setting up newMatrix(). Language is C Matrix ADT Specifications In addition to the main program Sparse.c and the altered List.c from pal, you will implement a Matrix ADT in a file called Matrix.c. This ADT will contain a private inner struct (similar to Node in your List ADT) encapsulating the column and value information corresponding to a matrix entry. You can give this inner struct any name you wish, but I will...

  • ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

    ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...

  • C++ Program To demonstrate ability to write sparse matrix operations. Write a C++ class to create...

    C++ Program To demonstrate ability to write sparse matrix operations. Write a C++ class to create 2D sparse matrices, sum them, and transpose them. You don't have to use operator overloading in this program. Each matrix should be shown in sparse format (which shown only non-zero items). This program will be a command line-based program. Do not interact with user with a menu. Instead, user will use "command language" (aka command entry) as detailed below. Your program will keep at...

  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • Write a program that searches for specified words hidden within a matrix of letters. C++ HELP...

    Write a program that searches for specified words hidden within a matrix of letters. C++ HELP -pls keep basic as possible, thanksssss 1. Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2. Following these two numbers, there will be a number of rows of letters. These letters should...

  • An Triangular matrix is a square matrix whose elements below the diagonal are defined to be...

    An Triangular matrix is a square matrix whose elements below the diagonal are defined to be 0. For example, the matrix element Mr,c = 0 if r > c. The following is an example matrix of size 4. 0 1 2 3 0 100 200 300 400 1 0 500 600 700 2 0 0 800 900 3 0 0 0 1000 While it is possible to use a regular 2D array to represent an Triangular matrix, doing so is...

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Find an optimal parenthesization for matrix-chain multiplications using any language PYTHON/Java/C++ ,C for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a strai...

    Find an optimal parenthesization for matrix-chain multiplications using any language PYTHON/Java/C++ ,C for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a straight forward-recursive solution. The output must be three lines: 1) the first line contains the optimal number of multiplication 2) the second line contains the optimal parenthesization and 3) the third line is the time required to compute the optimal parenthesization

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT