Created
January 25, 2016 17:52
-
-
Save rajabiy/88f0ab29616db4bf9c3e to your computer and use it in GitHub Desktop.
Cramer's rule is an explicit formula for the solution of a system of linear equations with as many equations as unknowns
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 <math.h> | |
#include <iostream> | |
using namespace std; | |
double **minor(double **arr, int ident, int n); | |
double determinant(double **arr, int ident); | |
double *kramer(double **arr, double *col_vector, int ident); | |
double **vector_matrix(double **arr, double *col_vector, int ident, int n); | |
int main() | |
{ | |
int n; | |
cin>>n; | |
double ** matrix = new double *[n]; | |
double *vector_det = new double [n]; | |
double *solve = new double [n]; | |
for(int i=0; i<n; i++) | |
{ | |
matrix[i] = new double[n]; | |
for(int j=0; j<n; j++) | |
cin>>matrix[i][j]; | |
} | |
for(int i=0; i<n; i++) | |
cin>>vector_det[i]; | |
solve=kramer(matrix, vector_det, n); | |
for(int i=0; i<n; i++) | |
cout<<solve[i]<<endl; | |
return 0; | |
} | |
double *kramer(double **arr, double *col_vector, int ident) | |
{ | |
/* | |
решение линейного уравнения методом крамера | |
*/ | |
double main_det=0; | |
double *vector_det = new double [ident]; | |
main_det = determinant(arr, ident); | |
if (main_det!=0) | |
for(int i=0; i<ident; i++) | |
vector_det[i]=determinant(vector_matrix(arr, col_vector, ident, i), ident)/main_det; | |
else | |
for(int i=0; i<ident; i++) | |
vector_det[i]=0; | |
return vector_det; | |
} | |
double **vector_matrix(double **arr, double *col_vector, int ident, int n) | |
{ | |
/* | |
создание дполнительных матриц содержаших вектор столбец | |
*/ | |
double ** matrix = new double *[ident]; | |
for(int i=0; i<ident; i++) | |
{ | |
matrix[i] = new double[ident]; | |
for(int j=0; j<ident; j++) | |
{ | |
if (j==n) | |
matrix[i][j] = col_vector[i]; | |
else | |
matrix[i][j]=arr[i][j]; | |
} | |
} | |
return matrix; | |
} | |
double **minor(double **arr, int ident, int minor) | |
{ | |
/* | |
минорная матрицапо 0 сроке minor ному столбцу | |
*/ | |
double ** matrix = new double *[ident-1]; | |
int num_j; | |
for(int i=0; i<ident-1; i++) | |
{ | |
matrix[i] = new double[ident-1]; | |
for(int j=0; j<ident; j++) | |
{ | |
if(j>=minor) | |
num_j=j+1; | |
else | |
num_j=j; | |
matrix[i][j]=arr[i+1][num_j]; | |
} | |
} | |
return matrix; | |
} | |
double determinant(double **arr, int ident) | |
{ | |
/* | |
Вичисление детерминанта матрицы | |
arr матрица размерносьти ident | |
*/ | |
double result=0, det=0; | |
double ** matrix = new double *[ident-1]; | |
if (ident==1) | |
{ | |
result=arr[0][0]; | |
} | |
else | |
{ | |
/* | |
расчет 0, minor_i детеррминанта матрицы методом сложения миноров матрицы | |
*/ | |
for(int minor_i=0; minor_i<ident; minor_i++) | |
result = result + pow(-1, 2+minor_i) * arr[0][minor_i] * determinant(minor(arr, ident, minor_i), ident-1); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment