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

Мои записи


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

[CPP] Метод вращений Якоби
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x04, Phile #0x0a of 0x0f
|=-----------------------------------------------------------------=|
|=-------------------=[ Метод вращений Якоби ]=--------------------=|
|=-----------------------------------------------------------------=|
|=----------------=[ Copyright (c) Flame Of Soul ]=----------------=|
|=---------------=[
[E-mail]: _allbasse@yandex.ru ]=---------------=|
|=-----------------------------------------------------------------=|
|=-----------------------------=[ CPP ]=---------------------------=|
|=-----------------------------------------------------------------=|
#include "stdio.h"
#include "iostream.h"
#include "math.h"
const double PI = 3.1415926536;
bool isSimmetrial( double **coefficients, int numberOfEquation ) {
bool result = true;
int i, j;
for ( i = 0; i < numberOfEquation; i++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
if ( coefficients[i][j] != coefficients[j][i] ) {
result = false;
break;}}
if ( !result ) {break;}}
return result;}
int wrachenie( double **coefficients, int numberOfEquation,
double **solution, double precision ) {
int result = 1;
int i, j, k;
int maxI, maxJ;
double max, fi;
double** matricaPoworota;
matricaPoworota = new double*[numberOfEquation];
for ( i = 0; i < numberOfEquation; i ++ ) {
matricaPoworota[i] = new double[numberOfEquation];}
double** temp;
temp = new double*[numberOfEquation];
for ( i = 0; i < numberOfEquation; i ++ ) {
temp[i] = new double[numberOfEquation];}
double fault = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
fault = fault + coefficients[i][j]*coefficients[i][j];}}
fault = sqrt( 2*fault );
while ( fault > precision ) {
max = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
if ( coefficients[i][j] > 0 && coefficients[i][j] > max ) {
max = coefficients[i][j];
maxI = i;
maxJ = j;}
else if ( coefficients[i][j] < 0 && - coefficients[i][j] > max ) {
max = - coefficients[i][j];
maxI = i;
maxJ = j;}}}
for ( i = 0; i < numberOfEquation; i ++ ){
for ( j = 0; j < numberOfEquation; j ++ ){
matricaPoworota[i][j] = 0;}
matricaPoworota[i][i] = 1;}
if ( coefficients[maxI][maxI] == coefficients[maxJ][maxJ] ) {
matricaPoworota[maxI][maxI] = matricaPoworota[maxJ][maxJ] =
matricaPoworota[maxJ][maxI] = sqrt( 2.0 ) / 2.0;
matricaPoworota[maxI][maxJ] = - sqrt( 2.0 ) / 2.0;}
else {
fi = 0.5 * atan( ( 2.0 * coefficients[maxI][maxJ] ) /
( coefficients[maxI][maxI] - coefficients[maxJ][maxJ] ) );
matricaPoworota[maxI][maxI] = matricaPoworota[maxJ][maxJ] = cos( fi );
matricaPoworota[maxI][maxJ] = - sin( fi );
matricaPoworota[maxJ][maxI] = sin( fi );}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
temp[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
temp[i][j] = temp[i][j] + matricaPoworota[k][i] * coefficients[k][j];}}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
coefficients[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
coefficients[i][j] = coefficients[i][j] +
temp[i][k] * matricaPoworota[k][j];}}}
fault = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
fault = fault + coefficients[i][j]*coefficients[i][j];}}
fault = sqrt( 2*fault );
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
temp[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
temp[i][j] = temp[i][j] + solution[i][k] * matricaPoworota[k][j];}}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
solution[i][j] = temp[i][j];}}
result++;}
return result;}

void main() {
int i, j;
int size;
double **coefficients, **solution, precision;
cout << "Metod wracheniya.\nWweddite razmernost' matrici: ";
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 ++ ){
for ( j = 0; j < size; j ++ ){
solution[i][j] = 0;}
solution[i][i] = 1;}
for ( i = 0; i < size; i ++ ){
cout << "Enter " << i + 1 << " row: ";
for ( j = 0; j < size; j ++ ){
cin >> coefficients[i][j];}}
cout << "Wwedite tochnost' rascheta: ";
cin >> precision;
if ( !isSimmetrial( coefficients, size ) ) {
cout << "Matrica ne simmetrichna";}
else {
int steps = wrachenie( coefficients, size, solution, precision );
cout << "Reshenie:\n";
for ( i = 0; i < size; i++ ) {
cout << "Sobstwennii wektor nomer " << i + 1 << ":\n";
for ( j = 0; j < size; j ++ ){
cout << solution[j][i] << "\n";}}
cout << "Sobstwennie znacheniya:\n";
for ( i = 0; i < size; i++ ) {
cout << coefficients[i][i] << "\n";}
cout << "Obchee chislo shagow: " << steps;}
cout << "\nPress \"Enter\" to continue..." << endl;
getchar();}
|=-----------------------------=[ CPP ]=---------------------------=|
|=-----------------------------------------------------------------=|

Категория: Численные методы | Добавил: flame (23.05.2009)
Просмотров: 2536 | Комментарии: 1 | Рейтинг: 3.7/3 |
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]

Форма входа

Поиск

Статистика


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