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