算法背景:
①稀疏矩阵的类表示法:
template <class T>
class sparseMatrix
{
private:
int rows;
int cols; //这里的rows和cols存的是矩阵理论有的行列数
matrixTerm<T>* terms; //这里的terms存的是实际的矩阵结构
//每一个空间存的都是行数、列数、相应的元素值
int size;
public:
sparseMatrix() : rows(0), cols(0), size(0) {
}
sparseMatrix(int row, int col) : rows(row), cols(col), size(0) {
}
void multiply(const sparseMatrix<T>& m);
sparseMatrix<T> operator=(const sparseMatrix<T>& m);
};
②三元组表的结构体表示法:
template <class T>
struct matrixTerm
{
int row;
int col;
T value;
operator T() const {
return value; }
};
主要算法:行逻辑链接法。前提条件是三元组表按照行主索引法进行映射。同稀疏矩阵的转置操作一样,乘法操作也需要定义两个辅助的数组row_nums[i]和row_first_position[i],分别表示矩阵A第i行的非零元素数目和第i行第1个非零元素所在三元组表中对应的索引。
int* row_nums = new int[rows + 1]; //各行的元素数目
int* row_first_position = new int[rows + 1]; //各行第一个非零元对应的索引
对数组row_nums的赋值如下:
for (int i = 0; i <= rows; i++)
row_nums[i] = 0

本文解析了如何使用行逻辑链接法计算稀疏矩阵乘法,通过三元组表和辅助数组row_nums和row_first_position,实现高效地遍历和元素相乘。算法涉及行主索引映射,适用于大规模稀疏矩阵的乘法运算。
&spm=1001.2101.3001.5002&articleId=109233447&d=1&t=3&u=39ce1cc575654dd585fdec53fa09d61d)
1842

被折叠的 条评论
为什么被折叠?



