]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/normrnd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / normrnd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} normrnd (@var{mu}, @var{sigma})
22 ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r})
23 ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, [@var{sz}])
25 ## Return a matrix of random samples from the normal distribution with
26 ## parameters mean @var{mu} and standard deviation @var{sigma}.
27 ##
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}.
33 ## 
34 ## If no size arguments are given then the result matrix is the common size of
35 ## @var{mu} and @var{sigma}.
36 ## @end deftypefn
37
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from the normal distribution
40
41 function rnd = normrnd (mu, sigma, varargin)
42
43   if (nargin < 2)
44     print_usage ();
45   endif
46
47   if (!isscalar (mu) || !isscalar (sigma))
48     [retval, mu, sigma] = common_size (mu, sigma);
49     if (retval > 0)
50       error ("normrnd: mu and sigma must be of common size or scalars");
51     endif
52   endif
53
54   if (iscomplex (mu) || iscomplex (sigma))
55     error ("normrnd: MU and SIGMA must not be complex");
56   endif
57
58   if (nargin == 2)
59     sz = size (mu);
60   elseif (nargin == 3)
61     if (isscalar (varargin{1}) && varargin{1} >= 0)
62       sz = [varargin{1}, varargin{1}];
63     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
64       sz = varargin{1};
65     else
66       error ("normrnd: dimension vector must be row vector of non-negative integers");
67     endif
68   elseif (nargin > 3)
69     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
70       error ("normrnd: dimensions must be non-negative integers");
71     endif
72     sz = [varargin{:}];
73   endif
74
75   if (!isscalar (mu) && !isequal (size (mu), sz))
76     error ("normrnd: mu and sigma must be scalar or of size SZ");
77   endif
78
79   if (isa (mu, "single") || isa (sigma, "single"))
80     cls = "single";
81   else
82     cls = "double";
83   endif
84
85   if (isscalar (mu) && isscalar (sigma))
86     if (!isnan (mu) && !isinf (mu) && (sigma > 0) && (sigma < Inf))
87       rnd =  mu + sigma * randn (sz);
88     else
89       rnd = NaN (sz, cls);
90     endif
91   else
92     rnd = mu + sigma .* randn (sz);
93     k = isnan (mu) | isinf (mu) | !(sigma > 0) | !(sigma < Inf);
94     rnd(k) = NaN;
95   endif
96
97 endfunction
98
99
100 %!assert(size (normrnd (1,2)), [1, 1]);
101 %!assert(size (normrnd (ones(2,1), 2)), [2, 1]);
102 %!assert(size (normrnd (ones(2,2), 2)), [2, 2]);
103 %!assert(size (normrnd (1, 2*ones(2,1))), [2, 1]);
104 %!assert(size (normrnd (1, 2*ones(2,2))), [2, 2]);
105 %!assert(size (normrnd (1, 2, 3)), [3, 3]);
106 %!assert(size (normrnd (1, 2, [4 1])), [4, 1]);
107 %!assert(size (normrnd (1, 2, 4, 1)), [4, 1]);
108
109 %% Test class of input preserved
110 %!assert(class (normrnd (1, 2)), "double");
111 %!assert(class (normrnd (single(1), 2)), "single");
112 %!assert(class (normrnd (single([1 1]), 2)), "single");
113 %!assert(class (normrnd (1, single(2))), "single");
114 %!assert(class (normrnd (1, single([2 2]))), "single");
115
116 %% Test input validation
117 %!error normrnd ()
118 %!error normrnd (1)
119 %!error normrnd (ones(3),ones(2))
120 %!error normrnd (ones(2),ones(3))
121 %!error normrnd (i, 2)
122 %!error normrnd (2, i)
123 %!error normrnd (1,2, -1)
124 %!error normrnd (1,2, ones(2))
125 %!error normrnd (1, 2, [2 -1 2])
126 %!error normrnd (1,2, 1, ones(2))
127 %!error normrnd (1,2, 1, -1)
128 %!error normrnd (ones(2,2), 2, 3)
129 %!error normrnd (ones(2,2), 2, [3, 2])
130 %!error normrnd (ones(2,2), 2, 2, 3)
131