导航:首页 > 编程语言 > 编程逆矩阵

编程逆矩阵

发布时间:2023-05-21 15:34:00

A. 用c语言编程,求解逆矩阵

#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;}

B. C语言 求矩阵的逆

//源程序如下#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=0;i<max;i++)*(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=0;i<style;i++){
for(int j=0;j<style;j++){
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=0;i<n;i++){
for(int j=0;j<n;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阶数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=0;i<max;i++)*(po+i)=operate[i]; if(n==1)return *(*(p+0)+0);
else{
for(i=0;i<n;i++){
for(j1=1,j2=0;j1<n;j1++,j2++){
k1=-1;k2=-1;
while(k2<n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
/*for(int i1=0;i1<n-1;i1++){
for(int h1=0;h1<n-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;i<max;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("矩阵的逆为: 系数 1/%d *\n",num);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
j1=-1;j2=-1;
while(j2<n-1){
j1++;j2++;
if(j1==j)j1++; k1=-1;k2=-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=0;i<n;i++){
for(j=0;j<n;j++){
printf("%4d",tramform[i][j]);
}
printf("\n");
}
}
}
}
//运行结果//希望对你有帮助

C. fortran编程 逆矩阵

以下是求5阶方阵的逆矩阵,fortran77程序。供参考。


c 求逆矩阵
c 在主程序中,设两个数组a(5,5),b(5,5)
c a(5,5)--存放5阶方阵
c b(5,5)--存放单位阵
c 在子程序中,设一个数组c(5,10),该数组是a、b阵拼接起来的
c 要实现数组的拼接和拆分,用公用语句实现
reala(5,5),b(5,5)
common/x/a,b
do10i=1,5
do10j=1,5
if(i.eq.j)then
b(i,j)=1
else
b(i,j)=0
endif
10 continue
callinverse
write(*,20)b
write(*,*)
write(*,20)((b(i,j),j=1,5),i=1,5)
20 format(1x,5f10.3)
c read(*,*)
end
c************************************
subroutineinverse
realc(5,10)
common/x/c
do10k=1,5
do20j=10,k,-1
20 c(k,j)=c(k,j)/c(k,k)
do40i=1,5
if(i.ne.k)then
do30j=10,k,-1
30 c(i,j)=c(i,j)-c(i,k)*c(k,j)
endif
40 continue
10 continue
end
c******************************
blockdata
reala(5,5),b(5,5)
common/x/a,b
dataa/3,1,0,2,10,-2,0,1,3,1,9,3,1,0,1,
1 1,0,1,1,0,1,2,0,2,10/
end

阅读全文

与编程逆矩阵相关的资料

热点内容
考研人如何缓解压力 浏览:818
买电暖壶哪个app便宜 浏览:503
洛克王国忘记服务器了怎么办 浏览:782
为什么cf登录服务器没反应 浏览:695
服务器如何获取文件列表 浏览:672
creo五轴编程光盘 浏览:14
苹果app网络验证在哪里 浏览:14
博科清空命令 浏览:384
简爱英文pdf 浏览:376
cnc编程有前途吗 浏览:586
联想app怎么联网 浏览:722
linuxftp命令登录 浏览:1000
android获取图片缩略图 浏览:646
神户制钢螺杆压缩机 浏览:29
差分演化算法 浏览:567
中山市加密软件 浏览:446
mc反编译源码 浏览:141
企业商城网站源码 浏览:411
shell脚本编程是什么 浏览:762
单片机led闪烁汇编 浏览:203