#include
<stdlib.h>
#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
(p>d)
{
d=p
is[k]=i
js[k]=j}
}
if
(d+1.0==1.0)
{
free(is)
free(js)
printf("err**not
inv
")
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:
")
for
(i=0
i<=3
i++)
{
for
(j=0
j<=3
j++)
printf("%13.7e
",b[i][j])
printf("
")
}
printf("
")
printf("闷埋MAT
A-
IS:
")
for
(i=0
i<=3
i++)
{
for
(j=0
j<=3
j++)
printf("%13.7e
",a[i][j])
printf("
")
}
printf("
")
printf("MAT
AA-
IS:
")
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("
")
}
}
}参考资料:蚂衫蚂C常用算法程序集-徐士良
#define N 5 /*[注]:修改6为你所要的矩阵阶数*/#include "stdio.h"
#include "conio.h"
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(n>2) {for(z=0z<nz++)
{for(j=0j<n-1j++)
for(k=0k<n-1k++)
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) /*递归调用*/
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()函数用于求原矩阵各元素对应的余子式,存放在数组槐樱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=0z<nz++)
{l=z
for(j=0j<nj++)
{ m=j
for (k=0k<n-1k++)
for(g=0g<n-1g++)
{ if(g>=m&&k<l) a[k][g]=s[k][g+1]
else if(k>=l&&g<m) a[k][g]=s[k+1][g]
else if(k>=l&&g>=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=0z<Nz++) /*输入所需要的数据,为整型数据*/
for(j=0j<Nj++)
scanf("%d",&a[z][j])
printf("\nPress Enter continue......")
getchar()
//gotoxy(1,1)
printf("The original matrix is:\n")
for(z=0z<Nz++)/*打印原矩阵*/
{for(j=0j<Nj++)
printf("%5d",a[z][j])
printf("\n")
}
r=js(a,N)/*调用js()函数计算原矩阵的行列式值*/
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,则原矩阵无逆矩阵,反之则存在逆矩阵*/
else
{n_1(a,b,N) /*调用n_1()函数,得到原绝明姿矩阵各并绝元素对应的"余子式",存放在数组b[N][N]中*/
for(z=0z<Nz++) /*求代数余子式,此时b[N][N]中存放的为原矩阵各元素对应的"代数余子式"*/
for(j=0j<Nj++)
if((z+j)%2!=0 &&b[z][j]!=0) b[z][j]=-b[z][j]
for(z=0z<Nz++)/*对b[N][N]转置,此时b[N][N]中存放的为原矩阵的伴随矩阵*/
for(j=z+2j<Nj++)
{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=0z<Nz++)/* 打印伴随矩阵A* */
{for(j=0j<Nj++)
printf("%4.0f\t",b[z][j])
printf("\n")
}
for(z=0z<Nz++) /*求逆矩阵,此时b[N][N]中存放的是原矩阵的逆矩阵*/
for(j=0j<Nj++)
b[z][j]=b[z][j]/r
printf("\nThe nijuzhen is:(A*)/|A|(|A|=%d)\n",r) /*打印逆矩阵*/
for(z=0z<Nz++)
{for(j=0j<Nj++)
printf("%8.3f",b[z][j])
printf("\n")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)