0

我正在使用 Eigen 将代码从 Matlab 移植到 C++,但我发现了一些问题。我需要计算 QR 分解,

代码matlab: [Q,R] = qr(Xtilde, 0);

代码特征:

    HouseholderQR<MatrixXd> qr(X_TILDE.rows(), X_TILDE.cols());
    qr.compute(X_TILDE);
    MatrixXd q = qr.householderQ()*MatrixXd::Identity(X_TILDE.rows(), X_TILDE.cols());
    MatrixXd temp = qr.matrixQR().triangularView<Upper>();
    MatrixXd r = temp.topRows(X_TILDE.cols());

Xtilde(46 x 4) 是:

0.0577350269189626  -0.0823586409336270 0.117484066404866   -0.167590257713776
0.0577350269189626  -0.0776524328802769 0.104440937399917   -0.140470929246880
0.0577350269189626  -0.0729462248269268 0.0921650512776130  -0.116447378835011
0.0577350269189626  -0.0682400167735767 0.0806564080379527  -0.0953319835657442
0.0577350269189626  -0.0635338087202266 0.0699150076809364  -0.0769371205266531
0.0577350269189626  -0.0588276006668765 0.0599408502065641  -0.0610751668053119
0.0577350269189626  -0.0541213926135263 0.0507339356148359  -0.0475584994892948
0.0577350269189626  -0.0494151845601762 0.0422942639057516  -0.0361994956661756
0.0577350269189626  -0.0447089765068261 0.0346218350793114  -0.0268105324235286
0.0577350269189626  -0.0400027684534760 0.0277166491355153  -0.0192039868489278
0.0577350269189626  -0.0352965604001259 0.0215787060743631  -0.0131922360299474
0.0577350269189626  -0.0305903523467758 0.0162080058958549  -0.00858765705416130
0.0577350269189626  -0.0258841442934256 0.0116045485999908  -0.00520262700914369
0.0577350269189626  -0.0211779362400755 0.00776833418677071 -0.00284952298246863
0.0577350269189626  -0.0164717281867254 0.00469936265619463 -0.00134072206171021
0.0577350269189626  -0.0117655201333753 0.00239763400826257 -0.000488601334442496
0.0577350269189626  -0.00705931208002518    0.000863148242974523    -0.000105537888239579
0.0577350269189626  -0.00235310402667506    9.59053603305026e-05    -3.90881067553996e-06
0.0577350269189626  0.00235310402667506 9.59053603305026e-05    3.90881067553996e-06
0.0577350269189626  0.00705931208002518 0.000863148242974523    0.000105537888239579
0.0577350269189626  0.0117655201333753  0.00239763400826257 0.000488601334442496
0.0577350269189626  0.0164717281867254  0.00469936265619463 0.00134072206171021
0.0577350269189626  0.0211779362400755  0.00776833418677071 0.00284952298246863
0.0577350269189626  0.0258841442934256  0.0116045485999908  0.00520262700914369
0.0577350269189626  0.0305903523467758  0.0162080058958549  0.00858765705416130
0.0577350269189626  0.0352965604001259  0.0215787060743631  0.0131922360299474
0.0577350269189626  0.0400027684534760  0.0277166491355153  0.0192039868489278
0.0577350269189626  0.0447089765068261  0.0346218350793114  0.0268105324235286
0.0577350269189626  0.0494151845601762  0.0422942639057516  0.0361994956661756
0.0577350269189626  0.0541213926135263  0.0507339356148359  0.0475584994892948
0.0577350269189626  0.0588276006668765  0.0599408502065641  0.0610751668053119
0.0577350269189626  0.0635338087202266  0.0699150076809364  0.0769371205266531
0.0577350269189626  0.0682400167735767  0.0806564080379527  0.0953319835657442
0.0577350269189626  0.0729462248269268  0.0921650512776130  0.116447378835011
0.0577350269189626  0.0776524328802769  0.104440937399917   0.140470929246880
0.0577350269189626  0.0823586409336270  0.117484066404866   0.167590257713776
0.0577350269189626  0.0870648489869772  0.131294438292458   0.197992987148126
0.0577350269189626  0.0917710570403273  0.145872053062695   0.231866740462355
0.0577350269189626  0.0964772650936774  0.161216910715575   0.269399140568890
1.0000e-05          0               0                       0
0                   0.00100000000000000 0                   0
0                   0                   0.00100000000000000 0
0                   0                   0                   0.00100000000000000

当我执行 QR 分解时,我从 Eigen 获得的 Q 矩阵与我从 Matlab 获得的 Q 矩阵不同。此外,来自 Eigen 的 R 矩阵与我从 Matlab 获得的 Q 矩阵具有不同的符号:来自 Matlab 的 Q

-0.154303349906983  -0.260970906342325  -0.321232647415310  0.353839296299944
-0.154303349906984  -0.248240618228066  -0.274222991714045  0.250277645081041
-0.154303349906984  -0.235510330113806  -0.229563818797843  0.159661173523266
-0.154303349906984  -0.222780041999546  -0.187255128666704  0.0813260262593829
-0.154303349906984  -0.210049753885286  -0.147296921320628  0.0146083479221537
-0.154303349906984  -0.197319465771027  -0.109689196759616  -0.0411557168556593
-0.154303349906984  -0.184589177656767  -0.0744319549836670 -0.0866300234412932
-0.154303349906984  -0.171858889542507  -0.0415251959927814 -0.122478427201985
-0.154303349906984  -0.159128601428247  -0.0109689197869589 -0.149364783504973
-0.154303349906984  -0.146398313313987  0.0172368736338002  -0.167952947717494
-0.154303349906984  -0.133668025199728  0.0430921842694962  -0.178906775206785
-0.154303349906984  -0.120937737085468  0.0665970121201288  -0.182890121340084
-0.154303349906984  -0.108207448971208  0.0877513571856982  -0.180566841484628
-0.154303349906984  -0.0954771608569483 0.106555219466204   -0.172600791007654
-0.154303349906984  -0.0827468727426885 0.123008598961647   -0.159655825276400
-0.154303349906984  -0.0700165846284287 0.137111495672027   -0.142395799658103
-0.154303349906984  -0.0572862965141690 0.148863909597343   -0.121484569520001
-0.154303349906984  -0.0445560083999092 0.158265840737596   -0.0975859902293308
-0.154303349906984  -0.0318257202856494 0.165317289092786   -0.0713639171533296
-0.154303349906984  -0.0190954321713897 0.170018254662912   -0.0434822056592351
-0.154303349906984  -0.00636514405712988    0.172368737447976   -0.0146047111142846
-0.154303349906984  0.00636514405712989 0.172368737447976   0.0146047111142846
-0.154303349906984  0.0190954321713897  0.170018254662912   0.0434822056592352
-0.154303349906984  0.0318257202856494  0.165317289092786   0.0713639171533297
-0.154303349906984  0.0445560083999092  0.158265840737596   0.0975859902293308
-0.154303349906984  0.0572862965141690  0.148863909597343   0.121484569520001
-0.154303349906984  0.0700165846284287  0.137111495672027   0.142395799658103
-0.154303349906984  0.0827468727426885  0.123008598961647   0.159655825276400
-0.154303349906984  0.0954771608569483  0.106555219466204   0.172600791007654
-0.154303349906984  0.108207448971208   0.0877513571856981  0.180566841484628
-0.154303349906984  0.120937737085468   0.0665970121201288  0.182890121340084
-0.154303349906984  0.133668025199728   0.0430921842694961  0.178906775206785
-0.154303349906984  0.146398313313987   0.0172368736338002  0.167952947717494
-0.154303349906984  0.159128601428247   -0.0109689197869590 0.149364783504973
-0.154303349906984  0.171858889542507   -0.0415251959927814 0.122478427201985
-0.154303349906984  0.184589177656767   -0.0744319549836671 0.0866300234412932
-0.154303349906984  0.197319465771026   -0.109689196759616  0.0411557168556592
-0.154303349906984  0.210049753885286   -0.147296921320628  -0.0146083479221538
-0.154303349906984  0.222780041999546   -0.187255128666704  -0.0813260262593831
-0.154303349906984  0.235510330113806   -0.229563818797843  -0.159661173523266
-0.154303349906984  0.248240618228066   -0.274222991714045  -0.250277645081041
-0.154303349906984       0.260970906342325       -0.321232647415310     -0.353839296299943
-2.67261241816974e-05    -8.58599617264519e-22    2.99060305347150e-05   1.01643998560347e-21
0                        0.00270499900768257     -2.92157489996581e-19   0.00621245013765559
0                        0                       -0.00306354459354929   -4.46671225035349e-19
0                        0                        0                     -0.00353824252408127 

来自 Matlab 的 R

-0.374165738811025  -1.38777878078145e-17   -0.365257030483198      0
0                    0.369685902715625      -2.77555756156289e-17   0.649094916921038
0                    0                      -0.326419273316809      3.81639164714898e-17
0                    0                       0                     -0.282626188904237

来自本征的 Q

-0.154303 -0.260971 -0.321233 0.35384 
-0.154303 -0.248241 -0.274223 0.250278 
-0.154303 -0.23551 -0.229564 0.159661 
-0.154303 -0.22278 -0.187255 0.081326 
-0.154303 -0.21005 -0.147297 0.0146083 
-0.154303 -0.197319 -0.109689 -0.0411558 
-0.154303 -0.184589 -0.074432 -0.0866302 
-0.154303 -0.171859 -0.0415252 -0.122479 
-0.154303 -0.159129 -0.0109689 -0.149365 
-0.154303 -0.146398 0.0172369 -0.167953 
-0.154303 -0.133668 0.0430922 -0.178907 
-0.154303 -0.120938 0.066597 -0.18289 
-0.154303 -0.108207 0.0877514 -0.180567 
-0.154303 -0.0954772 0.106555 -0.172601 
-0.154303 -0.0827469 0.123009 -0.159656 
-0.154303 -0.0700166 0.137112 -0.142396 
-0.154303 -0.0572863 0.148864 -0.121485 
-0.154303 -0.044556 0.158266 -0.0975861 
-0.154303 -0.0318257 0.165317 -0.071364 
-0.154303 -0.0190954 0.170018 -0.0434823 
-0.154303 -0.00636514 0.172369 -0.0146047 
-0.154303 0.00636514 0.172369 0.0146047 
-0.154303 0.0190954 0.170018 0.0434823 
-0.154303 0.0318257 0.165317 0.071364 
-0.154303 0.044556 0.158266 0.0975861 
-0.154303 0.0572863 0.148864 0.121485 
-0.154303 0.0700166 0.137112 0.142396 
-0.154303 0.0827469 0.123009 0.159656 
-0.154303 0.0954772 0.106555 0.172601 
-0.154303 0.108207 0.0877514 0.180567 
-0.154303 0.120938 0.066597 0.18289 
-0.154303 0.133668 0.0430922 0.178907 
-0.154303 0.146398 0.0172369 0.167953 
-0.154303 0.159129 -0.0109689 0.149365 
-0.154303 0.171859 -0.0415252 0.122479 
-0.154303 0.184589 -0.074432 0.0866302 
-0.154303 0.197319 -0.109689 0.0411558 
-0.154303 0.21005 -0.147297 -0.0146083 
-0.154303 0.22278 -0.187255 -0.081326 
-0.154303 0.23551 -0.229564 -0.159661 
-0.154303 0.248241 -0.274223 -0.250278 
-0.154303 0.260971 -0.321233 -0.35384 
-2.67261e-05     3.38813e-21    2.9906e-05    -1.35525e-20 
0                0.0026726      4.33681e-19    0.00613805 
0                0              -0.0029906     1.02999e-18 
0                0              0              -0.00341263 

来自本征的 R

-0.374166     1.38778e-17    -0.374166      0 
0             0.374167        2.77556e-17   0.672987 
0             0              -0.334381      -6.245e-17 
0             0               0             -0.293029 

我不明白为什么我在Q矩阵的最后四行有不同,为什么我在Q矩阵中有不同的符号,

我获得 Q'*Q : Matlab :

1.0000000            7.979727989e-17    9.012480877e-17   6.938893903e-18
7.979727989e-17      1.0000000         -3.469446951e-17   3.093533729e-17
9.012480877e-17     -3.469446951e-17    1.000000          1.387915620e-17
6.938893903e-18      3.093533729e-17    1.387915620e-17   1.000000

特征:

1                   -1.8735e-16           -1.09631e-16           5.55112e-17 
-1.8735e-16           1                    -1.52654e-16           1.31595e-17 
-1.09631e-16         -1.52654e-16           1                     1.94291e-16 
5.55112e-17           1.31595e-17           1.94291e-16           1

对于 Q*R - X:

特征:

5.55112e-17    1.38778e-17     -1.66533e-16     5.55112e-17 
0               5.55112e-17     -5.55112e-17     1.11022e-16 
0               0               -2.77556e-17     2.77556e-17 
0               2.77556e-17     -2.77556e-17     8.32667e-17 
0               0               -2.77556e-17     2.77556e-17 
0               1.38778e-17     -2.77556e-17     4.16334e-17 
0               0               -2.77556e-17     1.38778e-17 
0               0               -2.77556e-17     0 
0               6.93889e-18     -2.08167e-17     1.38778e-17 
0               6.93889e-18     -2.08167e-17     0 
0               6.93889e-18     -2.08167e-17     4.85723e-17 
0               1.38778e-17     -6.93889e-18     1.38778e-17 
0               6.93889e-18     -2.08167e-17     2.42861e-17 
0               6.93889e-18     -3.1225e-17      2.25514e-17 
0               3.46945e-18     -1.04083e-17     2.08167e-17 
0               3.46945e-18     -3.1225e-17      2.60209e-17 
0               3.46945e-18     -7.80626e-18     1.38778e-17 
0               0               -2.42861e-17     2.1684e-18 
0               3.46945e-18     -2.60209e-17     1.31188e-17 
0               3.46945e-18      1.0842e-19      5.81403e-18 
0               3.46945e-18     -1.46367e-17     1.02703e-17 
0               2.60209e-18     -1.46367e-17     1.87279e-18 
0               5.20417e-18     -2.07083e-17     6.32903e-18 
0               3.46945e-18     -5.20417e-18    -2.71051e-18 
0               3.46945e-18     -2.42861e-17     1.30104e-18 
0               3.46945e-18     -1.47451e-17    -6.93889e-18 
0               -1.04083e-17    -1.73472e-17    -1.21431e-17 
0               0                3.46945e-18    -6.93889e-18 
0               0               -1.73472e-17    -1.56125e-17 
0              -6.93889e-18     -1.73472e-17    -2.42861e-17 
0               0               -2.08167e-17    -2.77556e-17 
0              -6.93889e-18     -2.08167e-17    -2.08167e-17 
0               1.38778e-17     -2.77556e-17    -3.46945e-17 
0              -6.93889e-18     -2.08167e-17    -2.08167e-17 
0               0               -1.38778e-17    -5.55112e-17 
0               0               -1.38778e-17    -2.77556e-17 
0              -1.38778e-17     -2.77556e-17    -1.38778e-17 
0              -1.38778e-17     -2.77556e-17     0 
0               1.38778e-17     -2.77556e-17     0 
0               1.38778e-17     -2.77556e-17     -5.55112e-17 
0               0               -2.77556e-17     0 
0               1.38778e-17      0               0 
0               2.55373e-22     -1.69407e-21     1.13989e-21 
0               0                1.07448e-20    -6.50521e-19 
0               0                0               2.32655e-20 
0               0                0               0

MATLAB:

-3.469446951e-17     1.387778780e-17    5.551115123e-17     -5.55111512e-17
6.9388939039e-18    -1.38777878e-17     0                   -1.11022302e-16
6.9388939039e-18    -1.38777878e-17     2.775557561e-17     -2.77555756e-17
6.9388939039e-18    -1.38777878e-17     1.387778780e-17      0
6.9388939039e-18    -1.38777878e-17     1.387778780e-17     -2.77555756e-17
6.9388939039e-18    -1.38777878e-17     1.387778780e-17     -4.16333632e-17
6.9388939039e-18    0                   1.387778780e-17     -1.38777878e-17
6.9388939039e-18    0                   1.387778780e-17      0
6.9388939039e-18    0                   1.387778780e-17     -6.93889390e-18
6.9388939039e-18    0                   2.081668171e-17     -2.77555756e-17
6.9388939039e-18    0                   1.387778780e-17      0
6.9388939039e-18    0                   1.387778780e-17     -3.46944695e-18
6.9388939039e-18    0                   0                   -1.04083408e-17
6.9388939039e-18    0                   1.040834085e-17     -1.90819582e-17
6.9388939039e-18    0   0   0
6.9388939039e-18    3.4694469519e-18    1.734723475e-17      7.806255641e-18
6.9388939039e-18    0                   9.540979117e-18     -3.035766082e-18
6.9388939039e-18    3.469446951e-18     7.806255641e-18      1.301042606e-18
6.9388939039e-18    1.734723475e-18     1.127570259e-17     -6.071532166e-18
6.9388939039e-18    2.602085213e-18     1.929879867e-17     -2.574980159e-18
6.9388939039e-18    3.035766082e-18     2.318837396e-17     -2.631731367e-18
6.9388939039e-18    2.602085213e-18     3.224146210e-17     -1.479766558e-18
6.9388939039e-18    4.336808689e-18     1.929879867e-17     -1.639855785e-18
6.9388939039e-18    3.469446951e-18     2.949029909e-17     -3.577867169e-18
6.9388939039e-18    3.469446951e-18     2.602085213e-17      4.3368086899e-19
6.9388939039e-18   -3.469446951e-18     2.775557561e-17      3.0357660829e-18
6.9388939039e-18   -3.469446951e-18     1.734723475e-17     -1.561251128e-17
6.9388939039e-18    3.469446951e-18     1.734723475e-17     -1.734723475e-18
6.9388939039e-18    6.938893903e-18     2.081668171e-17      1.7347234759e-18
6.9388939039e-18    6.938893903e-18     1.040834085e-17      1.0408340855e-17
6.9388939039e-18    0                   1.387778780e-17      3.4694469519e-18
6.9388939039e-18    -6.93889390e-18     1.387778780e-17     -6.938893903e-18
6.9388939039e-18    0                   1.387778780e-17     -2.081668171e-17
6.9388939039e-18    0                   1.387778780e-17      1.387778780e-17
6.9388939039e-18    0                   1.387778780e-17      0
6.9388939039e-18    0                   1.387778780e-17      1.387778780e-17
6.9388939039e-18    0                   1.387778780e-17      2.775557561e-17
6.9388939039e-18    -1.387778780e-17    1.387778780e-17      0
6.9388939039e-18    0                   1.387778780e-17      2.775557561e-17
6.9388939039e-18    0                   2.775557561e-17      2.775557561e-17
6.9388939039e-18   -1.387778780e-17     2.775557561e-17      0
6.9388939039e-18    1.387778780e-17     2.775557561e-17      5.5511151231e-17
0                   5.348730573e-23     2.819782315e-21      2.967460447e-22
0                   0                   2.028703108e-20      1.791731434e-19
0                   0                   0                    9.324126050e-21
0                   0                   0                    0

如您所见,Q' Q 等价于单位矩阵,并且 Q R - X 非常接近于零。

此致,

4

0 回答 0