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

Мои записи


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

[CPP] Поиск детерминанта матрицы методом Гаусса
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x08, Phile #0x0a of 0x0f
|=-----------------------------------------------------------------=|
|=---------=[ Поиск детерминанта матрицы методом Гаусса ]=---------=|
|=-----------------------------------------------------------------=|
|=--------------=[ [Author]: _ aka Flame of Soul_  ]=--------------=|
|=--------------=[ [E-mail]: _ allbasse@yandex.ru  ]=--------------=|
|=-----------------------------------------------------------------=|
|=----------------------------=[ СPP ]=----------------------------=|
|=-----------------------------------------------------------------=|
#include "stdio.h"
#include "iostream.h"
double getDeterminant( double **coefficients, int
currRowAndColumn, int numberOfEquation ) {
double result;
bool allElementsInCurrentColumnEqualsZero = true;
int i, k, row;
double tempItem;
if ( currRowAndColumn == numberOfEquation - 1 ) {
result = coefficients[currRowAndColumn][currRowAndColumn];
}else {
for ( i = currRowAndColumn; i < numberOfEquation; i++ ) {
if ( coefficients[i][currRowAndColumn] != 0 ) {
allElementsInCurrentColumnEqualsZero = false;
row = i; break;}}
if ( allElementsInCurrentColumnEqualsZero ) {
result = 0.0;}
else {
double normalizingCoef = 1.0;
if ( row != currRowAndColumn ) {
normalizingCoef = -1.0;
for ( i = currRowAndColumn; i < numberOfEquation; i++ ) {
tempItem = coefficients[currRowAndColumn][i];
coefficients[currRowAndColumn][i] = coefficients[row][i];
coefficients[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;}}
result = coefficients[currRowAndColumn][currRowAndColumn]*
normalizingCoef*getDeterminant( coefficients,
currRowAndColumn + 1, numberOfEquation );}}
return result;}

void main() {
int i, j;
int size;
double **coefficients;
cout << "Gauss'es method.\nEnter system dimension: ";
cin >> size;
coefficients = new double*[size];
for ( i = 0; i < size; i++ ) {
coefficients[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];}}
cout << "Determinant is: " << getDeterminant( coefficients, 0, size );
cout << "\nPress \"Enter\" to continue..." << endl;
getchar();}
|=----------------------------=[ СPP ]=----------------------------=|
|=-----------------------------------------------------------------=|
Категория: Численные методы | Добавил: flame (23.05.2009)
Просмотров: 591 | Комментарии: 1 | Рейтинг: 0.0/0 |
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]

Форма входа

Поиск

Статистика


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