File: example_reinit.c

package info (click to toggle)
unuran 1.11.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,344 kB
  • sloc: ansic: 204,607; sh: 4,783; perl: 3,160; makefile: 839; cpp: 24
file content (104 lines) | stat: -rw-r--r-- 3,748 bytes parent folder | download | duplicates (2)
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
/* ------------------------------------------------------------- */
/* File: example_reinit.c                                        */
/* ------------------------------------------------------------- */

/* Include UNURAN header file.                                   */
#include <unuran.h>

/* ------------------------------------------------------------- */

/* In this example we show how the parameters of the underlying  */
/* distribution can be changed for an existing generator object. */

/* We use the GIG distribution with method CSTD.                 */

/* ------------------------------------------------------------- */

int main(void)
{
  int    i;     /* loop variable                                 */
  double x;     /* will hold the random number                   */

  /* Parameters of distribution.                                 */ 
  double dparam[3] = {0.5, 1., 5.};

  /* Declare the three UNURAN objects.                           */
  UNUR_DISTR *distr;    /* distribution object                   */
  UNUR_PAR   *par;      /* parameter object                      */
  UNUR_GEN   *gen;      /* generator object                      */

  /* Create initial GIG distribution object                      */
  distr = unur_distr_gig(dparam, 3);

  /* Choose a method: CSTD.                                      */
  par = unur_cstd_new(distr);

  /* Create the generator object.                                */
  gen = unur_init(par);

  /* It is important to check if the creation of the generator   */
  /* object was successful. Otherwise `gen' is the NULL pointer  */ 
  /* and would cause a segmentation fault if used for sampling.  */
  if (gen == NULL) {
     fprintf(stderr, "ERROR: cannot create generator object\n");
     exit (EXIT_FAILURE);
  }

  /* It is possible to reuse the distribution object to create   */
  /* another generator object. If you do not need it any more,   */
  /* it should be destroyed to free memory.                      */
  unur_distr_free(distr);
  
  /* Now you can use the generator object `gen' to sample from   */
  /* the distribution. Eg.:                                      */
  for (i=0; i<10; i++) {
    x = unur_sample_cont(gen);
    printf("%f\n",x);
  }

  /* It is possible for method CSTD to change the parameters of  */
  /* underlying distribution. However, we have to extract to     */
  /* pointer to the distribution. Be carefull with this pointer! */
  distr = unur_get_distr(gen);

  /* Change the parameter(s).                                    */
  dparam[2] = 0.001;
  unur_distr_cont_set_pdfparams(distr,dparam,3);

  /* Do not forget to reinitialize the generator object.         */
  /* Check the return code.                                      */
  /* (and try to find a better error handling)                   */
  if (unur_reinit(gen) != UNUR_SUCCESS) {
    fprintf(stderr, "ERROR: cannot reinitialize generator object\n");
    exit (EXIT_FAILURE);
  }

  /* Draw a new sample.                                          */
  for (i=0; i<10; i++) {
    x = unur_sample_cont(gen);
    printf("%f\n",x);
  }

  /* Changing parameters can be repeated.                        */
  dparam[2] = 1000;
  unur_distr_cont_set_pdfparams(distr,dparam,3);
  if (unur_reinit(gen) != UNUR_SUCCESS) {
    fprintf(stderr, "ERROR: cannot reinitialize generator object\n");
    exit (EXIT_FAILURE);
  }
  for (i=0; i<10; i++) {
    x = unur_sample_cont(gen);
    printf("%f\n",x);
  }

  /* When you do not need the generator object any more, you     */
  /* can destroy it.                                             */
  unur_free(gen);

  exit (EXIT_SUCCESS);

} /* end of main() */

/* ------------------------------------------------------------- */