AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters > C/C++
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Jan 25, 2005, 18:04   #1
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,632
Rep Power: 11
Reputation: 207
Send a message via ICQ to Agregat Send a message via MSN to Agregat
Гаусс

Господа - решение системы линейных уравнений методом гаусса.
Предлагаю лишь основную функцию:
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 ов
кто может скатать короче
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Old Jan 25, 2005, 20:44   #2
ЙЦУКЕН
 
Join Date: Jul 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Posts: 3,114
Rep Power: 7
Reputation: 10
Send a message via ICQ to nm
я могу сказать, что метод гаусса надо выкинуть ошибки просто недетцкие получаются ... из-за накопления погрешности
nm is offline   Reply With Quote Quote selected
Old Jan 25, 2005, 21:41   #3
Профессор
 
Nikita's Avatar
 
Join Date: Jan 2005
Location: Perm
Posts: 2,142
Rep Power: 4
Reputation: 10
Send a message via ICQ to Nikita
это короче ?
это не я писал ...
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');
}
Nikita is offline   Reply With Quote Quote selected
Old Jan 25, 2005, 21:59   #4
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,632
Rep Power: 11
Reputation: 207
Send a message via ICQ to Agregat Send a message via MSN to Agregat
Quote:
Originally Posted by nm
я могу сказать, что метод гаусса надо выкинуть ошибки просто недетцкие получаются ... из-за накопления погрешности
Гаспарыч не по факту фурычишь, на умную лошадь сел - отмазоны лепишь
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Old Jan 25, 2005, 21:59   #5
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,632
Rep Power: 11
Reputation: 207
Send a message via ICQ to Agregat Send a message via MSN to Agregat
вроде - длиннее...
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Old Jan 25, 2005, 22:02   #6
Профессор
 
Nikita's Avatar
 
Join Date: Jan 2005
Location: Perm
Posts: 2,142
Rep Power: 4
Reputation: 10
Send a message via ICQ to Nikita
Агрегат
А ты полный соурс код покажы, чтобы можно было сравнивать
кстате а если убрать пользовательский интерфейс ?
там слишком много сообщений выводится ...
Nikita is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 20:50.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link