{
for(int j=0;j mat[i][j]*=-1; } mat[i][i]=0; } } //END OF 'REARRANGE' FUNCTION /********************************** * iteration funtion--迭代函数 * **********************************/ //performes a single iteration on the system, using passed assumed values void Csimple::iteration(double lambda,double *t) { for(int i=0;i variable[i]=0; for(int j=0;j variable[i]+=mat[i][j]*t[j]; } variable[i]+=mat[i][column-1]; //new value after relaxation variable[i]=t[i]+lambda*(variable[i]-t[i]); } } /******************************** * solve function--求解函数 * ********************************/ void Csimple::solve(double lambda) { //DECLARATIONS AND INITIALIZATIONS for (int i=0;i itercount=0; //this is the allowable error this value can be changed to suit the problem double criterion=0.0001; double *newest=new double[varnum]; double *last=new double[varnum]; for( i=0;i { newest[i]=variable[i]; } //END OF DECLARATIONS AND INITIALIZATIONS //MAIN BODY OF THE FUNCTION STARTS HERE- //while('condition not met'){perform another iteration} do { for(int i=0;i last[i]=newest[i]; } //this is the most important part, //everything else in this loop is only to check that the criterion is met if(flag==1){iteration(realx,last);} else iteration(lambda,last); for( i=0;i /******************************************* * Epsilon Criterion-Epsilon 精度要求 * *******************************************/ bool Csimple::epsilon(double *newest,double *last,int size,double criterion) { for(int i=0;i //if(it has not met the criterion) if((fabs(newest[i]-last[i])/newest[i])>criterion) { //then (return 1)=(condition not met) and the loop is repeated return 1; } } //criterion has been met return 0; } /************************************************ * show answer function-- 输出求解结果函数 * *************************************************/ //'solve' function must be executed before 'show_answer' function void Csimple::show_answer() { cout<<\ for(int i=0;i cout< /*********************************************************** * stream operators--矩阵输入、输出流重载函数 * ***********************************************************/ void operator<<(ostream& out,Csimple& m) { for (int i=0;i out< void operator>>(istream& in,Csimple& m) { for(int i=0;i for(int j=0;j cout<<\ in>>m.mat[i][j]; } } //clrscr(); } class matrix //高斯-赛德尔矩阵算法类 { friend void operator<<(ostream &,matrix &); friend void operator>>(istream &,matrix &); protected: int flag; int row,column; //'mat' is my actual matrix double **mat; //size of 'variable' always =(column-1) int varnum; //contains the solution set double *variable; //counts number of iterations int itercount; //performs a single iteration on all the equations //the argument is the relaxation coefficient double realx; void iteration(double); //checks the allowable error bool epsilon(double*,double*,int,double); public: matrix(int nRow,int nColumn,double rex,double Mat[MAX][MAX+1]); matrix(); matrix(int,int); //small funtion for asking the user for the number of rows and columns void ReadFromFile(); void WriteToFile(); static void initialize(int&,int&); //prepares function for reduction void rearrange(); //solves by gauss-seigel method, //perfomes multiple iterations until a solution is found //the argument is the relaxation-coefficient, and this is \void solve(double); //prints the solution void show_answer(); }; //END OF CLASS MATRIX /************************** * constructor--构造函数 * **************************/ matrix::matrix(int nRow,int nColumn,double Mat[MAX][MAX+1]):row(nRow),column(nColumn),realx(rex) { mat=new double*[row]; for(int i=0;i rex,double matrix::matrix(){ mat[MAX][MAX+1]; flag=0; } matrix::matrix(int r, int c):row(r),column(c) { //creates my matrix dynamically, but there are still no values in the positions mat=new double*[row]; for(int i=0;i //end of constructor /********************************* * 矩阵行列设置 * ROWS:线性方程数 * COLUMNS:系数及常数的个数 *********************************/ /********************************* * static initialization * *********************************/ void matrix::initialize(int&i,int&j) { cout<<\cin>>i; cout<<\cin>>j; } void matrix::ReadFromFile() { char str[10][100]; int i=0; ifstream infile(\ while(!infile.eof()) {infile.getline(str[i],100); i++; } row=i; column=i+1; mat=new double*[row]; for(i=0;i