Mercurial > hg > octave-lyh
annotate src/DLD-FUNCTIONS/rand.cc @ 14688:42edbe6f6a8d stable
Fix bad two-arg call of randperm (bug #36535)
* rand.cc (Frandperm): Rewrite algorithm for short_shuffle. Add another test.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Fri, 25 May 2012 11:24:02 -0400 |
parents | 72c96de7a403 |
children | 52c5fb67fa5f |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14038
diff
changeset
|
3 Copyright (C) 1996-2012 John W. Eaton |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
4 Copyright (C) 2009 VZLU Prague |
2928 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <ctime> | |
13727
478efc95cb7a
Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13255
diff
changeset
|
29 #if defined (HAVE_UNORDERED_MAP) |
478efc95cb7a
Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13255
diff
changeset
|
30 #include <unordered_map> |
478efc95cb7a
Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13255
diff
changeset
|
31 #elif defined (HAVE_TR1_UNORDERED_MAP) |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
32 #include <tr1/unordered_map> |
13727
478efc95cb7a
Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13255
diff
changeset
|
33 #endif |
2928 | 34 #include <string> |
35 | |
36 #include "f77-fcn.h" | |
37 #include "lo-mappers.h" | |
4307 | 38 #include "oct-rand.h" |
4153 | 39 #include "quit.h" |
2928 | 40 |
41 #include "defun-dld.h" | |
42 #include "error.h" | |
43 #include "gripes.h" | |
44 #include "oct-obj.h" | |
45 #include "unwind-prot.h" | |
46 #include "utils.h" | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
47 #include "ov-re-mat.h" |
2928 | 48 |
6437 | 49 /* |
50 %!shared __random_statistical_tests__ | |
51 %! % Flag whether the statistical tests should be run in "make check" or not | |
52 %! __random_statistical_tests__ = 0; | |
53 */ | |
54 | |
4307 | 55 static octave_value |
5730 | 56 do_rand (const octave_value_list& args, int nargin, const char *fcn, |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
57 const std::string& distribution, bool additional_arg = false) |
2928 | 58 { |
4307 | 59 octave_value retval; |
5730 | 60 NDArray a; |
61 int idx = 0; | |
62 dim_vector dims; | |
2928 | 63 |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
64 unwind_protect frame; |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
65 // Restore current distribution on any exit. |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
66 frame.add_fcn (octave_rand::distribution, |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
67 octave_rand::distribution ()); |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
68 |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
69 octave_rand::distribution (distribution); |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
70 |
5730 | 71 if (additional_arg) |
72 { | |
73 if (nargin == 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
74 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
75 error ("%s: expecting at least one argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
76 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
77 } |
5730 | 78 else if (args(0).is_string()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
79 additional_arg = false; |
5730 | 80 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
81 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
82 a = args(0).array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
83 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
84 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
85 error ("%s: expecting scalar or matrix arguments", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
86 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
87 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
88 idx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
89 nargin--; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
90 } |
5730 | 91 } |
2928 | 92 |
4543 | 93 switch (nargin) |
2928 | 94 { |
4543 | 95 case 0: |
96 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
97 if (additional_arg) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
98 dims = a.dims (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
99 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
100 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
101 dims.resize (2); |
4543 | 102 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
103 dims(0) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
104 dims(1) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
105 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
106 goto gen_matrix; |
4543 | 107 } |
108 break; | |
2928 | 109 |
4543 | 110 case 1: |
111 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
112 octave_value tmp = args(idx); |
4543 | 113 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
114 if (tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
115 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
116 std::string s_arg = tmp.string_value (); |
2928 | 117 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
118 if (s_arg == "dist") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
119 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
120 retval = octave_rand::distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
121 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
122 else if (s_arg == "seed") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
123 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
124 retval = octave_rand::seed (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
125 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
126 else if (s_arg == "state" || s_arg == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
127 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
128 retval = octave_rand::state (fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
129 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
130 else if (s_arg == "uniform") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
131 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
132 octave_rand::uniform_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
133 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
134 else if (s_arg == "normal") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
135 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
136 octave_rand::normal_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
137 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
138 else if (s_arg == "exponential") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
139 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
140 octave_rand::exponential_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
141 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
142 else if (s_arg == "poisson") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
143 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
144 octave_rand::poisson_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
145 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
146 else if (s_arg == "gamma") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
147 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
148 octave_rand::gamma_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
149 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
150 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
151 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
152 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
153 else if (tmp.is_scalar_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
154 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
155 double dval = tmp.double_value (); |
2928 | 156 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
157 if (xisnan (dval)) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
158 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
159 error ("%s: NaN is invalid matrix dimension", fcn); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
160 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
161 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
162 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
163 dims.resize (2); |
4543 | 164 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
165 dims(0) = NINTbig (tmp.double_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
166 dims(1) = NINTbig (tmp.double_value ()); |
2928 | 167 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
168 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
169 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
170 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
171 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
172 else if (tmp.is_range ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
173 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
174 Range r = tmp.range_value (); |
4543 | 175 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
176 if (r.all_elements_are_ints ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
177 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
178 octave_idx_type n = r.nelem (); |
4543 | 179 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
180 dims.resize (n); |
4543 | 181 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
182 octave_idx_type base = NINTbig (r.base ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
183 octave_idx_type incr = NINTbig (r.inc ()); |
2928 | 184 |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
185 for (octave_idx_type i = 0; i < n; i++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
186 { |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
187 //Negative dimensions are treated as zero for Matlab |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
188 //compatibility |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
189 dims(i) = base >= 0 ? base : 0; |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
190 base += incr; |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
191 } |
2928 | 192 |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
193 goto gen_matrix; |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
194 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
195 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
196 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
197 error ("%s: all elements of range must be integers", |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
198 fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
199 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
200 else if (tmp.is_matrix_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
201 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
202 Array<int> iv = tmp.int_vector_value (true); |
4543 | 203 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
204 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
205 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
206 octave_idx_type len = iv.length (); |
2928 | 207 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
208 dims.resize (len); |
4543 | 209 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
210 for (octave_idx_type i = 0; i < len; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
211 { |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
212 //Negative dimensions are treated as zero for Matlab |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
213 //compatibility |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
214 octave_idx_type elt = iv(i); |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
215 dims(i) = elt >=0 ? elt : 0; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
216 } |
2928 | 217 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
218 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
219 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
220 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
221 error ("%s: expecting integer vector", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
222 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
223 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
224 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
225 gripe_wrong_type_arg ("rand", tmp); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
226 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
227 } |
4543 | 228 } |
229 break; | |
230 | |
231 default: | |
232 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
233 octave_value tmp = args(idx); |
4543 | 234 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
235 if (nargin == 2 && tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
236 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
237 std::string ts = tmp.string_value (); |
5164 | 238 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
239 if (ts == "seed") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
240 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
241 if (args(idx+1).is_real_scalar ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
242 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
243 double d = args(idx+1).double_value (); |
2928 | 244 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
245 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
246 octave_rand::seed (d); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
247 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
248 else if (args(idx+1).is_string () |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
249 && args(idx+1).string_value() == "reset") |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
250 octave_rand::reset (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
251 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
252 error ("%s: seed must be a real scalar", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
253 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
254 else if (ts == "state" || ts == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
255 { |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
256 if (args(idx+1).is_string () |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
257 && args(idx+1).string_value() == "reset") |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
258 octave_rand::reset (fcn); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
259 else |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
260 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
261 ColumnVector s = |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
262 ColumnVector (args(idx+1).vector_value(false, true)); |
5730 | 263 |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
264 if (! error_state) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
265 octave_rand::state (s, fcn); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
266 } |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
267 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
268 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
269 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
270 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
271 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
272 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
273 dims.resize (nargin); |
4543 | 274 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
275 for (int i = 0; i < nargin; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
276 { |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
277 octave_idx_type elt = args(idx+i).int_value (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
278 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
279 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
280 error ("%s: expecting integer arguments", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
281 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
282 } |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
283 //Negative is zero for Matlab compatibility |
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
284 dims(i) = elt >= 0 ? elt : 0; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
285 } |
4543 | 286 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
287 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
288 } |
4543 | 289 } |
290 break; | |
2928 | 291 } |
292 | |
4543 | 293 done: |
2928 | 294 |
295 return retval; | |
296 | |
297 gen_matrix: | |
298 | |
5355 | 299 dims.chop_trailing_singletons (); |
300 | |
5730 | 301 if (additional_arg) |
302 { | |
303 if (a.length() == 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
304 return octave_rand::nd_array (dims, a(0)); |
5730 | 305 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
306 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
307 if (a.dims() != dims) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
308 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
309 error ("%s: mismatch in argument size", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
310 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
311 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
312 octave_idx_type len = a.length (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
313 NDArray m (dims); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
314 double *v = m.fortran_vec (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
315 for (octave_idx_type i = 0; i < len; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
316 v[i] = octave_rand::scalar (a(i)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
317 return m; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
318 } |
5730 | 319 } |
320 else | |
321 return octave_rand::nd_array (dims); | |
2928 | 322 } |
323 | |
4665 | 324 DEFUN_DLD (rand, args, , |
3369 | 325 "-*- texinfo -*-\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
326 @deftypefn {Loadable Function} {} rand (@var{n})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
327 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m}, @dots{})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
328 @deftypefnx {Loadable Function} {} rand ([@var{n} @var{m} @dots{}])\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
329 @deftypefnx {Loadable Function} {@var{v} =} rand (\"state\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
330 @deftypefnx {Loadable Function} {} rand (\"state\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
331 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
332 @deftypefnx {Loadable Function} {@var{v} =} rand (\"seed\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
333 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
334 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\ |
3369 | 335 Return a matrix with random elements uniformly distributed on the\n\ |
336 interval (0, 1). The arguments are handled the same as the arguments\n\ | |
5730 | 337 for @code{eye}.\n\ |
338 \n\ | |
339 You can query the state of the random number generator using the\n\ | |
3369 | 340 form\n\ |
2928 | 341 \n\ |
3369 | 342 @example\n\ |
5730 | 343 v = rand (\"state\")\n\ |
344 @end example\n\ | |
345 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
346 This returns a column vector @var{v} of length 625. Later, you can\n\ |
5730 | 347 restore the random number generator to the state @var{v}\n\ |
348 using the form\n\ | |
349 \n\ | |
350 @example\n\ | |
351 rand (\"state\", v)\n\ | |
3369 | 352 @end example\n\ |
353 \n\ | |
354 @noindent\n\ | |
5730 | 355 You may also initialize the state vector from an arbitrary vector of\n\ |
10840 | 356 length @leq{} 625 for @var{v}. This new state will be a hash based on the\n\ |
5798 | 357 value of @var{v}, not @var{v} itself.\n\ |
5730 | 358 \n\ |
359 By default, the generator is initialized from @code{/dev/urandom} if it is\n\ | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
360 available, otherwise from CPU time, wall clock time, and the current\n\ |
5730 | 361 fraction of a second.\n\ |
362 \n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
363 To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
364 Twister with a period of @math{2^{19937}-1} (See M. Matsumoto and\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
365 T. Nishimura,\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
366 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
367 pseudorandom number generator}, ACM Trans. on\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
368 Modeling and Computer Simulation Vol. 8, No. 1, pp. 3-30, January 1998,\n\ |
7171 | 369 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\ |
6547 | 370 Do @strong{not} use for cryptography without securely hashing\n\ |
371 several returned values together, otherwise the generator state\n\ | |
372 can be learned after reading 624 consecutive values.\n\ | |
5730 | 373 \n\ |
7096 | 374 Older versions of Octave used a different random number generator.\n\ |
375 The new generator is used by default\n\ | |
5730 | 376 as it is significantly faster than the old generator, and produces\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
377 random numbers with a significantly longer cycle time. However, in\n\ |
5798 | 378 some circumstances it might be desirable to obtain the same random\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
379 sequences as used by the old generators. To do this the keyword\n\ |
5730 | 380 \"seed\" is used to specify that the old generators should be use,\n\ |
381 as in\n\ | |
2928 | 382 \n\ |
3369 | 383 @example\n\ |
5730 | 384 rand (\"seed\", val)\n\ |
3369 | 385 @end example\n\ |
386 \n\ | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
387 @noindent\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
388 which sets the seed of the generator to @var{val}. The seed of the\n\ |
5730 | 389 generator can be queried with\n\ |
390 \n\ | |
391 @example\n\ | |
392 s = rand (\"seed\")\n\ | |
393 @end example\n\ | |
394 \n\ | |
395 However, it should be noted that querying the seed will not cause\n\ | |
396 @code{rand} to use the old generators, only setting the seed will.\n\ | |
397 To cause @code{rand} to once again use the new generators, the\n\ | |
398 keyword \"state\" should be used to reset the state of the @code{rand}.\n\ | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
399 \n\ |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
400 The state or seed of the generator can be reset to a new random value\n\ |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
401 using the \"reset\" keyword.\n\ |
5798 | 402 @seealso{randn, rande, randg, randp}\n\ |
3369 | 403 @end deftypefn") |
2928 | 404 { |
4307 | 405 octave_value retval; |
2928 | 406 |
407 int nargin = args.length (); | |
408 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
409 retval = do_rand (args, nargin, "rand", "uniform"); |
2928 | 410 |
411 return retval; | |
412 } | |
413 | |
8871 | 414 // FIXME -- The old generator (selected when "seed" is set) will not |
415 // work properly if compiled to use 64-bit integers. | |
416 | |
5730 | 417 /* |
418 %!test # 'state' can be a scalar | |
419 %! rand('state',12); x = rand(1,4); | |
420 %! rand('state',12); y = rand(1,4); | |
421 %! assert(x,y); | |
422 %!test # 'state' can be a vector | |
423 %! rand('state',[12,13]); x=rand(1,4); | |
424 %! rand('state',[12;13]); y=rand(1,4); | |
425 %! assert(x,y); | |
426 %!test # querying 'state' doesn't disturb sequence | |
427 %! rand('state',12); rand(1,2); x=rand(1,2); | |
428 %! rand('state',12); rand(1,2); | |
429 %! s=rand('state'); y=rand(1,2); | |
430 %! assert(x,y); | |
431 %! rand('state',s); z=rand(1,2); | |
432 %! assert(x,z); | |
433 %!test # 'seed' must be a scalar | |
434 %! rand('seed',12); x = rand(1,4); | |
435 %! rand('seed',12); y = rand(1,4); | |
436 %! assert(x,y); | |
437 %!error(rand('seed',[12,13])) | |
438 %!test # querying 'seed' returns a value which can be used later | |
439 %! s=rand('seed'); x=rand(1,2); | |
440 %! rand('seed',s); y=rand(1,2); | |
441 %! assert(x,y); | |
442 %!test # querying 'seed' doesn't disturb sequence | |
443 %! rand('seed',12); rand(1,2); x=rand(1,2); | |
444 %! rand('seed',12); rand(1,2); | |
445 %! s=rand('seed'); y=rand(1,2); | |
446 %! assert(x,y); | |
447 %! rand('seed',s); z=rand(1,2); | |
448 %! assert(x,z); | |
449 */ | |
450 | |
451 /* | |
452 %!test | |
6437 | 453 %! % Test fixed state |
454 %! rand("state",1); | |
6443 | 455 %! assert (rand(1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382],1e-6); |
6437 | 456 %!test |
6443 | 457 %! % Test fixed seed |
6437 | 458 %! rand("seed",1); |
6443 | 459 %! assert (rand(1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759],1e-6); |
5730 | 460 %!test |
6437 | 461 %! if (__random_statistical_tests__) |
462 %! % statistical tests may fail occasionally. | |
463 %! rand("state",12); | |
464 %! x = rand(100000,1); | |
465 %! assert(max(x)<1.); %*** Please report this!!! *** | |
466 %! assert(min(x)>0.); %*** Please report this!!! *** | |
467 %! assert(mean(x),0.5,0.0024); | |
468 %! assert(var(x),1/48,0.0632); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
469 %! assert(skewness(x),0,0.012); |
6437 | 470 %! assert(kurtosis(x),-6/5,0.0094); |
471 %! endif | |
472 %!test | |
473 %! if (__random_statistical_tests__) | |
474 %! % statistical tests may fail occasionally. | |
475 %! rand("seed",12); | |
476 %! x = rand(100000,1); | |
477 %! assert(max(x)<1.); %*** Please report this!!! *** | |
478 %! assert(min(x)>0.); %*** Please report this!!! *** | |
479 %! assert(mean(x),0.5,0.0024); | |
480 %! assert(var(x),1/48,0.0632); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
481 %! assert(skewness(x),0,0.012); |
6437 | 482 %! assert(kurtosis(x),-6/5,0.0094); |
483 %! endif | |
5730 | 484 */ |
485 | |
486 | |
4307 | 487 static std::string current_distribution = octave_rand::distribution (); |
488 | |
4665 | 489 DEFUN_DLD (randn, args, , |
3369 | 490 "-*- texinfo -*-\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
491 @deftypefn {Loadable Function} {} randn (@var{n})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
492 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m}, @dots{})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
493 @deftypefnx {Loadable Function} {} randn ([@var{n} @var{m} @dots{}])\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
494 @deftypefnx {Loadable Function} {@var{v} =} randn (\"state\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
495 @deftypefnx {Loadable Function} {} randn (\"state\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
496 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
497 @deftypefnx {Loadable Function} {@var{v} =} randn (\"seed\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
498 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
499 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
500 Return a matrix with normally distributed random\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
501 elements having zero mean and variance one. The arguments are\n\ |
7780
08125419efcb
Extend explanation of randn's return value
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7533
diff
changeset
|
502 handled the same as the arguments for @code{rand}.\n\ |
3369 | 503 \n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
504 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
505 to transform from a uniform to a normal distribution.\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
506 \n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
507 Reference: G. Marsaglia and W.W. Tsang,\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
508 @cite{Ziggurat Method for Generating Random Variables},\n\ |
5730 | 509 J. Statistical Software, vol 5, 2000,\n\ |
510 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
2928 | 511 \n\ |
6547 | 512 @seealso{rand, rande, randg, randp}\n\ |
3369 | 513 @end deftypefn") |
2928 | 514 { |
4307 | 515 octave_value retval; |
2928 | 516 |
517 int nargin = args.length (); | |
518 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
519 retval = do_rand (args, nargin, "randn", "normal"); |
2928 | 520 |
521 return retval; | |
522 } | |
523 | |
524 /* | |
5730 | 525 %!test |
6437 | 526 %! % Test fixed state |
527 %! randn("state",1); | |
6443 | 528 %! assert (randn(1,6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116],1e-6); |
6437 | 529 %!test |
6443 | 530 %! % Test fixed seed |
6437 | 531 %! randn("seed",1); |
6443 | 532 %! assert (randn(1,6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133],1e-6); |
5730 | 533 %!test |
6437 | 534 %! if (__random_statistical_tests__) |
535 %! % statistical tests may fail occasionally. | |
536 %! randn("state",12); | |
537 %! x = randn(100000,1); | |
538 %! assert(mean(x),0,0.01); | |
539 %! assert(var(x),1,0.02); | |
540 %! assert(skewness(x),0,0.02); | |
541 %! assert(kurtosis(x),0,0.04); | |
542 %! endif | |
543 %!test | |
544 %! if (__random_statistical_tests__) | |
545 %! % statistical tests may fail occasionally. | |
546 %! randn("seed",12); | |
547 %! x = randn(100000,1); | |
548 %! assert(mean(x),0,0.01); | |
549 %! assert(var(x),1,0.02); | |
550 %! assert(skewness(x),0,0.02); | |
551 %! assert(kurtosis(x),0,0.04); | |
552 %! endif | |
5730 | 553 */ |
554 | |
555 DEFUN_DLD (rande, args, , | |
556 "-*- texinfo -*-\n\ | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
557 @deftypefn {Loadable Function} {} rande (@var{n})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
558 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m}, @dots{})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
559 @deftypefnx {Loadable Function} {} rande ([@var{n} @var{m} @dots{}])\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
560 @deftypefnx {Loadable Function} {@var{v} =} rande (\"state\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
561 @deftypefnx {Loadable Function} {} rande (\"state\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
562 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
563 @deftypefnx {Loadable Function} {@var{v} =} rande (\"seed\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
564 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
565 @deftypefnx {Loadable Function} {} rande (\"seed\", \"reset\")\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
566 Return a matrix with exponentially distributed random elements. The\n\ |
5730 | 567 arguments are handled the same as the arguments for @code{rand}.\n\ |
568 \n\ | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
569 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
570 to transform from a uniform to an exponential distribution.\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
571 \n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
572 Reference: G. Marsaglia and W.W. Tsang,\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
573 @cite{Ziggurat Method for Generating Random Variables},\n\ |
5730 | 574 J. Statistical Software, vol 5, 2000,\n\ |
575 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
576 \n\ |
6547 | 577 @seealso{rand, randn, randg, randp}\n\ |
5730 | 578 @end deftypefn") |
579 { | |
580 octave_value retval; | |
581 | |
582 int nargin = args.length (); | |
583 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
584 retval = do_rand (args, nargin, "rande", "exponential"); |
5730 | 585 |
586 return retval; | |
587 } | |
588 | |
589 /* | |
590 %!test | |
6437 | 591 %! % Test fixed state |
592 %! rande("state",1); | |
6443 | 593 %! assert (rande(1,6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291],1e-6); |
6437 | 594 %!test |
6443 | 595 %! % Test fixed seed |
6437 | 596 %! rande("seed",1); |
6443 | 597 %! assert (rande(1,6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087],1e-6); |
5730 | 598 %!test |
6437 | 599 %! if (__random_statistical_tests__) |
600 %! % statistical tests may fail occasionally | |
601 %! rande("state",1); | |
602 %! x = rande(100000,1); | |
603 %! assert(min(x)>0); % *** Please report this!!! *** | |
604 %! assert(mean(x),1,0.01); | |
605 %! assert(var(x),1,0.03); | |
606 %! assert(skewness(x),2,0.06); | |
607 %! assert(kurtosis(x),6,0.7); | |
608 %! endif | |
609 %!test | |
610 %! if (__random_statistical_tests__) | |
611 %! % statistical tests may fail occasionally | |
612 %! rande("seed",1); | |
613 %! x = rande(100000,1); | |
614 %! assert(min(x)>0); % *** Please report this!!! *** | |
615 %! assert(mean(x),1,0.01); | |
616 %! assert(var(x),1,0.03); | |
617 %! assert(skewness(x),2,0.06); | |
618 %! assert(kurtosis(x),6,0.7); | |
619 %! endif | |
5730 | 620 */ |
621 | |
622 DEFUN_DLD (randg, args, , | |
623 "-*- texinfo -*-\n\ | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
624 @deftypefn {Loadable Function} {} randg (@var{n})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
625 @deftypefnx {Loadable Function} {} randg (@var{n}, @var{m}, @dots{})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
626 @deftypefnx {Loadable Function} {} randg ([@var{n} @var{m} @dots{}])\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
627 @deftypefnx {Loadable Function} {@var{v} =} randg (\"state\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
628 @deftypefnx {Loadable Function} {} randg (\"state\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
629 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
630 @deftypefnx {Loadable Function} {@var{v} =} randg (\"seed\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
631 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
632 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\ |
5730 | 633 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\ |
634 The arguments are handled the same as the arguments for @code{rand},\n\ | |
635 except for the argument @var{a}.\n\ | |
636 \n\ | |
637 This can be used to generate many distributions:\n\ | |
638 \n\ | |
639 @table @asis\n\ | |
6547 | 640 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\ |
10840 | 641 \n\ |
5730 | 642 @example\n\ |
6547 | 643 r = b * randg (a)\n\ |
5730 | 644 @end example\n\ |
10840 | 645 \n\ |
6547 | 646 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\ |
10840 | 647 \n\ |
5730 | 648 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
649 @group\n\ |
6547 | 650 r1 = randg (a, 1)\n\ |
651 r = r1 / (r1 + randg (b, 1))\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
652 @end group\n\ |
5730 | 653 @end example\n\ |
10840 | 654 \n\ |
6547 | 655 @item @code{Erlang (a, n)}\n\ |
10840 | 656 \n\ |
5730 | 657 @example\n\ |
6547 | 658 r = a * randg (n)\n\ |
5730 | 659 @end example\n\ |
10840 | 660 \n\ |
6547 | 661 @item @code{chisq (df)} for @code{df > 0}\n\ |
10840 | 662 \n\ |
5730 | 663 @example\n\ |
6547 | 664 r = 2 * randg (df / 2)\n\ |
5730 | 665 @end example\n\ |
10840 | 666 \n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
667 @item @code{t (df)} for @code{0 < df < inf} (use randn if df is infinite)\n\ |
10840 | 668 \n\ |
5730 | 669 @example\n\ |
6547 | 670 r = randn () / sqrt (2 * randg (df / 2) / df)\n\ |
5730 | 671 @end example\n\ |
10840 | 672 \n\ |
6547 | 673 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\ |
10840 | 674 \n\ |
5730 | 675 @example\n\ |
7096 | 676 @group\n\ |
677 ## r1 equals 1 if n1 is infinite\n\ | |
678 r1 = 2 * randg (n1 / 2) / n1\n\ | |
679 ## r2 equals 1 if n2 is infinite\n\ | |
680 r2 = 2 * randg (n2 / 2) / n2\n\ | |
5730 | 681 r = r1 / r2\n\n\ |
7096 | 682 @end group\n\ |
5730 | 683 @end example\n\ |
10840 | 684 \n\ |
5730 | 685 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\ |
10840 | 686 \n\ |
5730 | 687 @example\n\ |
6547 | 688 r = randp ((1 - p) / p * randg (n))\n\ |
5730 | 689 @end example\n\ |
10840 | 690 \n\ |
6547 | 691 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\ |
5730 | 692 (use chisq if @code{L = 0})\n\ |
10840 | 693 \n\ |
5730 | 694 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
695 @group\n\ |
6547 | 696 r = randp (L / 2)\n\ |
697 r(r > 0) = 2 * randg (r(r > 0))\n\ | |
698 r(df > 0) += 2 * randg (df(df > 0)/2)\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
699 @end group\n\ |
5730 | 700 @end example\n\ |
10840 | 701 \n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
702 @item @code{Dirichlet (a1, @dots{} ak)}\n\ |
10840 | 703 \n\ |
5730 | 704 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
705 @group\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
706 r = (randg (a1), @dots{}, randg (ak))\n\ |
6547 | 707 r = r / sum (r)\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
708 @end group\n\ |
5730 | 709 @end example\n\ |
10840 | 710 \n\ |
5730 | 711 @end table\n\ |
6547 | 712 @seealso{rand, randn, rande, randp}\n\ |
5730 | 713 @end deftypefn") |
714 { | |
715 octave_value retval; | |
716 | |
717 int nargin = args.length (); | |
718 | |
719 if (nargin < 1) | |
720 error ("randg: insufficient arguments"); | |
721 else | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
722 retval = do_rand (args, nargin, "randg", "gamma", true); |
5730 | 723 |
724 return retval; | |
725 } | |
726 | |
727 /* | |
728 %!test | |
6437 | 729 %! randg("state",12) |
730 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report | |
731 | |
732 | |
733 %!test | |
734 %! % Test fixed state | |
735 %! randg("state",1); | |
6443 | 736 %! assert (randg(0.1,1,6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12],1e-6); |
6437 | 737 %!test |
738 %! % Test fixed state | |
739 %! randg("state",1); | |
6443 | 740 %! assert (randg(0.95,1,6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939],1e-6); |
6437 | 741 %!test |
742 %! % Test fixed state | |
743 %! randg("state",1); | |
6443 | 744 %! assert (randg(1,1,6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533],1e-6); |
6437 | 745 %!test |
746 %! % Test fixed state | |
747 %! randg("state",1); | |
6443 | 748 %! assert (randg(10,1,6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059],1e-5); |
6437 | 749 %!test |
750 %! % Test fixed state | |
751 %! randg("state",1); | |
6443 | 752 %! assert (randg(100,1,6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118],1e-4); |
6437 | 753 %!test |
754 %! % Test fixed seed | |
755 %! randg("seed",1); | |
6443 | 756 %! assert (randg(0.1,1,6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12],1e-6); |
6437 | 757 %!test |
758 %! % Test fixed seed | |
759 %! randg("seed",1); | |
6443 | 760 %! assert (randg(0.95,1,6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648],1e-6); |
6437 | 761 %!test |
762 %! % Test fixed seed | |
763 %! randg("seed",1); | |
6443 | 764 %! assert (randg(1,1,6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303],1e-6); |
6437 | 765 %!test |
766 %! % Test fixed seed | |
767 %! randg("seed",1); | |
6443 | 768 %! assert (randg(10,1,6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186],1e-5); |
6437 | 769 %!test |
770 %! % Test fixed seed | |
771 %! randg("seed",1); | |
6443 | 772 %! assert (randg(100,1,6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625],1e-4); |
6437 | 773 %!test |
774 %! if (__random_statistical_tests__) | |
775 %! % statistical tests may fail occasionally. | |
776 %! randg("state",12) | |
777 %! a=0.1; x = randg(a,100000,1); | |
778 %! assert(mean(x), a, 0.01); | |
779 %! assert(var(x), a, 0.01); | |
780 %! assert(skewness(x),2/sqrt(a), 1.); | |
781 %! assert(kurtosis(x),6/a, 50.); | |
782 %! endif | |
783 %!test | |
784 %! if (__random_statistical_tests__) | |
785 %! % statistical tests may fail occasionally. | |
786 %! randg("state",12) | |
787 %! a=0.95; x = randg(a,100000,1); | |
788 %! assert(mean(x), a, 0.01); | |
789 %! assert(var(x), a, 0.04); | |
790 %! assert(skewness(x),2/sqrt(a), 0.2); | |
791 %! assert(kurtosis(x),6/a, 2.); | |
792 %! endif | |
793 %!test | |
794 %! if (__random_statistical_tests__) | |
795 %! % statistical tests may fail occasionally. | |
796 %! randg("state",12) | |
797 %! a=1; x = randg(a,100000,1); | |
798 %! assert(mean(x),a, 0.01); | |
799 %! assert(var(x),a, 0.04); | |
800 %! assert(skewness(x),2/sqrt(a), 0.2); | |
801 %! assert(kurtosis(x),6/a, 2.); | |
802 %! endif | |
803 %!test | |
804 %! if (__random_statistical_tests__) | |
805 %! % statistical tests may fail occasionally. | |
806 %! randg("state",12) | |
807 %! a=10; x = randg(a,100000,1); | |
808 %! assert(mean(x), a, 0.1); | |
809 %! assert(var(x), a, 0.5); | |
810 %! assert(skewness(x),2/sqrt(a), 0.1); | |
811 %! assert(kurtosis(x),6/a, 0.5); | |
812 %! endif | |
813 %!test | |
814 %! if (__random_statistical_tests__) | |
815 %! % statistical tests may fail occasionally. | |
816 %! randg("state",12) | |
817 %! a=100; x = randg(a,100000,1); | |
818 %! assert(mean(x), a, 0.2); | |
819 %! assert(var(x), a, 2.); | |
820 %! assert(skewness(x),2/sqrt(a), 0.05); | |
821 %! assert(kurtosis(x),6/a, 0.2); | |
822 %! endif | |
823 %!test | |
824 %! randg("seed",12) | |
5730 | 825 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report |
826 %!test | |
6437 | 827 %! if (__random_statistical_tests__) |
828 %! % statistical tests may fail occasionally. | |
829 %! randg("seed",12) | |
830 %! a=0.1; x = randg(a,100000,1); | |
831 %! assert(mean(x), a, 0.01); | |
832 %! assert(var(x), a, 0.01); | |
833 %! assert(skewness(x),2/sqrt(a), 1.); | |
834 %! assert(kurtosis(x),6/a, 50.); | |
835 %! endif | |
5730 | 836 %!test |
6437 | 837 %! if (__random_statistical_tests__) |
838 %! % statistical tests may fail occasionally. | |
839 %! randg("seed",12) | |
840 %! a=0.95; x = randg(a,100000,1); | |
841 %! assert(mean(x), a, 0.01); | |
842 %! assert(var(x), a, 0.04); | |
843 %! assert(skewness(x),2/sqrt(a), 0.2); | |
844 %! assert(kurtosis(x),6/a, 2.); | |
845 %! endif | |
5730 | 846 %!test |
6437 | 847 %! if (__random_statistical_tests__) |
848 %! % statistical tests may fail occasionally. | |
849 %! randg("seed",12) | |
850 %! a=1; x = randg(a,100000,1); | |
851 %! assert(mean(x),a, 0.01); | |
852 %! assert(var(x),a, 0.04); | |
853 %! assert(skewness(x),2/sqrt(a), 0.2); | |
854 %! assert(kurtosis(x),6/a, 2.); | |
855 %! endif | |
5730 | 856 %!test |
6437 | 857 %! if (__random_statistical_tests__) |
858 %! % statistical tests may fail occasionally. | |
859 %! randg("seed",12) | |
860 %! a=10; x = randg(a,100000,1); | |
861 %! assert(mean(x), a, 0.1); | |
862 %! assert(var(x), a, 0.5); | |
863 %! assert(skewness(x),2/sqrt(a), 0.1); | |
864 %! assert(kurtosis(x),6/a, 0.5); | |
865 %! endif | |
5730 | 866 %!test |
6437 | 867 %! if (__random_statistical_tests__) |
868 %! % statistical tests may fail occasionally. | |
869 %! randg("seed",12) | |
870 %! a=100; x = randg(a,100000,1); | |
871 %! assert(mean(x), a, 0.2); | |
872 %! assert(var(x), a, 2.); | |
873 %! assert(skewness(x),2/sqrt(a), 0.05); | |
874 %! assert(kurtosis(x),6/a, 0.2); | |
875 %! endif | |
5730 | 876 */ |
877 | |
878 | |
879 DEFUN_DLD (randp, args, , | |
880 "-*- texinfo -*-\n\ | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
881 @deftypefn {Loadable Function} {} randp (@var{l}, @var{n})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
882 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m}, @dots{})\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
883 @deftypefnx {Loadable Function} {} randp (@var{l}, [@var{n} @var{m} @dots{}])\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
884 @deftypefnx {Loadable Function} {@var{v} =} randp (\"state\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
885 @deftypefnx {Loadable Function} {} randp (\"state\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
886 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
887 @deftypefnx {Loadable Function} {@var{v} =} randp (\"seed\")\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
888 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{v})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
889 @deftypefnx {Loadable Function} {} randp (\"seed\", \"reset\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
890 Return a matrix with Poisson distributed random elements with mean value\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
891 parameter given by the first argument, @var{l}. The arguments\n\ |
5730 | 892 are handled the same as the arguments for @code{rand}, except for the\n\ |
893 argument @var{l}.\n\ | |
894 \n\ | |
895 Five different algorithms are used depending on the range of @var{l}\n\ | |
896 and whether or not @var{l} is a scalar or a matrix.\n\ | |
897 \n\ | |
898 @table @asis\n\ | |
10840 | 899 @item For scalar @var{l} @leq{} 12, use direct method.\n\ |
900 W.H. Press, et al., @cite{Numerical Recipes in C},\n\ | |
901 Cambridge University Press, 1992.\n\ | |
902 \n\ | |
5730 | 903 @item For scalar @var{l} > 12, use rejection method.[1]\n\ |
10840 | 904 W.H. Press, et al., @cite{Numerical Recipes in C},\n\ |
905 Cambridge University Press, 1992.\n\ | |
906 \n\ | |
907 @item For matrix @var{l} @leq{} 10, use inversion method.[2]\n\ | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
908 E. Stadlober, et al., WinRand source code, available via FTP.\n\ |
10840 | 909 \n\ |
5730 | 910 @item For matrix @var{l} > 10, use patchwork rejection method.\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
911 E. Stadlober, et al., WinRand source code, available via FTP, or\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
912 H. Zechner, @cite{Efficient sampling from continuous and discrete\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
913 unimodal distributions}, Doctoral Dissertation, 156pp., Technical\n\ |
5730 | 914 University Graz, Austria, 1994.\n\ |
10840 | 915 \n\ |
5730 | 916 @item For @var{l} > 1e8, use normal approximation.\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
917 L. Montanet, et al., @cite{Review of Particle Properties}, Physical Review\n\ |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
918 D 50 p1284, 1994.\n\ |
5730 | 919 @end table\n\ |
6547 | 920 @seealso{rand, randn, rande, randg}\n\ |
5730 | 921 @end deftypefn") |
922 { | |
923 octave_value retval; | |
924 | |
925 int nargin = args.length (); | |
926 | |
927 if (nargin < 1) | |
928 error ("randp: insufficient arguments"); | |
929 else | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
930 retval = do_rand (args, nargin, "randp", "poisson", true); |
5730 | 931 |
932 return retval; | |
933 } | |
934 | |
935 /* | |
936 %!test | |
6437 | 937 %! randp("state",12) |
938 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report | |
939 %!test | |
940 %! % Test fixed state | |
941 %! randp("state",1); | |
942 %! assert(randp(5,1,6),[5 5 3 7 7 3]) | |
943 %!test | |
944 %! % Test fixed state | |
945 %! randp("state",1); | |
946 %! assert(randp(15,1,6),[13 15 8 18 18 15]) | |
947 %!test | |
948 %! % Test fixed state | |
949 %! randp("state",1); | |
7421 | 950 %! assert(randp(1e9,1,6),[999915677 999976657 1000047684 1000019035 999985749 999977692],-1e-6) |
6437 | 951 %!test |
952 %! % Test fixed state | |
953 %! randp("seed",1); | |
6449 | 954 %! %%assert(randp(5,1,6),[8 2 3 6 6 8]) |
955 %! assert(randp(5,1,5),[8 2 3 6 6]) | |
6437 | 956 %!test |
957 %! % Test fixed state | |
958 %! randp("seed",1); | |
959 %! assert(randp(15,1,6),[15 16 12 10 10 12]) | |
960 %!test | |
961 %! % Test fixed state | |
962 %! randp("seed",1); | |
7421 | 963 %! assert(randp(1e9,1,6),[1000006208 1000012224 999981120 999963520 999963072 999981440],-1e-6) |
6437 | 964 %!test |
965 %! if (__random_statistical_tests__) | |
966 %! % statistical tests may fail occasionally. | |
967 %! randp("state",12) | |
968 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
969 %! x = randp(a(1),100000,1); | |
970 %! assert(min(x)>=0); % *** Please report this!!! *** | |
971 %! assert(mean(x),a(1),a(2)); | |
972 %! assert(var(x),a(1),0.02*a(1)); | |
973 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
974 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
975 %! endfor | |
976 %! endif | |
977 %!test | |
978 %! if (__random_statistical_tests__) | |
979 %! % statistical tests may fail occasionally. | |
980 %! randp("state",12) | |
981 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
982 %! x = randp(a(1)*ones(100000,1),100000,1); | |
983 %! assert(min(x)>=0); % *** Please report this!!! *** | |
984 %! assert(mean(x),a(1),a(2)); | |
985 %! assert(var(x),a(1),0.02*a(1)); | |
986 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
987 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
988 %! endfor | |
989 %! endif | |
990 %!test | |
991 %! randp("seed",12) | |
5730 | 992 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report |
993 %!test | |
6449 | 994 %! if (__random_statistical_tests__) |
995 %! % statistical tests may fail occasionally. | |
996 %! randp("seed",12) | |
997 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
998 %! x = randp(a(1),100000,1); | |
999 %! assert(min(x)>=0); % *** Please report this!!! *** | |
1000 %! assert(mean(x),a(1),a(2)); | |
1001 %! assert(var(x),a(1),0.02*a(1)); | |
1002 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
1003 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
1004 %! endfor | |
1005 %! endif | |
5730 | 1006 %!test |
6449 | 1007 %! if (__random_statistical_tests__) |
1008 %! % statistical tests may fail occasionally. | |
1009 %! randp("seed",12) | |
1010 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
1011 %! x = randp(a(1)*ones(100000,1),100000,1); | |
1012 %! assert(min(x)>=0); % *** Please report this!!! *** | |
1013 %! assert(mean(x),a(1),a(2)); | |
1014 %! assert(var(x),a(1),0.02*a(1)); | |
1015 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
1016 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
1017 %! endfor | |
1018 %! endif | |
5730 | 1019 */ |
1020 | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1021 DEFUN_DLD (randperm, args, , |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1022 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
1023 @deftypefn {Loadable Function} {} randperm (@var{n})\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1024 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1025 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\ |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1026 If @var{m} is supplied, return @var{m} unique entries, sampled without\n\ |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13739
diff
changeset
|
1027 replacement from @code{1:@var{n}}. The complexity is O(@var{n}) in\n\ |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1028 memory and O(@var{m}) in time, unless @var{m} < @var{n}/5, in which case\n\ |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13739
diff
changeset
|
1029 O(@var{m}) memory is used as well. The randomization is performed using\n\ |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1030 rand(). All permutations are equally likely.\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1031 @seealso{perms}\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1032 @end deftypefn") |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1033 { |
13739
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1034 |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1035 #ifdef USE_UNORDERED_MAP_WITH_TR1 |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1036 using std::tr1::unordered_map; |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1037 #else |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1038 using std::unordered_map; |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1039 #endif |
0206484682c6
rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13727
diff
changeset
|
1040 |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1041 int nargin = args.length (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1042 octave_value retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1043 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1044 if (nargin == 1 || nargin == 2) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1045 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1046 octave_idx_type n, m; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
1047 |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1048 n = args(0).idx_type_value (true); |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1049 |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1050 if (nargin == 2) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1051 m = args(1).idx_type_value (true); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1052 else |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1053 m = n; |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1054 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1055 if (m < 0 || n < 0) |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1056 error ("randperm: M and N must be non-negative"); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1057 |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1058 if (m > n) |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1059 error ("randperm: M must be less than or equal to N"); |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1060 |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1061 // Quick and dirty heuristic to decide if we allocate or not the |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1062 // whole vector for tracking the truncated shuffle. |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1063 bool short_shuffle = m < n/5 && m < 1e5; |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1064 |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1065 if (! error_state) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1066 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1067 // Generate random numbers. |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1068 NDArray r = octave_rand::nd_array (dim_vector (1, m)); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1069 double *rvec = r.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1070 |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1071 octave_idx_type idx_len = short_shuffle ? m : n; |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1072 Array<octave_idx_type> idx (dim_vector (1, idx_len)); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1073 octave_idx_type *ivec = idx.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1074 |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1075 for (octave_idx_type i = 0; i < idx_len; i++) |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1076 ivec[i] = i; |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1077 |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1078 if (short_shuffle) |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1079 { |
13727
478efc95cb7a
Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13255
diff
changeset
|
1080 unordered_map<octave_idx_type, octave_idx_type> map (m); |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1081 |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1082 // Perform the Knuth shuffle only keeping track of moved |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1083 // entries in the map |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1084 for (octave_idx_type i = 0; i < m; i++) |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1085 { |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1086 octave_idx_type k = i + |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1087 gnulib::floor (rvec[i] * (n - i)); |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1088 |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1089 //For shuffling first m entries, no need to use extra |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1090 //storage |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1091 if (k < m) |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1092 { |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1093 std::swap (ivec[i], ivec[k]); |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1094 } |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1095 else |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1096 { |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1097 if (map.find (k) == map.end ()) |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1098 map[k] = k; |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1099 |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1100 std::swap (ivec[i], map[k]); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1101 } |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1102 } |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1103 } |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1104 else |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1105 { |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1106 |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1107 // Perform the Knuth shuffle of the first m entries |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1108 for (octave_idx_type i = 0; i < m; i++) |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1109 { |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1110 octave_idx_type k = i + |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1111 gnulib::floor (rvec[i] * (n - i)); |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1112 std::swap (ivec[i], ivec[k]); |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1113 } |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1114 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1115 |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1116 // Convert to doubles, reusing r. |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1117 for (octave_idx_type i = 0; i < m; i++) |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1118 rvec[i] = ivec[i] + 1; |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1119 |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1120 if (m < n) |
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1121 idx.resize (dim_vector (1, m)); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1122 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1123 // Now create an array object with a cached idx_vector. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
1124 retval = new octave_matrix (r, idx_vector (idx)); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1125 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1126 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1127 else |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1128 print_usage (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1129 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1130 return retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1131 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1132 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1133 /* |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1134 %!assert (sort (randperm (20)), 1:20) |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1135 %!assert (length (randperm (20,10)), 10) |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1136 |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1137 %!test |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1138 %! rand ("seed", 0); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1139 %! for i = 1:100 |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1140 %! p = randperm (305, 30); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1141 %! assert (length (unique (p)), 30); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1142 %! endfor |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1143 */ |