1 /*****************************************************************************
2  *                                                                           *
3  *          UNURAN -- Universal Non-Uniform Random number generator          *
4  *                                                                           *
5  *****************************************************************************
6  *                                                                           *
7  *   FILE: urng_set.c                                                        *
8  *                                                                           *
9  *   routines to set, change and get the pointers to the                     *
10  *   uniform random number generators used in generator objects.
11  *                                                                           *
12  *****************************************************************************
13  *                                                                           *
14  *   Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold             *
15  *   Department of Statistics and Mathematics, WU Wien, Austria              *
16  *                                                                           *
17  *   This program is free software; you can redistribute it and/or modify    *
18  *   it under the terms of the GNU General Public License as published by    *
19  *   the Free Software Foundation; either version 2 of the License, or       *
20  *   (at your option) any later version.                                     *
21  *                                                                           *
22  *   This program is distributed in the hope that it will be useful,         *
23  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
24  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
25  *   GNU General Public License for more details.                            *
26  *                                                                           *
27  *   You should have received a copy of the GNU General Public License       *
28  *   along with this program; if not, write to the                           *
29  *   Free Software Foundation, Inc.,                                         *
30  *   59 Temple Place, Suite 330, Boston, MA 02111-1307, USA                  *
31  *                                                                           *
32  *****************************************************************************/
33 
34 /*---------------------------------------------------------------------------*/
35 
36 #include <unur_source.h>
37 #include "urng.h"
38 
39 /*---------------------------------------------------------------------------*/
40 
41 /*****************************************************************************/
42 /**                                                                         **/
43 /**  Main uniform random number generator                                   **/
44 /**                                                                         **/
45 /*****************************************************************************/
46 
47 int
unur_set_urng(struct unur_par * par,UNUR_URNG * urng)48 unur_set_urng( struct unur_par *par, UNUR_URNG *urng )
49      /*----------------------------------------------------------------------*/
50      /* set uniform random number generator                                  */
51      /*                                                                      */
52      /* parameters:                                                          */
53      /*   par     ... pointer to parameter for building generator object     */
54      /*   urng    ... pointer to uniform random number generator             */
55      /*                                                                      */
56      /* return:                                                              */
57      /*   UNUR_SUCCESS ... on success                                        */
58      /*   error code   ... on error                                          */
59      /*----------------------------------------------------------------------*/
60 {
61   /* check arguments */
62   _unur_check_NULL( NULL, par, UNUR_ERR_NULL );
63   _unur_check_NULL("URNG", urng, UNUR_ERR_NULL);
64 
65   /* main generator */
66   par->urng = urng;
67 
68   /* overwrite auxilliary generator */
69   if (par->urng_aux) par->urng_aux = urng;
70 
71   return UNUR_SUCCESS;
72 } /* end of unur_set_urng() */
73 
74 /*---------------------------------------------------------------------------*/
75 
76 UNUR_URNG *
unur_get_urng(struct unur_gen * gen)77 unur_get_urng( struct unur_gen *gen )
78      /*----------------------------------------------------------------------*/
79      /* get uniform random number generator                                  */
80      /*                                                                      */
81      /* parameters:                                                          */
82      /*   gen     ... pointer to generator object                            */
83      /*                                                                      */
84      /* return:                                                              */
85      /*   Pointer to old uniform RNG                                         */
86      /*                                                                      */
87      /* error:                                                               */
88      /*   return NULL                                                        */
89      /*----------------------------------------------------------------------*/
90 {
91   /* check arguments */
92   CHECK_NULL(gen,NULL);
93 
94   return gen->urng;
95 } /* end of unur_get_urng() */
96 
97 /*---------------------------------------------------------------------------*/
98 
99 UNUR_URNG *
unur_chg_urng(struct unur_gen * gen,UNUR_URNG * urng)100 unur_chg_urng( struct unur_gen *gen, UNUR_URNG *urng )
101      /*----------------------------------------------------------------------*/
102      /* set uniform random number generator                                  */
103      /*                                                                      */
104      /* parameters:                                                          */
105      /*   gen     ... pointer to generator object                            */
106      /*   urng    ... pointer to uniform random number generator             */
107      /*                                                                      */
108      /* return:                                                              */
109      /*   Pointer to old uniform RNG                                         */
110      /*                                                                      */
111      /* error:                                                               */
112      /*   return NULL                                                        */
113      /*----------------------------------------------------------------------*/
114 {
115   UNUR_URNG *urng_old;
116 
117   /* check arguments */
118   CHECK_NULL(gen,NULL);
119   CHECK_NULL(urng,NULL);
120 
121   urng_old = gen->urng;
122 
123   /* set pointer to main URNG */
124   gen->urng = urng;
125 
126   /* also set pointer in auxiliary generator objects */
127   if (gen->gen_aux)
128     unur_chg_urng(gen->gen_aux,urng);
129 
130   if (gen->gen_aux_list && gen->n_gen_aux_list) {
131     int i;
132     for (i=0; i<gen->n_gen_aux_list; i++) {
133       if (gen->gen_aux_list[i])
134 	unur_chg_urng(gen->gen_aux_list[i],urng);
135     }
136   }
137 
138   /* overwrite auxilliary URNG */
139   if (gen->urng_aux) gen->urng_aux = urng;
140 
141   return urng_old;
142 } /* end of unur_chg_urng() */
143 
144 /*---------------------------------------------------------------------------*/
145 
146 /*****************************************************************************/
147 /**                                                                         **/
148 /**  Auxiliary uniform random number generator                              **/
149 /**                                                                         **/
150 /*****************************************************************************/
151 
152 int
unur_set_urng_aux(struct unur_par * par,UNUR_URNG * urng_aux)153 unur_set_urng_aux( struct unur_par *par, UNUR_URNG *urng_aux )
154      /*----------------------------------------------------------------------*/
155      /* set auxilliary uniform random number generator                       */
156      /*                                                                      */
157      /* parameters:                                                          */
158      /*   par_aux ... pointer to parameter for building generator object     */
159      /*   urng    ... pointer to auxilliary uniform random number generator  */
160      /*                                                                      */
161      /* return:                                                              */
162      /*   UNUR_SUCCESS ... on success                                        */
163      /*   error code   ... on error                                          */
164      /*----------------------------------------------------------------------*/
165 {
166   /* check arguments */
167   _unur_check_NULL( NULL, par, UNUR_ERR_NULL );
168   _unur_check_NULL("URNGaux", urng_aux, UNUR_ERR_NULL);
169 
170   if (par->urng_aux == NULL)
171     /* no auxilliary generator is required */
172     return UNUR_ERR_GENERIC;
173 
174   par->urng_aux = urng_aux;
175 
176   return UNUR_SUCCESS;
177 } /* end of unur_set_urng_aux() */
178 
179 /*---------------------------------------------------------------------------*/
180 
181 UNUR_URNG *
unur_get_urng_aux(struct unur_gen * gen)182 unur_get_urng_aux( struct unur_gen *gen )
183      /*----------------------------------------------------------------------*/
184      /* get auxilliary uniform random number generator                       */
185      /*                                                                      */
186      /* parameters:                                                          */
187      /*   gen     ... pointer to generator object                            */
188      /*                                                                      */
189      /* return:                                                              */
190      /*   Pointer to old auxilliary uniform RNG                              */
191      /*                                                                      */
192      /* error:                                                               */
193      /*   return NULL                                                        */
194      /*----------------------------------------------------------------------*/
195 {
196   /* check arguments */
197   CHECK_NULL(gen,NULL);
198 
199   return gen->urng_aux;
200 } /* end of unur_get_urng_aux() */
201 
202 /*---------------------------------------------------------------------------*/
203 
204 UNUR_URNG *
unur_chg_urng_aux(struct unur_gen * gen,UNUR_URNG * urng_aux)205 unur_chg_urng_aux( struct unur_gen *gen, UNUR_URNG *urng_aux )
206      /*----------------------------------------------------------------------*/
207      /* set auxilliary uniform random number generator                       */
208      /*                                                                      */
209      /* parameters:                                                          */
210      /*   gen      ... pointer to generator object                           */
211      /*   urng_aux ... pointer to auxilliary uniform random number generator */
212      /*                                                                      */
213      /* return:                                                              */
214      /*   Pointer to old auxilliary uniform RNG                              */
215      /*                                                                      */
216      /* error:                                                               */
217      /*   return NULL                                                        */
218      /*----------------------------------------------------------------------*/
219 {
220   UNUR_URNG *urng_aux_old;
221 
222   /* check arguments */
223   CHECK_NULL(gen,NULL);
224   CHECK_NULL(urng_aux,NULL);
225 
226   if (gen->urng_aux == NULL)
227     /* no auxilliary generator is required */
228     return NULL;
229 
230   urng_aux_old = gen->urng_aux;
231 
232   /* set pointer to main URNG */
233   gen->urng_aux = urng_aux;
234 
235   /* also set pointer in auxiliary generator objects */
236   if (gen->gen_aux)
237     unur_chg_urng_aux(gen->gen_aux,urng_aux);
238 
239   if (gen->gen_aux_list && gen->n_gen_aux_list) {
240     int i;
241     for (i=0; i<gen->n_gen_aux_list; i++) {
242       if (gen->gen_aux_list[i])
243 	unur_chg_urng_aux(gen->gen_aux_list[i],urng_aux);
244     }
245   }
246 
247   return urng_aux_old;
248 } /* end of unur_chg_urng_aux() */
249 
250 /*---------------------------------------------------------------------------*/
251 
252 int
unur_use_urng_aux_default(UNUR_PAR * par)253 unur_use_urng_aux_default( UNUR_PAR *par )
254      /*----------------------------------------------------------------------*/
255      /* set auxilliary uniform random number generator to default            */
256      /* (initialize generator if necessary)                                  */
257      /*                                                                      */
258      /* parameters:                                                          */
259      /*   par     ... pointer to parameter for building generator object     */
260      /*                                                                      */
261      /* return:                                                              */
262      /*   UNUR_SUCCESS ... on success                                        */
263      /*   error code   ... on error                                          */
264      /*----------------------------------------------------------------------*/
265 {
266   if (par->urng_aux == NULL)
267     /* no auxilliary generator is required */
268     return UNUR_ERR_GENERIC;
269 
270   /* set aux URNG */
271   par->urng_aux = unur_get_default_urng_aux();
272 
273   return UNUR_SUCCESS;
274 
275 } /* end of unur_use_urng_aux_default() */
276 
277 /*---------------------------------------------------------------------------*/
278 
279 int
unur_chgto_urng_aux_default(UNUR_GEN * gen)280 unur_chgto_urng_aux_default( UNUR_GEN *gen )
281      /*----------------------------------------------------------------------*/
282      /* set auxilliary uniform random number generator to default            */
283      /* (initialize generator if necessary)                                  */
284      /*                                                                      */
285      /* parameters:                                                          */
286      /*   gen     ... pointer to generator object                            */
287      /*                                                                      */
288      /* return:                                                              */
289      /*   UNUR_SUCCESS ... on success                                        */
290      /*   error code   ... on error                                          */
291      /*----------------------------------------------------------------------*/
292 {
293   if (gen->urng_aux == NULL)
294     /* no auxilliary generator is required */
295     return UNUR_ERR_GENERIC;
296 
297   /* set aux URNG */
298   gen->urng_aux = unur_get_default_urng_aux();
299 
300   return UNUR_SUCCESS;
301 
302 } /* end of unur_chgto_urng_aux_default() */
303 
304 /*---------------------------------------------------------------------------*/
305 
306 
307