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