Math(1)---Eigen稀疏矩阵乘法

Eigen稀疏矩阵乘法

  • 稀疏矩阵能够节省存储空间;
  • Eigen中稀疏矩阵的存储方式:CRS, CCS;
  • 稀疏矩阵缩短遍历元素的时间。

Eigen稀疏矩阵乘以向量

  • 计算公式: \(MatResult = Matsparse*Vec\)
  • 利用Eigen可以直接根据公式计算出来,但是笔者想弄楚,Eigen是怎样实现的,于是用迭代法实现计算

示例:

#include <Eigen/Sparse>
#include <vector>
#include <iostream>

using namespace Eigen;
using namespace std;

SparseMatrix<int,RowMajor> SpMat(5,5);
int main(void)
{
	MatrixXi A(5,5);
	VectorXi V(5);
	VectorXi buffer(5);
	VectorXi Ibus(5);
	buffer << 0, 0, 0, 0, 0;
	Ibus << 0, 0, 0, 0, 0;
	V << 1, 2, 3, 4, 5;
	A << 0, 3, 0, 0, 0,
		22, 0, 0, 0, 17,
		7, 5, 0, 1, 0,
		0, 0, 0, 0, 0,
		0, 0, 14, 0, 8;
	SpMat = A.sparseView();
	
	for (int k = 0; k < SpMat.outerSize(); ++k)
	{
		for (SparseMatrix<int, RowMajor>::InnerIterator it(SpMat, k); it; ++it)
		{
			//cout<<it.value()<<endl;  //Eigen 存储稀疏矩阵的值,默认是CCS,这里指定成了CRS
			//cout << it.row() << endl; //Eigen 行索引
			//cout << it.col() << endl; //Eigen 列索引
			//cout << it.index() << endl; //这里index=it.col()

			buffer(k) += it.value()*V(it.index());
		}
		Ibus(k) += buffer(k);
	}

	cout << SpMat << endl;
	cout <<	Ibus << endl;
	return 0;
}
上一篇:利用Opencl加速Eigen矩阵(二)


下一篇:Eigen