実対称行列の全ての固有値とオプションで全ての固有ベクトルを計算

C言語によるサンプルソースコード : 使用関数名:nag_dsyevd (f08fcc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 実対称行列の全ての固有値とオプションで全ての固有ベクトルを計算 (C言語/C++)

Keyword: 実対称行列, 固有値, 固有ベクトル

概要

本サンプルは実対称行列の全ての固有値とオプションで全ての固有ベクトルの計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される実対称行列の全ての固有値と全ての固有ベクトルを計算し出力します。

実対称行列のデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_dsyevd() のExampleコードです。本サンプル及び関数の詳細情報は nag_dsyevd のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_dsyevd のマニュアルページを参照)
1
2
3
4
5
6
7
8

このデータをダウンロード
nag_dsyevd (f08fcc) Example Program Data
  4                         :Value of n
  Nag_Lower                 :Value of uplo
  1.0
  2.0   2.0
  3.0   3.0   3.0
  4.0   4.0   4.0   4.0     :End of matrix A
  Nag_EigVecs               :Value of job

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に行列Aの次数(n)を指定しています。
  • 3行目に行列Aの上三角部分を格納するか下三角部分を格納するか(uplo)を指定しています。"Nag_Lower"は下三角部分を格納することを意味します。
  • 4~7行目に行列Aの要素を指定しています。
  • 8行目に固有ベクトルを計算するかどうか(job)を指定しています。"Nag_EigVecs"は固有値と固有ベクトルが計算されることを意味します。

出力結果

(本関数の詳細はnag_dsyevd のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10

この出力例をダウンロード
nag_dsyevd (f08fcc) Example Program Results

Eigenvalues
   -2.0531   -0.5146   -0.2943   12.8621
 Eigenvectors
            1          2          3          4
 1      1.0000     1.0000     1.0000     1.0000
 2      0.5129    -0.9431    -2.3976     1.0777
 3     -0.2240    -1.0537     2.3508     1.2393
 4     -0.8518     0.8831    -0.8879     1.4972

  • 4行目に固有値が出力されています。
  • 7~11行目に固有ベクトルが出力されています。

ソースコード

(本関数の詳細はnag_dsyevd のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

このソースコードをダウンロード
/* nag_dsyevd (f08fcc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer i, j, n, pda, w_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_JobType job;
  Nag_UploType uplo;
  Nag_OrderType order;
  /* Arrays */
  char nag_enum_arg[40];
  double *a = 0, *w = 0;

#ifdef nAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dsyevd (f08fcc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &n);
  pda = n;
  w_len = n;

  /* Allocate memory */
  if (!(a = nAG_ALLOC(n * n, double)) || !(w = nAG_ALLOC(w_len, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read whether Upper or Lower part of A is stored */
  scanf("%39s%*[^\n] ", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts nAG enum member name to value
   */
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);
  /* Read A from data file */
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= n; ++j)
        scanf("%lf", &A(i, j));
    }
    scanf("%*[^\n] ");
  }
  else {
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= i; ++j)
        scanf("%lf", &A(i, j));
    }
    scanf("%*[^\n] ");
  }
  /* Read type of job to be performed */
  scanf("%39s%*[^\n] ", nag_enum_arg);
  job = (Nag_JobType) nag_enum_name_to_value(nag_enum_arg);
  /* Calculate all the eigenvalues and eigenvectors of A */
  /* nag_dsyevd (f08fcc).
   * All eigenvalues and optionally all eigenvectors of real
   * symmetric matrix (divide-and-conquer)
   */
  nag_dsyevd(order, job, uplo, n, a, pda, w, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dsyevd (f08fcc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Normalize the eigenvectors */
  for (j = 1; j <= n; j++) {
    for (i = n; i >= 1; i--) {
      A(i, j) = A(i, j) / A(1, j);
    }
  }
  /* Print eigenvalues and eigenvectors */
  printf("Eigenvalues\n");
  for (i = 0; i < n; ++i)
    printf("  %8.4f", w[i]);
  printf("\n");
  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a,
                         pda, "Eigenvectors", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
END:
  nAG_FREE(a);
  nAG_FREE(w);
  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks