使用对的矩阵乘法

发布于 2024-08-27 09:59:33 字数 539 浏览 4 评论 0 原文

我正在研究进行矩阵乘法的替代方法。我没有将矩阵存储为二维数组,而是使用向量来

vector<pair<pair<int,int >,int > > 

存储矩阵。我的对 (pair) 中的对存储我的索引 (i,j),另一个 int 存储给定 (i,j) 对的值。我想我可能会幸运地以这种方式实现我的稀疏数组。

问题是当我尝试将该矩阵与其自身相乘时。

如果这是一个二维数组实现,我会按如下方式乘以矩阵:

   for(i=0; i<row1; i++)
    {
        for(j=0; j<col1; j++)
        {
          C[i][j] = 0;
         for(k=0; k<col2; k++) 
           C[i][j] += A[i][j] * A[j][k];
      }
    }

有人可以指出一种使用我的“对对”向量实现相同结果的方法吗?

谢谢

I am looking into alternate ways to do a Matrix Multiplication. Instead of storing my matrix as a two-dimensional array, I am using a vector such as

vector<pair<pair<int,int >,int > > 

to store my matrix. The pair within my pair (pair) stores my indices (i,j) and the other int stores the value for the given (i,j) pair. I thought I might have some luck implementing my sparse array this way.

The problem is when I try to multiply this matrix with itself.

If this was a 2-d array implementation, I would have multiplied the matrix as follows:

   for(i=0; i<row1; i++)
    {
        for(j=0; j<col1; j++)
        {
          C[i][j] = 0;
         for(k=0; k<col2; k++) 
           C[i][j] += A[i][j] * A[j][k];
      }
    }

Can somebody point out a way to achieve the same result using my vector of 'pair of pairs'?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-09-03 09:59:33

到目前为止,您可以在一个位置存储一个值。如果要在矩阵中存储多个非零条目,则需要在更大的结构中添加更多对。

map, int> 将是下一个逻辑步骤。现在您可以迭代行,因为first 坐标在map 的排序顺序中更重要。

要迭代列,请颠倒该优先级:

typedef pair<int, int> mx_coord;
struct reverse_compare {
    bool operator() (mx_coord const &l, mx_coord const &r) const
        { return l.second < r.second? true :
                 r.second < l.second? false : l.first < r.first; }
};

typedef map< mx_coord, int > matrix;
typedef map< mx_coord, int, reverse_compare > matrix_transpose;

要将 A 乘以 B,请转置 B 并迭代 A 和 B,将不太重要的坐标匹配的任何元素相乘,因为顺序自然允许您逐行和逐列进行-柱子。

转置 B:

matrix_transpose b_t( b.begin(), b.end() ); // complexity: n log n

So far you can store one value at one location. If you want to store several nonzero entries in the matrix, you will need more pairs of pairs in a larger structure.

map<pair<int, int>, int> would be the next logical step. Now you can iterate over rows because the first coordinate is more significant in the map's sorting order.

To iterate over columns, reverse that precedence:

typedef pair<int, int> mx_coord;
struct reverse_compare {
    bool operator() (mx_coord const &l, mx_coord const &r) const
        { return l.second < r.second? true :
                 r.second < l.second? false : l.first < r.first; }
};

typedef map< mx_coord, int > matrix;
typedef map< mx_coord, int, reverse_compare > matrix_transpose;

To multiply A by B, transpose B and iterate over A and B, multiplying any elements whose less-significant coordinates match, as the ordering naturally lets you go line-by-line and column-by-column.

To transpose B:

matrix_transpose b_t( b.begin(), b.end() ); // complexity: n log n
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文