【问题标题】:Trouble with algorithm for multiplying matrix (c++)矩阵乘法算法的问题(c ++)
【发布时间】:2014-12-07 14:33:50
【问题描述】:

您好,我正在帮助我的一个朋友编写一个程序,该程序将两个上三角矩阵相乘而不展开矩阵。当我说不扩展矩阵时,我的意思是不使用零填充上三角矩阵的下部(目标是节省空间)。我正在从一个文件中读取矩阵,该文件只有矩阵的上三角值,而省略了下半部分。

我想要完成的是编写一个函数,它给定一个数组和一对索引,将返回一个扩展矩阵在该位置具有的元素(0 表示低于对角线,值高于对角线)。我正在考虑使用通常的矩阵乘法算法,该算法在需要访问元素时使用此函数。我已经为此工作了几个小时,但我无法想出一种方法将双矩阵索引 (i,j) (i 沿行)转换为单数组索引,反之亦然(提醒一下我' m 使用一维数组来存储上三角矩阵)。任何帮助将不胜感激!

【问题讨论】:

  • 对于j == i,主对角线上的元素位于0nn + (n-1)n + (n-1) + (n-2) 等等(找出ith 元素这个序列的一部分留给读者练习)。对于j > i,只需将j - i添加到之前的计算结果即可。

标签: c++ arrays matrix


【解决方案1】:

如果 p = &a[0][0] 则 a[i][j] 与 c++ 中的 *(p+i*row_size+j) 相同。 其中 p 是指向与矩阵元素相同类型的数据的指针。 我希望这是您想要的,并且您知道指针。 一切顺利。

【讨论】:

    【解决方案2】:

    您可以将算法划分为更小、更易于理解的块。

    // Get the array index given the rank, the row, and the column.
    int getArrayIndex(int rank, int row, int col)
    {
       return (row*rank + col - col*(col+1)/2);
    }
    
    // Get the term of a matrix, given the rank, the row, and the column.
    int getMatrixTerm(int a[], int rank, int row, int col)
    {
       if ( col < row )
       {
          return 0;
       }
       else
       {
          return a[getArrayIndex(rank, row, col)];
       }
    }
    
    // Get the term for a row and column resulting from mulitiplication.
    int getMultipliedTerm(int a[], int b[], int rank, int row, int col)
    {
       int term = 0;
       int k = 0;
       for ( ; k < rank; ++k )
       {
          term += getMatrixTerm(a, rank, row, k)*getMatrixTerm(b, rank, k, col);
       }
       return term;
    }
    
    // Set the term in c given the rank, the row, and the column.
    void setMultipliedTerm(int a[], int b[], int c[], int rank, int row, int col)
    {
       if ( j >= i )
       {
          c[getArrayIndex(rank, i, j)] = getMultipliedTerm(a, b, rank, i, j);
       }
    }
    
    // High level function to multiply two upper triangular matrices
    // The lower part of the matrix is not stored at all.
    void multiply(int a[], int b[], int c[], int rank)
    {
       int i = 0;
       int j = 0;
       for ( i = 0; i < rank; ++i )
       {
          for ( j = 0; j < rank; ++j )
          {
             setMultipliedTerm(a, b, c, rank, i, j);
          }
       }
    }
    

    【讨论】:

      【解决方案3】:
      // mat is an array containing the upper triangle data for a square matrix of size n
      // returns element at (i,j), or 0 for the lower triangle
      int getFromTriangle(const int* mat, int n, int i, int j)
      {
        if (i > j) {
          return 0; // lower triangle
        } else {
          return mat[j + (i*n) - i*(i+1)/2];
        }
      }
      

      if 子句负责处理下三角形。 else 子句计算数组索引如下:

      j + (i*n) - i*(i+1)/2
      

      这只是常规矩形矩阵索引函数减去正好是ith triangular number 的偏移量,因为在i 的任何行中,存储都省略了triangle(i) 元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-07
        相关资源
        最近更新 更多