116.复矩阵乘法

举报
C语言与CPP编程 发表于 2022/05/09 22:49:18 2022/05/09
【摘要】 #include "stdio.h"#define MAX 255 void CMatrixMul(ar,ai,br,bi,m,n,k,cr,ci) /*复矩阵相乘*/int m,n,k; /*m是矩阵A的行数,n是矩阵B的行数,k是矩阵B的列数*//*ar,br,cr分别是矩阵A,B,C的实部,ai,bi,ci分别是矩阵A,B, C的...

  
  1. #include "stdio.h"
  2. #define MAX 255
  3. void CMatrixMul(ar,ai,br,bi,m,n,k,cr,ci) /*复矩阵相乘*/
  4. int m,n,k; /*m是矩阵A的行数,n是矩阵B的行数,k是矩阵B的列数*/
  5. /*ar,br,cr分别是矩阵A,B,C的实部,ai,bi,ci分别是矩阵A,B, C的虚部*/
  6. double ar[],ai[],br[],bi[],cr[],ci[];
  7. {
  8. int i,j,l,u,v,w;
  9. double p,q,s;
  10. for (i=0; i<=m-1; i++)
  11. for (j=0; j<=k-1; j++)
  12. {
  13. u=i*k+j;
  14. cr[u]=0.0; ci[u]=0.0;
  15. for (l=0; l<=n-1; l++)
  16. {
  17. v=i*n+l; w=l*k+j;
  18. p=ar[v]*br[w];
  19. q=ai[v]*bi[w];
  20. s=(ar[v]+ai[v])*(br[w]+bi[w]);
  21. cr[u]=cr[u]+p-q;
  22. ci[u]=ci[u]+s-p-q;
  23. }
  24. }
  25. return;
  26. }
  27. print_matrix(A,m,n)/*打印的矩阵A(m*n)的元素*/
  28. int m,n; /*矩阵的阶数*/
  29. double A[]; /*矩阵A*/
  30. {
  31. int i,j;
  32. for (i=0; i<m; i++)
  33. {
  34. for (j=0; j<n; j++)
  35. printf("%13.7f\t",A[i*n+j]);
  36. printf("\n");
  37. }
  38. }
  39. main()
  40. {
  41. int i,j,n,m,k;
  42. double Ar[MAX],Br[MAX],Cr[MAX],Ai[MAX],Bi[MAX],Ci[MAX];
  43. static double cr[3][4],ci[3][4];
  44. static double ar[3][4]={ {1.0,2.0,3.0,-2.0}, /*矩阵A的实部*/
  45. {1.0,5.0,1.0,3.0},
  46. {0.0,4.0,2.0,-1.0}};
  47. static double ai[3][4]={ {1.0,-1.0,2.0,1.0}, /*矩阵A的虚部*/
  48. {-1.0,-1.0,2.0,0.0},
  49. {-3.0,-1.0,2.0,2.0}};
  50. static double br[4][4]={ {1.0,4.0,5.0,-2.0}, /*矩阵B的实部*/
  51. {3.0,0.0,2.0,-1.0},
  52. {6.0,3.0,1.0,2.0},
  53. {2.0,-3.0,-2.0,1.0}};
  54. static double bi[4][4]={ {-1.0,-1.0,1.0,1.0}, /*矩阵B的虚部*/
  55. {2.0,1.0,0.0,5.0},
  56. {-3.0,2.0,1.0,-1.0},
  57. {-1.0,-2.0,1.0,-2.0}};
  58. clrscr();
  59. puts("**********************************************************");
  60. puts("* This is a complex-matrix-multiplication program. *");
  61. puts("* It calculate the two matrixes C(m*k)=A(m*n)B(n*k). *");
  62. puts("**********************************************************");
  63. printf(" >> Please input the number of rows in A, m= ");
  64. scanf("%d",&m);
  65. printf(" >> Please input the number of cols in A, n= ");
  66. scanf("%d",&n);
  67. printf(" >> Please input the number of cols in B, k= ");
  68. scanf("%d",&k);
  69. printf(" >> Please input the %d elements in Ar one by one:\n >> ",m*n);
  70. for(i=0;i<m*n;i++)
  71. scanf("%lf",&Ar[i]);
  72. printf(" >> Please input the %d elements in Ai one by one:\n >> ",m*n);
  73. for(i=0;i<m*n;i++)
  74. scanf("%lf",&Ai[i]);
  75. printf(" >> Please input the %d elements in Br one by one:\n >> ",n*k);
  76. for(i=0;i<n*k;i++)
  77. scanf("%lf",&Br[i]);
  78. printf(" >> Please input the %d elements in Bi one by one:\n >> ",n*k);
  79. for(i=0;i<n*k;i++)
  80. scanf("%lf",&Bi[i]);
  81. CMatrixMul(Ar,Ai,Br,Bi,m,n,k,Cr,Ci); /*进行计算*/
  82. /*输出乘积结果的实部*/
  83. printf(" Real part of C(%d*%d)=A(%d*%d)*B(%d*%d):\n",m,k,m,n,n,k);
  84. print_matrix(Cr,m,k);
  85. /*输出乘积结果的虚部*/
  86. printf(" Complex part of C(%d*%d)=A(%d*%d)*B(%d*%d):\n",m,k,m,n,n,k);
  87. print_matrix(Ci,m,k);
  88. printf(" Press any key to quit...");
  89. getch();
  90. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124655254

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。