Matrix 如何快速计算A';A在特征值中,其中A是稀疏矩阵?

Matrix 如何快速计算A';A在特征值中,其中A是稀疏矩阵?,matrix,sparse-matrix,eigen,Matrix,Sparse Matrix,Eigen,如问题所示,是否有计算此矩阵乘法的示例代码 是到密集矩阵的链接。我认为这个示例演示了您需要什么 #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 demonst

如问题所示,是否有计算此矩阵乘法的示例代码


是到密集矩阵的链接。

我认为这个示例演示了您需要什么

#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";
 -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