Сопряженная и обратная матрица
Дана квадратная матрица, найдите сопряженную и обратную матрицу.
Мы настоятельно рекомендуем вам сделать это в качестве предварительного условия ниже.
Определитель матрицы
Что такое сопряженный?
Сопряженная (или сопряженная) матрица - это матрица, полученная путем транспонирования матрицы кофакторов данной квадратной матрицы, которая называется ее сопряженной или сопряженной матрицей. Сопряжение любой квадратной матрицы 'A' (скажем) представляется как Adj (A).
Пример:
Ниже взяты пример и объяснение. 5 -2 2 7 1 0 0 3 -3 1 5 0 3 -1-9 4 Например, сомножитель в верхнем левом углу "5" равен + | 0 0 3 | ... | 1 5 0 | = 3 (1 * -9 - (-1) * 5) = -12. ... | -1 -9 4 | (Младшая матрица формируется удалением строки и столбец данной записи.) В качестве другого примера, сомножитель угла "-2" верхнего ряда равен - | 1 0 3 | ... | -3 5 0 | = - [1 (20-0) - 0 + 3 (27-15)] = -56. ... | 3 -9 4 | Поступая таким образом, получаем матрицу [-12 -56 4 4] [76 208 4 4] [-60 -82-2 20] [-36 -58 -10 12] Наконец, чтобы получить сопряженный, просто возьмите предыдущий транспонирование матрицы: [-12 76-60 -36] [-56 208 -82 -58] [4 4 -2 -10] [4 4 20 12]
Важные свойства:
- Произведение квадратной матрицы A на сопряженную дает диагональную матрицу, в которой каждый диагональный элемент равен определителю A.
т.е.A.adj (A) = det (A) .II => Матрица идентичности того же порядка, что и у A. det (A) => Определяющее значение A
- Ненулевая квадратная матрица A порядка n называется обратимой, если существует единственная квадратная матрица B порядка n такая, что
AB = BA = I Матрица «B» называется обратной по отношению к «A». т.е. B = A -1
Как найти Adjoint?
Мы следуем определению, данному выше.
Пусть A [N] [N] будет входной матрицей. 1) Создайте матрицу adj [N] [N], сохраните сопряженную матрицу. 2) Для каждой записи A [i] [j] во входной матрице, где 0 <= i <N и 0 <= j <N. а) Найдите сомножитель A [i] [j] б) Найдите знак въезда. Знак +, если (i + j) даже иначе знак нечетный. c) Поместите сомножитель в adj [j] [i].
Как найти инверс?
Матрица, обратная матрице, существует только в том случае, если матрица неособая, т. Е. Определитель не должен быть равен 0.
Используя определитель и сопряженный, мы можем легко найти обратную квадратную матрицу, используя формулу ниже:
Если det (A)! = 0 А -1 = прил (А) / дет (А) Еще «Обратного не существует»
Обратное используется для нахождения решения системы линейных уравнений.
Below are implementation for finding adjoint and inverse of a matrix.
C++
// 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 :
" ; display(A); cout << "
The Adjoint is :
" ; adjoint(A, adj); display(adj); cout << "
The Inverse is :
" ; if (inverse(A, inv)) display(inv); return 0; } |
Java
// Java program to find adjoint and inverse of a matrix class GFG { static final int N = 4 ; // Function to get cofactor of A[p][q] in temp[][]. n is current // dimension of A[][] static void getCofactor( int A[][], int temp[][], 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[][]. */ static int determinant( int A[][], int n) { int D = 0 ; // Initialize result // Base case : if matrix contains single element if (n == 1 ) return A[ 0 ][ 0 ]; int [][]temp = new int [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]. static void adjoint( int A[][], int [][]adj) { if (N == 1 ) { adj[ 0 ][ 0 ] = 1 ; return ; } // temp is used to store cofactors of A[][] int sign = 1 ; int [][]temp = new int [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 static boolean inverse( int A[][], float [][]inverse) { // Find determinant of A[][] int det = determinant(A, N); if (det == 0 ) { System.out.print( "Singular matrix, can"t find its inverse" ); return false ; } // Find adjoint int [][]adj = new int [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. static void display( int A[][]) { for ( int i = 0 ; i < N; i++) { for ( int j = 0 ; j < N; j++) System.out.print(A[i][j]+ " " ); System.out.println(); } } static void display( float A[][]) { for ( int i = 0 ; i < N; i++) { for ( int j = 0 ; j < N; j++) System.out.printf( "%.6f " ,A[i][j]); System.out.println(); } } // Driver program public static void main(String[] args) { int A[][] = { { 5 , - 2 , 2 , 7 }, { 1 , 0 , 0 , 3 }, {- 3 , 1 , 5 , 0 }, { 3 , - 1 , - 9 , 4 }}; int [][]adj = new int [N][N]; // To store adjoint of A[][] float [][]inv = new float [N][N]; // To store inverse of A[][] System.out.print( "Input matrix is :
" ); display(A); System.out.print( "
The Adjoint is :
" ); adjoint(A, adj); display(adj); System.out.print( "
The Inverse is :
" ); if (inverse(A, inv)) display(inv); } } // This code is contributed by Rajput-Ji |
C#
// C# program to find adjoint and inverse of a matrix using System; using System.Collections.Generic; class GFG { static readonly int N = 4; // Function to get cofactor of A[p,q] in [,]temp. n is current // dimension of [,]A static void getCofactor( int [,]A, int [,]temp, 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++) { &nb
РЕКОМЕНДУЕМЫЕ СТАТЬИ |