Flame of Soul
Главная | Регистрация | Вход

Мои записи


Главная » Статьи » Алгоритмы » Численные методы

[CPP] Поиск обратной матрицы методом Гаусса
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x09, Phile #0x0a of 0x0f
|=-----------------------------------------------------------------=|
|=-----------=[ Поиск обратной матрицы методом Гаусса ]=
-----------=|
|=-----------------------------------------------------------------=|
|=----------------=[ Copyright (c) Flame Of Soul ]=----------------=|
|=---------------=[
[E-mail]: _allbasse@yandex.ru ]=---------------=|
|=-----------------------------------------------------------------=|
|=-----------------------------=[ CPP ]=---------------------------=|
|=-----------------------------------------------------------------=|
#include "stdio.h"
#include "iostream.h"
bool prepareBMatrix( double **coefficients, double **bMatrix,
int currRowAndColumn, int numberOfEquation ) {
bool result;
bool allElementsInCurrentColumnEqualsZero = true;
int i, k, row;
double tempItem;
if ( currRowAndColumn == numberOfEquation - 1 ) {
result = ( coefficients[currRowAndColumn][currRowAndColumn] != 0 );}
else {
for ( i = currRowAndColumn; i < numberOfEquation; i++ ) {
if ( coefficients[i][currRowAndColumn] != 0 ) {
allElementsInCurrentColumnEqualsZero = false;
row = i; break;}}
if ( allElementsInCurrentColumnEqualsZero ) {
result = false;} else {
if ( row != currRowAndColumn ) {
for ( i = currRowAndColumn; i < numberOfEquation; i++ ) {
tempItem = coefficients[currRowAndColumn][i];
coefficients[currRowAndColumn][i] = coefficients[row][i];
coefficients[row][i] = tempItem;}
for ( i = 0; i < numberOfEquation; i++ ) {
tempItem = bMatrix[currRowAndColumn][i];
bMatrix[currRowAndColumn][i] = bMatrix[row][i];
bMatrix[row][i] = tempItem;}}
for ( i = currRowAndColumn + 1; i < numberOfEquation; i++ ) {
tempItem = -coefficients[i][currRowAndColumn] /
coefficients[currRowAndColumn][currRowAndColumn];
for ( k = currRowAndColumn; k < numberOfEquation; k++ ) {
coefficients[i][k] = coefficients[i][k] +
coefficients[currRowAndColumn][k]*tempItem;}
for ( k = 0; k < numberOfEquation; k++ ) {
bMatrix[i][k] = bMatrix[i][k] + bMatrix[currRowAndColumn][k]*tempItem;}}
result = prepareBMatrix( coefficients, bMatrix,
currRowAndColumn + 1, numberOfEquation );}}
return result;}

bool inverseMatrix( double **coefficients,
int numberOfEquation, double **solution ) {
bool result;
int i, k, j;
double **bMatrix = new double*[numberOfEquation];
for ( i = 0; i < numberOfEquation; i ++ ){
bMatrix[i] = new double[numberOfEquation];
for ( k = 0; k < numberOfEquation; k ++ ){
bMatrix[i][k] = ( i == k ? 1 : 0 );}}
result = prepareBMatrix( coefficients, bMatrix, 0, numberOfEquation );
if ( result ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( i = numberOfEquation - 1; i >= 0; i -- ) {
solution[i][j] = bMatrix[i][j];
for ( k = i + 1; k < numberOfEquation; k ++ ) {
solution[i][j] = solution[i][j] - solution[k][j]*coefficients[i][k];}
solution[i][j] = solution[i][j] / coefficients[i][i];}}}
return result;}


void main() {
int i, j;
int size;
double **coefficients, **solution;
cout << "Gauss'es method of inversion matrix.\nEnter system dimension: ";
cin >> size;
coefficients = new double*[size];
solution = new double*[size];
for ( i = 0; i < size; i++ ) {
coefficients[i] = new double[size];
solution[i] = new double[size];}
for ( i = 0; i < size; i ++ ){
cout << "Enter " << i + 1 << " row: ";
for ( j = 0; j < size; j ++ ){
cin >> coefficients[i][j];}}
if ( !inverseMatrix( coefficients, size, solution ) ) {
cout << "Solution for this matrix of coefficients not exist";}
else {
cout << "Inverse matrix is:\n";
for ( j = 0; j < size; j ++ ){
for ( i = 0; i < size; i ++ ){
cout << solution[j][i] << " ";}
cout << "\n";}}
cout << "\nPress \"Enter\" to continue..." << endl;
getchar();}
|=-----------------------------=[ CPP ]=---------------------------=|
|=-----------------------------------------------------------------=|
Категория: Численные методы | Добавил: flame (23.05.2009)
Просмотров: 410 | Рейтинг: 0.0/0 |
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]

Форма входа

Поиск

Статистика


Онлайн всего: 1
Гостей: 1
Пользователей: 0