Mercurial > hg > octave-thorsten
changeset 10782:d1f920d1ce0c
simplify code in rand.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Jul 2010 11:09:59 +0200 |
parents | f7584d0ba5d3 |
children | fc9e07fdf9c2 |
files | src/ChangeLog src/DLD-FUNCTIONS/rand.cc |
diffstat | 2 files changed, 19 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2010-07-12 Jaroslav Hajek <highegg@gmail.com> + + * DLD-FUNCTIONS/rand.cc (do_rand): Pass the distribution name as an + argument. Ensure restoration using unwind_protect. + (Frand, Frandn, Frandg, Frandp, Frande): Update. + 2010-07-12 Jaroslav Hajek <highegg@gmail.com> * unwind-prot.h (unwind_protect::fcn_crefarg_elem): New class.
--- a/src/DLD-FUNCTIONS/rand.cc +++ b/src/DLD-FUNCTIONS/rand.cc @@ -51,13 +51,20 @@ static octave_value do_rand (const octave_value_list& args, int nargin, const char *fcn, - bool additional_arg = false) + const std::string& distribution, bool additional_arg = false) { octave_value retval; NDArray a; int idx = 0; dim_vector dims; + unwind_protect frame; + // Restore current distribution on any exit. + frame.add_fcn (octave_rand::distribution, + octave_rand::distribution ()); + + octave_rand::distribution (distribution); + if (additional_arg) { if (nargin == 0) @@ -397,7 +404,7 @@ int nargin = args.length (); - retval = do_rand (args, nargin, "rand"); + retval = do_rand (args, nargin, "rand", "uniform"); return retval; } @@ -508,21 +515,7 @@ int nargin = args.length (); - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "normal"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randn"); + retval = do_rand (args, nargin, "randn", "normal"); return retval; } @@ -581,21 +574,7 @@ int nargin = args.length (); - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "exponential"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "rande"); + retval = do_rand (args, nargin, "rande", "exponential"); return retval; } @@ -712,23 +691,7 @@ if (nargin < 1) error ("randg: insufficient arguments"); else - { - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "gamma"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randg", true); - } + retval = do_rand (args, nargin, "randg", "gamma", true); return retval; } @@ -927,23 +890,7 @@ if (nargin < 1) error ("randp: insufficient arguments"); else - { - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "poisson"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randp", true); - } + retval = do_rand (args, nargin, "randp", "poisson", true); return retval; }