如何在 C++ 中计算列随机矩阵的特征向量
我有一个列随机矩阵 A,想要用 C++ 求解以下方程: Ax=x
我假设我需要找出一个特征向量 x,其中特征值设置为 1(对吗?),但我无法在 C++ 中找到它。到目前为止,我已经检查了一些数学库,例如 Seldon、CPPScaLapack、Eigen...其中,Eigen 似乎是一个不错的选择,但我无法理解如何利用它们中的任何一个来求解上面的方程。
你能给我一些建议/代码片段或想法来解决这个方程吗? 非常感谢任何帮助。
谢谢。
编辑: A 是 n×n 实数非负矩阵。
I have a column stochastic matrix A and want to solve the following equation in C++:
Ax=x
I am assuming that I need to find out an eigenvector x where eigenvalue is set to be 1(right?) but I couldn't figure it out in C++. So far I have checked out some math-libs such as Seldon, CPPScaLapack, Eigen... Among them, Eigen seems a good option but I couldn't understand how to utilize any of them to solve the equation above.
Can you give me some suggestions/code snippets or ideas to solve the equation?
Any help is highly appreciated.
Thanks.
edit: A is n-by-n real, non-negative matrix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,我没有使用过 Eigen,但它的
EigenSolver
和SelfAdjointEigenSolver
看起来能够解决这个问题。这些被列为“实验性”,因此可能存在错误,并且 API 将来可能会发生变化。这两个求解器类对于特征值和特征向量集合具有不同的类型,但由于它们都基于类 Matrix 并且矩阵是可转换的,因此上面的方法应该可以工作。
或者,您可以将问题视为齐次线性方程 (AIn< /sub>) x = 0,可以通过将 AIn 转换为上三角矩阵来求解。 高斯消除 可以做到这一点(尽管您需要跳过每行的归一化步骤)确保前导系数为 1,因为整数不是字段)。快速浏览上述项目并没有发现对行梯形转换的支持,这可能意味着我错过了它。无论如何,使用一些辅助类(下面的
RowMajor
、RowMajor::iterator
、RowWithPivot
)来实现并不太难。我什至还没有测试过它是否可以编译,所以将其更多地视为算法的说明,而不是完整的嵌入式解决方案。尽管示例使用函数,但使用类(à la EigenSolver)可能更有意义。帮助器类的接口,尚未经过 const 正确性和其他使 C++ 工作所需的细节的审查。
Keep in mind I've not used Eigen, but its
EigenSolver
andSelfAdjointEigenSolver
look to be able to solve this. These are listed as "experimental", so there might be bugs and the API might change in the future.The two solver classes have different types for the eigen value and eigen vector collections, but as they're both based on class Matrix and Matrices are convertible, the above it should work.
Alternatively, you could approach the problem as the homogeneous linear equation (A-In) x = 0, which can be solved by converting A-In to an upper triangular matrix. Gaussian elimination will do that (though you'll need to skip the normalizing step for each row where you ensure the leading coefficient is 1, as integers aren't a field). A quick perusal of the above projects didn't turn up support for row echelon conversion, which probably means I missed it. In any case, it's not too hard to implement with a few helper classes (
RowMajor
,RowMajor::iterator
,RowWithPivot
in the following). I haven't even tested whether or not this will compile, so take it as more of an illustration of the algorithm than a complete, drop-in solution. Though the sample uses functions, it might make more sense to use a class (à la EigenSolver).The interfaces for the helper classes, which haven't been vetted for const-correctness and other necessary details that make C++ work.