Mercurial > octave
annotate liboctave/numeric/oct-rand.cc @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | cc7ca74e883d |
children | e88a07dec498 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30059
diff
changeset
|
3 // Copyright (C) 2003-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4308 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21723
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
4308 | 28 #endif |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
30 #include <cassert> |
21912
d92dcbcd7691
prefer C++ wrappers for C standard headers in C++ code
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
31 #include <cstdint> |
d92dcbcd7691
prefer C++ wrappers for C standard headers in C++ code
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
32 |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
33 #include <limits> |
4308 | 34 |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
35 #include "lo-error.h" |
5730 | 36 #include "lo-ieee.h" |
37 #include "lo-mappers.h" | |
22322
93b3cdd36854
move most f77 function decls to separate header files
John W. Eaton <jwe@octave.org>
parents:
22197
diff
changeset
|
38 #include "lo-ranlib-proto.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
39 #include "mach-info.h" |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
40 #include "oct-locbuf.h" |
4308 | 41 #include "oct-rand.h" |
4415 | 42 #include "oct-time.h" |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23457
diff
changeset
|
43 #include "quit.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
44 #include "randgamma.h" |
5730 | 45 #include "randmtzig.h" |
46 #include "randpoisson.h" | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
47 #include "singleton-cleanup.h" |
4308 | 48 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
49 namespace octave |
4308 | 50 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
51 rand *rand::m_instance = nullptr; |
6435 | 52 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
53 rand::rand (void) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
54 : m_current_distribution (uniform_dist), m_use_old_generators (false), |
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
55 m_rand_states () |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
56 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
57 initialize_ranlib_generators (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
58 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
59 initialize_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
60 } |
24866
441b27c0fd5e
use C++11 features to eliminate some macros in oct-rand.cc
John W. Eaton <jwe@octave.org>
parents:
24856
diff
changeset
|
61 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
62 bool rand::instance_ok (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
63 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
64 bool retval = true; |
5730 | 65 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
66 if (! m_instance) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
67 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
68 m_instance = new rand (); |
26369
0249ba4c9589
Fix static analyzer detected V668 issues (bug #55347).
Andreas Weber <octave@josoansi.de>
parents:
25433
diff
changeset
|
69 singleton_cleanup_list::add (cleanup_instance); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
70 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
71 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
72 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
73 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
74 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
75 double rand::do_seed (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
76 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
77 union d2i { double d; int32_t i[2]; }; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
78 union d2i u; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
79 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
80 mach_info::float_format ff = mach_info::native_float_format (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
81 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
82 switch (ff) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
83 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
84 case mach_info::flt_fmt_ieee_big_endian: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
85 F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
86 break; |
4308 | 87 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
88 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
89 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
90 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
91 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
92 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
93 return u.d; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
94 } |
7536 | 95 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
96 static int32_t |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
97 force_to_fit_range (int32_t i, int32_t lo, int32_t hi) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
98 { |
26436
0dcb3e515370
oct-rand.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
99 assert (hi > lo && lo >= 0); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
100 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
101 i = (i > 0 ? i : -i); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
102 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
103 if (i < lo) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
104 i = lo; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
105 else if (i > hi) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
106 i = i % hi; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
107 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
108 return i; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
109 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
111 void rand::do_seed (double s) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
112 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
113 m_use_old_generators = true; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
114 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
115 int i0, i1; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
116 union d2i { double d; int32_t i[2]; }; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
117 union d2i u; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
118 u.d = s; |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
119 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
120 mach_info::float_format ff = mach_info::native_float_format (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
121 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
122 switch (ff) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
123 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
124 case mach_info::flt_fmt_ieee_big_endian: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
125 i1 = force_to_fit_range (u.i[0], 1, 2147483563); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
126 i0 = force_to_fit_range (u.i[1], 1, 2147483399); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
127 break; |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
128 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
129 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
130 i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
131 i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
132 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
133 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
134 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
135 F77_FUNC (setsd, SETSD) (i0, i1); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
136 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
137 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
138 void rand::do_reset (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
139 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
140 m_use_old_generators = true; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
141 initialize_ranlib_generators (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
142 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
143 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
144 uint32NDArray rand::do_state (const std::string& d) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
145 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
146 return m_rand_states[d.empty () ? m_current_distribution : get_dist_id (d)]; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
147 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
148 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
149 void rand::do_state (const uint32NDArray& s, const std::string& d) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
150 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
151 m_use_old_generators = false; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
152 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
153 int old_dist = m_current_distribution; |
4308 | 154 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
155 int new_dist = (d.empty () ? m_current_distribution : get_dist_id (d)); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
156 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
157 uint32NDArray saved_state; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
158 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
159 if (old_dist != new_dist) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
160 saved_state = get_internal_state (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
161 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
162 set_internal_state (s); |
4308 | 163 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
164 m_rand_states[new_dist] = get_internal_state (); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
165 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
166 if (old_dist != new_dist) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
167 m_rand_states[old_dist] = saved_state; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
168 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
169 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
170 void rand::do_reset (const std::string& d) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
171 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
172 m_use_old_generators = false; |
4308 | 173 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
174 int old_dist = m_current_distribution; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
175 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
176 int new_dist = (d.empty () ? m_current_distribution : get_dist_id (d)); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
177 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
178 uint32NDArray saved_state; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
179 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
180 if (old_dist != new_dist) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
181 saved_state = get_internal_state (); |
4308 | 182 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
183 init_mersenne_twister (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
184 m_rand_states[new_dist] = get_internal_state (); |
4308 | 185 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
186 if (old_dist != new_dist) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
187 m_rand_states[old_dist] = saved_state; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
188 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
189 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
190 std::string rand::do_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
191 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
192 std::string retval; |
5730 | 193 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
194 switch (m_current_distribution) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
195 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
196 case uniform_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
197 retval = "uniform"; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
198 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
199 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
200 case normal_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
201 retval = "normal"; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
202 break; |
5730 | 203 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
204 case expon_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
205 retval = "exponential"; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
206 break; |
5730 | 207 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
208 case poisson_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
209 retval = "poisson"; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
210 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
211 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
212 case gamma_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
213 retval = "gamma"; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
214 break; |
5730 | 215 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
216 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
217 (*current_liboctave_error_handler) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
218 ("rand: invalid distribution ID = %d", m_current_distribution); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
219 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
220 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
221 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
222 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
223 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
224 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
225 void rand::do_distribution (const std::string& d) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
226 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
227 int id = get_dist_id (d); |
5730 | 228 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
229 switch (id) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
230 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
231 case uniform_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
232 rand::uniform_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
233 break; |
5730 | 234 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
235 case normal_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
236 rand::normal_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
237 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
238 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
239 case expon_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
240 rand::exponential_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
241 break; |
4308 | 242 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
243 case poisson_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
244 rand::poisson_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
245 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
246 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
247 case gamma_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
248 rand::gamma_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
249 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
250 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
251 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
252 (*current_liboctave_error_handler) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
253 ("rand: invalid distribution ID = %d", id); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
254 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
255 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
256 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
257 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
258 void rand::do_uniform_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
259 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
260 switch_to_generator (uniform_dist); |
4308 | 261 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
262 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
263 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
264 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
265 void rand::do_normal_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
266 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
267 switch_to_generator (normal_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
268 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
269 F77_FUNC (setcgn, SETCGN) (normal_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
270 } |
4308 | 271 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
272 void rand::do_exponential_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
273 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
274 switch_to_generator (expon_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
275 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
276 F77_FUNC (setcgn, SETCGN) (expon_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
277 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
278 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
279 void rand::do_poisson_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
280 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
281 switch_to_generator (poisson_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
282 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
283 F77_FUNC (setcgn, SETCGN) (poisson_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
284 } |
4308 | 285 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
286 void rand::do_gamma_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
287 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
288 switch_to_generator (gamma_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
289 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
290 F77_FUNC (setcgn, SETCGN) (gamma_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
291 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
292 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
293 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
294 OCTAVE_API double rand::uniform<double> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
295 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
296 double retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
297 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
298 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
299 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
300 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
301 retval = rand_uniform<double> (); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
302 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
303 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
304 } |
4308 | 305 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
306 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
307 OCTAVE_API double rand::normal<double> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
308 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
309 double retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
310 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
311 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
312 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
313 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
314 retval = rand_normal<double> (); |
4308 | 315 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
316 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
317 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
318 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
319 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
320 OCTAVE_API double rand::exponential<double> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
321 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
322 double retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
323 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
324 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
325 F77_FUNC (dgenexp, DGENEXP) (1.0, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
326 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
327 retval = rand_exponential<double> (); |
4543 | 328 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
329 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
330 } |
5730 | 331 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
332 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
333 OCTAVE_API double rand::poisson<double> (double a) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
334 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
335 double retval; |
5730 | 336 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
337 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
338 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
339 if (a < 0.0 || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
340 retval = numeric_limits<double>::NaN (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
341 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
342 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
343 // workaround bug in ignpoi, by calling with different Mu |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
344 F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
345 F77_FUNC (dignpoi, DIGNPOI) (a, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
346 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
347 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
348 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
349 retval = rand_poisson<double> (a); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
350 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
351 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
352 } |
5730 | 353 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
354 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
355 OCTAVE_API double rand::gamma<double> (double a) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
356 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
357 double retval; |
4543 | 358 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
359 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
360 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
361 if (a <= 0.0 || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
362 retval = numeric_limits<double>::NaN (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
363 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
364 F77_FUNC (dgengam, DGENGAM) (1.0, a, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
365 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
366 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
367 retval = rand_gamma<double> (a); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
368 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
369 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
370 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
371 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
372 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
373 OCTAVE_API float rand::uniform<float> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
374 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
375 float retval; |
4543 | 376 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
377 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
378 F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
379 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
380 retval = rand_uniform<float> (); |
4543 | 381 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
382 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
383 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
384 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
385 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
386 OCTAVE_API float rand::normal<float> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
387 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
388 float retval; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
389 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
390 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
391 F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
392 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
393 retval = rand_normal<float> (); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
394 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
395 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
396 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
397 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
398 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
399 OCTAVE_API float rand::exponential<float> (void) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
400 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
401 float retval; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
402 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
403 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
404 F77_FUNC (fgenexp, FGENEXP) (1.0f, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
405 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
406 retval = rand_exponential<float> (); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
407 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
408 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
409 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
410 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
411 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
412 OCTAVE_API float rand::poisson<float> (float a) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
413 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
414 float retval; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
415 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
416 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
417 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
418 if (a < 0.0f || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
419 retval = numeric_limits<float>::NaN (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
420 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
421 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
422 // workaround bug in ignpoi, by calling with different Mu |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
423 F77_FUNC (fignpoi, FIGNPOI) (a + 1, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
424 F77_FUNC (fignpoi, FIGNPOI) (a, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
425 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
426 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
427 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
428 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
429 // Keep poisson distribution in double precision for accuracy |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
430 retval = rand_poisson<double> (a); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
431 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
432 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
433 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
434 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
435 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
436 template <> |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
437 OCTAVE_API float rand::gamma<float> (float a) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
438 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
439 float retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
440 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
441 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
442 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
443 if (a <= 0.0f || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
444 retval = numeric_limits<float>::NaN (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
445 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
446 F77_FUNC (fgengam, FGENGAM) (1.0f, a, retval); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
447 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
448 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
449 retval = rand_gamma<float> (a); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
450 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
451 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
452 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
453 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
454 template <typename T> |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
455 T rand::do_scalar (T a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
456 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
457 T retval = 0; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
458 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
459 switch (m_current_distribution) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
460 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
461 case uniform_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
462 retval = uniform<T> (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
463 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
464 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
465 case normal_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
466 retval = normal<T> (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
467 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
468 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
469 case expon_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
470 retval = exponential<T> (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
471 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
472 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
473 case poisson_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
474 retval = poisson<T> (a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
475 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
476 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
477 case gamma_dist: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
478 retval = gamma<T> (a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
479 break; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
480 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
481 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
482 (*current_liboctave_error_handler) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
483 ("rand: invalid distribution ID = %d", m_current_distribution); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
484 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
485 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
486 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
487 if (! m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
488 save_state (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
489 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
490 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
491 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
492 |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
493 template OCTAVE_API double rand::do_scalar<double> (double); |
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
494 template OCTAVE_API float rand::do_scalar<float> (float); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
495 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
496 template <typename T> |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
497 Array<T> |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
498 rand::do_vector (octave_idx_type n, T a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
499 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
500 Array<T> retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
501 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
502 if (n > 0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
503 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
504 retval.clear (n, 1); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
505 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
506 fill (retval.numel (), retval.fortran_vec (), a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
507 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
508 else if (n < 0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
509 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
510 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
511 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
512 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
513 |
29228
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
514 template OCTAVE_API Array<double> |
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
515 rand::do_vector<double> (octave_idx_type, double); |
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
516 template OCTAVE_API Array<float> |
5c14f81e0937
Set API tags in files in liboctave/numeric (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents:
27923
diff
changeset
|
517 rand::do_vector<float> (octave_idx_type, float); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
518 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
519 NDArray rand::do_nd_array (const dim_vector& dims, double a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
520 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
521 NDArray retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
522 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
523 if (! dims.all_zero ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
524 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
525 retval.clear (dims); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
526 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
527 fill (retval.numel (), retval.fortran_vec (), a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
528 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
529 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
530 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
531 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
532 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
533 FloatNDArray rand::do_float_nd_array (const dim_vector& dims, float a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
534 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
535 FloatNDArray retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
536 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
537 if (! dims.all_zero ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
538 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
539 retval.clear (dims); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
540 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
541 fill (retval.numel (), retval.fortran_vec (), a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
542 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
543 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
544 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
545 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
546 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
547 // Make the random number generator give us a different sequence every |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
548 // time we start octave unless we specifically set the seed. The |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
549 // technique used below will cycle monthly, but it does seem to |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
550 // work ok to give fairly different seeds each time Octave starts. |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
551 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
552 void rand::initialize_ranlib_generators (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
553 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
554 sys::localtime tm; |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
555 int stored_distribution = m_current_distribution; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
556 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
557 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
558 int hour = tm.hour () + 1; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
559 int minute = tm.min () + 1; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
560 int second = tm.sec () + 1; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
561 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
562 int32_t s0 = tm.mday () * hour * minute * second; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
563 int32_t s1 = hour * minute * second; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
564 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
565 s0 = force_to_fit_range (s0, 1, 2147483563); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
566 s1 = force_to_fit_range (s1, 1, 2147483399); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
567 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
568 F77_FUNC (setall, SETALL) (s0, s1); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
569 F77_FUNC (setcgn, SETCGN) (stored_distribution); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
570 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
571 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
572 void rand::initialize_mersenne_twister (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
573 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
574 uint32NDArray s; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
575 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
576 init_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
577 s = get_internal_state (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
578 m_rand_states[uniform_dist] = s; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
579 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
580 init_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
581 s = get_internal_state (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
582 m_rand_states[normal_dist] = s; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
583 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
584 init_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
585 s = get_internal_state (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
586 m_rand_states[expon_dist] = s; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
587 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
588 init_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
589 s = get_internal_state (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
590 m_rand_states[poisson_dist] = s; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
591 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
592 init_mersenne_twister (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
593 s = get_internal_state (); |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
594 m_rand_states[gamma_dist] = s; |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
595 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
596 // All of the initializations above have messed with the internal state. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
597 // Restore the state of the currently selected distribution. |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
598 set_internal_state (m_rand_states[m_current_distribution]); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
599 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
600 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
601 uint32NDArray rand::get_internal_state (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
602 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
603 uint32NDArray s (dim_vector (MT_N + 1, 1)); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
604 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
605 get_mersenne_twister_state (reinterpret_cast<uint32_t *> (s.fortran_vec ())); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
606 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
607 return s; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
608 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
609 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
610 void rand::save_state (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
611 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
612 m_rand_states[m_current_distribution] = get_internal_state ();; |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
613 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
614 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
615 int rand::get_dist_id (const std::string& d) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
616 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
617 int retval = unknown_dist; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
618 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
619 if (d == "uniform" || d == "rand") |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
620 retval = uniform_dist; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
621 else if (d == "normal" || d == "randn") |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
622 retval = normal_dist; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
623 else if (d == "exponential" || d == "rande") |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
624 retval = expon_dist; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
625 else if (d == "poisson" || d == "randp") |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
626 retval = poisson_dist; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
627 else if (d == "gamma" || d == "randg") |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
628 retval = gamma_dist; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
629 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
630 (*current_liboctave_error_handler) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
631 ("rand: invalid distribution '%s'", d.c_str ()); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
632 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
633 return retval; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
634 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
635 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
636 void rand::set_internal_state (const uint32NDArray& s) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
637 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
638 octave_idx_type len = s.numel (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
639 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
640 const uint32_t *sdata = reinterpret_cast <const uint32_t *> (s.data ()); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
641 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
642 if (len == MT_N + 1 && sdata[MT_N] <= MT_N && sdata[MT_N] > 0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
643 set_mersenne_twister_state (sdata); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
644 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
645 init_mersenne_twister (sdata, len); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
646 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
647 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
648 void rand::switch_to_generator (int dist) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
649 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
650 if (dist != m_current_distribution) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
651 { |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
652 m_current_distribution = dist; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
653 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
654 set_internal_state (m_rand_states[dist]); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
655 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
656 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
657 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
658 void rand::fill (octave_idx_type len, double *v, double a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
659 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
660 if (len < 1) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
661 return; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
662 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
663 switch (m_current_distribution) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
664 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
665 case uniform_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
666 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
667 std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
668 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
669 rand_uniform<double> (len, v); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
670 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
671 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
672 case normal_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
673 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
674 std::generate_n (v, len, [](void) { double x; F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
675 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
676 rand_normal<double> (len, v); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
677 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
678 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
679 case expon_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
680 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
681 std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenexp, DGENEXP) (1.0, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
682 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
683 rand_exponential<double> (len, v); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
684 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
685 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
686 case poisson_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
687 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
688 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
689 if (a < 0.0 || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
690 std::fill_n (v, len, numeric_limits<double>::NaN ()); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
691 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
692 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
693 // workaround bug in ignpoi, by calling with different Mu |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
694 double tmp; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
695 F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
696 std::generate_n (v, len, [a](void) { double x; F77_FUNC (dignpoi, DIGNPOI) (a, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
697 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
698 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
699 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
700 rand_poisson<double> (a, len, v); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
701 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
702 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
703 case gamma_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
704 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
705 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
706 if (a <= 0.0 || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
707 std::fill_n (v, len, numeric_limits<double>::NaN ()); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
708 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
709 std::generate_n (v, len, [a](void) { double x; F77_FUNC (dgengam, DGENGAM) (1.0, a, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
710 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
711 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
712 rand_gamma<double> (a, len, v); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
713 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
714 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
715 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
716 (*current_liboctave_error_handler) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
717 ("rand: invalid distribution ID = %d", m_current_distribution); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
718 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
719 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
720 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
721 save_state (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
722 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
723 return; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
724 } |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
725 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
726 void rand::fill (octave_idx_type len, float *v, float a) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
727 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
728 if (len < 1) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
729 return; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
730 |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
731 switch (m_current_distribution) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
732 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
733 case uniform_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
734 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
735 std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
736 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
737 rand_uniform<float> (len, v); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
738 break; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
739 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
740 case normal_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
741 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
742 std::generate_n (v, len, [](void) { float x; F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
743 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
744 rand_normal<float> (len, v); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
745 break; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
746 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
747 case expon_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
748 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
749 std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenexp, FGENEXP) (1.0f, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
750 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
751 rand_exponential<float> (len, v); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
752 break; |
24852
53db3b152397
Correctly set rand state at startup (bug #53300).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
753 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
754 case poisson_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
755 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
756 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
757 if (a < 0.0f || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
758 std::fill_n (v, len, numeric_limits<float>::NaN ()); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
759 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
760 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
761 // workaround bug in ignpoi, by calling with different Mu |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
762 float tmp; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
763 F77_FUNC (fignpoi, FIGNPOI) (a + 1, tmp); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
764 std::generate_n (v, len, [a](void) { float x; F77_FUNC (fignpoi, FIGNPOI) (a, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
765 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
766 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
767 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
768 rand_poisson<float> (a, len, v); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
769 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
770 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
771 case gamma_dist: |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
772 if (m_use_old_generators) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
773 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
774 if (a <= 0.0f || ! math::isfinite (a)) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
775 std::fill_n (v, len, numeric_limits<float>::NaN ()); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
776 else |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
777 std::generate_n (v, len, [a](void) { float x; F77_FUNC (fgengam, FGENGAM) (1.0f, a, x); return x; }); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
778 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
779 else |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26446
diff
changeset
|
780 rand_gamma<float> (a, len, v); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
781 break; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
782 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
783 default: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
784 (*current_liboctave_error_handler) |
30059
cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
785 ("rand: invalid distribution ID = %d", m_current_distribution); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
786 break; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
787 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
788 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
789 save_state (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
790 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
791 return; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
792 } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
793 } |