C语言 求矩阵的逆

C语言 求矩阵的逆,第1张

//源程序如下#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<iostream.h>

#include<stdlib.h>

#include<math.h>

#define max 100void inputstyle(int *) //输入函数

void input(int **,int) //输入函数

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=0i<maxi++)*(p+i)=matrix[i] //*(p+i)是指针,指向第i个字符串

char exit1=' '

while(exit1!='E'&&exit1!='e'){ printf("求n阶矩阵的逆\n") inputstyle(&style)

input(p,style)

printf("原矩阵为:\n")

for(i=0i<stylei++){

for(int j=0j<stylej++){

printf("%4d",matrix[i][j])

}

printf("\n")

}

martx(p,style)

printf("\n")

printf("Exit=e Continue=Press any key\n")

cin>>exit1

fflush(stdin)

printf("\n\n")}

return(0)

} void input(int **p,int n){

for(int i=0i<ni++){

for(int j=0j<nj++){

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阶数n(0<n<%d):",max)

fflush(stdin)

scanf("%d",style)

fflush(stdin)

}while(*style<=0 &&*style>max)

}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=0i<maxi++)*(po+i)=operate[i]if(n==1)return *(*(p+0)+0)

else{

for(i=0i<ni++){

for(j1=1,j2=0j1<nj1++,j2++){

k1=-1k2=-1

while(k2<n-1){

k1++

k2++

if(k1==i)k1++

*(*(po+j2)+k2)=*(*(p+j1)+k1)

}

}

/*for(int i1=0i1<n-1i1++){

for(int h1=0h1<n-1h1++){

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=0i<maxi++)*(po+i)=operate[i]

num=danx(p,n)

if(num==0)printf("矩阵不可逆\n")

else{

if(n==1)printf("矩阵的逆为: 1/%d\n",num)

else{

printf("矩阵的逆为: 系数 1/%d *\n",num)

for(i=0i<ni++){

for(j=0j<nj++){

j1=-1j2=-1

while(j2<n-1){

j1++j2++

if(j1==j)j1++ k1=-1k2=-1

while(k2<n-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=0i<ni++){

for(j=0j<nj++){

printf("%4d",tramform[i][j])

}

printf("\n")

}

}

}

}

//运行结果//希望对你有帮助

下面是实现Gauss-Jordan法实矩阵求逆。

#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=0k<=n-1k++)

{ d=0.0

for (i=ki<=n-1i++)

for (j=kj<=n-1j++)

{ l=i*n+jp=fabs(a[l])

if (p>d) { d=pis[k]=ijs[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=0j<=n-1j++)

{ u=k*n+jv=is[k]*n+j

p=a[u]a[u]=a[v]a[v]=p

}

if (js[k]!=k)

for (i=0i<=n-1i++)

{ u=i*n+kv=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=0j<=n-1j++)

if (j!=k)

{ u=k*n+ja[u]=a[u]*a[l]}

for (i=0i<=n-1i++)

if (i!=k)

for (j=0j<=n-1j++)

if (j!=k)

{ u=i*n+j

a[u]=a[u]-a[i*n+k]*a[k*n+j]

}

for (i=0i<=n-1i++)

if (i!=k)

{ u=i*n+ka[u]=-a[u]*a[l]}

}

for (k=n-1k>=0k--)

{ if (js[k]!=k)

for (j=0j<=n-1j++)

{ u=k*n+jv=js[k]*n+j

p=a[u]a[u]=a[v]a[v]=p

}

if (is[k]!=k)

for (i=0i<=n-1i++)

{ u=i*n+kv=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=0i<=m-1i++)

for (j=0j<=k-1j++)

{ u=i*k+jc[u]=0.0

for (l=0l<=n-1l++)

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=0i<=3i++)

for (j=0j<=3j++)

b[i][j]=a[i][j]

i=brinv(a,4)

if (i!=0)

{ printf("MAT A IS:\n")

for (i=0i<=3i++)

{ for (j=0j<=3j++)

printf("%13.7e ",b[i][j])

printf("\n")

}

printf("\n")

printf("MAT A- IS:\n")

for (i=0i<=3i++)

{ for (j=0j<=3j++)

printf("%13.7e ",a[i][j])

printf("\n")

}

printf("\n")

printf("MAT AA- IS:\n")

brmul(b,a,4,4,4,c)

for (i=0i<=3i++)

{ for (j=0j<=3j++)

printf("%13.7e ",c[i][j])

printf("\n")

}

}

}

算法的大致思想是通过行列式初等变换来求。

代码如下:

private double[,] ReverseMatrix( double[,] dMatrix, int Level )

{

double dMatrixValue = MatrixValue( dMatrix, Level )

if( dMatrixValue == 0 ) return null

double[,] dReverseMatrix = new double[Level,2*Level]

double x, c

// Init Reverse matrix

for( int i = 0i <Leveli++ )

{

for( int j = 0j <2 * Levelj++ )

{

if( j <Level )

dReverseMatrix[i,j] = dMatrix[i,j]

else

dReverseMatrix[i,j] = 0

}

dReverseMatrix[i,Level + i ] = 1

}

for( int i = 0, j = 0i <Level &&j <Leveli++, j++ )

{

if( dReverseMatrix[i,j] == 0 )

{

int m = i

for( dMatrix[m,j] == 0m++ )

if( m == Level )

return null

else

{

// Add i-row with m-row

for( int n = jn <2 * Leveln++ )

dReverseMatrix[i,n] += dReverseMatrix[m,n]

}

}

// Format the i-row with "1" start

x = dReverseMatrix[i,j]

if( x != 1 )

{

for( int n = jn <2 * Leveln++ )

if( dReverseMatrix[i,n] != 0 )

dReverseMatrix[i,n] /= x

}

// Set 0 to the current column in the rows after current row

for( int s = Level - 1s >is-- )

{

x = dReverseMatrix[s,j]

for( int t = jt <2 * Levelt++ )

dReverseMatrix[s,t] -= ( dReverseMatrix[i,t]* x )

}

}

// Format the first matrix into unit-matrix

for( int i = Level - 2i >= 0i-- )

{

for( int j = i + 1 j <Levelj++ )

if( dReverseMatrix[i,j] != 0 )

{

c = dReverseMatrix[i,j]

for( int n = jn <2*Leveln++ )

dReverseMatrix[i,n] -= ( c * dReverseMatrix[j,n] )

}

}

double[,] dReturn = new double[Level, Level]

for( int i = 0i <Leveli++ )

for( int j = 0j <Levelj++ )

dReturn[i,j] = dReverseMatrix[i,j+Level]

return dReturn

}

private double MatrixValue( double[,] MatrixList, int Level )

{

double[,] dMatrix = new double[Level, Level]

for( int i = 0i <Leveli++ )

for( int j = 0j <Levelj++ )

dMatrix[i,j] = MatrixList[i,j]

double c, x

int k = 1

for( int i = 0, j = 0i <Level &&j <Leveli++, j++ )

{

if( dMatrix[i,j] == 0 )

{

int m = i

for( dMatrix[m,j] == 0m++ )

if( m == Level )

return 0

else

{

// Row change between i-row and m-row

for( int n = jn <Leveln++ )

{

c = dMatrix[i,n]

dMatrix[i,n] = dMatrix[m,n]

dMatrix[m,n] = c

}

// Change value pre-value

k *= (-1)

}

}

// Set 0 to the current column in the rows after current row

for( int s = Level - 1s >is-- )

{

x = dMatrix[s,j]

for( int t = jt <Levelt++ )

dMatrix[s,t] -= dMatrix[i,t]* ( x/dMatrix[i,j] )

}

}

double sn = 1

for( int i = 0i <Leveli++ )

{

if( dMatrix[i,i] != 0 )

sn *= dMatrix[i,i]

else

return 0

}

return k*sn

}

调用如下:

double[,] dMatrix = new double[3,3]{{0,1,2},{1,0,1},{4,2,1}}

double[,] dReturn = ReverseMatrix( dMatrix, 3 )

if( dReturn != null )

{

for( int i=0i <3i++ )

Debug.WriteLine( string.Format( "{0} {1} {2}",

dReturn[i,0], dReturn[i,1],dReturn[i,2] ) )

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11084892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存