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