Mercurial > octave-nkf
annotate liboctave/oct-rand.cc @ 14026:3781981be535 ss-3-5-90
snapshot 3.5.90
* configure.ac (AC_INIT): Version is now 3.5.90.
(OCTAVE_API_VERSION_NUMBER): Now 46.
(OCTAVE_RELEASE_DATE): Now 2011-12-11.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 11 Dec 2011 23:18:31 -0500 |
parents | 7dd7cccf0757 |
children | 72c96de7a403 |
rev | line source |
---|---|
4308 | 1 /* |
2 | |
11523 | 3 Copyright (C) 2003-2011 John W. Eaton |
4308 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4308 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4308 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
26 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
27 #include <map> |
5730 | 28 #include <vector> |
4308 | 29 |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
30 #include <stdint.h> |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
31 |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
32 #include "data-conv.h" |
4308 | 33 #include "f77-fcn.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
34 #include "lo-error.h" |
5730 | 35 #include "lo-ieee.h" |
36 #include "lo-mappers.h" | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
37 #include "mach-info.h" |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
38 #include "oct-locbuf.h" |
4308 | 39 #include "oct-rand.h" |
4415 | 40 #include "oct-time.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
41 #include "randgamma.h" |
5730 | 42 #include "randmtzig.h" |
43 #include "randpoisson.h" | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
44 #include "singleton-cleanup.h" |
4308 | 45 |
46 extern "C" | |
47 { | |
4552 | 48 F77_RET_T |
49 F77_FUNC (dgennor, DGENNOR) (const double&, const double&, double&); | |
4308 | 50 |
4552 | 51 F77_RET_T |
52 F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, double&); | |
53 | |
54 F77_RET_T | |
5730 | 55 F77_FUNC (dgenexp, DGENEXP) (const double&, double&); |
56 | |
57 F77_RET_T | |
58 F77_FUNC (dignpoi, DIGNPOI) (const double&, double&); | |
59 | |
60 F77_RET_T | |
61 F77_FUNC (dgengam, DGENGAM) (const double&, const double&, double&); | |
62 | |
63 F77_RET_T | |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
64 F77_FUNC (setall, SETALL) (const int32_t&, const int32_t&); |
4308 | 65 |
4552 | 66 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
67 F77_FUNC (getsd, GETSD) (int32_t&, int32_t&); |
4308 | 68 |
4552 | 69 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
70 F77_FUNC (setsd, SETSD) (const int32_t&, const int32_t&); |
4308 | 71 |
4552 | 72 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
73 F77_FUNC (setcgn, SETCGN) (const int32_t&); |
4308 | 74 } |
75 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
76 octave_rand *octave_rand::instance = 0; |
6326 | 77 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
78 octave_rand::octave_rand (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
79 : current_distribution (uniform_dist), use_old_generators (false), |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
80 rand_states () |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
81 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
82 initialize_ranlib_generators (); |
4308 | 83 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
84 initialize_mersenne_twister (); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
85 } |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
86 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
87 bool |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
88 octave_rand::instance_ok (void) |
4308 | 89 { |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
90 bool retval = true; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
91 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
92 if (! instance) |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
93 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
94 instance = new octave_rand (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
95 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
96 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
97 singleton_cleanup_list::add (cleanup_instance); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
98 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
99 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
100 if (! instance) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
101 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
102 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
103 ("unable to create octave_rand object!"); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
105 retval = false; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
106 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
108 return retval; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
109 } |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
111 double |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
112 octave_rand::do_seed (void) |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
113 { |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
114 union d2i { double d; int32_t i[2]; }; |
4308 | 115 union d2i u; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
116 |
6435 | 117 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
118 | |
119 switch (ff) | |
120 { | |
121 case oct_mach_info::flt_fmt_ieee_big_endian: | |
122 F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]); | |
123 break; | |
124 default: | |
125 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); | |
126 break; | |
127 } | |
128 | |
4308 | 129 return u.d; |
130 } | |
131 | |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
132 static int32_t |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
133 force_to_fit_range (int32_t i, int32_t lo, int32_t hi) |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
134 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
135 assert (hi > lo && lo >= 0 && hi > lo); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
136 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
137 i = i > 0 ? i : -i; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
138 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
139 if (i < lo) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
140 i = lo; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
141 else if (i > hi) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
142 i = i % hi; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
143 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
144 return i; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
145 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
146 |
4308 | 147 void |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
148 octave_rand::do_seed (double s) |
4308 | 149 { |
5730 | 150 use_old_generators = true; |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
151 |
6435 | 152 int i0, i1; |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
153 union d2i { double d; int32_t i[2]; }; |
4308 | 154 union d2i u; |
155 u.d = s; | |
6435 | 156 |
157 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); | |
158 | |
159 switch (ff) | |
160 { | |
161 case oct_mach_info::flt_fmt_ieee_big_endian: | |
162 i1 = force_to_fit_range (u.i[0], 1, 2147483563); | |
163 i0 = force_to_fit_range (u.i[1], 1, 2147483399); | |
164 break; | |
165 default: | |
166 i0 = force_to_fit_range (u.i[0], 1, 2147483563); | |
167 i1 = force_to_fit_range (u.i[1], 1, 2147483399); | |
168 break; | |
169 } | |
170 | |
4308 | 171 F77_FUNC (setsd, SETSD) (i0, i1); |
172 } | |
173 | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
174 void |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
175 octave_rand::do_reset (void) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
176 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
177 use_old_generators = true; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
178 initialize_ranlib_generators (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
179 } |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
180 |
5730 | 181 ColumnVector |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
182 octave_rand::do_state (const std::string& d) |
5730 | 183 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
184 return rand_states[d.empty () ? current_distribution : get_dist_id (d)]; |
5730 | 185 } |
186 | |
187 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
188 octave_rand::do_state (const ColumnVector& s, const std::string& d) |
5730 | 189 { |
190 use_old_generators = false; | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
191 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
192 int old_dist = current_distribution; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
193 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
194 int new_dist = d.empty () ? current_distribution : get_dist_id (d); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
195 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
196 ColumnVector saved_state; |
5730 | 197 |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
198 if (old_dist != new_dist) |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
199 saved_state = get_internal_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
200 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
201 set_internal_state (s); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
202 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
203 rand_states[new_dist] = get_internal_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
204 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
205 if (old_dist != new_dist) |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
206 rand_states[old_dist] = saved_state; |
5730 | 207 } |
208 | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
209 void |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
210 octave_rand::do_reset (const std::string& d) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
211 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
212 use_old_generators = false; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
213 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
214 int old_dist = current_distribution; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
215 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
216 int new_dist = d.empty () ? current_distribution : get_dist_id (d); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
217 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
218 ColumnVector saved_state; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
219 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
220 if (old_dist != new_dist) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
221 saved_state = get_internal_state (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
222 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
223 oct_init_by_entropy (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
224 rand_states[new_dist] = get_internal_state (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
225 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
226 if (old_dist != new_dist) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
227 rand_states[old_dist] = saved_state; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
228 } |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
229 |
4308 | 230 std::string |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
231 octave_rand::do_distribution (void) |
4308 | 232 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
233 std::string retval; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
234 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
235 switch (current_distribution) |
4308 | 236 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
237 case uniform_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
238 retval = "uniform"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
239 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
240 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
241 case normal_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
242 retval = "normal"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
243 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
244 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
245 case expon_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
246 retval = "exponential"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
247 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
248 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
249 case poisson_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
250 retval = "poisson"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
251 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
252 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
253 case gamma_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
254 retval = "gamma"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
255 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
256 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
257 default: |
7536 | 258 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
259 ("rand: invalid distribution ID = %d", current_distribution); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
260 break; |
4308 | 261 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
262 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
263 return retval; |
4308 | 264 } |
265 | |
266 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
267 octave_rand::do_distribution (const std::string& d) |
4308 | 268 { |
7536 | 269 int id = get_dist_id (d); |
270 | |
271 switch (id) | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
272 { |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
273 case uniform_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
274 octave_rand::uniform_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
275 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
276 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
277 case normal_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
278 octave_rand::normal_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
279 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
280 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
281 case expon_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
282 octave_rand::exponential_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
283 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
284 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
285 case poisson_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
286 octave_rand::poisson_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
287 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
288 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
289 case gamma_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
290 octave_rand::gamma_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
291 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
292 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
293 default: |
7536 | 294 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
295 ("rand: invalid distribution ID = %d", id); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
296 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
297 } |
4308 | 298 } |
299 | |
300 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
301 octave_rand::do_uniform_distribution (void) |
4308 | 302 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
303 switch_to_generator (uniform_dist); |
4308 | 304 |
305 F77_FUNC (setcgn, SETCGN) (uniform_dist); | |
306 } | |
307 | |
308 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
309 octave_rand::do_normal_distribution (void) |
4308 | 310 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
311 switch_to_generator (normal_dist); |
4308 | 312 |
313 F77_FUNC (setcgn, SETCGN) (normal_dist); | |
314 } | |
315 | |
5730 | 316 void |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
317 octave_rand::do_exponential_distribution (void) |
5730 | 318 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
319 switch_to_generator (expon_dist); |
5730 | 320 |
321 F77_FUNC (setcgn, SETCGN) (expon_dist); | |
322 } | |
323 | |
324 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
325 octave_rand::do_poisson_distribution (void) |
5730 | 326 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
327 switch_to_generator (poisson_dist); |
5730 | 328 |
329 F77_FUNC (setcgn, SETCGN) (poisson_dist); | |
330 } | |
331 | |
332 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
333 octave_rand::do_gamma_distribution (void) |
5730 | 334 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
335 switch_to_generator (gamma_dist); |
5730 | 336 |
337 F77_FUNC (setcgn, SETCGN) (gamma_dist); | |
338 } | |
339 | |
340 | |
4308 | 341 double |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
342 octave_rand::do_scalar (double a) |
4308 | 343 { |
344 double retval = 0.0; | |
345 | |
5730 | 346 if (use_old_generators) |
4308 | 347 { |
5730 | 348 switch (current_distribution) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
349 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
350 case uniform_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
351 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
352 break; |
4308 | 353 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
354 case normal_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
355 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
356 break; |
4308 | 357 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
358 case expon_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
359 F77_FUNC (dgenexp, DGENEXP) (1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
360 break; |
4308 | 361 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
362 case poisson_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
363 if (a < 0.0 || xisnan(a) || xisinf(a)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
364 retval = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
365 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
366 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
367 // workaround bug in ignpoi, by calling with different Mu |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
368 F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
369 F77_FUNC (dignpoi, DIGNPOI) (a, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
370 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
371 break; |
4308 | 372 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 case gamma_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
375 retval = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
376 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
377 F77_FUNC (dgengam, DGENGAM) (1.0, a, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
378 break; |
4308 | 379 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
380 default: |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
381 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
382 ("rand: invalid distribution ID = %d", current_distribution); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
383 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
384 } |
4308 | 385 } |
386 else | |
4543 | 387 { |
388 switch (current_distribution) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
389 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
390 case uniform_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
391 retval = oct_randu (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
392 break; |
4543 | 393 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
394 case normal_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
395 retval = oct_randn (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
396 break; |
5730 | 397 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
398 case expon_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
399 retval = oct_rande (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
400 break; |
5730 | 401 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
402 case poisson_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
403 retval = oct_randp (a); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
404 break; |
5730 | 405 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
406 case gamma_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
407 retval = oct_randg (a); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
408 break; |
4543 | 409 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
410 default: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
411 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
412 ("rand: invalid distribution ID = %d", current_distribution); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
413 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
414 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
415 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
416 save_state (); |
4543 | 417 } |
418 | |
419 return retval; | |
420 } | |
421 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
422 Matrix |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
423 octave_rand::do_matrix (octave_idx_type n, octave_idx_type m, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
424 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
425 Matrix retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
426 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
427 if (n >= 0 && m >= 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
428 { |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
429 retval.clear (n, m); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
430 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
431 if (n > 0 && m > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
432 fill (retval.capacity(), retval.fortran_vec(), a); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
433 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
434 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
435 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
436 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
437 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
438 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
439 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
440 NDArray |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
441 octave_rand::do_nd_array (const dim_vector& dims, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
442 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
443 NDArray retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
444 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
445 if (! dims.all_zero ()) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
446 { |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
447 retval.clear (dims); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
448 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
449 fill (retval.capacity(), retval.fortran_vec(), a); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
450 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
451 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
452 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
453 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
454 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
455 Array<double> |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
456 octave_rand::do_vector (octave_idx_type n, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
457 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
458 Array<double> retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
459 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
460 if (n > 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
461 { |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10331
diff
changeset
|
462 retval.clear (n, 1); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
463 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
464 fill (retval.capacity (), retval.fortran_vec (), a); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
465 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
466 else if (n < 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
467 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
468 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
469 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
470 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
471 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
472 // Make the random number generator give us a different sequence every |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
473 // time we start octave unless we specifically set the seed. The |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
474 // technique used below will cycle monthly, but it it does seem to |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
475 // work ok to give fairly different seeds each time Octave starts. |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
476 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
477 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
478 octave_rand::initialize_ranlib_generators (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
479 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
480 octave_localtime tm; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
481 int stored_distribution = current_distribution; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
482 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
483 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
484 int hour = tm.hour() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
485 int minute = tm.min() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
486 int second = tm.sec() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
487 |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
488 int32_t s0 = tm.mday() * hour * minute * second; |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
489 int32_t s1 = hour * minute * second; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
490 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
491 s0 = force_to_fit_range (s0, 1, 2147483563); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
492 s1 = force_to_fit_range (s1, 1, 2147483399); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
493 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
494 F77_FUNC (setall, SETALL) (s0, s1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
495 F77_FUNC (setcgn, SETCGN) (stored_distribution); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
496 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
497 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
498 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
499 octave_rand::initialize_mersenne_twister (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
500 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
501 oct_init_by_entropy (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
502 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
503 ColumnVector s = get_internal_state (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
504 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
505 rand_states[uniform_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
506 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
507 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
508 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
509 rand_states[normal_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
510 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
511 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
512 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
513 rand_states[expon_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
514 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
515 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
516 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
517 rand_states[poisson_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
518 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
519 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
520 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
521 rand_states[gamma_dist] = s; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
522 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
523 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
524 ColumnVector |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
525 octave_rand::get_internal_state (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
526 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
527 ColumnVector s (MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
528 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
529 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
530 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
531 oct_get_state (tmp); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
532 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
533 for (octave_idx_type i = 0; i <= MT_N; i++) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
534 s.elem (i) = static_cast<double> (tmp [i]); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
535 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
536 return s; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
537 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
538 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
539 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
540 octave_rand::save_state (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
541 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
542 rand_states[current_distribution] = get_internal_state ();; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
543 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
544 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
545 int |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
546 octave_rand::get_dist_id (const std::string& d) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
547 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
548 int retval = unknown_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
549 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
550 if (d == "uniform" || d == "rand") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
551 retval = uniform_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
552 else if (d == "normal" || d == "randn") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
553 retval = normal_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
554 else if (d == "exponential" || d == "rande") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
555 retval = expon_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
556 else if (d == "poisson" || d == "randp") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
557 retval = poisson_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
558 else if (d == "gamma" || d == "randg") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
559 retval = gamma_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
560 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
561 (*current_liboctave_error_handler) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
562 ("rand: invalid distribution `%s'", d.c_str ()); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
563 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
564 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
565 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
566 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
567 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
568 octave_rand::set_internal_state (const ColumnVector& s) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
569 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
570 octave_idx_type len = s.length (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
571 octave_idx_type n = len < MT_N + 1 ? len : MT_N + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
572 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
573 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
574 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
575 for (octave_idx_type i = 0; i < n; i++) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
576 tmp[i] = static_cast<uint32_t> (s.elem(i)); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
577 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
578 if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
579 oct_set_state (tmp); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
580 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
581 oct_init_by_array (tmp, len); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
582 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
583 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
584 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
585 octave_rand::switch_to_generator (int dist) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
586 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
587 if (dist != current_distribution) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
588 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
589 current_distribution = dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
590 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
591 set_internal_state (rand_states[dist]); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
592 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
593 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
594 |
5730 | 595 #define MAKE_RAND(len) \ |
4308 | 596 do \ |
597 { \ | |
598 double val; \ | |
5730 | 599 for (volatile octave_idx_type i = 0; i < len; i++) \ |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
600 { \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
601 octave_quit (); \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
602 RAND_FUNC (val); \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
603 v[i] = val; \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
604 } \ |
4308 | 605 } \ |
606 while (0) | |
607 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
608 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
609 octave_rand::fill (octave_idx_type len, double *v, double a) |
5730 | 610 { |
611 if (len < 1) | |
612 return; | |
613 | |
614 switch (current_distribution) | |
615 { | |
616 case uniform_dist: | |
617 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
618 { |
5730 | 619 #define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
620 MAKE_RAND (len); |
5730 | 621 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
622 } |
5730 | 623 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
624 oct_fill_randu (len, v); |
5730 | 625 break; |
626 | |
627 case normal_dist: | |
628 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
629 { |
5730 | 630 #define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
631 MAKE_RAND (len); |
5730 | 632 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
633 } |
5730 | 634 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 oct_fill_randn (len, v); |
5730 | 636 break; |
637 | |
638 case expon_dist: | |
639 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
640 { |
5730 | 641 #define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
642 MAKE_RAND (len); |
5730 | 643 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
644 } |
5730 | 645 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
646 oct_fill_rande (len, v); |
5730 | 647 break; |
648 | |
649 case poisson_dist: | |
650 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
651 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
652 if (a < 0.0 || xisnan(a) || xisinf(a)) |
5730 | 653 #define RAND_FUNC(x) x = octave_NaN; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
654 MAKE_RAND (len); |
5730 | 655 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
656 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
657 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
658 // workaround bug in ignpoi, by calling with different Mu |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
659 double tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
660 F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp); |
5730 | 661 #define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
662 MAKE_RAND (len); |
5730 | 663 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
664 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
665 } |
5730 | 666 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
667 oct_fill_randp (a, len, v); |
5730 | 668 break; |
669 | |
670 case gamma_dist: | |
671 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
672 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
673 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
5730 | 674 #define RAND_FUNC(x) x = octave_NaN; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
675 MAKE_RAND (len); |
5730 | 676 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
677 else |
5730 | 678 #define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
679 MAKE_RAND (len); |
5730 | 680 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
681 } |
5730 | 682 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
683 oct_fill_randg (a, len, v); |
5730 | 684 break; |
685 | |
686 default: | |
7536 | 687 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
688 ("rand: invalid distribution ID = %d", current_distribution); |
5730 | 689 break; |
690 } | |
691 | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
692 save_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
693 |
5730 | 694 return; |
695 } |