スパース固有値問題

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > スパース固有値問題 (C言語/C++)

Keyword: スパース, 固有値問題

概要

本サンプルはスパース固有値問題を解くC言語によるサンプルプログラムです。 本サンプルは以下に示されるスパース固有値問題を解いて固有値を出力します。本サンプルプログラムでは、ルーチン f12acc のほか、f12aac、f12abc、f12adc や f12aec を呼び出しています。

スパース固有値問題のデータ 

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

入力データ

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

このデータをダウンロード
nag_real_sparse_eigensystem_sol (f12acc) Example Program Data
 10  4  20  10.0 : Values for nx, nev, ncv, rho

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に正方行列の行(列)の数(nx)、固有値の数(nev)、計算時に使用するためのArnoldi 規定ベクトルの数(ncv)、ρ(ロー)の値(rho)を指定しています。

出力結果

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

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

  The    4 generalized Ritz values of largest magnitude are:

       1     (   20383.0384 ,      0.0000 )
       2     (   20338.7563 ,      0.0000 )
       3     (   20265.2844 ,      0.0000 )
       4     (   20163.1142 ,      0.0000 )

  • 3行目には収束した固有値が4つあることが示されています。
  • 5から8行目に収束した近似固有値の実部と虚部が出力されています。

ソースコード

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

※本サンプルソースコードは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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

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

#include <nag.h>
#include <nag_stdlib.h>
#include <stdio.h>
#include <nagf12.h>
#include <nagf16.h>

static void av(Integer, double, double *, double *);
static void mv(Integer, double *, double *);
static void my_dpttrf(Integer, double *, double *, Integer *);
static void my_dpttrs(Integer, double *, double *, double *);

int main(void)
{
  /* Constants */
  Integer licomm = 140, imon = 0;
  /* Scalars */
  double estnrm, h, rho, sigmai = 0.0, sigmar = 0.0;
  Integer exit_status, info, irevcm, j, lcomm, n, nconv, ncv;
  Integer nev, niter, nshift, nx;
  /* Nag types */
  NagError fail;
  /* Arrays */
  double *comm = 0, *eigvr = 0, *eigvi = 0, *eigest = 0, *md = 0, *me = 0;
  double *resid = 0, *v = 0;
  Integer *icomm = 0;
  /* Pointers */
  double *mx = 0, *x = 0, *y = 0;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_real_sparse_eigensystem_sol (f12acc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read problem parameter values from data file. */
  scanf("%ld%ld%ld%lf%*[^\n] ", &nx, &nev, &ncv,
        &rho);
  n = nx * nx;
  lcomm = 3 * n + 3 * ncv * ncv + 6 * ncv + 60;
  /* Allocate memory */
  if (!(comm = nAG_ALLOC(lcomm, double)) ||
      !(eigvr = nAG_ALLOC(ncv, double)) ||
      !(eigvi = nAG_ALLOC(ncv, double)) ||
      !(eigest = nAG_ALLOC(ncv, double)) ||
      !(md = nAG_ALLOC(n, double)) ||
      !(me = nAG_ALLOC(n, double)) ||
      !(resid = nAG_ALLOC(n, double)) ||
      !(v = nAG_ALLOC(n * ncv, double)) ||
      !(icomm = nAG_ALLOC(licomm, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Initialize communication arrays for problem using
     nag_real_sparse_eigensystem_init (f12aac). */
  nag_real_sparse_eigensystem_init(n, nev, ncv, icomm, licomm, comm,
                                   lcomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_real_sparse_eigensystem_init (f12aac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  /* Set the mode. */
  /* Select the mode using
     nag_real_sparse_eigensystem_option (f12adc). */
  nag_real_sparse_eigensystem_option("REGULAR INVERSE", icomm, comm, &fail);
  /* Select the problem type using
     nag_real_sparse_eigensystem_option (f12adc). */
  nag_real_sparse_eigensystem_option("GENERALIZED", icomm, comm, &fail);

  /* Construct M, and factorize using my_dpttrf. */
  h = 1.0 / (double) (n + 1);
  for (j = 0; j <= n - 2; ++j) {
    md[j] = h * 4.0;
    me[j] = h;
  }
  md[n - 1] = h * 4.0;

  my_dpttrf(n, md, me, &info);

  irevcm = 0;
REVCOMLOOP:
  /* repeated calls to reverse communication routine
     nag_real_sparse_eigensystem_iter (f12abc). */
  nag_real_sparse_eigensystem_iter(&irevcm, resid, v, &x, &y, &mx,
                                   &nshift, comm, icomm, &fail);
  if (irevcm != 5) {
    if (irevcm == -1 || irevcm == 1) {
      /* Perform  y <--- OP*x = inv[M]*A*x using my_dpttrs. */
      av(nx, rho, x, y);
      my_dpttrs(n, md, me, y);
    }
    else if (irevcm == 2) {
      /* Perform  y <--- M*x. */
      mv(nx, x, y);
    }
    else if (irevcm == 4 && imon == 1) {
      /* If imon=1, get monitoring information using
         nag_real_sparse_eigensystem_monit (f12aec). */
      nag_real_sparse_eigensystem_monit(&niter, &nconv, eigvr,
                                        eigvi, eigest, icomm, comm);
      /* Compute 2-norm of Ritz estimates using
         nag_dge_norm (f16rac). */
      nag_dge_norm(Nag_ColMajor, Nag_FrobeniusNorm, nev, 1, eigest,
                   nev, &estnrm, &fail);
      printf("Iteration %3ld, ", niter);
      printf(" No. converged = %3ld,", nconv);
      printf(" norm of estimates = %17.8e\n", estnrm);
    }
    goto REVCOMLOOP;
  }
  if (fail.code == NE_NOERROR) {
    /* Post-Process using nag_real_sparse_eigensystem_sol (f12acc)
       to compute eigenvalues/vectors. */
    nag_real_sparse_eigensystem_sol(&nconv, eigvr, eigvi, v, sigmar,
                                    sigmai, resid, v, comm, icomm, &fail);
    /* Print computed eigenvalues. */
    printf("\n  The %4ld generalized", nconv);
    printf(" Ritz values of largest magnitude are:\n\n");
    for (j = 0; j <= nconv - 1; ++j) {
      printf("%8ld%5s( %12.4f ,%12.4f )\n", j + 1, "",
             eigvr[j], eigvi[j]);
    }
  }
  else {
    printf(" Error from nag_real_sparse_eigensystem_iter (f12abc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
END:
  nAG_FREE(comm);
  nAG_FREE(eigvr);
  nAG_FREE(eigvi);
  nAG_FREE(eigest);
  nAG_FREE(md);
  nAG_FREE(me);
  nAG_FREE(resid);
  nAG_FREE(v);
  nAG_FREE(icomm);
  return exit_status;
}

static void av(Integer nx, double rho, double *v, double *y)
{
  /* Scalars */
  double dd, dl, du, h, s;
  Integer j, n;
  /* Function Body */
  n = nx * nx;
  h = 1.0 / (double) (n + 1);
  s = rho / 2.0;
  dd = 2.0 / h;
  dl = -1.0 / h - s;
  du = -1.0 / h + s;
  y[0] = dd * v[0] + du * v[1];
  for (j = 1; j <= n - 2; ++j) {
    y[j] = dl * v[j - 1] + dd * v[j] + du * v[j + 1];
  }
  y[n - 1] = dl * v[n - 2] + dd * v[n - 1];
  return;
} /* av */

static void mv(Integer nx, double *v, double *y)
{
  /* Scalars */
  double h;
  Integer j, n;
  /* Function Body */
  n = nx * nx;
  h = 1. / (double) (n + 1);
  y[0] = h * (v[0] * 4. + v[1]);
  for (j = 1; j <= n - 2; ++j) {
    y[j] = h * (v[j - 1] + v[j] * 4. + v[j + 1]);
  }
  y[n - 1] = h * (v[n - 2] + v[n - 1] * 4.);
  return;
} /* mv */

static void my_dpttrf(Integer n, double d[], double e[], Integer *info)
{
  /* A simple C version of the Lapack routine dpttrf with argument
     checking removed */
  /* Scalars */
  double ei;
  Integer i;
  /* Function Body */
  *info = 0;
  for (i = 0; i < n - 1; ++i) {
    if (d[i] <= 0.0) {
      *info = i + 1;
      goto END_DPTTRF;
    }
    ei = e[i];
    e[i] = ei / d[i];
    d[i + 1] = d[i + 1] - e[i] * ei;
  }
  if (d[n - 1] <= 0.0) {
    *info = n;
  }
END_DPTTRF:
  return;
}

static void my_dpttrs(Integer n, double d[], double e[], double b[])
{
  /* A simple C version of the Lapack routine dpttrs with argument
     checking removed and nrhs=1 */
  /* Scalars */
  Integer i;
  /* Function Body */
  for (i = 1; i < n; ++i) {
    b[i] = b[i] - b[i - 1] * e[i - 1];
  }
  b[n - 1] = b[n - 1] / d[n - 1];
  for (i = n - 2; i >= 0; --i) {
    b[i] = b[i] / d[i] - b[i + 1] * e[i];
  }
  return;
}


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