#include stdlib.h
創(chuàng)新互聯(lián)公司是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、重慶小程序開發(fā)公司、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十載以來,已經(jīng)為成百上千家門簾各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的成百上千家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。
#include math.h
#include stdio.h
int brinv(double a[], int n)
{ int *is,*js,i,j,k,l,u,v;
double d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
for (k=0; k=n-1; k++)
{ d=0.0;
for (i=k; i=n-1; i++)
for (j=k; j=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (pd) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i=n-1; i++)
if (i!=k)
for (j=0; j=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k=0; k--)
{ if (js[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}
void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i=m-1; i++)
for (j=0; j=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int main()
{ int i,j;
static double a[4][4]={ {0.2368,0.2471,0.2568,1.2671},
{1.1161,0.1254,0.1397,0.1490},
{0.1582,1.1675,0.1768,0.1871},
{0.1968,0.2071,1.2168,0.2271}};
static double b[4][4],c[4][4];
for (i=0; i=3; i++)
for (j=0; j=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
}
#include?stdio.h
#include?stdlib.h
#include?malloc.h
void?MatrixOpp(double?*A,?int?m,?int?n,?double*?invmat);
void?MatrixInver(double?*A,?int?m,?int?n,?double*?invmat);
double?Surplus(double?A[],?int?m,?int?n);
int?matrix_inv(double*?p,?int?num,?double*?invmat);
void?MatrixOpp(double?A[],?int?m,?int?n,?double*?invmat)
{
int?i,?j,?x,?y,?k;
double?*SP?=?NULL,?*AB?=?NULL,?*B?=?NULL,?X;
SP?=?(double?*)?malloc(m?*?n?*?sizeof(double));
AB?=?(double?*)?malloc(m?*?n?*?sizeof(double));
B?=?(double?*)?malloc(m?*?n?*?sizeof(double));
X?=?Surplus(A,?m,?n);
X?=?1?/?X;
for?(i?=?0;?i??m;?i++)
for?(j?=?0;?j??n;?j++)
{
for?(k?=?0;?k??m?*?n;?k++)
B[k]?=?A[k];
{
for?(x?=?0;?x??n;?x++)
B[i?*?n?+?x]?=?0;
for?(y?=?0;?y??m;?y++)
B[m?*?y?+?j]?=?0;
B[i?*?n?+?j]?=?1;
SP[i?*?n?+?j]?=?Surplus(B,?m,?n);
AB[i?*?n?+?j]?=?X?*?SP[i?*?n?+?j];
}
}
MatrixInver(AB,?m,?n,?invmat);
free(SP);
free(AB);
free(B);
}
void?MatrixInver(double?A[],?int?m,?int?n,?double*?invmat)
{
int?i,?j;
double?*B?=?invmat;
for?(i?=?0;?i??n;?i++)
for?(j?=?0;?j??m;?j++)
B[i?*?m?+?j]?=?A[j?*?n?+?i];
}
double?Surplus(double?A[],?int?m,?int?n)
{
int?i,?j,?k,?p,?r;
double?X,?temp?=?1,?temp1?=?1,?s?=?0,?s1?=?0;
if?(n?==?2)
{
for?(i?=?0;?i??m;?i++)
for?(j?=?0;?j??n;?j++)
if?((i?+?j)?%?2)
temp1?*=?A[i?*?n?+?j];
else
temp?*=?A[i?*?n?+?j];
X?=?temp?-?temp1;
}
else
{
for?(k?=?0;?k??n;?k++)
{
for?(i?=?0,?j?=?k;?i??m,?j??n;?i++,?j++)
temp?*=?A[i?*?n?+?j];
if?(m?-?i)
{
for?(p?=?m?-?i,?r?=?m?-?1;?p??0;?p--,?r--)
temp?*=?A[r?*?n?+?p?-?1];
}
s?+=?temp;
temp?=?1;
}
for?(k?=?n?-?1;?k?=?0;?k--)
{
for?(i?=?0,?j?=?k;?i??m,?j?=?0;?i++,?j--)
temp1?*=?A[i?*?n?+?j];
if?(m?-?i)
{
for?(p?=?m?-?1,?r?=?i;?r??m;?p--,?r++)
temp1?*=?A[r?*?n?+?p];
}
s1?+=?temp1;
temp1?=?1;
}
X?=?s?-?s1;
}
return?X;
}
int?matrix_inv(double*?p,?int?num,?double*?invmat)
{
if?(p?==?NULL?||?invmat?==?NULL)
{
return?1;
}
if?(num??10)
{
return?2;
}
MatrixOpp(p,?num,?num,?invmat);
return?0;
}
int?main()
{
int?i,?j;
int?num;
double?*arr=NULL;
double?*result=NULL;
int?flag;
printf("請輸入矩陣維數(shù):\n");
scanf("%d",num);
arr=(double?*)malloc(sizeof(double)*num*num);
result=(double?*)malloc(sizeof(double)*num*num);
printf("請輸入%d*%d矩陣:\n",?num,?num);
for?(i?=?0;?i??num;?i++)
{
for?(j?=?0;?j??num;?j++)
{
scanf("%lf",?arr[i?*?num?+?j]);
}
}
flag?=?matrix_inv(arr,?num,?result);
if(flag==0)
{
printf("逆矩陣為:\n");
for?(i?=?0;?i??num?*?num;?i++)
{
printf("%lf\t?",?*(result?+?i));
if?(i?%?num?==?(num?-?1))
printf("\n");
}
}
else?if(flag==1)
{
printf("p/q為空\n");
}
else
{
printf("超過最大維數(shù)\n");
}
system("PAUSE");
free(arr);
free(result);
return?0;
}
//源程序如下#includestdio.h
#includeconio.h
#includestring.h
#includeiostream.h
#includestdlib.h
#includemath.h
#define max 100void inputstyle(int *); //輸入函數(shù)
void input(int **,int); //輸入函數(shù)
long danx(int **,int);
int sgnx(int);
void martx(int **,int);int main(void)
{
int style=0,i=0;
int matrix[max][max],*p[max];
for(i=0;imax;i++)*(p+i)=matrix[i]; //*(p+i)是指針,指向第i個(gè)字符串
char exit1=' ';
while(exit1!='E' exit1!='e'){ printf("求n階矩陣的逆\n"); inputstyle(style);
input(p,style);
printf("原矩陣為:\n");
for(i=0;istyle;i++){
for(int j=0;jstyle;j++){
printf("%4d",matrix[i][j]);
}
printf("\n");
}
martx(p,style);
printf("\n");
printf("Exit=e Continue=Press any key\n");
cinexit1;
fflush(stdin);
printf("\n\n"); }
return(0);
} void input(int **p,int n){
for(int i=0;in;i++){
for(int j=0;jn;j++){
printf("輸入矩陣(%d行,%d列)元素:",j+1,i+1);
*(*(p+j)+i)=0;
scanf("%d",*(p+j)+i);
fflush(stdin);
}
}
}void inputstyle(int *style){
do{
printf("輸入矩陣n*n階數(shù)n(0n%d):",max);
fflush(stdin);
scanf("%d",style);
fflush(stdin);
}while(*style=0 *stylemax);
}long danx(int **p,int n){
int i=0,j1=0,k1=0,j2=0,k2=0;
long sum=0;
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i]; if(n==1)return *(*(p+0)+0);
else{
for(i=0;in;i++){
for(j1=1,j2=0;j1n;j1++,j2++){
k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
/*for(int i1=0;i1n-1;i1++){
for(int h1=0;h1n-1;h1++){
printf("(%d,%d)%d ",i1,h1,*(*(po+h1)+i1));
}
printf("\n");
}*/
sum+=*(*(p+0)+i) * sgnx(1+i+1) * danx(po,n-1);
}
return sum;
}
}int sgnx(int i){
if(i%2==0)return(1);
else return(-1);
}void martx(int **p,int n){
int i=0,j=0,j1=0,k1=0,j2=0,k2=0,num=0;
int tramform[max][max];
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i];
num=danx(p,n);
if(num==0)printf("矩陣不可逆\n");
else{
if(n==1)printf("矩陣的逆為: 1/%d\n",num);
else{
printf("矩陣的逆為: 系數(shù) 1/%d *\n",num);
for(i=0;in;i++){
for(j=0;jn;j++){
j1=-1;j2=-1;
while(j2n-1){
j1++;j2++;
if(j1==j)j1++; k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
tramform[i][j]=sgnx(2+i+j) * danx(po,n-1);
}
}
for(i=0;in;i++){
for(j=0;jn;j++){
printf("%4d",tramform[i][j]);
}
printf("\n");
}
}
}
}
//運(yùn)行結(jié)果//希望對你有幫助
當(dāng)前名稱:c語言定義函數(shù)求逆矩陣,矩陣求逆c語言實(shí)現(xiàn)
轉(zhuǎn)載來于:http://www.rwnh.cn/article34/dsijdse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、微信小程序、域名注冊、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)