Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > C/C++

Reply
 
Thread Tools

Гаусс
Old 25.01.2005, 18:04   #1
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 42
Posts: 21,717
Rep Power: 9
Default Гаусс

Господа - решение системы линейных уравнений методом гаусса.
Предлагаю лишь основную функцию:
PHP Code:
void GaussEliminator::solve()

{

m_vecResult.clear();

std::vector<doublev(m_dataMatrix.size() + 1);

unsigned char curr_index 0add_index 0;

//now triagonalize matrix

for (data_matrix::iterator i m_dataMatrix.begin(), m_dataMatrix.end(), k!= e; ++i, ++curr_index) {

//normalize next row

std::transform(i->begin(), i->end(), i->begin(), std::bind2nd(std::divides<double>(), *(i->begin() + curr_index)));

//remove that item from the next ones

for (1!= e; ++k) {

//base row

v.assign(i->begin(), i->end());

//multiply by the next modified row and then substract

std::transform(v.begin(), v.end(), v.begin(), std::bind2nd(std::multiplies<double>(), *(k->begin() + curr_index)));

std::transform(k->begin(), k->end(), v.begin(), k->begin(), std::minus<double>()); 

}

}

//now perform reverse process

curr_index m_dataMatrix.size();

for (
data_matrix::reverse_iterator i m_dataMatrix.rbegin(), m_dataMatrix.rend(); != e; ++i, ++add_index, --curr_index)

m_vecResult.push_front((i->back() - std::inner_product<std::vector<double>::iteratorstd::deque<double>::iteratordouble>(i->begin() + curr_indexi->end() - 1m_vecResult.begin(), 0)));

}

где m_dataMatrix это vector vector ов
кто может скатать короче

Old 25.01.2005, 20:44   #2
nm
ЙЦУКЕН
 
Join Date: 07 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Age: 54
Posts: 3,118
Rep Power: 0
Default

я могу сказать, что метод гаусса надо выкинуть ошибки просто недетцкие получаются ... из-за накопления погрешности

Old 25.01.2005, 21:41   #3
Профессор
 
Nikita's Avatar
 
Join Date: 01 2005
Location: Perm
Age: 45
Posts: 2,142
Rep Power: 0
Default

это короче ?
это не я писал ...
Quote:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>

// global variable declarations

int nun,neq;
double **sys,*back;
int moreeq=0;

// function prototypes

void solution();
double evaluate(int);
void result();
int consitent();

//function definitions

double evaluate(int g)
{
int u;
double rt=0;
rt=sys[g][nun];
for(u=0;u<nun;u++)
{
if(g==u) continue;
rt-=sys[g][u]*back[u];
}
rt/=sys[g][g];
return rt;
}

int consistent(){
int i,j,n=0;
for(i=0;i<neq;i++) {
for(j=0;j<nun;j++){
if(sys[i][j]!=0){
n++;
break;
}
}
}
if(n==neq)
return 1;
return 0;
}

void result()
{
int i,j;
if(!consistent()){
getch();
clrscr();
cout<<"\n\n Given system is inconsistent system...\n Solution can't be found\n";
getch();
return;
}else{
for(i=nun-1;i>=0;i--){
back[i]=evaluate(i);
}
return;
}
}

void solution(){
int i,j,k,r;
float c;
for(r=0;r<neq;r++){
c=sys[r][r];

for(k=0;k<=nun;k++){
sys[r][k]/=c;
}

for(i=r+1;i<neq;i++){
c=sys[i][r];
for(j=0;j<=nun;j++){
sys[i][j]-=c*sys[r][j];
}
}

for(i=0;i<neq;i++){
cout<<"\n\n";
for(j=0;j<=nun;j++){
printf("%15.4f",sys[i][j]);
}
}
cout<<"\n\n";
getch();
}
result();
}

//main program

void main()
{
int i,j;
char ch='y';
do{
clrscr();
cout<<"\n\n\t GAUSS-ELIMINATION METHOD FOR SOLVING A SYSTEM OF EQUATIONS\n\n";
cout<<"\n\n Developed by:G Vijayan III BE EEE PSG COLLEGE OF TECHNOLOGY",
cout<<"\n\n COIMBATORE";
cout<<"\n Number of Equations:";
cin>>neq;
cout<<"\n Number of unkowns:";
cin>>nun;
if(nun>neq){
cout<<"\n\n Data inadequate to find the solution.....";
getch();
exit(0);
}
if(neq>nun) moreeq=1;
sys=(double **)malloc((neq)*sizeof(double *));
back=(double *)malloc((nun)*sizeof(double ));
for(i=0;i<neq;i++){
*(sys+i)=(double *)malloc((nun+1)*sizeof(double));
}

clrscr();
printf("\n\n\t ENTER THE COEFFICIENTS OF EQUATIONS\n");
for(i=0;i<neq;i++){
cout<<"\n Equation ";
cout<<i+1;
for(j=0;j<nun;j++){
cout<<"\nx";
cout<<j+1;
cout<<" : ";
cin>>*(*(sys+i)+j);
back[i]=0;
}
cout<<" b";
cout<<i+1;
cout<<" : ";
cin>>*(*(sys+i)+j);
}
clrscr();
cout<<"\n\n\t GIVEN SYSTEM OF EQUATION\n\n";
for(i=0;i<neq;i++) {
cout<<"\n";
cout<<"\t";
for(j=0;j<nun;j++){
if(*(*(sys+i)+j)<0){
cout<<"\b";
cout<<"\b";
}
cout<<*(*(sys+i)+j);
cout<<" x";
cout<<j+1;
if(!(j==nun-1))
cout<<" + ";
}
cout<<" = ";
cout<<*(*(sys+i)+j);
}
solution();
getch();
clrscr();
cout<<"\n\n\n \t SOLUTION FOR THE GIVEN SYSTEM OF LINEAR SYSTEM OF EQUATIONS\n\n";

for(i=0;i<nun;i++){
cout<<"\n\t";
cout<<'x';
cout<<i+1;
cout<<':';
cout<<back[i];
}
cout<<"\n do u want 2 continue(y/n) ? :";
ch=getch();
}while(ch=='y');
}

Old 25.01.2005, 21:59   #4
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 42
Posts: 21,717
Rep Power: 9
Default

Quote:
Originally Posted by nm
я могу сказать, что метод гаусса надо выкинуть ошибки просто недетцкие получаются ... из-за накопления погрешности
Гаспарыч не по факту фурычишь, на умную лошадь сел - отмазоны лепишь

Old 25.01.2005, 21:59   #5
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 42
Posts: 21,717
Rep Power: 9
Default

вроде - длиннее...

Old 25.01.2005, 22:02   #6
Профессор
 
Nikita's Avatar
 
Join Date: 01 2005
Location: Perm
Age: 45
Posts: 2,142
Rep Power: 0
Default

Агрегат
А ты полный соурс код покажы, чтобы можно было сравнивать
кстате а если убрать пользовательский интерфейс ?
там слишком много сообщений выводится ...
Reply




Реклама:
реклама

All times are GMT. The time now is 23:24.
Top

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.