Mercurial > octave
annotate liboctave/numeric/oct-rand.h @ 27102:84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
Affected files:
file-editor-tab.cc, file-editor.cc, main-window.cc, octave-cmd.h,
octave-qt-link.cc, settings-dialog.cc, call-stack.cc, environment.cc,
ft-text-renderer.cc, gl2ps-print.cc, help.cc, input.cc,
interpreter-private.cc, interpreter-private.h, interpreter.cc,
load-path.cc, oct-hist.cc, oct-process.cc, oct-stream.cc, pager.cc,
syminfo.cc, symtab.h, sysdep.cc, text-renderer.cc, utils.cc, gzip.cc,
cdef-manager.cc, ov-fcn-handle.cc, ov-java.cc, jit-typeinfo.cc,
oct-parse.yy, pt-eval.cc, pt-tm-const.cc, chol.cc, gsvd.cc,
oct-rand.cc, oct-rand.h, randmtzig.cc, randpoisson.cc, file-ops.cc,
lo-sysdep.cc, file-info.cc, oct-glob.cc, oct-sparse.cc, and
url-transfer.cc.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 May 2019 13:57:30 +0000 |
parents | fff643eb3514 |
children | b442ec6dda5c |
rev | line source |
---|---|
4308 | 1 /* |
2 | |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25433
diff
changeset
|
3 Copyright (C) 2003-2019 John W. Eaton |
4308 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
10 (at your option) any later version. |
4308 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
15 GNU General Public License for more details. |
4308 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
4308 | 20 |
21 */ | |
22 | |
20791
f7084eae3318
maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
23 #if ! defined (octave_oct_rand_h) |
17822
ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
24 #define octave_oct_rand_h 1 |
4308 | 25 |
21244
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21017
diff
changeset
|
26 #include "octave-config.h" |
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21017
diff
changeset
|
27 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
28 #include <map> |
4308 | 29 #include <string> |
30 | |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23433
diff
changeset
|
31 #include "Array.h" |
4543 | 32 #include "dNDArray.h" |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
33 #include "fNDArray.h" |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
34 #include "lo-ieee.h" |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
35 #include "uint32NDArray.h" |
4308 | 36 |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23433
diff
changeset
|
37 //class dim_vector; |
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23433
diff
changeset
|
38 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
39 namespace octave |
4308 | 40 { |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
41 class OCTAVE_API rand |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
42 { |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
43 protected: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
44 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
45 rand (void); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
46 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
47 public: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
48 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
49 ~rand (void) = default; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
50 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
51 static bool instance_ok (void); |
4308 | 52 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
53 // Return the current seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
54 static double seed (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
55 { |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
56 return (instance_ok () |
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
57 ? instance->do_seed () : numeric_limits<double>::NaN ()); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
58 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
59 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
60 // Set the seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
61 static void seed (double s) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
62 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
63 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
64 instance->do_seed (s); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
65 } |
4308 | 66 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
67 // Reset the seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
68 static void reset (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
69 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
70 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
71 instance->do_reset (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
72 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
73 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
74 // Return the current state. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
75 static uint32NDArray 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
|
76 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
77 return instance_ok () ? instance->do_state (d) : uint32NDArray (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
78 } |
4308 | 79 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
80 // Set the current state/ |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
81 static void state (const uint32NDArray& s, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
82 const std::string& d = "") |
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 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
85 instance->do_state (s, d); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
86 } |
4308 | 87 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
88 // Reset the current state/ |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
89 static void 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
|
90 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
91 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
92 instance->do_reset (d); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
93 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
94 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
95 // Return the current distribution. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
96 static std::string distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
97 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
98 return instance_ok () ? instance->do_distribution () : ""; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
99 } |
5730 | 100 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
101 // Set the current distribution. May be either "uniform" (the |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
102 // default), "normal", "exponential", "poisson", or "gamma". |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
103 static void 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
|
104 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
105 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
106 instance->do_distribution (d); |
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 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
109 static void uniform_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
110 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
111 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
112 instance->do_uniform_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
113 } |
5730 | 114 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
115 static void normal_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
116 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
117 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
118 instance->do_normal_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
119 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
120 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
121 static void exponential_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
122 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
123 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
124 instance->do_exponential_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
125 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
126 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
127 static void poisson_distribution (void) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
128 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
129 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
130 instance->do_poisson_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
131 } |
5730 | 132 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
133 static void gamma_distribution (void) |
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 if (instance_ok ()) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
136 instance->do_gamma_distribution (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
137 } |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
138 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
139 // Return the next number from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
140 static double scalar (double a = 1.0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
141 { |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
142 return (instance_ok () |
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
143 ? instance->do_scalar (a) : numeric_limits<double>::NaN ()); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
144 } |
4308 | 145 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
146 // Return the next number from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
147 static float float_scalar (float a = 1.0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
148 { |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
149 return (instance_ok () |
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
150 ? instance->do_scalar (a) : numeric_limits<float>::NaN ()); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
151 } |
4308 | 152 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
153 // Return an array of numbers from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
154 static Array<double> vector (octave_idx_type n, double a = 1.0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
155 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
156 return instance_ok () ? instance->do_vector (n, a) : Array<double> (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
157 } |
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 // Return an array of numbers from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
160 static Array<float> float_vector (octave_idx_type n, float a = 1.0) |
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 return instance_ok () ? instance->do_vector (n, a) : Array<float> (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
163 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
164 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
165 // Return an N-dimensional array of numbers from the sequence, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
166 // filled in column major order. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
167 static NDArray nd_array (const dim_vector& dims, double a = 1.0) |
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 return instance_ok () ? instance->do_nd_array (dims, a) : NDArray (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
170 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
171 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
172 // Return an N-dimensional array of numbers from the sequence, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
173 // filled in column major order. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
174 static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0) |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
175 { |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
176 return (instance_ok () |
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26377
diff
changeset
|
177 ? instance->do_float_nd_array (dims, a) : FloatNDArray ()); |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
178 } |
4543 | 179 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
180 private: |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
181 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
182 static rand *instance; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
183 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
184 static void cleanup_instance (void) { delete instance; instance = nullptr; } |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
185 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
186 enum |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
187 { |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
188 unknown_dist, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
189 uniform_dist, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
190 normal_dist, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
191 expon_dist, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
192 poisson_dist, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
193 gamma_dist |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
194 }; |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
195 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
196 // Current distribution of random numbers. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
197 int current_distribution; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
198 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
199 // If TRUE, use old RANLIB generators. Otherwise, use Mersenne |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
200 // Twister generator. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
201 bool use_old_generators; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
202 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
203 // Saved MT states. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
204 std::map<int, uint32NDArray> rand_states; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
205 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
206 // Return the current seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
207 double do_seed (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
208 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
209 // Set the seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
210 void do_seed (double s); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
211 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
212 // Reset the seed. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
213 void do_reset (); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
214 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
215 // Return the current state. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
216 uint32NDArray do_state (const std::string& d); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
217 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
218 // Set the current state/ |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
219 void do_state (const uint32NDArray& s, const std::string& d); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
220 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
221 // Reset the current state/ |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
222 void do_reset (const std::string& d); |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10312
diff
changeset
|
223 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
224 // Return the current distribution. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
225 std::string do_distribution (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
226 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
227 // Set the current distribution. May be either "uniform" (the |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
228 // default), "normal", "exponential", "poisson", or "gamma". |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
229 void do_distribution (const std::string& d); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
230 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
231 void do_uniform_distribution (void); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
232 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
233 void do_normal_distribution (void); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
234 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
235 void do_exponential_distribution (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
236 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
237 void do_poisson_distribution (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
238 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
239 void do_gamma_distribution (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
240 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
241 // The following templates only make sense for double and float |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
242 // types. |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
243 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
244 template <typename T> T uniform (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
245 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
246 template <typename T> T normal (void); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
247 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
248 template <typename T> T exponential (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
249 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
250 template <typename T> T poisson (T a); |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
251 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
252 template <typename T> T gamma (T a); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
253 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
254 // Return the next number from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
255 template <typename T> T do_scalar (T a = 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
|
256 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
257 // Return an array of numbers from the sequence. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
258 template <typename T> Array<T> do_vector (octave_idx_type n, T a = 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
|
259 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
260 // Return an N-dimensional array of numbers from the sequence, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
261 // filled in column major order. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
262 NDArray do_nd_array (const dim_vector& dims, double a = 1.); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
263 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
264 // Return an N-dimensional array of numbers from the sequence, |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
265 // filled in column major order. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
266 FloatNDArray do_float_nd_array (const dim_vector& dims, float a = 1.); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
267 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
268 // Some helper functions. |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
269 |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
270 void initialize_ranlib_generators (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
271 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
272 void initialize_mersenne_twister (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
273 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
274 uint32NDArray get_internal_state (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
275 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
276 void save_state (void); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
277 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
278 int get_dist_id (const std::string& d); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
279 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
280 void set_internal_state (const uint32NDArray& s); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
281 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
282 void switch_to_generator (int dist); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
283 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
284 void fill (octave_idx_type len, double *v, double a); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7533
diff
changeset
|
285 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
286 void 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
|
287 }; |
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
288 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14138
diff
changeset
|
289 |
4308 | 290 #endif |