|
楼主 |
发表于 2008-8-1 10:22:08
|
显示全部楼层
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- double **make_matrix(int m,int n)
- {
- double **p;
- int i,k,h;
- k=0;
- do
- {
- p=(double **)malloc(m*sizeof(double *));
- k++;
- if(k>100){printf("Function make_matrix() failed!\n");exit(0);}
- }while(p==NULL);
- k=0;
- for(i=0;i<m;i++)p[i]=NULL;
- do
- {
- for(i=0,h=0;i<m;i++)
- {
- if(p[i]==NULL)p[i]=(double *)malloc(n*sizeof(double));
- if(p[i]==NULL)h++;
- }
- k++;
- if(k>100){printf("Function make_matrix() failed!\n");exit(0);}
- }while(h!=0);
- return p;
- }
- double **make_parr(int m)
- {
- double **p;
- int k;
- k=0;
- do
- {
- p=(double **)malloc(m*sizeof(double *));
- k++;
- if(k>100){printf("Funtion make_parr() failed!\n");exit(0);}
- }while(p==NULL);
- return p;
- }
- double *make_array(int n)
- {
- double *p;
- int k;
- k=0;
- do
- {
- p=(double *)malloc(n*sizeof(double));
- k++;
- if(k>100){printf("Function make_array() failed!\n");exit(0);}
- }while(p==NULL);
- return p;
- }
- void free_matrix(double **mtr,int m)
- {
- int i;
- for(i=0;i<m;i++)free(mtr[i]);
- free(mtr);
- }
- void val_cpy(double **mtr1,double **mtr2,int m,int n)
- {
- int i;
- for(i=0;i<m;i++)memcpy(mtr1[i],mtr2[i],n*sizeof(double));
- }
- void idt_mtr(double **mtr,int n)
- {
- int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- {
- if(i==j)mtr[i][j]=1;
- else mtr[i][j]=0;
- }
- }
- void exch_mtr(double **mtr,int n,int r,int s)
- {
- double *t;
- t=make_array(n);
- memcpy(t,mtr[r],n*sizeof(double));
- memcpy(mtr[r],mtr[s],n*sizeof(double));
- memcpy(mtr[s],t,n*sizeof(double));
- }
复制代码 有问题函数:- double **rdechl_mtr(double **mtr,int m,int n)
- {
- double **p,tmp;
- int i,j,k,max,zero,r;
- p=make_matrix(m,n);
- val_cpy(p,mtr,m,n);
- for(j=0,k=0;j<n;j++,k++)
- {
- for(i=k,max=k,zero=k;i<m;i++)
- {
- if(fabs(p[i][j])>fabs(p[max][j]))max=i;
- if(p[i][j]==0)zero++;
- }
- if(zero==m){k--;continue;}
- else
- {
- if(max!=k)exch_mtr(p,n,k,max);
- for(i=j,tmp=p[k][j];i<n;i++)p[k][i]/=tmp;
- for(i=0;i<m;i++)
- {
- if(i==k)continue;
- for(r=j,tmp=p[i][j];r<n;r++)p[i][r]-=p[k][r]*tmp;
- }
- }
- }
- return p;
- }
复制代码 |
|