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 のマニュアルページを参照)このデータをダウンロード |
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 のマニュアルページを参照)この出力例をダウンロード |
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等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
このソースコードをダウンロード |
/* 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; }