C++で固有値・固有ベクトルを求める (反復法)
n × n の正方行列 A と
n次元のベクトル x について
Ax = λx (ただし x ≠ 0) が成り立つとき
λを固有値, x を固有ベクトルという.
最初に適当なベクトルx0から始めて xk+1 = Axk を反復すると
xk は行列 A の最大固有値に対応する固有ベクトルに収束する.
固有値はレイリー(Rayleigh)商
λ = (xkTxk+1) / (xkTxk)
により求める.
べき乗法、累乗法とも言う.
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ヤコビの反復法 void jacobi(double a[N][N], double b[N], double c[N]); // 1次元配列を表示 void disp_vector(double row[N]); int main() { double a[N][N] = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}}; double b[N] = {20,16,8,17}; double c[N] = {0,0,0,0}; // ヤコビの反復法 jacobi(a,b,c); cout << "X" << endl; disp_vector(c); return 0; } // ヤコビの反復法 void jacobi(double a[N][N], double b[N], double x0[N]) { while (true) { double x1[N]; bool finish = true; for (int i = 0; i < N; i++) { x1[i] = 0; for (int j = 0; j < N; j++) if (j != i) x1[i] += a[i][j] * x0[j]; x1[i] = (b[i] - x1[i]) / a[i][i]; if (fabs(x1[i] - x0[i]) > 0.0000000001) finish = false; } for (int i = 0; i < N; i++) x0[i] = x1[i]; if (finish) return; disp_vector(x0); } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; }
z:\cpp>bcc32 -q CP1101.cpp cp1101.cpp: z:\cpp>CP1101 1 1.0000000000 0.0000000000 0.0000000000 0.0000000000 2 0.7624928517 0.6099942813 0.1524985703 0.1524985703 3 0.6745979924 0.6589096670 0.2353248811 0.2353248811 4 0.6517382447 0.6501601860 0.2761602732 0.2761602732 5 0.6421032522 0.6419452154 0.2963188771 0.2963188771 6 0.6373267026 0.6373108932 0.3063082594 0.3063082594 7 0.6349074765 0.6349058954 0.3112772079 0.3112772079 8 0.6336860412 0.6336858831 0.3137548428 0.3137548428 9 0.6330719648 0.6330719490 0.3149919005 0.3149919005 10 0.6327640473 0.6327640457 0.3156099832 0.3156099832 11 0.6326098648 0.6326098646 0.3159189122 0.3159189122 12 0.6325327172 0.6325327172 0.3160733485 0.3160733485 13 0.6324941293 0.6324941293 0.3161505596 0.3161505596 14 0.6324748319 0.6324748319 0.3161891634 0.3161891634 15 0.6324651822 0.6324651822 0.3162084649 0.3162084649 16 0.6324603572 0.6324603572 0.3162181155 0.3162181155 17 0.6324579446 0.6324579446 0.3162229408 0.3162229408 18 0.6324567383 0.6324567383 0.3162253534 0.3162253534 19 0.6324561352 0.6324561352 0.3162265597 0.3162265597 20 0.6324558336 0.6324558336 0.3162271629 0.3162271629 21 0.6324556828 0.6324556828 0.3162274644 0.3162274644 22 0.6324556074 0.6324556074 0.3162276152 0.3162276152 23 0.6324555697 0.6324555697 0.3162276906 0.3162276906 24 0.6324555509 0.6324555509 0.3162277283 0.3162277283 25 0.6324555415 0.6324555415 0.3162277472 0.3162277472 26 0.6324555367 0.6324555367 0.3162277566 0.3162277566 27 0.6324555344 0.6324555344 0.3162277613 0.3162277613 28 0.6324555332 0.6324555332 0.3162277637 0.3162277637 29 0.6324555326 0.6324555326 0.3162277648 0.3162277648 30 0.6324555323 0.6324555323 0.3162277654 0.3162277654 31 0.6324555322 0.6324555322 0.3162277657 0.3162277657 32 0.6324555321 0.6324555321 0.3162277659 0.3162277659 33 0.6324555321 0.6324555321 0.3162277659 0.3162277659 34 0.6324555321 0.6324555321 0.3162277660 0.3162277660 35 0.6324555320 0.6324555320 0.3162277660 0.3162277660 eigenvalue 10.0000000000 eigenvector 0.6324555320 0.6324555320 0.3162277660 0.3162277660