Mercurial > octave
annotate src/DLD-FUNCTIONS/rand.cc @ 10709:92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 16 Jun 2010 23:27:58 +0200 |
parents | a8ce6bdecce5 |
children | d1f920d1ce0c |
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 } |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
246 else 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
|
247 && 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
|
248 octave_rand::reset (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
249 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
250 error ("%s: seed must be a real scalar", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
251 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
252 else if (ts == "state" || ts == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
253 { |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
254 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
|
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 (fcn); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
257 else |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
258 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
259 ColumnVector s = |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
260 ColumnVector (args(idx+1).vector_value(false, true)); |
5730 | 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 (! 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
|
263 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
|
264 } |
10154
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 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
267 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
268 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
269 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
270 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
271 dims.resize (nargin); |
4543 | 272 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
273 for (int i = 0; i < nargin; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
274 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
275 dims(i) = args(idx+i).int_value (); |
4543 | 276 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
277 if (error_state) |
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 error ("%s: expecting integer arguments", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
280 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
281 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
282 } |
4543 | 283 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
284 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
285 } |
4543 | 286 } |
287 break; | |
2928 | 288 } |
289 | |
4543 | 290 done: |
2928 | 291 |
292 return retval; | |
293 | |
294 gen_matrix: | |
295 | |
5355 | 296 dims.chop_trailing_singletons (); |
297 | |
5730 | 298 if (additional_arg) |
299 { | |
300 if (a.length() == 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
301 return octave_rand::nd_array (dims, a(0)); |
5730 | 302 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
303 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
304 if (a.dims() != dims) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
305 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
306 error ("%s: mismatch in argument size", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
307 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
308 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
309 octave_idx_type len = a.length (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
310 NDArray m (dims); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
311 double *v = m.fortran_vec (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
312 for (octave_idx_type i = 0; i < len; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
313 v[i] = octave_rand::scalar (a(i)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
314 return m; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
315 } |
5730 | 316 } |
317 else | |
318 return octave_rand::nd_array (dims); | |
2928 | 319 } |
320 | |
4665 | 321 DEFUN_DLD (rand, args, , |
3369 | 322 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
323 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
3369 | 324 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
5730 | 325 @deftypefnx {Loadable Function} {} rand (\"state\", @var{x})\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
|
326 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\ |
5730 | 327 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{x})\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
|
328 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\ |
3369 | 329 Return a matrix with random elements uniformly distributed on the\n\ |
330 interval (0, 1). The arguments are handled the same as the arguments\n\ | |
5730 | 331 for @code{eye}.\n\ |
332 \n\ | |
333 You can query the state of the random number generator using the\n\ | |
3369 | 334 form\n\ |
2928 | 335 \n\ |
3369 | 336 @example\n\ |
5730 | 337 v = rand (\"state\")\n\ |
338 @end example\n\ | |
339 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
340 This returns a column vector @var{v} of length 625. Later, you can\n\ |
5730 | 341 restore the random number generator to the state @var{v}\n\ |
342 using the form\n\ | |
343 \n\ | |
344 @example\n\ | |
345 rand (\"state\", v)\n\ | |
3369 | 346 @end example\n\ |
347 \n\ | |
348 @noindent\n\ | |
5730 | 349 You may also initialize the state vector from an arbitrary vector of\n\ |
350 length <= 625 for @var{v}. This new state will be a hash based on the\n\ | |
5798 | 351 value of @var{v}, not @var{v} itself.\n\ |
5730 | 352 \n\ |
353 By default, the generator is initialized from @code{/dev/urandom} if it is\n\ | |
354 available, otherwise from cpu time, wall clock time and the current\n\ | |
355 fraction of a second.\n\ | |
356 \n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
357 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
|
358 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
|
359 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator}, ACM Trans. on\n\ |
7001 | 360 Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 1998,\n\ |
7171 | 361 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\ |
6547 | 362 Do @strong{not} use for cryptography without securely hashing\n\ |
363 several returned values together, otherwise the generator state\n\ | |
364 can be learned after reading 624 consecutive values.\n\ | |
5730 | 365 \n\ |
7096 | 366 Older versions of Octave used a different random number generator.\n\ |
367 The new generator is used by default\n\ | |
5730 | 368 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
|
369 random numbers with a significantly longer cycle time. However, in\n\ |
5798 | 370 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
|
371 sequences as used by the old generators. To do this the keyword\n\ |
5730 | 372 \"seed\" is used to specify that the old generators should be use,\n\ |
373 as in\n\ | |
2928 | 374 \n\ |
3369 | 375 @example\n\ |
5730 | 376 rand (\"seed\", val)\n\ |
3369 | 377 @end example\n\ |
378 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
379 which sets the seed of the generator to @var{val}. The seed of the\n\ |
5730 | 380 generator can be queried with\n\ |
381 \n\ | |
382 @example\n\ | |
383 s = rand (\"seed\")\n\ | |
384 @end example\n\ | |
385 \n\ | |
386 However, it should be noted that querying the seed will not cause\n\ | |
387 @code{rand} to use the old generators, only setting the seed will.\n\ | |
388 To cause @code{rand} to once again use the new generators, the\n\ | |
389 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
|
390 \n\ |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
391 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
|
392 using the \"reset\" keyword.\n\ |
5798 | 393 @seealso{randn, rande, randg, randp}\n\ |
3369 | 394 @end deftypefn") |
2928 | 395 { |
4307 | 396 octave_value retval; |
2928 | 397 |
398 int nargin = args.length (); | |
399 | |
4543 | 400 retval = do_rand (args, nargin, "rand"); |
2928 | 401 |
402 return retval; | |
403 } | |
404 | |
8871 | 405 // FIXME -- The old generator (selected when "seed" is set) will not |
406 // work properly if compiled to use 64-bit integers. | |
407 | |
5730 | 408 /* |
409 %!test # 'state' can be a scalar | |
410 %! rand('state',12); x = rand(1,4); | |
411 %! rand('state',12); y = rand(1,4); | |
412 %! assert(x,y); | |
413 %!test # 'state' can be a vector | |
414 %! rand('state',[12,13]); x=rand(1,4); | |
415 %! rand('state',[12;13]); y=rand(1,4); | |
416 %! assert(x,y); | |
417 %!test # querying 'state' doesn't disturb sequence | |
418 %! rand('state',12); rand(1,2); x=rand(1,2); | |
419 %! rand('state',12); rand(1,2); | |
420 %! s=rand('state'); y=rand(1,2); | |
421 %! assert(x,y); | |
422 %! rand('state',s); z=rand(1,2); | |
423 %! assert(x,z); | |
424 %!test # 'seed' must be a scalar | |
425 %! rand('seed',12); x = rand(1,4); | |
426 %! rand('seed',12); y = rand(1,4); | |
427 %! assert(x,y); | |
428 %!error(rand('seed',[12,13])) | |
429 %!test # querying 'seed' returns a value which can be used later | |
430 %! s=rand('seed'); x=rand(1,2); | |
431 %! rand('seed',s); y=rand(1,2); | |
432 %! assert(x,y); | |
433 %!test # querying 'seed' doesn't disturb sequence | |
434 %! rand('seed',12); rand(1,2); x=rand(1,2); | |
435 %! rand('seed',12); rand(1,2); | |
436 %! s=rand('seed'); y=rand(1,2); | |
437 %! assert(x,y); | |
438 %! rand('seed',s); z=rand(1,2); | |
439 %! assert(x,z); | |
440 */ | |
441 | |
442 /* | |
443 %!test | |
6437 | 444 %! % Test fixed state |
445 %! rand("state",1); | |
6443 | 446 %! assert (rand(1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382],1e-6); |
6437 | 447 %!test |
6443 | 448 %! % Test fixed seed |
6437 | 449 %! rand("seed",1); |
6443 | 450 %! assert (rand(1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759],1e-6); |
5730 | 451 %!test |
6437 | 452 %! if (__random_statistical_tests__) |
453 %! % statistical tests may fail occasionally. | |
454 %! rand("state",12); | |
455 %! x = rand(100000,1); | |
456 %! assert(max(x)<1.); %*** Please report this!!! *** | |
457 %! assert(min(x)>0.); %*** Please report this!!! *** | |
458 %! assert(mean(x),0.5,0.0024); | |
459 %! assert(var(x),1/48,0.0632); | |
460 %! assert(skewness(x),0,0.012); | |
461 %! assert(kurtosis(x),-6/5,0.0094); | |
462 %! endif | |
463 %!test | |
464 %! if (__random_statistical_tests__) | |
465 %! % statistical tests may fail occasionally. | |
466 %! rand("seed",12); | |
467 %! x = rand(100000,1); | |
468 %! assert(max(x)<1.); %*** Please report this!!! *** | |
469 %! assert(min(x)>0.); %*** Please report this!!! *** | |
470 %! assert(mean(x),0.5,0.0024); | |
471 %! assert(var(x),1/48,0.0632); | |
472 %! assert(skewness(x),0,0.012); | |
473 %! assert(kurtosis(x),-6/5,0.0094); | |
474 %! endif | |
5730 | 475 */ |
476 | |
477 | |
4307 | 478 static std::string current_distribution = octave_rand::distribution (); |
479 | |
2928 | 480 static void |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
481 reset_rand_generator (void) |
2928 | 482 { |
4307 | 483 octave_rand::distribution (current_distribution); |
2928 | 484 } |
485 | |
4665 | 486 DEFUN_DLD (randn, args, , |
3369 | 487 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
488 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
3369 | 489 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
5730 | 490 @deftypefnx {Loadable Function} {} randn (\"state\", @var{x})\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
|
491 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\ |
5730 | 492 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{x})\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
|
493 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
494 Return a matrix with normally distributed random\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
495 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
|
496 handled the same as the arguments for @code{rand}.\n\ |
3369 | 497 \n\ |
8497
63d2f6508dde
[docs] Ziggurat technique => ``Ziggurat technique''
Brian Gough <bjg@gnu.org>
parents:
8483
diff
changeset
|
498 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
|
499 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
|
500 W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ |
5730 | 501 J. Statistical Software, vol 5, 2000,\n\ |
502 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
2928 | 503 \n\ |
6547 | 504 @seealso{rand, rande, randg, randp}\n\ |
3369 | 505 @end deftypefn") |
2928 | 506 { |
4307 | 507 octave_value retval; |
2928 | 508 |
509 int nargin = args.length (); | |
510 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
511 unwind_protect frame; |
2928 | 512 |
4543 | 513 // This relies on the fact that elements are popped from the unwind |
514 // stack in the reverse of the order they are pushed | |
515 // (i.e. current_distribution will be reset before calling | |
516 // reset_rand_generator()). | |
2928 | 517 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
518 frame.add_fcn (reset_rand_generator); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
519 frame.protect_var (current_distribution); |
2928 | 520 |
4543 | 521 current_distribution = "normal"; |
2928 | 522 |
4543 | 523 octave_rand::distribution (current_distribution); |
2928 | 524 |
4543 | 525 retval = do_rand (args, nargin, "randn"); |
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\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
563 @deftypefn {Loadable Function} {} rande (@var{x})\n\ |
5730 | 564 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m})\n\ |
565 @deftypefnx {Loadable Function} {} rande (\"state\", @var{x})\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
|
566 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\ |
5730 | 567 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{x})\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 (\"seed\", \"reset\")\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
569 Return a matrix with exponentially distributed random elements. The\n\ |
5730 | 570 arguments are handled the same as the arguments for @code{rand}.\n\ |
571 \n\ | |
8497
63d2f6508dde
[docs] Ziggurat technique => ``Ziggurat technique''
Brian Gough <bjg@gnu.org>
parents:
8483
diff
changeset
|
572 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
|
573 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
|
574 W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ |
5730 | 575 J. Statistical Software, vol 5, 2000,\n\ |
576 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
6547 | 577 @seealso{rand, randn, randg, randp}\n\ |
5730 | 578 @end deftypefn") |
579 { | |
580 octave_value retval; | |
581 | |
582 int nargin = args.length (); | |
583 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
584 unwind_protect frame; |
5730 | 585 |
586 // This relies on the fact that elements are popped from the unwind | |
587 // stack in the reverse of the order they are pushed | |
588 // (i.e. current_distribution will be reset before calling | |
589 // reset_rand_generator()). | |
590 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
591 frame.add_fcn (reset_rand_generator); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
592 frame.protect_var (current_distribution); |
5730 | 593 |
594 current_distribution = "exponential"; | |
595 | |
596 octave_rand::distribution (current_distribution); | |
597 | |
598 retval = do_rand (args, nargin, "rande"); | |
599 | |
600 return retval; | |
601 } | |
602 | |
603 /* | |
604 %!test | |
6437 | 605 %! % Test fixed state |
606 %! rande("state",1); | |
6443 | 607 %! assert (rande(1,6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291],1e-6); |
6437 | 608 %!test |
6443 | 609 %! % Test fixed seed |
6437 | 610 %! rande("seed",1); |
6443 | 611 %! assert (rande(1,6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087],1e-6); |
5730 | 612 %!test |
6437 | 613 %! if (__random_statistical_tests__) |
614 %! % statistical tests may fail occasionally | |
615 %! rande("state",1); | |
616 %! x = rande(100000,1); | |
617 %! assert(min(x)>0); % *** Please report this!!! *** | |
618 %! assert(mean(x),1,0.01); | |
619 %! assert(var(x),1,0.03); | |
620 %! assert(skewness(x),2,0.06); | |
621 %! assert(kurtosis(x),6,0.7); | |
622 %! endif | |
623 %!test | |
624 %! if (__random_statistical_tests__) | |
625 %! % statistical tests may fail occasionally | |
626 %! rande("seed",1); | |
627 %! x = rande(100000,1); | |
628 %! assert(min(x)>0); % *** Please report this!!! *** | |
629 %! assert(mean(x),1,0.01); | |
630 %! assert(var(x),1,0.03); | |
631 %! assert(skewness(x),2,0.06); | |
632 %! assert(kurtosis(x),6,0.7); | |
633 %! endif | |
5730 | 634 */ |
635 | |
636 DEFUN_DLD (randg, args, , | |
637 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
638 @deftypefn {Loadable Function} {} randg (@var{a}, @var{x})\n\ |
5730 | 639 @deftypefnx {Loadable Function} {} randg (@var{a}, @var{n}, @var{m})\n\ |
640 @deftypefnx {Loadable Function} {} randg (\"state\", @var{x})\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
|
641 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\ |
5730 | 642 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{x})\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
|
643 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\ |
5730 | 644 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\ |
645 The arguments are handled the same as the arguments for @code{rand},\n\ | |
646 except for the argument @var{a}.\n\ | |
647 \n\ | |
648 This can be used to generate many distributions:\n\ | |
649 \n\ | |
650 @table @asis\n\ | |
6547 | 651 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\ |
5730 | 652 @example\n\ |
6547 | 653 r = b * randg (a)\n\ |
5730 | 654 @end example\n\ |
6547 | 655 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\ |
5730 | 656 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
657 @group\n\ |
6547 | 658 r1 = randg (a, 1)\n\ |
659 r = r1 / (r1 + randg (b, 1))\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
660 @end group\n\ |
5730 | 661 @end example\n\ |
6547 | 662 @item @code{Erlang (a, n)}\n\ |
5730 | 663 @example\n\ |
6547 | 664 r = a * randg (n)\n\ |
5730 | 665 @end example\n\ |
6547 | 666 @item @code{chisq (df)} for @code{df > 0}\n\ |
5730 | 667 @example\n\ |
6547 | 668 r = 2 * randg (df / 2)\n\ |
5730 | 669 @end example\n\ |
670 @item @code{t(df)} for @code{0 < df < inf} (use randn if df is infinite)\n\ | |
671 @example\n\ | |
6547 | 672 r = randn () / sqrt (2 * randg (df / 2) / df)\n\ |
5730 | 673 @end example\n\ |
6547 | 674 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\ |
5730 | 675 @example\n\ |
7096 | 676 @group\n\ |
677 ## r1 equals 1 if n1 is infinite\n\ | |
678 r1 = 2 * randg (n1 / 2) / n1\n\ | |
679 ## r2 equals 1 if n2 is infinite\n\ | |
680 r2 = 2 * randg (n2 / 2) / n2\n\ | |
5730 | 681 r = r1 / r2\n\n\ |
7096 | 682 @end group\n\ |
5730 | 683 @end example\n\ |
684 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\ | |
685 @example\n\ | |
6547 | 686 r = randp ((1 - p) / p * randg (n))\n\ |
5730 | 687 @end example\n\ |
6547 | 688 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\ |
5730 | 689 (use chisq if @code{L = 0})\n\ |
690 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
691 @group\n\ |
6547 | 692 r = randp (L / 2)\n\ |
693 r(r > 0) = 2 * randg (r(r > 0))\n\ | |
694 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
|
695 @end group\n\ |
5730 | 696 @end example\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
697 @item @code{Dirichlet (a1, @dots{} ak)}\n\ |
5730 | 698 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
699 @group\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
700 r = (randg (a1), @dots{}, randg (ak))\n\ |
6547 | 701 r = r / sum (r)\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
702 @end group\n\ |
5730 | 703 @end example\n\ |
704 @end table\n\ | |
6547 | 705 @seealso{rand, randn, rande, randp}\n\ |
5730 | 706 @end deftypefn") |
707 { | |
708 octave_value retval; | |
709 | |
710 int nargin = args.length (); | |
711 | |
712 if (nargin < 1) | |
713 error ("randg: insufficient arguments"); | |
714 else | |
715 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
716 unwind_protect frame; |
5730 | 717 |
718 // This relies on the fact that elements are popped from the unwind | |
719 // stack in the reverse of the order they are pushed | |
720 // (i.e. current_distribution will be reset before calling | |
721 // reset_rand_generator()). | |
722 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
723 frame.add_fcn (reset_rand_generator); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
724 frame.protect_var (current_distribution); |
5730 | 725 |
726 current_distribution = "gamma"; | |
727 | |
728 octave_rand::distribution (current_distribution); | |
729 | |
730 retval = do_rand (args, nargin, "randg", true); | |
731 } | |
732 | |
733 return retval; | |
734 } | |
735 | |
736 /* | |
737 %!test | |
6437 | 738 %! randg("state",12) |
739 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report | |
740 | |
741 | |
742 %!test | |
743 %! % Test fixed state | |
744 %! randg("state",1); | |
6443 | 745 %! assert (randg(0.1,1,6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12],1e-6); |
6437 | 746 %!test |
747 %! % Test fixed state | |
748 %! randg("state",1); | |
6443 | 749 %! assert (randg(0.95,1,6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939],1e-6); |
6437 | 750 %!test |
751 %! % Test fixed state | |
752 %! randg("state",1); | |
6443 | 753 %! assert (randg(1,1,6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533],1e-6); |
6437 | 754 %!test |
755 %! % Test fixed state | |
756 %! randg("state",1); | |
6443 | 757 %! assert (randg(10,1,6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059],1e-5); |
6437 | 758 %!test |
759 %! % Test fixed state | |
760 %! randg("state",1); | |
6443 | 761 %! assert (randg(100,1,6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118],1e-4); |
6437 | 762 %!test |
763 %! % Test fixed seed | |
764 %! randg("seed",1); | |
6443 | 765 %! assert (randg(0.1,1,6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12],1e-6); |
6437 | 766 %!test |
767 %! % Test fixed seed | |
768 %! randg("seed",1); | |
6443 | 769 %! assert (randg(0.95,1,6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648],1e-6); |
6437 | 770 %!test |
771 %! % Test fixed seed | |
772 %! randg("seed",1); | |
6443 | 773 %! assert (randg(1,1,6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303],1e-6); |
6437 | 774 %!test |
775 %! % Test fixed seed | |
776 %! randg("seed",1); | |
6443 | 777 %! assert (randg(10,1,6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186],1e-5); |
6437 | 778 %!test |
779 %! % Test fixed seed | |
780 %! randg("seed",1); | |
6443 | 781 %! assert (randg(100,1,6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625],1e-4); |
6437 | 782 %!test |
783 %! if (__random_statistical_tests__) | |
784 %! % statistical tests may fail occasionally. | |
785 %! randg("state",12) | |
786 %! a=0.1; x = randg(a,100000,1); | |
787 %! assert(mean(x), a, 0.01); | |
788 %! assert(var(x), a, 0.01); | |
789 %! assert(skewness(x),2/sqrt(a), 1.); | |
790 %! assert(kurtosis(x),6/a, 50.); | |
791 %! endif | |
792 %!test | |
793 %! if (__random_statistical_tests__) | |
794 %! % statistical tests may fail occasionally. | |
795 %! randg("state",12) | |
796 %! a=0.95; x = randg(a,100000,1); | |
797 %! assert(mean(x), a, 0.01); | |
798 %! assert(var(x), a, 0.04); | |
799 %! assert(skewness(x),2/sqrt(a), 0.2); | |
800 %! assert(kurtosis(x),6/a, 2.); | |
801 %! endif | |
802 %!test | |
803 %! if (__random_statistical_tests__) | |
804 %! % statistical tests may fail occasionally. | |
805 %! randg("state",12) | |
806 %! a=1; x = randg(a,100000,1); | |
807 %! assert(mean(x),a, 0.01); | |
808 %! assert(var(x),a, 0.04); | |
809 %! assert(skewness(x),2/sqrt(a), 0.2); | |
810 %! assert(kurtosis(x),6/a, 2.); | |
811 %! endif | |
812 %!test | |
813 %! if (__random_statistical_tests__) | |
814 %! % statistical tests may fail occasionally. | |
815 %! randg("state",12) | |
816 %! a=10; x = randg(a,100000,1); | |
817 %! assert(mean(x), a, 0.1); | |
818 %! assert(var(x), a, 0.5); | |
819 %! assert(skewness(x),2/sqrt(a), 0.1); | |
820 %! assert(kurtosis(x),6/a, 0.5); | |
821 %! endif | |
822 %!test | |
823 %! if (__random_statistical_tests__) | |
824 %! % statistical tests may fail occasionally. | |
825 %! randg("state",12) | |
826 %! a=100; x = randg(a,100000,1); | |
827 %! assert(mean(x), a, 0.2); | |
828 %! assert(var(x), a, 2.); | |
829 %! assert(skewness(x),2/sqrt(a), 0.05); | |
830 %! assert(kurtosis(x),6/a, 0.2); | |
831 %! endif | |
832 %!test | |
833 %! randg("seed",12) | |
5730 | 834 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report |
835 %!test | |
6437 | 836 %! if (__random_statistical_tests__) |
837 %! % statistical tests may fail occasionally. | |
838 %! randg("seed",12) | |
839 %! a=0.1; x = randg(a,100000,1); | |
840 %! assert(mean(x), a, 0.01); | |
841 %! assert(var(x), a, 0.01); | |
842 %! assert(skewness(x),2/sqrt(a), 1.); | |
843 %! assert(kurtosis(x),6/a, 50.); | |
844 %! endif | |
5730 | 845 %!test |
6437 | 846 %! if (__random_statistical_tests__) |
847 %! % statistical tests may fail occasionally. | |
848 %! randg("seed",12) | |
849 %! a=0.95; x = randg(a,100000,1); | |
850 %! assert(mean(x), a, 0.01); | |
851 %! assert(var(x), a, 0.04); | |
852 %! assert(skewness(x),2/sqrt(a), 0.2); | |
853 %! assert(kurtosis(x),6/a, 2.); | |
854 %! endif | |
5730 | 855 %!test |
6437 | 856 %! if (__random_statistical_tests__) |
857 %! % statistical tests may fail occasionally. | |
858 %! randg("seed",12) | |
859 %! a=1; x = randg(a,100000,1); | |
860 %! assert(mean(x),a, 0.01); | |
861 %! assert(var(x),a, 0.04); | |
862 %! assert(skewness(x),2/sqrt(a), 0.2); | |
863 %! assert(kurtosis(x),6/a, 2.); | |
864 %! endif | |
5730 | 865 %!test |
6437 | 866 %! if (__random_statistical_tests__) |
867 %! % statistical tests may fail occasionally. | |
868 %! randg("seed",12) | |
869 %! a=10; x = randg(a,100000,1); | |
870 %! assert(mean(x), a, 0.1); | |
871 %! assert(var(x), a, 0.5); | |
872 %! assert(skewness(x),2/sqrt(a), 0.1); | |
873 %! assert(kurtosis(x),6/a, 0.5); | |
874 %! endif | |
5730 | 875 %!test |
6437 | 876 %! if (__random_statistical_tests__) |
877 %! % statistical tests may fail occasionally. | |
878 %! randg("seed",12) | |
879 %! a=100; x = randg(a,100000,1); | |
880 %! assert(mean(x), a, 0.2); | |
881 %! assert(var(x), a, 2.); | |
882 %! assert(skewness(x),2/sqrt(a), 0.05); | |
883 %! assert(kurtosis(x),6/a, 0.2); | |
884 %! endif | |
5730 | 885 */ |
886 | |
887 | |
888 DEFUN_DLD (randp, args, , | |
889 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
890 @deftypefn {Loadable Function} {} randp (@var{l}, @var{x})\n\ |
5730 | 891 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m})\n\ |
892 @deftypefnx {Loadable Function} {} randp (\"state\", @var{x})\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
|
893 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\ |
5730 | 894 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{x})\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\ | |
905 @item For scalar @var{l} <= 12, use direct method.\n\ | |
906 Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ | |
907 @item For scalar @var{l} > 12, use rejection method.[1]\n\ | |
908 Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ | |
909 @item For matrix @var{l} <= 10, use inversion method.[2]\n\ | |
910 Stadlober E., et al., WinRand source code, available via FTP.\n\ | |
911 @item For matrix @var{l} > 10, use patchwork rejection method.\n\ | |
912 Stadlober E., et al., WinRand source code, available via FTP, or\n\ | |
913 H. Zechner, 'Efficient sampling from continuous and discrete\n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
914 unimodal distributions', Doctoral Dissertation, 156pp., Technical\n\ |
5730 | 915 University Graz, Austria, 1994.\n\ |
916 @item For @var{l} > 1e8, use normal approximation.\n\ | |
917 L. Montanet, et al., 'Review of Particle Properties', Physical Review\n\ | |
918 D 50 p1284, 1994\n\ | |
919 @end table\n\ | |
6547 | 920 @seealso{rand, randn, rande, randg}\n\ |
5730 | 921 @end deftypefn") |
922 { | |
923 octave_value retval; | |
924 | |
925 int nargin = args.length (); | |
926 | |
927 if (nargin < 1) | |
928 error ("randp: insufficient arguments"); | |
929 else | |
930 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
931 unwind_protect frame; |
5730 | 932 |
933 // This relies on the fact that elements are popped from the unwind | |
934 // stack in the reverse of the order they are pushed | |
935 // (i.e. current_distribution will be reset before calling | |
936 // reset_rand_generator()). | |
937 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
938 frame.add_fcn (reset_rand_generator); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
939 frame.protect_var (current_distribution); |
5730 | 940 |
941 current_distribution = "poisson"; | |
942 | |
943 octave_rand::distribution (current_distribution); | |
944 | |
945 retval = do_rand (args, nargin, "randp", true); | |
946 } | |
947 | |
948 return retval; | |
949 } | |
950 | |
951 /* | |
952 %!test | |
6437 | 953 %! randp("state",12) |
954 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report | |
955 %!test | |
956 %! % Test fixed state | |
957 %! randp("state",1); | |
958 %! assert(randp(5,1,6),[5 5 3 7 7 3]) | |
959 %!test | |
960 %! % Test fixed state | |
961 %! randp("state",1); | |
962 %! assert(randp(15,1,6),[13 15 8 18 18 15]) | |
963 %!test | |
964 %! % Test fixed state | |
965 %! randp("state",1); | |
7421 | 966 %! assert(randp(1e9,1,6),[999915677 999976657 1000047684 1000019035 999985749 999977692],-1e-6) |
6437 | 967 %!test |
968 %! % Test fixed state | |
969 %! randp("seed",1); | |
6449 | 970 %! %%assert(randp(5,1,6),[8 2 3 6 6 8]) |
971 %! assert(randp(5,1,5),[8 2 3 6 6]) | |
6437 | 972 %!test |
973 %! % Test fixed state | |
974 %! randp("seed",1); | |
975 %! assert(randp(15,1,6),[15 16 12 10 10 12]) | |
976 %!test | |
977 %! % Test fixed state | |
978 %! randp("seed",1); | |
7421 | 979 %! assert(randp(1e9,1,6),[1000006208 1000012224 999981120 999963520 999963072 999981440],-1e-6) |
6437 | 980 %!test |
981 %! if (__random_statistical_tests__) | |
982 %! % statistical tests may fail occasionally. | |
983 %! randp("state",12) | |
984 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
985 %! x = randp(a(1),100000,1); | |
986 %! assert(min(x)>=0); % *** Please report this!!! *** | |
987 %! assert(mean(x),a(1),a(2)); | |
988 %! assert(var(x),a(1),0.02*a(1)); | |
989 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
990 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
991 %! endfor | |
992 %! endif | |
993 %!test | |
994 %! if (__random_statistical_tests__) | |
995 %! % statistical tests may fail occasionally. | |
996 %! randp("state",12) | |
997 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
998 %! x = randp(a(1)*ones(100000,1),100000,1); | |
999 %! assert(min(x)>=0); % *** Please report this!!! *** | |
1000 %! assert(mean(x),a(1),a(2)); | |
1001 %! assert(var(x),a(1),0.02*a(1)); | |
1002 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
1003 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
1004 %! endfor | |
1005 %! endif | |
1006 %!test | |
1007 %! randp("seed",12) | |
5730 | 1008 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report |
1009 %!test | |
6449 | 1010 %! if (__random_statistical_tests__) |
1011 %! % statistical tests may fail occasionally. | |
1012 %! randp("seed",12) | |
1013 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
1014 %! x = randp(a(1),100000,1); | |
1015 %! assert(min(x)>=0); % *** Please report this!!! *** | |
1016 %! assert(mean(x),a(1),a(2)); | |
1017 %! assert(var(x),a(1),0.02*a(1)); | |
1018 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
1019 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
1020 %! endfor | |
1021 %! endif | |
5730 | 1022 %!test |
6449 | 1023 %! if (__random_statistical_tests__) |
1024 %! % statistical tests may fail occasionally. | |
1025 %! randp("seed",12) | |
1026 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
1027 %! x = randp(a(1)*ones(100000,1),100000,1); | |
1028 %! assert(min(x)>=0); % *** Please report this!!! *** | |
1029 %! assert(mean(x),a(1),a(2)); | |
1030 %! assert(var(x),a(1),0.02*a(1)); | |
1031 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
1032 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
1033 %! endfor | |
1034 %! endif | |
5730 | 1035 */ |
1036 | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1037 DEFUN_DLD (randperm, args, , |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1038 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
1039 @deftypefn {Loadable Function} {} randperm (@var{n})\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1040 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1041 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
|
1042 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
|
1043 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
|
1044 memory. The randomization is performed using rand().\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1045 All permutations are equally likely.\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1046 @seealso{perms}\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1047 @end deftypefn") |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1048 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1049 int nargin = args.length (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1050 octave_value retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1051 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1052 if (nargin == 1 || nargin == 2) |
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 octave_idx_type n, m; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1055 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1056 if (nargin == 2) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1057 m = args(1).idx_type_value (true); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1058 else |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1059 m = 1; |
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 n = args(0).idx_type_value (true); |
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 if (m < 0 || n < 0) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1064 error ("randperm: m and n must be non-negative"); |
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 if (! error_state) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1067 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1068 // Generate random numbers. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1069 NDArray r = octave_rand::nd_array (dim_vector (m, n)); |
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 // Create transposed to allow faster access. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1072 Array<octave_idx_type> idx (dim_vector (n, m)); |
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 double *rvec = r.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1075 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1076 octave_idx_type *ivec = idx.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 // Perform the Knuth shuffle. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1079 for (octave_idx_type j = 0; j < m; j++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1080 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1081 for (octave_idx_type i = 0; i < n; i++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1082 ivec[i] = i; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1083 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1084 for (octave_idx_type i = 0; i < n; i++) |
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 octave_idx_type k = i + floor (rvec[i] * (n - i)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1087 std::swap (ivec[i], ivec[k]); |
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 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1090 ivec += n; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1091 rvec += n; |
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 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1094 // Transpose. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1095 idx = idx.transpose (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1096 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1097 // Re-fetch the pointers. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1098 ivec = idx.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1099 rvec = r.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1100 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1101 // Convert to doubles, reusing r. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1102 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
|
1103 rvec[i] = ivec[i] + 1; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1104 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1105 // Now create an array object with a cached idx_vector. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1106 retval = new octave_matrix (r, idx_vector (idx)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1107 } |
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 else |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1110 print_usage (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1111 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1112 return retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1113 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1114 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1115 /* |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1116 %!assert(sort(randperm(20)),1:20) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1117 %!assert(sort(randperm(20,50),2),repmat(1:20,50,1)) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1118 */ |