指针格式的矩阵乘法 [英] Matrix multiplication in pointer format

查看:66
本文介绍了指针格式的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用指针执行矩阵乘法?
参见代码scenerio.

How do I perform matrix multiplication using pointers?
See the code scenerio.

void tom::multiply(void*btr)
{
  short*X =(short*)btr;
  .
  .
  .
}


X是一个存储为16个元素的数组的4x4矩阵
int C[4][4]={1,1,1,1,1,-2,2,1,1,1,-1,-1,1,1,-1,-1};

我想要的是:CXC''其中C''表示C


X is a 4x4 matrix stored as an array of 16 elements
int C[4][4]={1,1,1,1,1,-2,2,1,1,1,-1,-1,1,1,-1,-1};

What I want is : CXC'' Where C'' Means transpose of C

推荐答案

Since


(CXC'') ik = C ij X jk C'' kl = C ij X jk C lk



我会这样:

Since


(CXC'')ik = CijXjkC''kl = CijXjkClk



I would do this way:

int C[]={1,1,1,1,1,-2,2,1,1,1,-1,-1,1,1,-1,-1};
// index {i,j} to offset inside the array
#define I(i,j)  (i)*4+(j)
// really I don't know why you would use 'void * btr'
void transform(int * btr)
{
  int tmp[16]; // backup of the source matrix
  memcpy(tmp,btr, 16*sizeof(int));
  int i,j,k,l;
  for (i=0; i<4; i++)
    for (l=0; l<4; l++)
    {
      btr[I(i,l)] = 0;
      for (j=0; j<4; j++)
        for (k=0; k<4; k++)
          btr[I(i,l)] += C[I(i,j)] * tmp[I(j,k)] * C[I(l,k)];
    }
}
int main()
{
  int X[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  transform(X);
}


这篇关于指针格式的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆