1
## Copyright (C) 2012 Rik Wehbring
2
## Copyright (C) 1995-2013 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} {} lognrnd (@var{mu}, @var{sigma})
22
## @deftypefnx {Function File} {} lognrnd (@var{mu}, @var{sigma}, @var{r})
23
## @deftypefnx {Function File} {} lognrnd (@var{mu}, @var{sigma}, @var{r}, @var{c}, @dots{})
24
## @deftypefnx {Function File} {} lognrnd (@var{mu}, @var{sigma}, [@var{sz}])
25
## Return a matrix of random samples from the lognormal distribution with
26
## parameters @var{mu} and @var{sigma}.
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 common size of
35
## @var{mu} and @var{sigma}.
38
## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39
## Description: Random deviates from the log normal distribution
41
function rnd = lognrnd (mu, sigma, varargin)
47
if (!isscalar (mu) || !isscalar (sigma))
48
[retval, mu, sigma] = common_size (mu, sigma);
50
error ("lognrnd: MU and SIGMA must be of common size or scalars");
54
if (iscomplex (mu) || iscomplex (sigma))
55
error ("lognrnd: MU and SIGMA must not be complex");
61
if (isscalar (varargin{1}) && varargin{1} >= 0)
62
sz = [varargin{1}, varargin{1}];
63
elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
66
error ("lognrnd: dimension vector must be row vector of non-negative integers");
69
if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
70
error ("lognrnd: dimensions must be non-negative integers");
75
if (!isscalar (mu) && !isequal (size (mu), sz))
76
error ("lognrnd: MU and SIGMA must be scalar or of size SZ");
79
if (isa (mu, "single") || isa (sigma, "single"))
85
if (isscalar (mu) && isscalar (sigma))
86
if ((sigma > 0) && (sigma < Inf))
87
rnd = exp (mu + sigma * randn (sz, cls));
92
rnd = exp (mu + sigma .* randn (sz, cls));
94
k = (sigma < 0) | (sigma == Inf);
101
%!assert (size (lognrnd (1,2)), [1, 1])
102
%!assert (size (lognrnd (ones (2,1), 2)), [2, 1])
103
%!assert (size (lognrnd (ones (2,2), 2)), [2, 2])
104
%!assert (size (lognrnd (1, 2*ones (2,1))), [2, 1])
105
%!assert (size (lognrnd (1, 2*ones (2,2))), [2, 2])
106
%!assert (size (lognrnd (1, 2, 3)), [3, 3])
107
%!assert (size (lognrnd (1, 2, [4 1])), [4, 1])
108
%!assert (size (lognrnd (1, 2, 4, 1)), [4, 1])
110
%% Test class of input preserved
111
%!assert (class (lognrnd (1, 2)), "double")
112
%!assert (class (lognrnd (single (1), 2)), "single")
113
%!assert (class (lognrnd (single ([1 1]), 2)), "single")
114
%!assert (class (lognrnd (1, single (2))), "single")
115
%!assert (class (lognrnd (1, single ([2 2]))), "single")
117
%% Test input validation
120
%!error lognrnd (ones (3), ones (2))
121
%!error lognrnd (ones (2), ones (3))
122
%!error lognrnd (i, 2)
123
%!error lognrnd (2, i)
124
%!error lognrnd (1,2, -1)
125
%!error lognrnd (1,2, ones (2))
126
%!error lognrnd (1, 2, [2 -1 2])
127
%!error lognrnd (1,2, 1, ones (2))
128
%!error lognrnd (1,2, 1, -1)
129
%!error lognrnd (ones (2,2), 2, 3)
130
%!error lognrnd (ones (2,2), 2, [3, 2])
131
%!error lognrnd (ones (2,2), 2, 2, 3)