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