![]() |
#2
rjsp2020-06-20 22:18
|
这是我写的宏定义函数实现的矩阵乘法

#include <stdio.h>
#define MATRIX_PRINT(mtx) matrix_print( sizeof(mtx)/sizeof(*(mtx)), sizeof(*(mtx))/sizeof(**(mtx)), (mtx) )
#define MATRIX_MUT(a,b,c) matrix_mut(sizeof(a)/sizeof(*(a)),sizeof(*(a))/sizeof(**(a)),sizeof(*(b))/sizeof(**(b)),(a),(b),(c))
void matrix_print( size_t row, size_t col, float mtx[row][col] )
{
size_t r=0,c=0;
for( r=0; r<row; r++)
{
for( c=0; c<col; c++ )
{
printf( "%f ", mtx[r][c]);
}
}
}
void matrix_mut(size_t row,size_t med,size_t col,float a[row][med],float b[med][col],float c[row][col])
{
size_t i,j,l;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
float sum=0;
for(l=0;l<med;l++)
{
sum=sum+a[i][l]*b[l][j];
}
c[i][j]=sum;
}
}
}
int main( void )
{
float a[2][3] = { {1,2,3}, {1,5,2} };
float b[3][2]={1,2,2,1,3,1};
float d[2][2];
MATRIX_MUT(a,b,d);
MATRIX_PRINT( d );
return 0;
}
#define MATRIX_PRINT(mtx) matrix_print( sizeof(mtx)/sizeof(*(mtx)), sizeof(*(mtx))/sizeof(**(mtx)), (mtx) )
#define MATRIX_MUT(a,b,c) matrix_mut(sizeof(a)/sizeof(*(a)),sizeof(*(a))/sizeof(**(a)),sizeof(*(b))/sizeof(**(b)),(a),(b),(c))
void matrix_print( size_t row, size_t col, float mtx[row][col] )
{
size_t r=0,c=0;
for( r=0; r<row; r++)
{
for( c=0; c<col; c++ )
{
printf( "%f ", mtx[r][c]);
}
}
}
void matrix_mut(size_t row,size_t med,size_t col,float a[row][med],float b[med][col],float c[row][col])
{
size_t i,j,l;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
float sum=0;
for(l=0;l<med;l++)
{
sum=sum+a[i][l]*b[l][j];
}
c[i][j]=sum;
}
}
}
int main( void )
{
float a[2][3] = { {1,2,3}, {1,5,2} };
float b[3][2]={1,2,2,1,3,1};
float d[2][2];
MATRIX_MUT(a,b,d);
MATRIX_PRINT( d );
return 0;
}