Keyword: 一様準乱数列
概要
本サンプルは一様準乱数列の生成を行うC言語によるサンプルプログラムです。 本サンプルは一様準乱数列を生成し以下に示される積分を推定し出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_quasi_rand_uniform() のExampleコードです。本サンプル及び関数の詳細情報は nag_quasi_rand_uniform のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はnag_quasi_rand_uniform のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14
この出力例をダウンロード |
nag_quasi_rand_uniform (g05ymc) Example Program Results Value of integral = 1.0410 First 10 variates 1 0.7197 0.5967 0.0186 0.1768 0.7803 0.4072 0.5459 0.3994 2 0.9697 0.3467 0.7686 0.9268 0.5303 0.1572 0.2959 0.1494 3 0.4697 0.8467 0.2686 0.4268 0.0303 0.6572 0.7959 0.6494 4 0.3447 0.4717 0.1436 0.3018 0.1553 0.7822 0.4209 0.0244 5 0.8447 0.9717 0.6436 0.8018 0.6553 0.2822 0.9209 0.5244 6 0.5947 0.2217 0.3936 0.0518 0.9053 0.0322 0.1709 0.7744 7 0.0947 0.7217 0.8936 0.5518 0.4053 0.5322 0.6709 0.2744 8 0.0635 0.1904 0.0498 0.4580 0.6240 0.2510 0.9521 0.8057 9 0.5635 0.6904 0.5498 0.9580 0.1240 0.7510 0.4521 0.3057 10 0.8135 0.4404 0.2998 0.2080 0.3740 0.5010 0.7021 0.0557
- 2行目に推定された積分の値が出力されています。
- 5~14行目に生成された一様準乱数列の最初の10行が出力されています。
ソースコード
(本関数の詳細はnag_quasi_rand_uniform のマニュアルページを参照)
※本サンプルソースコードは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
このソースコードをダウンロード |
/* nag_quasi_rand_uniform (g05ymc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ /* Pre-processor includes */ #include <stdio.h> #include <math.h> #include <nag.h> #include <nag_stdlib.h> #include <nagg05.h> #define QUAS(I, J) quas[(order == Nag_ColMajor)?(J*pdquas + I):(I*pdquas + J)] int main(void) { /* Integer scalar and array declarations */ Integer exit_status = 0; Integer liref, d, i, j, q_size; Integer *iref = 0; Integer pdquas; /* nAG structures */ NagError fail; /* Double scalar and array declarations */ double sum, tmp, vsbl; double *quas = 0; /* Number of dimensions */ Integer idim = 8; /* Set the sample size */ Integer n = 200; /* Skip the first 1000 variates */ Integer iskip = 1000; /* Use row major order */ Nag_OrderType order = Nag_RowMajor; /* Choose the quasi generator */ Nag_QuasiRandom_Sequence genid = Nag_QuasiRandom_Sobol; /* Initialize the error structure */ INIT_FAIL(fail); printf("nag_quasi_rand_uniform (g05ymc) Example Program Results\n"); pdquas = (order == Nag_RowMajor) ? idim : n; q_size = (order == Nag_RowMajor) ? pdquas * n : pdquas * idim; /* Calculate the size of the reference vector */ liref = (genid == Nag_QuasiRandom_Faure) ? 407 : 32 * idim + 7; /* Allocate arrays */ if (!(quas = nAG_ALLOC(q_size, double)) || !(iref = nAG_ALLOC(liref, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Initialize the Sobol generator */ nag_quasi_init(genid, idim, iref, liref, iskip, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_quasi_init (g05ylc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate n quasi-random variates */ nag_quasi_rand_uniform(order, n, quas, pdquas, iref, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_quasi_rand_uniform (g05ymc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Estimate integral by evaluating function at each variate and summing */ sum = 0.0e0; for (i = 0; i < n; i++) { tmp = 1.0e0; for (d = 0; d < idim; d++) tmp *= fabs(4.0e0 * QUAS(i, d) - 2.0e0); sum += tmp; } /* Convert sum to mean value */ vsbl = sum / (double) n; /* Print the estimated value of the integral */ printf("Value of integral = %8.4f\n\n", vsbl); /* Display the first 10 variates used */ printf("First 10 variates\n"); for (i = 0; i < 10; i++) { printf(" %3ld ", i + 1); for (j = 0; j < idim; j++) printf("%8.4f%s", QUAS(i, j), ((j + 1) % 20) ? " " : "\n"); if (idim % 20) printf("\n"); } END: nAG_FREE(quas); nAG_FREE(iref); return exit_status; }