目前,我正在尝试将一些原型Python代码实现到C++中,并且遇到了一个问题,当使用相同的精确数组时,我在2之间得到不同的svd计算结果。
代码:
python:
(U, S, V) = np.linalg.svd(H)
print("h\n",H)
print("u\n",U)
print("v\n",V)
print("s\n",S)
rotation_matrix = np.dot(U, V)版画
h
[[ 1.19586781e+00 -1.36504900e+00 3.04707238e+00]
[-3.24276981e-01 4.25640964e-01 -6.78455372e-02]
[ 4.58970250e-02 -7.33566042e-02 -2.96605698e-03]]
u
[[-0.99546325 -0.09501679 0.0049729 ]
[ 0.09441242 -0.97994807 0.17546529]
[-0.01179897 0.17513875 0.98447306]]
v
[[-0.34290622 0.39295764 -0.85322893]
[ 0.49311955 -0.6977843 -0.51954806]
[-0.79953014 -0.59890012 0.04549948]]
s
[3.5624894 0.43029207 0.00721429]C++代码:
std::cout << "H\n" << HTest << std::endl;
Eigen::JacobiSVD<Eigen::MatrixXd> svd;
svd.compute(HTest, Eigen::ComputeThinV | Eigen::ComputeThinU);
std::cout << "h is" << std::endl << HTest << std::endl;
std::cout << "Its singular values are:" << std::endl << svd.singularValues() << std::endl;
std::cout << "Its left singular vectors are the columns of the thin U matrix:" << std::endl << -1*svd.matrixU() << std::endl;
std::cout << "Its right singular vectors are the columns of the thin V matrix:" << std::endl << -1*svd.matrixV() << std::endl;指纹:
h is
1.19587 -1.36505 3.04707
-0.324277 0.425641 -0.0678455
0.045897 -0.0733566 -0.00296606
Its singular values are:
3.56249
0.430292
0.00721429
Its left singular vectors are the columns of the thin U matrix:
-0.995463 -0.0950168 0.0049729
0.0944124 -0.979948 0.175465
-0.011799 0.175139 0.984473
Its right singular vectors are the columns of the thin V matrix:
-0.342906 0.49312 -0.79953
0.392958 -0.697784 -0.5989
-0.853229 -0.519548 0.0454995因此,H,U,S在2之间是等价的,而V不是。是什么导致的?
发布于 2022-11-04 20:09:45
我没注意到V只是相互转座。用户chrslg对此有一个很好的解释,所以我将在这里复制它:
我会说,“因为”:-)我不认为有什么好的理由。只有两个实现。在数学课中,您可能已经学会了使用公式M=U.S.Vᵀ进行SVD分解。所以C++库可能坚持这个公式,给出U,S,V,例如M=U.S.Vᵀ。其中,正如linalg文档所述,它返回U、S、V(如M=(U*S)@V )。因此,其中一个调用V,另一个称为Vᵀ。很难说哪一个是对的。只要他们照医生说的做就行“
https://stackoverflow.com/questions/74322089
复制相似问题