1## Copyright (C) 2016 Dag Lyberg
2## Copyright (C) 1995-2015 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 {} {} nakacdf (@var{x}, @var{m}, @var{w})
22## For each element of @var{x}, compute the cumulative distribution function
23## (CDF) at @var{x} of the Nakagami distribution with shape parameter @var{m}
24## and scale parameter @var{w}.
25##
26## @end deftypefn
27
28## Author: Dag Lyberg <daglyberg80@gmail.com>
29## Description: CDF of the Nakagami distribution
30
31function cdf = nakacdf (x, m, w)
32
33  if (nargin != 3)
34    print_usage ();
35  endif
36
37  if (! isscalar (m) || ! isscalar (w))
38    [retval, x, m, w] = common_size (x, m, w);
39    if (retval > 0)
40      error ("nakacdf: X, M and W must be of common size or scalars");
41    endif
42  endif
43
44  if (iscomplex (x) || iscomplex (m) || iscomplex (w))
45    error ("nakacdf: X, M and W must not be complex");
46  endif
47
48  if (isa (x, "single") || isa (m, "single") || isa (w, "single"))
49    inv = zeros (size (x), "single");
50  else
51    inv = zeros (size (x));
52  endif
53
54  k = isnan (x) | ! (m > 0) | ! (w > 0);
55  cdf(k) = NaN;
56
57  k = (x == Inf) & (0 < m) & (m < Inf) & (0 < w) & (w < Inf);
58  cdf(k) = 1;
59
60  k = (0 < x) & (x < Inf) & (0 < m) & (m < Inf) & (0 < w) & (w < Inf);
61  if (isscalar(x) && isscalar (m) && isscalar(w))
62    left = m;
63    right = (m/w) * x^2;
64    cdf(k) = gammainc(right, left);
65  elseif (isscalar (m) && isscalar(w))
66    left = m * ones(size(x));
67    right = (m/w) * x.^2;
68    cdf(k) = gammainc(right(k), left(k));
69  else
70    left = m .* ones(size(x));
71    right = (m./w) .* x.^2;
72    cdf(k) = gammainc(right(k), left(k));
73  endif
74
75endfunction
76
77
78%!shared x,y
79%! x = [-1, 0, 1, 2, Inf];
80%! y = [0, 0, 0.63212055882855778, 0.98168436111126578, 1];
81%!assert (nakacdf (x, ones (1,5), ones (1,5)), y, eps)
82%!assert (nakacdf (x, 1, 1), y, eps)
83%!assert (nakacdf (x, [1, 1, NaN, 1, 1], 1), [y(1:2), NaN, y(4:5)])
84%!assert (nakacdf (x, 1, [1, 1, NaN, 1, 1]), [y(1:2), NaN, y(4:5)])
85%!assert (nakacdf ([x, NaN], 1, 1), [y, NaN], eps)
86
87## Test class of input preserved
88%!assert (nakacdf (single ([x, NaN]), 1, 1), single ([y, NaN]), eps('single'))
89%!assert (nakacdf ([x, NaN], single (1), 1), single ([y, NaN]), eps('single'))
90%!assert (nakacdf ([x, NaN], 1, single (1)), single ([y, NaN]), eps('single'))
91
92## Test input validation
93%!error nakacdf ()
94%!error nakacdf (1)
95%!error nakacdf (1,2)
96%!error nakacdf (1,2,3,4)
97%!error nakacdf (ones (3), ones (2), ones(2))
98%!error nakacdf (ones (2), ones (3), ones(2))
99%!error nakacdf (ones (2), ones (2), ones(3))
100%!error nakacdf (i, 2, 2)
101%!error nakacdf (2, i, 2)
102%!error nakacdf (2, 2, i)
103
104