Keyword: 最近傍相関行列
概要
本サンプルは最近傍相関行列の計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される行列に対して最近傍相関行列の計算を行います。
※本サンプルはnAG Cライブラリに含まれる関数 nag_nearest_correlation() のExampleコードです。本サンプル及び関数の詳細情報は nag_nearest_correlation のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はnag_nearest_correlation のマニュアルページを参照)この出力例をダウンロード |
nag_nearest_correlation (g02aac) Example Program Results Nearest Correlation Matrix 1.00000 -0.80841 0.19159 0.10678 -0.80841 1.00000 -0.65623 0.19159 0.19159 -0.65623 1.00000 -0.80841 0.10678 0.19159 -0.80841 1.00000 Number of Newton steps taken: 3 Number of function evaluations: 4
- 3〜7行目に最近傍相関行列が出力されています。
- 10行目に実行されたニュートンステップの数が出力されています。
- 11行目に関数評価の数が出力されています。
ソースコード
(本関数の詳細はnag_nearest_correlation のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
このソースコードをダウンロード |
/* nag_nearest_correlation (g02aac) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ /* Pre-processor includes */ #include <stdio.h> #include <math.h> #include <string.h> #include <nag.h> #include <nag_stdlib.h> #include <nagg02.h> int main(void) { /* Integer scalar and array declarations */ Integer exit_status = 0; Integer feval, i, iter, j, maxit, maxits, n; Integer pdg, pdx; /* Double scalar and array declarations */ double errtol, nrmgrd; double *g = 0, *x = 0; Nag_OrderType order; NagError fail; INIT_FAIL(fail); #ifdef nAG_COLUMN_MAJOR #define G(I, J) g[(J-1)*pdg + I-1] #define X(I, J) x[(J-1)*pdx + I-1] order = Nag_ColMajor; #else #define G(I, J) g[(I-1)*pdg + J-1] #define X(I, J) x[(I-1)*pdx + J-1] order = Nag_RowMajor; #endif printf("nag_nearest_correlation (g02aac) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n] "); /* Read in the problem size */ scanf("%ld%*[^\n] ", &n); pdg = n; pdx = n; if (!(g = nAG_ALLOC(pdg * n, double)) || !(x = nAG_ALLOC(pdx * n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read in the matrix g */ for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf("%lf", &G(i, j)); scanf("%*[^\n] "); /* Set up method parameters */ errtol = 1.00e-7; maxits = 200; maxit = 10; /* * nag_nearest_correlation (g02aac) * Computes the nearest correlation matrix to a real square matrix, * using the method of Qi and Sun */ nag_nearest_correlation(order, g, pdg, n, errtol, maxits, maxit, x, pdx, &iter, &feval, &nrmgrd, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_nearest_correlation (g02aac).\n%s\n", fail.message); exit_status = 1; goto END; } printf(" Nearest Correlation Matrix\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) printf("%11.5f%s", X(i, j), j % 4 ? " " : "\n"); } printf("\n"); printf("\n"); printf(" Number of Newton steps taken:%11ld\n", iter); printf(" Number of function evaluations:%9ld\n", feval); if (nrmgrd > errtol) printf(" Norm of gradient of last Newton step: %12.3e\n", nrmgrd); printf("\n"); END: nAG_FREE(g); nAG_FREE(x); return exit_status; }