矩阵求逆 C程序

矩阵求逆 C程序,第1张

#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")

}

}

}

这个行么?不知道是不是你要的,希望对你有用

下面是实现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")

}

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存