#include stdlib.h
創(chuàng)新互聯(lián)是專(zhuān)業(yè)的高碑店網(wǎng)站建設(shè)公司,高碑店接單;提供成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行高碑店網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
#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"
# define M 3
void main ( )
{
float MAT[M][2*M];
float MAT1[M][M];
float t;
int i,j,k,l;
/***********************************************/
/*對(duì)矩陣進(jìn)行初始化*/
for(i=0;iM;i++)
for(j=0;j2*M;j++)
MAT1[j]='\0';
for(i=0;iM;i++)
for(j=0;j2*M;j++)
MAT[j]='\0';
/*對(duì)MAT1矩陣賦初值 */
for(i=0;iM;i++)
for (j=0;jM;j++)
scanf("%f",MAT1[j]);
/*打印目標(biāo)矩陣?*/
printf("原矩陣為:\n");
for (i=0;iM;i++)
{
for (j=0;jM;j++)
printf("%5.2f",MAT1[j]);
printf("\n");
}
/********************************************/
/*對(duì)MAT1矩陣進(jìn)行擴(kuò)展,MAT1矩陣添加單位陣,由M*M變成2M*2M矩陣 */
for(i=0;iM;i++)
for(j=0;j2*M;j++)
if (jM) MAT[j]=MAT1[j];
else if (j==M+i) MAT[j]=1;
else MAT[j]=0;
/*對(duì)M矩陣進(jìn)行變換,使得前半部分矩陣成為單位陣,則 */
/*后半部分矩陣即為所求矩陣逆陣 */
for(i=0;iM;i++)
{
/*對(duì)第i行進(jìn)行歸一化 */
for (j=0;j2*M;j++)
for(k=i+1;kM;k++)
MAT[j]=MAT[j]+MAT[k][j];
t=MAT;
for(j=i;j2*M;j++)
MAT[j]=MAT[j]/t;
/*對(duì)矩陣進(jìn)行行變換,使得第i 列只有一個(gè)元素不為零,且為1*/
for(k=0;kM;k++)
if(k!=i)
{
t=MAT[k];
for (l=i;l2*M;l++)
MAT[k][l]=MAT[k][l]-MAT[l]*t;
}
}
/*將后半部分矩陣即所求矩陣逆陣存入MAT2矩陣。*/
for(i=0;iM;i++)
{
for(j=0;jM;j++)
MAT1[j]=MAT[j+M];
printf("\n");
}
/*********************************************/
/*輸出所求的逆陣*/
printf("逆陣為:\n");
for(i=0;iM;i++)
{
for(j=0;jM;j++)
printf("%5.2f",MAT1[j]);
printf("\n");
}
}
[樓 主] From:局域網(wǎng) | Posted:2007-10-08 00:11|
情有獨(dú)中
級(jí)別: 論壇版主
精華: 0
發(fā)帖: 8
威望: 8 點(diǎn)
金錢(qián): 36 RMB
在線時(shí)間:0(小時(shí))
注冊(cè)時(shí)間:2007-10-08
最后登陸:2007-10-07
--------------------------------------------------------------------------------
#define N 5 /*[注]:修改6為你所要的矩陣階數(shù)*/
#include "stdio.h"
#include "conio.h"
/*js()函數(shù)用于計(jì)算行列式,通過(guò)遞歸算法實(shí)現(xiàn)*/
int js(s,n)
int s[][N],n;
{int z,j,k,r,total=0;
int b[N][N];/*b[N][N]用于存放,在矩陣s[N][N]中元素s[0]的余子式*/
if(n2) {for(z=0;zn;z++)
{for(j=0;jn-1;j++)
for(k=0;kn-1;k++)
if(k=z) b[j][k]=s[j+1][k+1];
else b[j][k]=s[j+1][k];
if(z%2==0) r=s[0][z]*js(b,n-1); /*遞歸調(diào)用*/
else r=(-1)*s[0][z]*js(b,n-1);
total=total+r;
}
}
else if(n==2) total=s[0][0]*s[1][1]-s[0][1]*s[1][0];
return total;
}
/*n_1()函數(shù)用于求原矩陣各元素對(duì)應(yīng)的余子式,存放在數(shù)組b[N][N]中,定義為float型*/
void n_1(s,b,n)
int s[][N],n;
float b[][N];
{int z,j,k,l,m,g,a[N][N];
for(z=0;zn;z++)
{l=z;
for(j=0;jn;j++)
{ m=j;
for (k=0;kn-1;k++)
for(g=0;gn-1;g++)
{ if(g=mkl) a[k][g]=s[k][g+1];
else if(k=lgm) a[k][g]=s[k+1][g];
else if(k=lg=m) a[k][g]=s[k+1][g+1];
else a[k][g]=s[k][g];
}
b[z][j]=js(a,n-1);
}
}
}
main()
{int a[N][N];
float b[N][N];
int r,z,j;
float temp;
//clrscr();
printf("Input original data:\n");
for(z=0;zN;z++) /*輸入所需要的數(shù)據(jù),為整型數(shù)據(jù)*/
for(j=0;jN;j++)
scanf("%d",a[z][j]);
printf("\nPress Enter continue......");
getchar();
//gotoxy(1,1);
printf("The original matrix is:\n");
for(z=0;zN;z++)/*打印原矩陣*/
{for(j=0;jN;j++)
printf("%5d",a[z][j]);
printf("\n");
}
r=js(a,N); /*調(diào)用js()函數(shù)計(jì)算原矩陣的行列式值*/
printf("\nThe original matrix hanglieshi is:|A|==%d\n",r);
if (r==0) printf("Because |A|==0,the original matrix have no nijuzhen!"); /*判斷條件:若|A|==0,則原矩陣無(wú)逆矩陣,反之則存在逆矩陣*/
else
{n_1(a,b,N); /*調(diào)用n_1()函數(shù),得到原矩陣各元素對(duì)應(yīng)的"余子式",存放在數(shù)組b[N][N]中*/
for(z=0;zN;z++) /*求代數(shù)余子式,此時(shí)b[N][N]中存放的為原矩陣各元素對(duì)應(yīng)的"代數(shù)余子式"*/
for(j=0;jN;j++)
if((z+j)%2!=0 b[z][j]!=0) b[z][j]=-b[z][j];
for(z=0;zN;z++) /*對(duì)b[N][N]轉(zhuǎn)置,此時(shí)b[N][N]中存放的為原矩陣的伴隨矩陣*/
for(j=z+2;jN;j++)
{temp=b[z][j];
b[z][j]=b[j][z];
b[j][z]=temp;
}
printf("Because |A|!=0,the original matrix have nijuzhen!\n");
printf("The bansuijuzhen A* is:\n");
for(z=0;zN;z++)/* 打印伴隨矩陣A* */
{for(j=0;jN;j++)
printf("%4.0f\t",b[z][j]);
printf("\n");
}
for(z=0;zN;z++) /*求逆矩陣,此時(shí)b[N][N]中存放的是原矩陣的逆矩陣*/
for(j=0;jN;j++)
b[z][j]=b[z][j]/r;
printf("\nThe nijuzhen is:(A*)/|A|(|A|=%d)\n",r); /*打印逆矩陣*/
for(z=0;zN;z++)
{for(j=0;jN;j++)
printf("%8.3f",b[z][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("請(qǐng)輸入矩陣維數(shù):\n");
scanf("%d",num);
arr=(double?*)malloc(sizeof(double)*num*num);
result=(double?*)malloc(sizeof(double)*num*num);
printf("請(qǐng)輸入%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為空\(chéng)n");
}
else
{
printf("超過(guò)最大維數(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é)果//希望對(duì)你有幫助