[英]How can I use an Eigen::Map object for Eigen::EigenSolver?
[英]How do I assign “Eigen::EigenSolver<MatrixXd> es(Matrix)” to a variable that I can use later?
我能夠成功地從 Eigen::EigenSolver 命令檢索特征值和特征向量,但是,我無法將它們分配給新的矩陣。 我需要將 3 個特征向量分配給一個新的 3x3 矩陣,以便我可以對另一個 3x3 矩陣執行轉置然后矩陣乘法。
我不斷收到以下錯誤,綠色胡蘿卜指向“{ a = b; }”部分中的“b”:
/Eigen/src/Core/functors/AssignmentFunctors.h:24:104:錯誤:從不兼容的類型'const std::__1::complex'分配給'double' EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void assignCoeff(DstScalar& a, const SrcScalar& b) const { a = b; }
我嘗試將我試圖將其分配給的新變量聲明為 MatrixXd 或 Vector3d,但似乎都不起作用。 我認為這個問題與將每個元素返回為 (Real, Complex) 的 Eigensolver 有關,我沒有正確處理這個問題。 如果我只需要計算一次值,這將不是一件壞事。 但是,我需要將這些計算放入一個循環中,然后將根據一些條件檢查此過程的最終計算,以確定代碼中使用的隨機值是否會產生有效結果。
[編輯 2020 年 4 月 19 日,上午 1152 點]。 我只想在當前計算中使用真實部分。 感謝@stark 在評論中讓我注意到這一點。
任何幫助將不勝感激。 請記住,我不是受過培訓的程序員,我的研究領域是物理和數學,我習慣於使用符號軟件和 python。 '''
// Declare matrices for the up and down type quarks
MatrixXd MatrixYukawaUpQuark(3,3), MatrixYukawaDownQuark(3,3);
// Declare vectors for Up and Down Quark eigenvectors
Vector3d UpQVect, DownQVect;
Eigen::EigenSolver<MatrixXd> es1(MatrixYukawaUpQuark);
UpQVect = es1.eigenvectors(); // THE ERROR COMES FROM HERE, AND IF THIS IS COMMENTED OUT THE CODE WORKS FINE IN GENERATING THE EIGENVECTORS & VALUES IN THE PRINT STATEMENTS
// Printing the Up Quark Matrice values for the Eigenvectors, and Eigenvalues to the screen
std::cout << "\nThe eigenvalues for the Yukawa Up Quark Matrix are: " << std::endl << es1.eigenvalues() << std::endl;
std::cout << "\nThe Up Quark eigenvectors are:" << std::endl << es1.eigenvectors() << std::endl;
Eigen::EigenSolver<MatrixXd> es2(MatrixYukawaDownQuark);
//DownQVect = es2.eigenvectors(); // THIS WILL ALSO PRODUCE AN ERROR AS IT IS THE SAME PROBLEM FROM ABOVE
// Printing the Up Quark Matrice values for the Eigenvectors, and Eigenvalues to the screen
std::cout << "\nThe eigenvalues of the Yukawa Down Quark Matrix are: " << std::endl << es2.eigenvalues() << std::endl;
std::cout << "\nThe Down Quark eigenvectors, V, is:" << std::endl << es2.eigenvectors() << std::endl;
'''
以下是您在上面看到的打印語句:
Yukawa 上誇克矩陣的特征值為:
(-1.00393,0)
(1.01004,0)
(1.00393,0)
上誇克特征向量為: (-0.707819,0) (0.57735,0) (-0.407013,0)
(0.706393,0) (0.57735,0) (-0.409483,0)
(0.00142613,0) (0.57735,0) (0.816495,0)
Yukawa 下誇克矩陣的特征值為:
(-1.96316,0)
(2.07543,0)
(1.96316,0)
下誇克特征向量 V 是:
(-0.713131,0) (0.57735,0) (-0.397632,0)
(0.700925,0) (0.57735,0) (-0.418773,0)
(0.0122058,0) (0.57735,0) (0.816405,0)
您正在嘗試將 3x3 復矩陣分配給 3x1 實數向量。 將Matrix3cd
UpQVect
可以編譯您的代碼:
Matrix3cd UpQVect = es1.eigenvectors();
如果要提取實部並將它們存儲到 3x3 矩陣中,可以編寫:
Matrix3d UpQVect = es1.eigenvectors().real();
另請注意,多次訪問es1.eigenvectors()
基本上是免費的。 昂貴的計算僅在您構造es1
時發生(或者如果您稍后調用es1.compute(newInput);
。
但是您的矩陣似乎是自伴隨的(至少您的特征向量矩陣是單一的),因此您可能應該更喜歡使用SelfAdjointEigenSolver
。 如果您的矩陣在編譯時也恰好已知大小為 3x3,您甚至可以編寫更高效的矩陣:
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es1;
es1.computeDirect(MatrixYukawaUpQuark);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.