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} {} unifpdf (@var{x})
22 ## @deftypefnx {Function File} {} unifpdf (@var{x}, @var{a}, @var{b})
23 ## For each element of @var{x}, compute the probability density function (PDF)
24 ## at @var{x} of the uniform distribution on the interval [@var{a}, @var{b}].
26 ## Default values are @var{a} = 0, @var{b} = 1.
29 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
30 ## Description: PDF of the uniform distribution
32 function pdf = unifpdf (x, a = 0, b = 1)
34 if (nargin != 1 && nargin != 3)
38 if (!isscalar (a) || !isscalar (b))
39 [retval, x, a, b] = common_size (x, a, b);
41 error ("unifpdf: X, A, and B must be of common size or scalars");
45 if (iscomplex (x) || iscomplex (a) || iscomplex (b))
46 error ("unifpdf: X, A, and B must not be complex");
49 if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
50 pdf = zeros (size (x), "single");
52 pdf = zeros (size (x));
55 k = isnan (x) | !(a < b);
58 k = (x >= a) & (x <= b) & (a < b);
59 if (isscalar (a) && isscalar (b))
62 pdf(k) = 1 ./ (b(k) - a(k));
69 %! x = [-1 0 0.5 1 2] + 1;
71 %!assert(unifpdf (x, ones(1,5), 2*ones(1,5)), y);
72 %!assert(unifpdf (x, 1, 2*ones(1,5)), y);
73 %!assert(unifpdf (x, ones(1,5), 2), y);
74 %!assert(unifpdf (x, [2 NaN 1 1 1], 2), [NaN NaN y(3:5)]);
75 %!assert(unifpdf (x, 1, 2*[0 NaN 1 1 1]), [NaN NaN y(3:5)]);
76 %!assert(unifpdf ([x, NaN], 1, 2), [y, NaN]);
78 %% Test class of input preserved
79 %!assert(unifpdf (single([x, NaN]), 1, 2), single([y, NaN]));
80 %!assert(unifpdf (single([x, NaN]), single(1), 2), single([y, NaN]));
81 %!assert(unifpdf ([x, NaN], 1, single(2)), single([y, NaN]));
83 %% Test input validation
86 %!error unifpdf (1,2,3,4)
87 %!error unifpdf (ones(3),ones(2),ones(2))
88 %!error unifpdf (ones(2),ones(3),ones(2))
89 %!error unifpdf (ones(2),ones(2),ones(3))
90 %!error unifpdf (i, 2, 2)
91 %!error unifpdf (2, i, 2)
92 %!error unifpdf (2, 2, i)