Eigen教程6 - Matrix-free solvers

2023-05-16

博客新址: http://blog.xuezhisd.top
邮箱:xuezhisd@126.com


Matrix-free solvers

像ConjugateGradient 和 BiCGSTAB这样的迭代求解器可以用在 matrix free context。为此,用户必须提供一个继承EigenBase<>的封装类,并实现下面的方法:

  • Index rows() 和 Index cols():分别返回行数和列数。
  • operator* :操作数分别是你自己的类型和一个Eigen密集列向量。

Eigen::internal::traits<> 也必须为封装类型专门定制。

下面的例程,对Eigen::SparseMatrix进行了封装:

// 参考链接:http://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/IterativeLinearSolvers>
#include <unsupported/Eigen/IterativeSolvers>
class MatrixReplacement; //声明类,定义在下面
using Eigen::SparseMatrix;
namespace Eigen {
	namespace internal {
		// MatrixReplacement和SparseMatrix相似,因此继承它的特性
		template<>
		struct traits<MatrixReplacement> :  public Eigen::internal::traits<Eigen::SparseMatrix<double> >
		{};
	}
}
// matrix-free wrapper的简单例子:将用户类型转换成Eigen兼容的类型
// 为了简单,该例子简单封装了Eigen::SparseMatrix
class MatrixReplacement : public Eigen::EigenBase<MatrixReplacement> {
public:
	// Required typedefs, constants, and method:
	typedef double Scalar;
	typedef double RealScalar;
	typedef int StorageIndex;
	enum {
		ColsAtCompileTime = Eigen::Dynamic,
		MaxColsAtCompileTime = Eigen::Dynamic,
		IsRowMajor = false
	};
	Index rows() const { return mp_mat->rows(); }
	Index cols() const { return mp_mat->cols(); }
	template<typename Rhs>
	Eigen::Product<MatrixReplacement,Rhs,Eigen::AliasFreeProduct> operator*(const Eigen::MatrixBase<Rhs>& x) const {
		return Eigen::Product<MatrixReplacement,Rhs,Eigen::AliasFreeProduct>(*this, x.derived());
	}
	// 自定义API:
	MatrixReplacement() : mp_mat(0) {}
	void attachMyMatrix(const SparseMatrix<double> &mat) {
		mp_mat = &mat;
	}
	const SparseMatrix<double> my_matrix() const { return *mp_mat; }
private:
	const SparseMatrix<double> *mp_mat;
};
// Implementation of MatrixReplacement * Eigen::DenseVector though a specialization of internal::generic_product_impl:
namespace Eigen {
	namespace internal {
		template<typename Rhs>
		struct generic_product_impl<MatrixReplacement, Rhs, SparseShape, DenseShape, GemvProduct> // GEMV stands for matrix-vector
			: generic_product_impl_base<MatrixReplacement,Rhs,generic_product_impl<MatrixReplacement,Rhs> >
		{
			typedef typename Product<MatrixReplacement,Rhs>::Scalar Scalar;
			template<typename Dest>
			static void scaleAndAddTo(Dest& dst, const MatrixReplacement& lhs, const Rhs& rhs, const Scalar& alpha)
			{
				// This method should implement "dst += alpha * lhs * rhs" inplace,
				// however, for iterative solvers, alpha is always equal to 1, so let's not bother about it.
				assert(alpha==Scalar(1) && "scaling is not implemented");
				// Here we could simply call dst.noalias() += lhs.my_matrix() * rhs,
				// but let's do something fancier (and less efficient):
				for(Index i=0; i<lhs.cols(); ++i)
					dst += rhs(i) * lhs.my_matrix().col(i);
			}
		};
	}
}
int main()
{
	int n = 10;
	Eigen::SparseMatrix<double> S = Eigen::MatrixXd::Random(n,n).sparseView(0.5,1);
	S = S.transpose()*S; //S'S 对称正定矩阵
	MatrixReplacement A;
	A.attachMyMatrix(S);
	Eigen::VectorXd b(n), x;
	b.setRandom();
	// 使用不同的迭代求解器,来求解 Ax = b with matrix-free version:
	{
		Eigen::ConjugateGradient<MatrixReplacement, Eigen::Lower|Eigen::Upper, Eigen::IdentityPreconditioner> cg;
		cg.compute(A);
		x = cg.solve(b);
		std::cout << "CG:       #iterations: " << cg.iterations() << ", estimated error: " << cg.error() << std::endl;
	}
	{
		Eigen::BiCGSTAB<MatrixReplacement, Eigen::IdentityPreconditioner> bicg;
		bicg.compute(A);
		x = bicg.solve(b);
		std::cout << "BiCGSTAB: #iterations: " << bicg.iterations() << ", estimated error: " << bicg.error() << std::endl;
	}
	{
		Eigen::GMRES<MatrixReplacement, Eigen::IdentityPreconditioner> gmres;
		gmres.compute(A);
		x = gmres.solve(b);
		std::cout << "GMRES:    #iterations: " << gmres.iterations() << ", estimated error: " << gmres.error() << std::endl;
	}
	{
		Eigen::DGMRES<MatrixReplacement, Eigen::IdentityPreconditioner> gmres;
		gmres.compute(A);
		x = gmres.solve(b);
		std::cout << "DGMRES:   #iterations: " << gmres.iterations() << ", estimated error: " << gmres.error() << std::endl;
	}
	{
		Eigen::MINRES<MatrixReplacement, Eigen::Lower|Eigen::Upper, Eigen::IdentityPreconditioner> minres;
		minres.compute(A);
		x = minres.solve(b);
		std::cout << "MINRES:   #iterations: " << minres.iterations() << ", estimated error: " << minres.error() << std::endl;
	}
}

参考

  • http://eigen.tuxfamily.org/dox/group__MatrixfreeSolverExample.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Eigen教程6 - Matrix-free solvers 的相关文章

  • scipy 将一个稀疏矩阵的所有行附加到另一个稀疏矩阵

    我有一个 numpy 矩阵 想在其中附加另一个矩阵 这两个矩阵的形状为 m1 shape 2777 5902 m2 shape 695 5902 我想将 m2 附加到 m1 以便新矩阵的形状为 m new shape 3472 5902 当
  • C free() 是如何工作的? [复制]

    这个问题在这里已经有答案了 可能的重复 malloc 和 free 如何工作 https stackoverflow com questions 1119134 how malloc and free work include
  • 如何 free() 由 malloc() 分配的结构数组?

    我一直在研究一个使用结构作为字符串存储的项目 我声明了一个由 char 类型成员组成的结构 struct datastore1 char name 50 char address 50 char email 50 char number 5
  • 如何按物种矩阵显示站点内植物物种生物量?

    我之前问过 如何将两列显示为二进制 存在 不存在 矩阵 这个问题得到了两个很好的答案 我现在想更进一步 在原始站点按物种列添加第三列 该列反映每个地块中每个物种的生物量 第 1 列 地块 指定约 200 个地块的代码 第 2 列 物种 指定
  • Numpy 中矩阵乘以另一个矩阵的每一行

    我有一个大小为 4x4 的齐次变换矩阵和一个大小为 nx3 的轨迹 该轨迹的每一行都是一个向量 我想将齐次变换矩阵乘以轨迹的每一行 下面是代码 append zero column at last trajectory np hstack
  • 计算按前两列中的索引分组的 numpy 数组条目的第 N 列的总和?

    我想循环以下内容check matrix以这样的方式 代码可以识别第一个和第二个元素是否是1 and 1 or 1 and 2ETC 然后对于每个单独的类对 即1 1 or 1 2 or 2 2 代码应将最后一个元素 在本例中索引为 8 乘
  • 使用“const cv::Mat &”、“cv::Mat &”、“cv::Mat”或“const cv::Mat”作为函数参数的区别?

    我已经彻底搜索过 但没有找到一个简单的答案 传递 opencv 矩阵 cv Mat 作为函数的参数 我们传递一个智能指针 我们对函数内部的输入矩阵所做的任何更改也会改变函数范围之外的矩阵 我读到 通过将矩阵作为 const 引用传递 它不会
  • Python-矩阵中相同列/行的列表

    我有一个矩阵 A 和一个索引列表 比如说l 0 3 4 5 有没有一种简单的方法来访问 A 对应于这些行和列的 4x4 子矩阵 即A l l A l 访问 l 中行的所有列 A l 1 4 访问中的行l和前四列A 但我找不到访问的方法l以这
  • malloc 实现?

    我正在尝试实施malloc and free对于C 我不知道如何重用内存 我目前有一个struct看起来像这样 typedef struct mem dictionary void addr size t size int freed me
  • 在c中用以下结构填充矩阵

    我有以下结构 typedef struct arr integer int size int arr arr arr integer arr arr integer alloc arr integer int len arr arr int
  • 计算带状矩阵的 colCumsums 的更快替代方案

    我是 R 和 stats 的新手 在我当前工作的领域中 我需要以独特的方式计算累积列总和 最初提供宽度为 b 行数为 n 的方带矩阵 例如 n 8 且 b 3 0 1 2 7 0 0 0 0 0 0 3 6 7 0 0 0 0 0 0 3
  • Cuda:最小二乘求解,速度较差

    最近 我使用Cuda编写了一个名为 正交匹配追踪 的算法 在我丑陋的 Cuda 代码中 整个迭代需要 60 秒 而 Eigen lib 只需 3 秒 在我的代码中 矩阵 A 是 640 1024 y 是 640 1 在每一步中 我从 A 中
  • 使用 numpy tensordot 进行张量乘法

    我有一个张量 U 由 n 个维度 d k 的矩阵和一个维度 k n 的矩阵 V 组成 我想将它们相乘 以便结果返回一个维度为 d n 的矩阵 其中 j 列是 U 的矩阵 j 与 V 的 j 列之间的矩阵乘法的结果 获得此信息的一种可能方法是
  • MATLAB 中最有效的矩阵求逆

    在 MATLAB 中计算某个方阵 A 的逆矩阵时 使用 Ai inv A should be the same as Ai A 1 MATLAB 通常会通知我这不是最有效的求逆方法 那么什么是更有效率的呢 如果我有一个方程系统 可能会使用
  • MPI - 发送和接收列

    我需要从一个进程发送矩阵列并从另一个进程接收它 我尝试运行以下程序 但得到了一个奇怪的结果 至少我这么认为 仅复制矩阵的第一个元素 某些矩阵元素会发生意外变化 include
  • 将代表扩展到矩阵?

    如果你打电话rep在矩阵上 它重复其元素而不是整个矩阵 传统的修复方法是调用rep list theMatrix 我想延长rep以便它自动执行此操作 我尝试使用 rep matrix lt function x rep list x 这确实
  • 使用std::begin()、std::end()将ArrayXd转换为stl向量,

    在我看来我应该能够使用std begin and std end 转换ArrayXd to std vector
  • Matlab没有优化以下内容吗?

    我有一个很长的向量 1xrv 和一个很长的向量w1xs 和一个矩阵Arxs 它是稀疏的 但维度非常大 我期望 Matlab 对以下内容进行优化 这样我就不会遇到内存问题 A v w 但看起来 Matlab 实际上是在尝试生成完整的v w矩阵
  • CUDA 和 Eigen 的成员“已声明”错误

    我只是 CUDA 和 Nsight 的初学者 希望利用出色的 GPU 性能进行线性代数运算 例如 CUBLAS 我在以下人员的帮助下编写了很多自定义代码Eigen http eigen tuxfamily org index php tit
  • R 将向量重塑为多列

    假设我在 R 中有一个向量 如下所示 d lt seq 1 100 我想将这个向量重塑为 10x10 矩阵 这样我就可以得到以下数据 1 2 3 10 1 2 3 10 11 12 13 20 21 22 23 30 91 92 93 10

随机推荐

  • PCL系列——将点云数据写入PCD格式文件

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • awk one lines

    From http www student northpark edu pemente awk awk1line txt HANDY ONE LINERS FOR AWK 22 July 2003 compiled by Eric Peme
  • PCL系列——拼接两个点云

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——从深度图像(RangeImage)中提取NARF关键点

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——如何可视化深度图像

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——如何使用迭代最近点法(ICP)配准

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——如何逐渐地配准一对点云

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——三维重构之泊松重构

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——三维重构之贪婪三角投影算法

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——三维重构之移动立方体算法

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • 解决Ubuntu中文显示为乱码

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 1 安装所需软件 sudo apt get install zh autoconvert sudo apt get insta
  • hexo教程系列——hexo安装教程

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 本文详细描述了如何在Github上 xff0c 使用hexo部署博客 安装Hexo 安装node js node js官方下载
  • Python中类成员函数均为虚函数的理解

    python中类成员函数均为虚函数 我们可以通过下面的函数见识其威力 class A def foo self print 39 a 39 class B A def foo self print 39 b 39 for x in A B
  • MxNet系列——Windows上安装MxNet

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 开发环境 操作系统 xff1a Win7 64bit C 43 43 编译器 xff1a Visual Studio 2010
  • Eigen教程1 - 基础

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 固定大小的矩阵和向量 参考链接 xff1a http eigen tuxfamily org dox 2 0 Tutorial
  • Eigen教程2 - 入门

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 安装Eigen 无需安装 只需将Eigen位置添加到include路径中 Demo 1 MatrixXd xff0c X表示动
  • Eigen教程3 - 稀疏矩阵操作

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 稀疏矩阵操作 操作和求解稀疏问题需要的模块 xff1a SparseCore SparseMatrix 和 SparseVec
  • Eigen教程4 - 稀疏矩阵快速参考指南

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 本文对稀疏矩阵SparseMatrix的主要操作进行了总结 首先 xff0c 建议先阅读 Eigen教程2 稀疏矩阵操作 关于
  • Eigen教程5 - 求解稀疏线性方程组

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com Eigen中有一些求解稀疏系数矩阵的线性方程组 由于稀疏矩阵的特殊的表示方式 xff0c 因此获得较好的性能需要格外注意 查看
  • Eigen教程6 - Matrix-free solvers

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com Matrix free solvers 像ConjugateGradient 和 BiCGSTAB这样的迭代求解器可以用在 m