计算方法之高斯消去
2008-05-17 12:08
316 查看
#include "stdio.h" #include "math.h" #include "stdlib.h" typedef double arrtype; //高斯消去法 void GuassScale(arrtype coff[],int n,arrtype *x = NULL,int m = 1) { int col = n + m; arrtype *s = new arrtype ; int *nrow = new int ; arrtype *base = coff; for(int i = 0; i<n;i++) { s[i] = 0; for(int j = 0;j<n;j++) { arrtype t = fabs(coff[i*col + j]); if(s[i] < t) s[i] = t; } nrow[i] = i; } for(i=0;i<n-1;i++) { int p = i; arrtype t = fabs(coff[nrow[p]*col+i])/s[nrow[p]]; for(int j= i+1;j<n;j++) { arrtype temp= fabs(coff[nrow[j]*col+i])/s[nrow[j]]; if(t<temp) { t=temp; p=j; } } if(coff[nrow[p]*col+i]==0) return; if(nrow[p]!=nrow[i]) { int ncopy = nrow[i]; nrow[i] = nrow[p]; nrow[p] =ncopy; } for(j= i+1;j<n;j++) { arrtype m = coff[nrow[j]*col+i]/coff[nrow[i]*col+i]; for(int k=i+1;k<col;k++) { coff[nrow[j]*col+k]-=coff[nrow[i]*col+k]*m; } } } if(coff[nrow[n-1]*col+n-1]==0) return; for(i=n;i<col;i++) { x[(n-1)*m+(i-n)]=coff[nrow[n-1]*col+i]/coff[nrow[n-1]*col+n-1]; } for(i=n-2;i>=0;i--) { for(int j = n;j<col;j++) { for(int k=n-1;k>i;k--) coff[nrow[i]*col+j]-=coff[nrow[i]*col+k]*x[k*m+(j-n)]; x[i*m+(j-n)]=coff[nrow[i]*col+j]/coff[nrow[i]*col+i]; } } } void main(int argc,char* argv[]) { FILE *fpi,*fpo; if(argc > 1) fpi=fopen(argv[1],"r"); else fpi=stdin; if(!fpi)return; if(argc>2) fpo = fopen(argv[2],"w"); else fpo = stdout; if(!fpo)return; int m,n; printf("请输入n和m的值:/n"); fscanf(fpi,"%d",&n); fscanf(fpi,"%d",&m); arrtype *co; co = new arrtype[n*(n+m)]; for(int i = 0;i<n*(n+m);i++) { float t; fscanf(fpi,"%f",&t); co[i]=t; } fclose(fpi); arrtype *x = new arrtype[m*n]; GuassScale(co,n,x,m); delete[] co; for(i=0;i<n;i++) { fprintf(fpo,"x[%d]=",i); for(int j=0;j<m;j++) fprintf(fpo,"%f",x[i*m+j]); fprintf(fpo,"/n"); } delete[] x; fclose(fpo); }
相关文章推荐
- 计算方法:高斯-若当主元消去法解线性方程组
- 计算方法 -- 解线性方程组直接法(LU分解、列主元高斯消元、追赶法)
- 数值计算方法(高斯消元以及LU分解)
- 计算方法列主元素消去法求线性方程组的解
- 整数行列式计算 高斯方法 无精度损失
- 计算方法总体选主元素消去法求线性方程组的解
- 解线性方程组的直接方法——高斯消去和LU消去法
- 计算方法之用高斯列主元消去法求线性方程组
- 计算方法实验三 高斯消元
- 计算方法之用高斯列主元消去法求线性方程组
- 计算二进制中有多少个1的五种方法
- 子网掩码的简易计算方法
- ROC介绍及AUC计算方法
- Matlab中计算程序显示运行时间的几种方法
- MSSQL计算日期方法大全
- RMS计算方法
- 实现文本自动分类的基础----Term频率计算方法
- ntfs硬盘整数分区计算方法解析
- 步进电机选型的计算方法
- 【解题报告】 URAL 1333 Genie Bomber 2 -- 利用类似统计的方法计算覆盖效率