-
-
Save kateseniva/b876601802e81b0e601f23f417644557 to your computer and use it in GitHub Desktop.
Частина курсової
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <iomanip> | |
using namespace std; | |
int n=0; | |
double matrix_inverse(double **A, int n); | |
double matrix_multiplication(double** A, double** B, int n); | |
int main() | |
{ | |
cout << "Enter n:"; | |
cin >> n; | |
cout << "Enter elements of matrix A:"; | |
double **A, **A1, **Q, **X, **Y, **Z, **l11, **l12, **l21, **l22, **b11, **b12, **b21, **b22, **R; // matrix | |
A = new double *[n]; | |
A1 = new double *[n]; | |
Q = new double *[(int)n/2]; | |
X = new double *[(int)n/2]; | |
Y = new double *[(int)n/2]; | |
Z = new double *[(int)n/2]; | |
R = new double *[(int)n/2]; | |
l11 = new double *[(int)n/2]; | |
l12 = new double *[(int)n/2]; | |
l21 = new double *[(int)n/2]; | |
l22 = new double *[(int)n/2]; | |
b11 = new double *[(int)n/2]; | |
b12 = new double *[(int)n/2]; | |
b21 = new double *[(int)n/2]; | |
b22 = new double *[(int)n/2]; | |
for(int i=0; i<n; ++i) | |
{ | |
A[i]=new double[n]; | |
A1[i] = new double [n]; | |
Q[i] = new double [n]; | |
X[i] = new double [n]; | |
Y[i] = new double [n]; | |
Z[i] = new double [n]; | |
R[i] = new double [n]; | |
l11[i] = new double [n]; | |
l12[i] = new double [n]; | |
l21[i] = new double [n]; | |
l22[i] = new double [n]; | |
b11[i] = new double [n]; | |
b12[i] = new double [n]; | |
b21[i] = new double [n]; | |
b22[i] = new double [n]; | |
} | |
for(int i=0; i<n; i++) { | |
for(int j=0; j<n; j++) { | |
cin >> A[i][j]; | |
} | |
} | |
/*for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j< n; j++) | |
{ | |
cout << A[i][j] << " "; | |
} | |
cout << endl; | |
}*/ | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
if (i < n/2) | |
{ | |
if (j < n/2) | |
l11[i][j] = A[i][j]; | |
else | |
l12[i][j-n] = A[i][j]; | |
} | |
else | |
{ | |
if (j < n/2) | |
l21[i - n/2][j] = A[i][j]; | |
else | |
l22[i - n/2][j-n/2] = A[i][j]; | |
} | |
} | |
} | |
//1 2 3 4 2 1 2 3 3 2 -1 2 4 3 2 1 | |
cout << "Z2" << endl; | |
**Z = matrix_inverse(l11, (int)n/2); | |
for (int i = 0; i < n/2; i++) | |
{ | |
for (int j = 0; j < n/2; j++) | |
{ | |
cout << Z[i][j] << " "; | |
} | |
cout << endl; | |
} | |
**X = matrix_multiplication(Z, l12, (int)n/2); | |
**Y = matrix_multiplication(l12, Z, (int)n/2); | |
**Q = matrix_multiplication(Y,l12, (int)n/2); | |
for(int i = 0; i < (int)n/2; i++) | |
{ | |
for (int j = 0; j< (int)n/2; j++) | |
{ | |
Q[i][j]=l22[i][j]-Q[i][j]; | |
} | |
} | |
**R=matrix_inverse(Q, (int)n/2); | |
**b11=matrix_multiplication(R, Y, (int)n/2); | |
**b11=matrix_multiplication(X, b11, (int)n/2); | |
**b12=matrix_multiplication(X, R, (int)n/2); | |
**b21=matrix_multiplication(R, Y, (int)n/2); | |
b22=R; | |
for (int i = 0; i < (int)n/2; i++) | |
{ | |
for (int j = 0; j < (int)n/2; j++) | |
{ | |
b11[i][j]=Z[i][j]+b11[i][j]; | |
b12[i][j]=(-1)*b12[i][j]; | |
b21[i][j]=(-1)*b21[i][j]; | |
} | |
} | |
cout << "inverse matrix:" << endl; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
if (i < n/2) | |
{ | |
if (j < n/2) A1[i][j] = b11[i][j]; | |
else A1[i][j]=b12[i][j-(int)n/2]; | |
} | |
else | |
{ | |
if (j < n/2) A1[i][j]=b21[i-(int)n/2][j]; | |
else A1[i][j]=b22[i-(int)n/2][j-(int)n/2]; | |
} | |
cout << setw(13) << A1[i][j]; | |
} | |
cout << endl; | |
} | |
return 0; | |
} | |
double matrix_inverse(double **matr, int n) | |
{ | |
double **inv; | |
inv = new double *[n]; | |
for(int i = 0; i < n; i++) | |
{ | |
inv[i] = new double[n]; | |
} | |
double d = matr[0][0]*matr[1][1]-matr[0][1]*matr[1][0]; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
inv[i][j] = pow(-1, (i+j))*matr[n-i-1][n-j-1]/d; | |
} | |
} | |
return **inv; | |
} | |
double matrix_multiplication(double **matr, double **B, int n) | |
{ | |
double **C; | |
C = new double *[n]; | |
for(int i = 0; i < n; i++) | |
{ | |
C[i]=new double[n]; | |
} | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
double s=0; | |
for (int k = 0; k < n; k++) | |
{ | |
s=s+matr[i][k]*B[k][j]; | |
} | |
C[i][j]=s; | |
} | |
} | |
return **C; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment