复数矩阵乘法C语言实现
2013-05-05 16:25
417 查看
下面代码参考TI的实现:
/* NAME */ /* DSPF_dp_mat_mul_cplx -- Complex matrix multiplication */ /* */ /* USAGE */ /* */ /* This routine has the following C prototype: */ /* */ /* void DSPF_dp_mat_mul_cplx( */ /* const double* x, */ /* int r1, */ /* int c1, */ /* const double* y, */ /* int c2, */ /* double* restrict r */ /* ) */ /* */ /* x[2*r1*c1]: Input matrix containing r1*c1 complex */ /* floating point numbers having r1 rows and c1 */ /* columns of complex numbers. */ /* r1 : No. of rows in matrix x. */ /* c1 : No. of columns in matrix x. */ /* Also no. of rows in matrix y. */ /* y[2*c1*c2]: Input matrix containing c1*c2 complex */ /* floating point numbers having c1 rows and c2 */ /* columns of complex numbers. */ /* c2 : No. of columns in matrix y. */ /* r[2*r1*c2]: Output matrix of c1*c2 complex floating */ /* point numbers having c1 rows and c2 columns of */ /* complex numbers. */ /* */ /* Complex numbers are stored consecutively with */ /* real values stored in even positions and */ /* imaginary values in odd positions. */ /* */ /* DESCRIPTION */ /* */ /* This function computes the expression "r = x * y" for the */ /* matrices x and y. The columnar dimension of x must match the row */ /* dimension of y. The resulting matrix has the same number of rows */ /* as x and the same number of columns as y. */ /* */ /* Each element of the matrix is assumed to be complex numbers with */ /* Real values are stored in even positions and imaginary */ /* values in odd positions. */ /* */ /* TECHNIQUES */ /* */ /* 1. Innermost loop is unrolled twice. */ /* 2. Outermost loop is executed in parallel with innner loops. */ /* */ /* ASSUMPTIONS */ /* */ /* 1. r1,r2>=1,c1 should be a multiple of 2 and >=2. */ /* 2. x should be padded with 6 words */ /* */ /* */ /* C CODE */ /* */ void DSPF_dp_mat_mul_cplx(const double* x, int r1, int c1, const double* y, int c2, double* r) { double real, imag; int i, j, k; for(i = 0; i < r1; i++) { for(j = 0; j < c2; j++) { real=0; imag=0; for(k = 0; k < c1; k++) { real += (x[i*2*c1 + 2*k]*y[k*2*c2 + 2*j] -x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j + 1]); imag+=(x[i*2*c1 + 2*k] * y[k*2*c2 + 2*j + 1] + x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j]); } r[i*2*c2 + 2*j] = real; r[i*2*c2 + 2*j + 1] = imag; } } } /* NOTES */ /* */ /* 1. Real values are stored in even word positions and imaginary */ /* values in odd positions. */ /* 2. Endian: This code is LITTLE ENDIAN. */ /* 3. Interruptibility: This code is interrupt tolerant but not */ /* interruptible. */ /* */ /* CYCLES */ /* */ /* 8*r1*c1*c2'+ 18*(r1*c2)+40 WHERE c2'=2*ceil(c2/2) */ /* When r1=3, c1=4, c2=4, cycles = 640 */ /* When r1=4, c1=4, c2=5, cycles = 1040 */ /* */ /* CODESIZE */ /* */ /* 832 bytes */ /* ------------------------------------------------------------------------ */ /* Copyright (c) 2004 Texas Instruments, Incorporated. */ /* All Rights Reserved. */ /* ======================================================================== */ #ifndef DSPF_DP_MAT_MUL_CPLX_H_ #define DSPF_DP_MAT_MUL_CPLX_H_ 1 void DSPF_dp_mat_mul_cplx( const double* x, int r1, int c1, const double* y, int c2, double* restrict r ); #endif /* ======================================================================== */ /* End of file: DSPF_dp_mat_mul_cplx.h */ /* ------------------------------------------------------------------------ */
测试程序如下:
(说明:下面代码分别包含了三个矩阵相乘的函数)
#include <stdio.h>
#include <stdlib.h>#include "complex.h"void matrix_mul(const double* x,int r1,int c1,const double* y,int c2,double* r){int i,j,k;for(i=0;i<r1;++i){for(j=0;j<c2;++j){for(k=0;k<c1;++k){//r[i*r1 + j] =r[i*r1 + j] + x[i*r1 + k] * y[k*c1 + j];r[i*c2 + j] =r[i*c2 + j] + x[i*c1 + k] * y[k*c2 + j];}}}}void matrix_mul_cplx(const Complex* x,int r1,int c1,const Complex* y,int c2,Complex* r){int i,j,k;Complex temp;temp.real = 0;temp.image = 0;for(i=0;i<r1;++i){for(j=0;j<c2;++j){for(k=0;k<c1;++k){//r[i*r1 + j] =r[i*r1 + j] + x[i*r1 + k] * y[k*c1 + j];temp = complex_mul(x[i*c1 + k], y[k*c2 + j]);r[i*c2 + j] =complex_add(r[i*c2 + j], temp);}}}}void DSPF_dp_mat_mul_cplx(const double* x, int r1, int c1,const double* y, int c2, double* r){double real, imag;int i, j, k;for(i = 0; i < r1; i++){for(j = 0; j < c2; j++){real=0;imag=0;for(k = 0; k < c1; k++){real += (x[i*2*c1 + 2*k]*y[k*2*c2 + 2*j]-x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j + 1]);imag+=(x[i*2*c1 + 2*k] * y[k*2*c2 + 2*j + 1]+ x[i*2*c1 + 2*k + 1] * y[k*2*c2 + 2*j]);}r[i*2*c2 + 2*j] = real;r[i*2*c2 + 2*j + 1] = imag;}}}int main(){//matrix_mul test/*double x[9] = {1,2,3,4,5,6,7,8,9}; //矩阵按行存放int r1 = 3, c1 = 3,c2 = 1;double y[3] = {10,11,12};double r[3] = {0};int i,j;//printf("r : %lf\n%lf\n",r[0],r[1]);printf("r = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){//printf("%lf\t",r[i*r1 + j]);printf("%lf\t",r[i*c2 + j]);}printf("\n");}matrix_mul(x,r1,c1,y,c2,r);printf("x*y = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){printf("%lf\t",r[i*c2 + j]);}printf("\n");}*//*//matrix_mul_cplx testComplex x[4] = {{1,2}, {3,4}, {5,6}, {7,8}}; //矩阵按行存放int r1 = 2, c1 = 2,c2 = 1;Complex y[2] = {{1,2}, {3,4}};Complex r[2] = {{0,0}, {0,0}};int i,j;//printf("r : %lf\n%lf\n",r[0],r[1]);printf("r = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);}printf("\n");}matrix_mul_cplx(x,r1,c1,y,c2,r);printf("x*y = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);}printf("\n");}*///DSPF_dp_mat_mul_cplx testComplex x[4] = {{1,2}, {3,4}, {5,6}, {7,8}}; //矩阵按行存放int r1 = 2, c1 = 2,c2 = 1;Complex y[2] = {{1,2}, {3,4}};Complex r[2] = {{0,0}, {0,0}};int i,j;//printf("r : %lf\n%lf\n",r[0],r[1]);printf("r = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);}printf("\n");}DSPF_dp_mat_mul_cplx((double*)x, r1,c1, (double*)y, c2, (double*)r);printf("x*y = : \n");for(i=0;i<r1;++i){for(j=0;j<c2;++j){printf("%lf + j*%lf\t",r[i*c2 + j].real , r[i*c2 + j].image);}printf("\n");}return 0;}
相关文章推荐
- 复数矩阵乘法C语言实现
- 转置矩阵的分块并行乘法(C语言实现),计算矩阵C[rawn][rawn]=A[rawm][rawn]'*B[rawm][rawn],子块大小为S*T,其算法实现原理参加本代码的附件。
- C语言—用malloc函数实现任意阶矩阵的乘法
- C语言之两矩阵乘法的实现
- C语言实现的矩阵乘法运算器
- C语言实现矩阵乘法
- C语言实现矩阵乘法
- PAT Basic 1051. 复数乘法 (15)(C语言实现)
- C语言实现矩阵乘法(4*5乘以5*3)
- 矩阵乘法的C语言实现
- MapReduce实现矩阵乘法
- 复数乘法运算(三次实数乘法)-c++代码实现及运行实例结果
- C语言实现9-9乘法表
- C++实现矩阵链乘法利用动态规划及运行实例结果
- 分享矩阵乘法单线程与多线程的Java实现与效率对比,请教Strassen算法
- hadoop实现稀疏矩阵乘法
- 矩阵乘法的算法实现
- 矩阵乘法的实现-将2维数组作函数的参数
- 实现两个N×N矩阵的乘法,矩阵由一维数组表示
- 复数乘法中的结构体赋值实现代码