1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} poissrnd (@var{lambda})
22 ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, @var{r})
23 ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, [@var{sz}])
25 ## Return a matrix of random samples from the Poisson distribution with
26 ## parameter @var{lambda}.
28 ## When called with a single size argument, return a square matrix with
29 ## the dimension specified. When called with more than one scalar argument the
30 ## first two arguments are taken as the number of rows and columns and any
31 ## further arguments specify additional matrix dimensions. The size may also
32 ## be specified with a vector of dimensions @var{sz}.
34 ## If no size arguments are given then the result matrix is the size of
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from the Poisson distribution
41 function rnd = poissrnd (lambda, varargin)
50 if (isscalar (varargin{1}) && varargin{1} >= 0)
51 sz = [varargin{1}, varargin{1}];
52 elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
55 error ("poissrnd: dimension vector must be row vector of non-negative integers");
58 if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
59 error ("poissrnd: dimensions must be non-negative integers");
64 if (!isscalar (lambda) && !isequal (size (lambda), sz))
65 error ("poissrnd: LAMBDA must be scalar or of size SZ");
68 if (iscomplex (lambda))
69 error ("poissrnd: LAMBDA must not be complex");
72 if (isa (lambda, "single"))
78 if (isscalar (lambda))
79 if (lambda >= 0 && lambda < Inf)
80 rnd = randp (lambda, sz);
81 if (strcmp (cls, "single"))
90 k = (lambda >= 0) & (lambda < Inf);
91 rnd(k) = randp (lambda(k));
96 %!assert(size (poissrnd (2)), [1, 1]);
97 %!assert(size (poissrnd (ones(2,1))), [2, 1]);
98 %!assert(size (poissrnd (ones(2,2))), [2, 2]);
99 %!assert(size (poissrnd (1, 3)), [3, 3]);
100 %!assert(size (poissrnd (1, [4 1])), [4, 1]);
101 %!assert(size (poissrnd (1, 4, 1)), [4, 1]);
103 %% Test class of input preserved
104 %!assert(class (poissrnd (2)), "double");
105 %!assert(class (poissrnd (single(2))), "single");
106 %!assert(class (poissrnd (single([2 2]))), "single");
108 %% Test input validation
110 %!error poissrnd (1, -1)
111 %!error poissrnd (1, ones(2))
112 %!error poissrnd (1, 2, ones(2))
114 %!error poissrnd (1, 2, -1)
115 %!error poissrnd (1, [2 -1 2])
116 %!error poissrnd (ones(2,2), 3)
117 %!error poissrnd (ones(2,2), [3, 2])
118 %!error poissrnd (ones(2,2), 2, 3)
120 %!assert (poissrnd (0, 1, 1), 0)
121 %!assert (poissrnd ([0, 0, 0], [1, 3]), [0 0 0])