如何快速计算本征矩阵其中A是稀疏矩阵中的A'A?

问题描述

如问题所示,是否有任何示例代码可用于计算此矩阵乘法?

Here是Dense矩阵的链接

解决方法

我认为该示例演示了您的需求。

#include <Eigen/eigen>

int m = 8;
int n = 5;

//Eigen has no built-in random sparse function (that I know of!)
Eigen::MatrixXd A_dense = MatrixXd::Random(m,n);

//create a sparse copy to demonstrate functionality
Eigen::SparseMatrix<double> A = A_dense.sparseView();

//create a sparse matrix of compatible dimensions for A^T * A
Eigen::SparseMatrix<double> ATA(n,n);

//compute A^T * A
ATA.selfadjointView<Lower>().rankUpdate(A.transpose(),1.0);

//print A in dense format so its readable
std::cout << A.toDense() << "\n\n";

//print ATA in dense format so its readable
std::cout << ATA.toDense() << "\n\n";

//check with intuitive / less optimized operation
std::cout << (A.transpose() * A).toDense() << "\n\n";

这将使用专门的例程来计算A^T * A,并且只会计算出您喜欢的三角形(在本例中为下半部),因为结果是对称矩阵。

我的输出:

 -0.997497    0.64568  -0.817194  -0.982177 -0.0984222
  0.127171    0.49321  -0.271096   -0.24424  -0.295755
 -0.613392  -0.651784  -0.705374  0.0633259  -0.885922
  0.617481   0.717887  -0.668203   0.142369   0.215369
  0.170019   0.421003    0.97705   0.203528   0.566637
-0.0402539  0.0270699  -0.108615   0.214331   0.605213
 -0.299417   -0.39201  -0.761834  -0.667531  0.0397656
  0.791925  -0.970031  -0.990661    0.32609    -0.3961

  2.51603         0         0         0         0
-0.318591   2.87295         0         0         0
 0.414807  0.986724   4.21357         0         0
  1.48181 -0.656854   1.09012    1.6879         0
 0.483357    1.1462   1.49161  0.232797   1.77424

  2.51603 -0.318591  0.414807   1.48181  0.483357
-0.318591   2.87295  0.986724 -0.656854    1.1462
 0.414807  0.986724   4.21357   1.09012   1.49161
  1.48181 -0.656854   1.09012    1.6879  0.232797
 0.483357    1.1462   1.49161  0.232797   1.77424