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