#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")
}
}
}
#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("请输入矩阵维数:\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("超过最大维数\n")
}
system("PAUSE")
free(arr)
free(result)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)