现在的位置: 首页 > 综合 > 正文

关于矩阵的一些操作(求转置矩阵、行列式、矩阵的秩、矩阵的逆矩阵、两个矩阵的乘积矩阵)

2018年04月04日 ⁄ 综合 ⁄ 共 7919字 ⁄ 字号 评论关闭

该程序的功能主要解决一些简单矩阵计算问题。

主要功能有:

① 矩阵输入

② 矩阵输出

③ 输出矩阵的转置矩阵(可转置任意行列的矩阵)

④ 求方阵的行列式(如果你输入错误,程序将提示你错误,你可关闭程序,重新输入行列相同的矩阵,再进行计算)

⑤ 求矩阵的秩

⑥ 求矩阵的逆矩阵(前提:行和列相等)

⑦ 求两个矩阵的乘积矩阵(其中之一是你已经输入的那一个矩阵,另一个你可自行输入,可计算多次乘积)

  1. /* 
  2. author:wangchangshuai0010 sdust wangchangshuai0010.iteye.com 
  3. */  
  4. #include<stdio.h>  
  5. #include<math.h>  
  6. #define N 10  
  7.   
  8. void output(double a[][N],int am,int an)  
  9. {  
  10.     int i,j;  
  11.     printf("\nThe OriginalMatrix A is:\n");  
  12.     printf("**********************************************\n");  
  13.     for(i=0;i<am;i++)   
  14.     {   
  15.        for(j=0;j<an;j++)  
  16.        {  
  17.            printf("%10.4f",a[i][j]);  
  18.        }  
  19.        printf("\n");  
  20.     }  
  21. }  
  22.   
  23.   
  24.   
  25. void inver(double b[][N],int am,int an)  
  26. {  
  27.     int i,j;  
  28.     double a[N][N],c[N][N];  
  29.     for(i=0;i<am;i++)  
  30.     {  
  31.         for(j=0;j<an;j++)  
  32.         {  
  33.             a[i][j]=b[i][j];  
  34.         }  
  35.     }  
  36.     for(i=0;i<am;i++)  
  37.     {  
  38.         for(j=0;j<an;j++)  
  39.         {  
  40.             c[j][i]=a[i][j];   
  41.         }  
  42.     }  
  43.     for(i=0;i<an;i++)   
  44.     {  
  45.        for(j=0;j<am;j++)  
  46.        {  
  47.            printf("%10.4f",c[i][j]);  
  48.        }  
  49.        printf("\n");  
  50.     }  
  51. }  
  52.   
  53. double getdet(double b[][N],int am,int an)  
  54. {  
  55.     int i,j,k,l,nexti,count=0;  
  56.     double temp,a[N][N],detA=1.0,x;  
  57.     for(i=0;i<am;i++)  
  58.     {  
  59.         for(j=0;j<an;j++)  
  60.         {  
  61.             a[i][j]=b[i][j];  
  62.         }  
  63.     }  
  64.     if(am==an)  
  65.     {  
  66.         for(i=0,j=0;i<am-1;i++,j++)  
  67.         {  
  68.             nexti=i;  
  69.             while(a[i][j]==0)  
  70.             {  
  71.                 nexti++;  
  72.                 temp=a[i][j];  
  73.                 a[i][j]=a[nexti][j];  
  74.                 a[nexti][j]=temp;  
  75.             }  
  76.             for(k=j+1;k<am;k++)  
  77.             {  
  78.                 temp=a[i][k];  
  79.                 a[i][k]=a[nexti][k];  
  80.                 a[nexti][k]=temp;  
  81.             }  
  82.             for(l=i+1;l<an;l++)  
  83.             {  
  84.                 if(a[l][j]==0)  
  85.                 {l++;}  
  86.                 else  
  87.                 {  
  88.                     x=a[l][j]/a[i][j];  
  89.                     for(k=j;k<am;k++)  
  90.                     {  
  91.                           
  92.                         a[l][k]=a[l][k]-(x)*a[i][k];  
  93.                     }  
  94.                 }  
  95.             }  
  96.         }  
  97.         for(i=0,j=0;i<am;i++,j++)  
  98.         {  
  99.             detA*=a[i][j];  
  100.         }  
  101.     }  
  102.     else  
  103.     {  
  104.         printf("error!\n");  
  105.     }  
  106.     return detA;  
  107. }  
  108.   
  109.   
  110. void getrank(double b[][N],int am,int an)  
  111. {  
  112.     int i,s,j,k,l,nexti,t=0,count=0;  
  113.     double temp,x,a[N][N],detA=1.0;  
  114.     if(am>an)  
  115.     {  
  116.         s=am;  
  117.         am=an;  
  118.         an=s;  
  119.     }  
  120.     for(i=0;i<am;i++)  
  121.     {  
  122.         for(j=0;j<an;j++)  
  123.         {  
  124.             a[i][j]=b[i][j];  
  125.         }  
  126.     }  
  127.     for(i=0,j=0;i<am-1;i++,j++)  
  128.     {  
  129.         nexti=i;  
  130.         while(a[i][j]==0)  
  131.         {  
  132.             nexti=i+1;  
  133.             temp=a[i][j];  
  134.             a[i][j]=a[nexti][j];  
  135.             a[nexti][j]=temp;  
  136.         }  
  137.         for(k=j+1;k<am;k++)  
  138.         {  
  139.             temp=a[i][k];  
  140.             a[i][k]=a[nexti][k];  
  141.             a[nexti][k]=temp;  
  142.         }  
  143.         for(l=i+1;l<an;l++)  
  144.         {  
  145.             if(a[l][j]==0)  
  146.             {l++;}            
  147.             else  
  148.             {  
  149.                 x=a[l][j]/a[i][j];  
  150.                 for(k=j;k<am;k++)                  
  151.                 {     
  152.                     a[l][k]=a[l][k]-(x)*a[i][k];  
  153.                 }  
  154.             }  
  155.         }  
  156.     }  
  157.     for(i=an-1;i<an;i++)  
  158.     {  
  159.         for(j=am;j<an;j++)  
  160.         {  
  161.             if(a[i][j]!=0)  
  162.             {  
  163.                 t++;  
  164.             }  
  165.         }  
  166.     }  
  167.     if(t==0)  
  168.     {  
  169.         for(i=0,j=0;a[i][j]!=0&&i<an;i++,j++)  
  170.         {count++;}  
  171.         printf("the rank is %d\n",count);  
  172.     }  
  173.     else  
  174.     {  
  175.         printf("**********************************************\n");  
  176.         printf("the rank is %d\n",am);  
  177.     }  
  178. }  
  179.   
  180.   
  181.   
  182. void getnijuzhen(double b[][N],int am,int an)  
  183. {  
  184.     int i,j,k,l,m,n;  
  185.     double a[N][N],c[N][N],d[N][N],x;  
  186.     if(an!=am)  
  187.     {  
  188.         printf("error!\n");  
  189.     }  
  190.     else{  
  191.       for(i=0;i<am;i++)  
  192.       {  
  193.         for(j=0;j<an;j++)  
  194.         {  
  195.             a[i][j]=b[i][j];  
  196.         }  
  197.       }  
  198.      for(i=0;i<am;i++)  
  199.      {  
  200.         for(j=0;j<an;j++)  
  201.         {  
  202.             m=i;  
  203.             n=j;  
  204.             for(k=0,m=0;k<am-1;k++,m++)  
  205.             {  
  206.                 if(k==m)  
  207.                 {m++;}  
  208.                 for(l=0,n=0;l<am-1;l++,n++)  
  209.                 {  
  210.                     if(l==j)  
  211.                     {n++;}  
  212.                     c[k][l]=a[m][n];  
  213.                 }  
  214.             }  
  215.             d[i][j]=getdet(c,an-1,an-1)*pow(-1,i+j);  
  216.         }  
  217.      }  
  218.      x=getdet(a,an,am);  
  219.      if(x==0)  
  220.      {  
  221.          printf("error!can't get nijuzhen!\n");  
  222.      }  
  223.      else  
  224.      {  
  225.      for(i=0;i<am;i++)  
  226.      {  
  227.         for(j=0;j<an;j++)  
  228.         {  
  229.             d[i][j]=d[i][j]/x;  
  230.         }  
  231.       }  
  232.      printf("the nijuzhen of the matrix is:\n");  
  233.      printf("**********************************************\n");  
  234.      inver(d,an,an);  
  235.      }  
  236.     }  
  237. }  
  238.   
  239.   
  240. void multiply(double c[N][N],int am,int an)  
  241. {  
  242.     int i,j,k,bm,bn;  
  243.     double a[N][N],b[N][N],d[N][N];  
  244.     printf("please input the row and line of matrix B:\n");  
  245.     scanf("%d %d",&bm,&bn);  
  246.     if(an!=bm)  
  247.     {  
  248.         printf("error!\n");  
  249.     }  
  250.     else  
  251.     {  
  252.         printf("please input the matrix A:\n");  
  253.         for(i=0;i<bm;i++)  
  254.         {  
  255.             for(j=0;j<bn;j++)  
  256.             {  
  257.                 scanf("%lf",&b[i][j]);  
  258.             }  
  259.         }  
  260.         for(i=0;i<am;i++)  
  261.         {  
  262.             for(j=0;j<an;j++)  
  263.             {  
  264.                 a[i][j]=c[i][j];  
  265.             }  
  266.         }  
  267.         for(i=0;i<am;i++)  
  268.         {  
  269.             for(j=0;j<bn;j++)  
  270.             {  
  271.                 d[i][j]=0;  
  272.                 for(k=0;k<bn;k++)  
  273.                     d[i][j]+=a[i][k]*b[k][j];  
  274.             }  
  275.         }  
  276.         printf("the multiply of A and B is:\n");  
  277.         printf("**********************************************\n");  
  278.         for(i=0;i<am;i++)  
  279.         {  
  280.             for(j=0;j<bn;j++)  
  281.             {  
  282.                 printf("%10.4lf",d[i][j]);  
  283.             }  
  284.             printf("\n");  
  285.         }  
  286.     }  
  287. }  
  288.   
  289.   
  290.   
  291.   
  292. void main()  
  293. {   
  294.    int m,n,i,j,select;  
  295.    double a[N][N],detA;  
  296.    printf("please input the row and line of matrix A:\n");  
  297.    scanf("%d %d",&m,&n);  
  298.    printf("please input the matrix A:\n");  
  299.    for(i=0;i<m;i++)  
  300.    {  
  301.      for(j=0;j<n;j++)  
  302.      {  
  303.          scanf("%lf",&a[i][j]);  
  304.      }  
  305.    }  
  306.    do  
  307.    {  
  308.        printf("**********************************************\n");  
  309.        printf("1-->output\n2-->inver\n3-->get detmination\n4->get rank\n5-->get nijuzhen\n6->get multiply of two matrixs\n0-->exit!\n");  
  310.        printf("**********************************************\n");  
  311.        do  
  312.        {  
  313.            printf("Please input your choice:\n(Warming:Please input number from 0 to 6,or it will be error!)\n");  
  314.            scanf("%d",&select);  
  315.            if(select>6||select<0)  
  316.            {  
  317.                printf("Error!please input the number again!\n");  
  318.            }  
  319.        }while(select>6||select<0);  
  320.              switch(select)  
  321.              {  
  322.              case 1:  
  323.                  output(a,m,n);  
  324.                  break;  
  325.              case 2:  
  326.                  printf("\nThe InverseMatrix is:\n");  
  327.                  printf("**********************************************\n");  
  328.                  inver(a,m,n);  
  329.                  break;  
  330.              case 3:  
  331.                  detA=getdet(a,m,n);  
  332.                  printf("the detmination is %.4lf\n",detA);  
  333.                  break;  
  334.              case 4:  
  335.                  getrank(a,m,n);  
  336.                  break;  
  337.              case 5:  
  338.                  getnijuzhen(a,m,n);  
  339.                  break;  
  340.              case 6:  
  341.                  multiply(a,m,n);  
  342.              default:  
  343.                  break;  
  344.              }  
  345.          }while(select!=0);  
  346. }  
  347.   
  348.   
  349.      

抱歉!评论已关闭.