1.构建Matrix
1. ` Matrix4f A;` 2. ` A << 1,0,0,0,` 3. ` 0,1,0,0,` 4. ` 0,0,1,0,` 5. ` 0,0,0,1;`
2.计算特征值,特征向量
1. ` EigenSolver<Matrix4f> es(A);` 2. ` ` 3. ` //输ê?出3?矩?阵ó` 4. ` cout << "矩?阵óA:" << endl << A << endl << endl;` 5. ` ` 6. ` //特ì?征÷值μ` 7. ` cout << "特ì?征÷值μ:£o" << endl << es.eigenvalues() << endl << endl;` 8. ` //特ì?征÷向ò量á?` 9. ` cout << "特ì?征÷向ò量á?:£o" << endl;` 10. ` cout << "The 1st eigenvector of the matrix is:" ` 11. ` << endl << "["<< es.eigenvectors().col(0).transpose() << "]"<<endl;` 12. ` cout << "The 2nd eigenvector of the matrix is:" ` 13. ` << endl << "["<< es.eigenvectors().col(1).transpose() << "]"<< endl;` 14. ` cout << "The 3rd eigenvector of the matrix is:" ` 15. ` << endl << "["<< es.eigenvectors().col(2).transpose() << "]" << endl;` 16. ` cout << "The 4th eigenvector of the matrix is:" ` 17. ` << endl << "["<< es.eigenvectors().col(3).transpose() << "]" << endl;`
3.至此输出的特征值特征向量都为复数形式。通过查看文档了解到Eigen中复数采用的数据结构是std::complex
1. ` cout << es.eigenvalues()(0).real() << endl;` 2. ` cout << es.eigenvalues()(1).real() << endl;` 3. ` cout << es.eigenvalues()(2).real() << endl;` 4. ` cout << es.eigenvalues()(3).real() << endl;`
输出:
1. `矩阵A:` 2. `1 0 0 0` 3. `0 1 0 0` 4. `0 0 1 0` 5. `0 0 0 1` 6.7. `特征值:` 8. `(1,0)` 9. `(1,0)` 10. `(1,0)` 11. `(1,0)` 12.13. `特征向量:` 14. `The 1st eigenvector of the matrix is:` 15. `[(1,0) (0,0) (0,0) (0,0)]` 16. `The 2nd eigenvector of the matrix is:` 17. `[(0,0) (1,0) (0,0) (0,0)]` 18. `The 3rd eigenvector of the matrix is:` 19. `[(0,0) (0,0) (1,0) (0,0)]` 20. `The 4th eigenvector of the matrix is:` 21. `[(0,0) (0,0) (0,0) (1,0)]` 22.23. `提取特征值(实部)` 24. `1` 25. `1` 26. `1` 27. `1` 28. `0.021s` 29. `请按任意键继续. . .`
[来自为知笔记(Wiz)](http://www.wiz.cn/i/c09d0bb5 "来自为知笔记(Wiz)")