comparison src/DLD-FUNCTIONS/rand.cc @ 11553:01f703952eff

Improve docstrings for functions in DLD-FUNCTIONS directory. Use same variable names in error() strings and in documentation.
author Rik <octave@nomad.inbox5.com>
date Sun, 16 Jan 2011 22:13:23 -0800
parents fd0a3ac60b0e
children 12df7854fa7c
comparison
equal deleted inserted replaced
11552:6b6e9051ecb8 11553:01f703952eff
150 { 150 {
151 double dval = tmp.double_value (); 151 double dval = tmp.double_value ();
152 152
153 if (xisnan (dval)) 153 if (xisnan (dval))
154 { 154 {
155 error ("%s: NaN is invalid a matrix dimension", fcn); 155 error ("%s: NaN is invalid matrix dimension", fcn);
156 } 156 }
157 else 157 else
158 { 158 {
159 dims.resize (2); 159 dims.resize (2);
160 160
178 octave_idx_type base = NINTbig (r.base ()); 178 octave_idx_type base = NINTbig (r.base ());
179 octave_idx_type incr = NINTbig (r.inc ()); 179 octave_idx_type incr = NINTbig (r.inc ());
180 octave_idx_type lim = NINTbig (r.limit ()); 180 octave_idx_type lim = NINTbig (r.limit ());
181 181
182 if (base < 0 || lim < 0) 182 if (base < 0 || lim < 0)
183 error ("%s: all dimensions must be nonnegative", fcn); 183 error ("%s: all dimensions must be positive", fcn);
184 else 184 else
185 { 185 {
186 for (octave_idx_type i = 0; i < n; i++) 186 for (octave_idx_type i = 0; i < n; i++)
187 { 187 {
188 dims(i) = base; 188 dims(i) = base;
191 191
192 goto gen_matrix; 192 goto gen_matrix;
193 } 193 }
194 } 194 }
195 else 195 else
196 error ("%s: expecting all elements of range to be integers", 196 error ("%s: all elements of range must be integers",
197 fcn); 197 fcn);
198 } 198 }
199 else if (tmp.is_matrix_type ()) 199 else if (tmp.is_matrix_type ())
200 { 200 {
201 Array<int> iv = tmp.int_vector_value (true); 201 Array<int> iv = tmp.int_vector_value (true);
210 { 210 {
211 octave_idx_type elt = iv(i); 211 octave_idx_type elt = iv(i);
212 212
213 if (elt < 0) 213 if (elt < 0)
214 { 214 {
215 error ("%s: all dimensions must be nonnegative", fcn); 215 error ("%s: all dimensions must be positive", fcn);
216 goto done; 216 goto done;
217 } 217 }
218 218
219 dims(i) = iv(i); 219 dims(i) = iv(i);
220 } 220 }
326 326
327 DEFUN_DLD (rand, args, , 327 DEFUN_DLD (rand, args, ,
328 "-*- texinfo -*-\n\ 328 "-*- texinfo -*-\n\
329 @deftypefn {Loadable Function} {} rand (@var{x})\n\ 329 @deftypefn {Loadable Function} {} rand (@var{x})\n\
330 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ 330 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\
331 @deftypefnx {Loadable Function} {} rand (\"state\", @var{x})\n\ 331 @deftypefnx {Loadable Function} {@var{v} =} rand (\"state\")\n\
332 @deftypefnx {Loadable Function} {} rand (\"state\", @var{v})\n\
332 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\ 333 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\
333 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{x})\n\ 334 @deftypefnx {Loadable Function} {@var{v} =} rand (\"seed\")\n\
335 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{v})\n\
334 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\ 336 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\
335 Return a matrix with random elements uniformly distributed on the\n\ 337 Return a matrix with random elements uniformly distributed on the\n\
336 interval (0, 1). The arguments are handled the same as the arguments\n\ 338 interval (0, 1). The arguments are handled the same as the arguments\n\
337 for @code{eye}.\n\ 339 for @code{eye}.\n\
338 \n\ 340 \n\
488 490
489 DEFUN_DLD (randn, args, , 491 DEFUN_DLD (randn, args, ,
490 "-*- texinfo -*-\n\ 492 "-*- texinfo -*-\n\
491 @deftypefn {Loadable Function} {} randn (@var{x})\n\ 493 @deftypefn {Loadable Function} {} randn (@var{x})\n\
492 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ 494 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\
493 @deftypefnx {Loadable Function} {} randn (\"state\", @var{x})\n\ 495 @deftypefnx {Loadable Function} {@var{v} =} randn (\"state\")\n\
496 @deftypefnx {Loadable Function} {} randn (\"state\", @var{v})\n\
494 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\ 497 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\
495 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{x})\n\ 498 @deftypefnx {Loadable Function} {@var{v} =} randn (\"seed\")\n\
499 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{v})\n\
496 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\ 500 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\
497 Return a matrix with normally distributed random\n\ 501 Return a matrix with normally distributed random\n\
498 elements having zero mean and variance one. The arguments are\n\ 502 elements having zero mean and variance one. The arguments are\n\
499 handled the same as the arguments for @code{rand}.\n\ 503 handled the same as the arguments for @code{rand}.\n\
500 \n\ 504 \n\
551 555
552 DEFUN_DLD (rande, args, , 556 DEFUN_DLD (rande, args, ,
553 "-*- texinfo -*-\n\ 557 "-*- texinfo -*-\n\
554 @deftypefn {Loadable Function} {} rande (@var{x})\n\ 558 @deftypefn {Loadable Function} {} rande (@var{x})\n\
555 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m})\n\ 559 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m})\n\
556 @deftypefnx {Loadable Function} {} rande (\"state\", @var{x})\n\ 560 @deftypefnx {Loadable Function} {@var{v} =} rande (\"state\")\n\
561 @deftypefnx {Loadable Function} {} rande (\"state\", @var{v})\n\
557 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\ 562 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\
558 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{x})\n\ 563 @deftypefnx {Loadable Function} {@var{v} =} rande (\"seed\")\n\
564 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{v})\n\
559 @deftypefnx {Loadable Function} {} rande (\"seed\", \"reset\")\n\ 565 @deftypefnx {Loadable Function} {} rande (\"seed\", \"reset\")\n\
560 Return a matrix with exponentially distributed random elements. The\n\ 566 Return a matrix with exponentially distributed random elements. The\n\
561 arguments are handled the same as the arguments for @code{rand}.\n\ 567 arguments are handled the same as the arguments for @code{rand}.\n\
562 \n\ 568 \n\
563 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\ 569 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\
564 to transform from a uniform to a exponential distribution.\n\ 570 to transform from a uniform to an exponential distribution.\n\
565 \n\ 571 \n\
566 Reference: G. Marsaglia and W.W. Tsang,\n\ 572 Reference: G. Marsaglia and W.W. Tsang,\n\
567 @cite{Ziggurat Method for Generating Random Variables},\n\ 573 @cite{Ziggurat Method for Generating Random Variables},\n\
568 J. Statistical Software, vol 5, 2000,\n\ 574 J. Statistical Software, vol 5, 2000,\n\
569 @url{http://www.jstatsoft.org/v05/i08/})\n\ 575 @url{http://www.jstatsoft.org/v05/i08/})\n\
615 621
616 DEFUN_DLD (randg, args, , 622 DEFUN_DLD (randg, args, ,
617 "-*- texinfo -*-\n\ 623 "-*- texinfo -*-\n\
618 @deftypefn {Loadable Function} {} randg (@var{a}, @var{x})\n\ 624 @deftypefn {Loadable Function} {} randg (@var{a}, @var{x})\n\
619 @deftypefnx {Loadable Function} {} randg (@var{a}, @var{n}, @var{m})\n\ 625 @deftypefnx {Loadable Function} {} randg (@var{a}, @var{n}, @var{m})\n\
620 @deftypefnx {Loadable Function} {} randg (\"state\", @var{x})\n\ 626 @deftypefnx {Loadable Function} {@var{v} =} randg (\"state\")\n\
627 @deftypefnx {Loadable Function} {} randg (\"state\", @var{v})\n\
621 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\ 628 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\
622 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{x})\n\ 629 @deftypefnx {Loadable Function} {@var{v} =} randg (\"seed\")\n\
630 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{v})\n\
623 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\ 631 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\
624 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\ 632 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\
625 The arguments are handled the same as the arguments for @code{rand},\n\ 633 The arguments are handled the same as the arguments for @code{rand},\n\
626 except for the argument @var{a}.\n\ 634 except for the argument @var{a}.\n\
627 \n\ 635 \n\
869 877
870 DEFUN_DLD (randp, args, , 878 DEFUN_DLD (randp, args, ,
871 "-*- texinfo -*-\n\ 879 "-*- texinfo -*-\n\
872 @deftypefn {Loadable Function} {} randp (@var{l}, @var{x})\n\ 880 @deftypefn {Loadable Function} {} randp (@var{l}, @var{x})\n\
873 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m})\n\ 881 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m})\n\
874 @deftypefnx {Loadable Function} {} randp (\"state\", @var{x})\n\ 882 @deftypefnx {Loadable Function} {@var{v} =} randp (\"state\")\n\
883 @deftypefnx {Loadable Function} {} randp (\"state\", @var{v})\n\
875 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\ 884 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\
876 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{x})\n\ 885 @deftypefnx {Loadable Function} {@var{v} =} randp (\"seed\")\n\
886 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{v})\n\
877 @deftypefnx {Loadable Function} {} randp (\"seed\", \"reset\")\n\ 887 @deftypefnx {Loadable Function} {} randp (\"seed\", \"reset\")\n\
878 Return a matrix with Poisson distributed random elements with mean value\n\ 888 Return a matrix with Poisson distributed random elements with mean value\n\
879 parameter given by the first argument, @var{l}. The arguments\n\ 889 parameter given by the first argument, @var{l}. The arguments\n\
880 are handled the same as the arguments for @code{rand}, except for the\n\ 890 are handled the same as the arguments for @code{rand}, except for the\n\
881 argument @var{l}.\n\ 891 argument @var{l}.\n\
1010 "-*- texinfo -*-\n\ 1020 "-*- texinfo -*-\n\
1011 @deftypefn {Loadable Function} {} randperm (@var{n})\n\ 1021 @deftypefn {Loadable Function} {} randperm (@var{n})\n\
1012 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\ 1022 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\
1013 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\ 1023 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\
1014 If @var{m} is supplied, return @var{m} permutations,\n\ 1024 If @var{m} is supplied, return @var{m} permutations,\n\
1015 one in each row of a @nospell{NxM} matrix. The complexity is O(M*N) in both\n\ 1025 one in each row of an @nospell{NxM} matrix. The complexity is O(M*N) in both\n\
1016 time and memory. The randomization is performed using rand().\n\ 1026 time and memory. The randomization is performed using rand().\n\
1017 All permutations are equally likely.\n\ 1027 All permutations are equally likely.\n\
1018 @seealso{perms}\n\ 1028 @seealso{perms}\n\
1019 @end deftypefn") 1029 @end deftypefn")
1020 { 1030 {
1031 m = 1; 1041 m = 1;
1032 1042
1033 n = args(0).idx_type_value (true); 1043 n = args(0).idx_type_value (true);
1034 1044
1035 if (m < 0 || n < 0) 1045 if (m < 0 || n < 0)
1036 error ("randperm: m and n must be non-negative"); 1046 error ("randperm: M and N must be non-negative");
1037 1047
1038 if (! error_state) 1048 if (! error_state)
1039 { 1049 {
1040 // Generate random numbers. 1050 // Generate random numbers.
1041 NDArray r = octave_rand::nd_array (dim_vector (m, n)); 1051 NDArray r = octave_rand::nd_array (dim_vector (m, n));