复数矩阵乘法C语言实现

来至http://blog.csdn.net/gtatcs/article/details/8887082;

下面代码参考TI的实现:

[cpp]  view plain copy
  1. <span style="font-size:14px;">/* NAME                                                                    */  
  2. /*     DSPF_dp_mat_mul_cplx -- Complex matrix multiplication                    */  
  3. /*                                                                         */  
  4. /*  USAGE                                                                   */  
  5. /*                                                                          */  
  6. /*       This routine has the following C prototype:                        */  
  7. /*                                                                          */  
  8. /*       void DSPF_dp_mat_mul_cplx(                                              */  
  9. /*                              const double* x,                            */  
  10. /*                              int r1,                                     */  
  11. /*                              int c1,                                     */  
  12. /*                              const double* y,                            */  
  13. /*                              int c2,                                     */  
  14. /*                              double* restrict r                          */  
  15. /*                           )                                              */  
  16. /*                                                                          */  
  17. /*            x[2*r1*c1]:   Input matrix containing r1*c1 complex           */  
  18. /*                          floating point numbers having r1 rows and c1    */  
  19. /*                          columns of complex numbers.                     */  
  20. /*            r1        :   No. of rows in matrix x.                        */  
  21. /*            c1        :   No. of columns in matrix x.                     */  
  22. /*                          Also no. of rows in matrix y.                   */  
  23. /*            y[2*c1*c2]:   Input matrix containing c1*c2 complex           */  
  24. /*                          floating point numbers having c1 rows and c2    */  
  25. /*                          columns of complex numbers.                     */  
  26. /*            c2        :   No. of columns in matrix y.                     */  
  27. /*            r[2*r1*c2]:   Output matrix of c1*c2 complex floating         */  
  28. /*                          point numbers having c1 rows and c2 columns of  */  
  29. /*                          complex numbers.                                */  
  30. /*                                                                          */  
  31. /*                          Complex numbers are stored consecutively with   */  
  32. /*                          real values stored in even positions and        */  
  33. /*                          imaginary values in odd positions.              */  
  34. /*                                                                          */  
  35. /*  DESCRIPTION                                                             */  
  36. /*                                                                          */  
  37. /*        This function computes the expression "r = x * y" for the         */  
  38. /*        matrices x and y. The columnar dimension of x must match the row  */  
  39. /*        dimension of y. The resulting matrix has the same number of rows  */  
  40. /*        as x and the same number of columns as y.                         */  
  41. /*                                                                          */  
  42. /*        Each element of the matrix is assumed to be complex numbers with  */  
  43. /*        Real values are stored in even positions and imaginary            */  
  44. /*        values in odd positions.                                          */  
  45. /*                                                                          */  
  46. /*  TECHNIQUES                                                              */  
  47. /*                                                                          */  
  48. /*        1. Innermost loop is unrolled twice.                              */  
  49. /*        2. Outermost loop is executed in parallel with innner loops.      */  
  50. /*                                                                          */  
  51. /*  ASSUMPTIONS                                                             */  
  52. /*                                                                          */  
  53. /*        1. r1,r2>=1,c1 should be a multiple of 2 and >=2.                 */  
  54. /*        2. x should be padded with 6 words                                */  
  55. /*                                                                          */  
  56. /*                                                                          */  
  57. /*  C CODE                                                                  */  
  58. /*                                                                          */  
  59. void DSPF_dp_mat_mul_cplx(const double* x, int r1, int c1,   
  60.                           const double* y, int c2, double* r)  
  61. {                                                                   
  62.     double real, imag;                                              
  63.     int i, j, k;                                                    
  64.                                                         
  65.     for(i = 0; i < r1; i++)                                        
  66.     {                                                               
  67.         for(j = 0; j < c2; j++)                                       
  68.         {                                                             
  69.             real=0;                                                     
  70.             imag=0;                                                     
  71.                                                                  
  72.             for(k = 0; k < c1; k++)                                     
  73.             {                                                           
  74.                 real += (x[i*2*c1 + 2*k]*y[k*2*c2 + 2*j]                    
  75.                 -x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j + 1]);                
  76.                                                                      
  77.                 imag+=(x[i*2*c1 + 2*k] * y[k*2*c2 + 2*j + 1]               
  78.                 + x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j]);                
  79.             }                                                           
  80.             r[i*2*c2 + 2*j] = real;                                    
  81.             r[i*2*c2 + 2*j + 1] = imag;    
  82.         }  
  83.     }  
  84. }    
  85. /*  NOTES                                                                   */  
  86. /*                                                                          */  
  87. /*        1. Real values are stored in even word positions and imaginary    */  
  88. /*           values in odd positions.                                       */  
  89. /*        2. Endian: This code is LITTLE ENDIAN.                            */  
  90. /*        3. Interruptibility: This code is interrupt tolerant but not      */  
  91. /*           interruptible.                                                 */  
  92. /*                                                                          */  
  93. /*  CYCLES                                                                  */  
  94. /*                                                                          */  
  95. /*        8*r1*c1*c2'+ 18*(r1*c2)+40 WHERE c2'=2*ceil(c2/2)                 */  
  96. /*        When r1=3, c1=4, c2=4, cycles = 640                               */  
  97. /*        When r1=4, c1=4, c2=5, cycles = 1040                              */  
  98. /*                                                                          */  
  99. /*  CODESIZE                                                                */  
  100. /*                                                                          */  
  101. /*        832 bytes                                                         */  
  102. /* ------------------------------------------------------------------------ */  
  103. /*            Copyright (c) 2004 Texas Instruments, Incorporated.           */  
  104. /*                           All Rights Reserved.                           */  
  105. /* ======================================================================== */  
  106. #ifndef DSPF_DP_MAT_MUL_CPLX_H_  
  107. #define DSPF_DP_MAT_MUL_CPLX_H_ 1  
  108.   
  109. void DSPF_dp_mat_mul_cplx(  
  110.                        const double* x,  
  111.                        int r1,  
  112.                        int c1,  
  113.                        const double* y,  
  114.                        int c2,  
  115.                        double* restrict r  
  116.                     );  
  117.   
  118. #endif  
  119. /* ======================================================================== */  
  120. /*  End of file:  DSPF_dp_mat_mul_cplx.h                                         */  
  121. /* ------------------------------------------------------------------------ */</span>  
[cpp]  view plain copy
  1. <span style="font-size:14px;">  
  2. </span>  
[cpp]  view plain copy
  1. <span style="font-size:14px;">测试程序如下:</span>  
[cpp]  view plain copy
  1. <span style="font-size:14px;">(说明:下面代码分别包含了三个矩阵相乘的函数)</span>  
[cpp]  view plain copy
  1. <pre name="code" class="cpp"><span style="font-size:14px;">#include <stdio.h></span></pre><pre name="code" class="cpp"><span style="font-size:14px;">#include <stdlib.h>  
  2. #include "complex.h"  
  3.   
  4. void matrix_mul(  
  5.                      const double* x,  
  6.                      int r1,  
  7.                      int c1,  
  8.                      const double* y,  
  9.                      int c2,  
  10.                      double* r  
  11.                     )  
  12. {  
  13.     int i,j,k;  
  14.     for(i=0;i<r1;++i)  
  15.     {  
  16.         for(j=0;j<c2;++j)  
  17.         {  
  18.             for(k=0;k<c1;++k)  
  19.             {  
  20.                 //r[i*r1 + j] =r[i*r1 + j] + x[i*r1 + k] * y[k*c1 + j];  
  21.                 r[i*c2 + j] =r[i*c2 + j] + x[i*c1 + k] * y[k*c2 + j];  
  22.             }  
  23.         }  
  24.     }  
  25. }  
  26.   
  27. void matrix_mul_cplx(  
  28.                 const Complex* x,  
  29.                 int r1,  
  30.                 int c1,  
  31.                 const Complex* y,  
  32.                 int c2,  
  33.                 Complex* r  
  34.                 )  
  35. {  
  36.     int i,j,k;  
  37.     Complex temp;  
  38.     temp.real = 0;  
  39.     temp.image = 0;  
  40.     for(i=0;i<r1;++i)  
  41.     {  
  42.         for(j=0;j<c2;++j)  
  43.         {  
  44.             for(k=0;k<c1;++k)  
  45.             {  
  46.                 //r[i*r1 + j] =r[i*r1 + j] + x[i*r1 + k] * y[k*c1 + j];  
  47.                 temp = complex_mul(x[i*c1 + k], y[k*c2 + j]);  
  48.                 r[i*c2 + j] =complex_add(r[i*c2 + j], temp);  
  49.             }  
  50.         }  
  51.     }  
  52. }  
  53.   
  54. void DSPF_dp_mat_mul_cplx(const double* x, int r1, int c1,   
  55.                           const double* y, int c2, double* r)  
  56. {                                                                   
  57.     double real, imag;                                              
  58.     int i, j, k;                                                    
  59.                                                         
  60.     for(i = 0; i < r1; i++)                                        
  61.     {                                                               
  62.         for(j = 0; j < c2; j++)                                       
  63.         {                                                             
  64.             real=0;                                                     
  65.             imag=0;                                                     
  66.                                                                  
  67.             for(k = 0; k < c1; k++)                                     
  68.             {                                                           
  69.                 real += (x[i*2*c1 + 2*k]*y[k*2*c2 + 2*j]                    
  70.                 -x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j + 1]);                
  71.                                                                      
  72.                 imag+=(x[i*2*c1 + 2*k] * y[k*2*c2 + 2*j + 1]               
  73.                 + x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j]);                
  74.             }                                                           
  75.             r[i*2*c2 + 2*j] = real;                                    
  76.             r[i*2*c2 + 2*j + 1] = imag;    
  77.         }  
  78.     }  
  79. }       
  80.           
  81. int main()  
  82. {  
  83.     //matrix_mul test  
  84.     /* 
  85.     double x[9] = {1,2,3,4,5,6,7,8,9};  //矩阵按行存放 
  86.     int r1 = 3, c1 = 3,c2 = 1; 
  87.     double y[3] = {10,11,12}; 
  88.     double r[3] = {0}; 
  89.     int i,j; 
  90.     //printf("r  :  %lf\n%lf\n",r[0],r[1]); 
  91.  
  92.     printf("r = : \n"); 
  93.     for(i=0;i<r1;++i) 
  94.     { 
  95.         for(j=0;j<c2;++j) 
  96.         { 
  97.             //printf("%lf\t",r[i*r1 + j]); 
  98.             printf("%lf\t",r[i*c2 + j]); 
  99.         } 
  100.         printf("\n"); 
  101.     } 
  102.  
  103.     matrix_mul(x,r1,c1,y,c2,r); 
  104.     printf("x*y = : \n"); 
  105.     for(i=0;i<r1;++i) 
  106.     { 
  107.         for(j=0;j<c2;++j) 
  108.         { 
  109.             printf("%lf\t",r[i*c2 + j]); 
  110.         } 
  111.         printf("\n"); 
  112.     } 
  113. */  
  114.     /* 
  115.     //matrix_mul_cplx test 
  116.     Complex x[4] = {{1,2}, {3,4}, {5,6}, {7,8}};    //矩阵按行存放 
  117.     int r1 = 2, c1 = 2,c2 = 1; 
  118.     Complex y[2] = {{1,2}, {3,4}}; 
  119.     Complex r[2] = {{0,0}, {0,0}}; 
  120.     int i,j; 
  121.     //printf("r  :  %lf\n%lf\n",r[0],r[1]); 
  122.      
  123.     printf("r = : \n"); 
  124.     for(i=0;i<r1;++i) 
  125.     { 
  126.         for(j=0;j<c2;++j) 
  127.         { 
  128.             printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image); 
  129.         } 
  130.         printf("\n"); 
  131.     } 
  132.      
  133.     matrix_mul_cplx(x,r1,c1,y,c2,r); 
  134.     printf("x*y = : \n"); 
  135.     for(i=0;i<r1;++i) 
  136.     { 
  137.         for(j=0;j<c2;++j) 
  138.         { 
  139.             printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image); 
  140.         } 
  141.         printf("\n"); 
  142.     } 
  143.     */  
  144.     //DSPF_dp_mat_mul_cplx test  
  145.     Complex x[4] = {{1,2}, {3,4}, {5,6}, {7,8}};    //矩阵按行存放  
  146.     int r1 = 2, c1 = 2,c2 = 1;  
  147.     Complex y[2] = {{1,2}, {3,4}};  
  148.     Complex r[2] = {{0,0}, {0,0}};  
  149.     int i,j;  
  150.     //printf("r  :  %lf\n%lf\n",r[0],r[1]);  
  151.       
  152.     printf("r = : \n");  
  153.     for(i=0;i<r1;++i)  
  154.     {  
  155.         for(j=0;j<c2;++j)  
  156.         {  
  157.             printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);  
  158.         }  
  159.         printf("\n");  
  160.     }  
  161.       
  162.     DSPF_dp_mat_mul_cplx((double*)x, r1,c1, (double*)y, c2,  (double*)r);  
  163.   
  164.     printf("x*y = : \n");  
  165.     for(i=0;i<r1;++i)  
  166.     {  
  167.         for(j=0;j<c2;++j)  
  168.         {  
  169.             printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);  
  170.         }  
  171.         printf("\n");  
  172.     }  
  173.   
  174.     return 0;  
  175. }  
  176. </span></pre><pre name="code" class="cpp"></pre><span style="font-size:14px"><br>  
  177. </span><p></p>  
  178. <pre></pre>  
  179. <span style="font-size:14px"><br>  
  180. </span>  
  181. <p></p>  
  182. <p><span style="font-size:14px"><br>  
  183. </span></p>  
  184. <p><br>  
  185. </p>  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值