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