fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define SIZE 4
  6. #define EPSILON 1e-9
  7. #define MAX_ITER 1000
  8.  
  9. double vector_norm(double *v, int n) {
  10. double sum = 0.0;
  11. for (int i = 0; i < n; i++) {
  12. sum += v[i] * v[i];
  13. }
  14. return sqrt(sum);
  15. }
  16.  
  17. void mat_vec_mul(double A[SIZE][SIZE], double *x, double *y, int n) {
  18. for (int i = 0; i < n; i++) {
  19. y[i] = 0.0;
  20. for (int j = 0; j < n; j++) {
  21. y[i] += A[i][j] * x[j];
  22. }
  23. }
  24. }
  25.  
  26. int main() {
  27. double A[SIZE][SIZE] = {
  28. {1.0, 2.0, 3.0, 5.0},
  29. {3.0, -5.0, 1.0, 4.0},
  30. {5.0, 9.0, 2.0, -6.0},
  31. {1.0, 7.0, 4.0, 1.0}
  32. };
  33.  
  34. double x[SIZE], y[SIZE], Ax[SIZE];
  35. double lambda_old = 0.0, lambda_new = 0.0;
  36.  
  37. for (int i = 0; i < SIZE; i++) {
  38. x[i] = 1.0;
  39. }
  40.  
  41. printf("初期ベクトル x(0): [%.1f, %.1f, %.1f, %.1f]\n\n", x[0], x[1], x[2], x[3]);
  42. printf("%-5s | %-12s | %-12s | %-30s\n", "Iter", "固有値(新)", "差分値", "固有ベクトルx(k)");
  43. int actual_iters = 0;
  44. for (int iter = 1; iter <= MAX_ITER; iter++) {
  45. actual_iters = iter;
  46. mat_vec_mul(A, x, y, SIZE);
  47.  
  48. double norm_y = vector_norm(y, SIZE);
  49. if (norm_y < 1e-12) break;
  50. for (int i = 0; i < SIZE; i++) {
  51. y[i] /= norm_y;
  52. }
  53.  
  54. mat_vec_mul(A, y, Ax, SIZE);
  55. double num = 0.0, den = 0.0;
  56. for (int i = 0; i < SIZE; i++) {
  57. num += y[i] * Ax[i];
  58. den += y[i] * y[i];
  59. }
  60. lambda_new = num / den;
  61. double diff = fabs(lambda_new - lambda_old);
  62.  
  63.  
  64. if (iter <= 5 || iter % 5 == 0) {
  65. printf("%-5d | %-12.8f | %-12.4e | [%.5f, %.5f, %.5f, %.5f]\n",
  66. iter, lambda_new, diff, y[0], y[1], y[2], y[3]);
  67. }
  68.  
  69. if (diff < EPSILON) {
  70. if (iter % 5 != 0 && iter > 5) { // 最終ステップを確実に表示させる
  71. printf("%-5d | %-12.8f | %-12.4e | [%.5f, %.5f, %.5f, %.5f]\n",
  72. iter, lambda_new, diff, y[0], y[1], y[2], y[3]);
  73. }
  74. break;
  75. }
  76.  
  77. // 次のステップに向けて x を更新
  78. for (int i = 0; i < SIZE; i++) {
  79. x[i] = y[i];
  80. }
  81. lambda_old = lambda_new;
  82. }
  83.  
  84.  
  85. printf("\n反復回数 %d 回で収束しました。\n", actual_iters);
  86. printf("最大固有値 (λ1) : %.10f\n", lambda_new);
  87. printf("対応する固有ベクトル (x1) : [");
  88. for (int i = 0; i < SIZE; i++) {
  89. printf("%.10f%s", y[i], (i == SIZE - 1) ? "]\n" : ", ");
  90. }
  91.  
  92. printf("\n 検証: Ax と λx の比較 \n");
  93. mat_vec_mul(A, y, Ax, SIZE); // Axを再計算
  94. printf("Ax : [");
  95. for (int i = 0; i < SIZE; i++) {
  96. printf("%.6f%s", Ax[i], (i == SIZE - 1) ? "]\n" : ", ");
  97. }
  98. printf("λ * x : [");
  99. for (int i = 0; i < SIZE; i++) {
  100. printf("%.6f%s", lambda_new * y[i], (i == SIZE - 1) ? "]\n" : ", ");
  101. }
  102.  
  103. return 0;
  104. }
  105.  
  106.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
初期ベクトル x(0): [1.0, 1.0, 1.0, 1.0]

Iter  | 固有値(新) | 差分値    | 固有ベクトルx(k)        
1     | 7.06015038   | 7.0602e+00   | [0.55069, 0.15019, 0.50063, 0.65081]
2     | 7.56272200   | 5.0257e-01   | [0.68485, 0.48918, 0.14675, 0.51975]
3     | 7.79665790   | 2.3394e-01   | [0.53345, 0.20811, 0.56744, 0.59172]
4     | 8.36088120   | 5.6422e-01   | [0.66241, 0.41253, 0.25086, 0.57281]
5     | 8.52180634   | 1.6093e-01   | [0.58882, 0.28456, 0.47179, 0.59139]
10    | 8.69758623   | 9.4221e-03   | [0.62524, 0.34192, 0.38099, 0.58908]
15    | 8.69591999   | 7.0277e-04   | [0.62205, 0.33644, 0.39006, 0.58967]
20    | 8.69624282   | 7.2845e-05   | [0.62237, 0.33699, 0.38916, 0.58962]
25    | 8.69621204   | 7.2757e-06   | [0.62234, 0.33693, 0.38925, 0.58962]
30    | 8.69621514   | 7.2934e-07   | [0.62235, 0.33694, 0.38924, 0.58962]
35    | 8.69621483   | 7.3086e-08   | [0.62234, 0.33694, 0.38924, 0.58962]
40    | 8.69621486   | 7.3240e-09   | [0.62234, 0.33694, 0.38924, 0.58962]
45    | 8.69621486   | 7.3395e-10   | [0.62234, 0.33694, 0.38924, 0.58962]

反復回数 45 回で収束しました。
最大固有値 (λ1)          : 8.6962148610
対応する固有ベクトル (x1) : [0.6223447229, 0.3369369049, 0.3892384556, 0.5896219066]

   検証: Ax と λx の比較   
Ax    : [5.412043, 2.930076, 3.384901, 5.127479]
λ * x : [5.412043, 2.930076, 3.384901, 5.127479]