# HG changeset patch
# User Colin Macdonald <cbm@m.fsf.org>
# Date 1428420276 -3600
#      Tue Apr 07 16:24:36 2015 +0100
# Node ID 1d17949618accd73cf90199186399f6483de5f84
# Parent  0e712de0af4d5761b46a99c5ab3bb3b5e7349473
Add dirac and heaviside functions

diff -r 0e712de0af4d -r 1d17949618ac scripts/help/__unimplemented__.m
--- a/scripts/help/__unimplemented__.m	Mon Apr 06 23:06:08 2015 -0400
+++ b/scripts/help/__unimplemented__.m	Tue Apr 07 16:24:36 2015 +0100
@@ -482,11 +482,11 @@
     ## symbolic
     case {"argnames", "bernoulli", "catalan", "charpoly", "chebyshevT", ...
           "chebyshevU", "children", "coshint", "cosint", "dawson", ...
-          "digits", "dilog", "dirac", "divisors", "dsolve", "ei", ...
+          "digits", "dilog", "divisors", "dsolve", "ei", ...
           "ellipticCE", "ellipticCK", "ellipticCPi", "ellipticE", ...
           "ellipticF", "ellipticK", "ellipticPi", "euler", "eulergamma", ...
           "finverse", "formula", "fresnelc", "fresnels", "funtool", ...
-          "gegenbauerC", "harmonic", "heaviside", "hermiteH", "hypergeom", ...
+          "gegenbauerC", "harmonic", "hermiteH", "hypergeom", ...
           "igamma", "jacobiP", "kroneckerDelta", "kummerU", "laguerreL", ...
           "legendreP", "logint", "minpoly", "pochhammer", "poles", ...
           "poly2sym", "polylog", "pretty", "quorem", "rectangularPulse", ...
diff -r 0e712de0af4d -r 1d17949618ac scripts/specfun/dirac.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/specfun/dirac.m	Tue Apr 07 16:24:36 2015 +0100
@@ -0,0 +1,65 @@
+## Copyright (C) 2015 Colin B. Macdonald
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {Function File} {@var{y} =} dirac (@var{x})
+## Dirac delta (generalized) function.
+##
+## The Dirac delta "function" is a generalized function or distribution
+## which is zero almost everywhere, except at the origin where it is
+## infinite.
+##
+## Examples:
+## @example
+## @group
+## dirac (0)
+## @result{} Inf
+## dirac (1)
+## @result{} 0
+## dirac ([-10 -1 0 1 inf])
+## @result{} 0     0   Inf     0     0
+## @end group
+## @end example
+## @seealso{heaviside}
+## @end deftypefn
+
+## Author: Colin B. Macdonald <cbm@m.fsf.org>
+
+function y = dirac(x)
+
+  if (nargin != 1)
+    print_usage ();
+  endif
+
+  if (iscomplex (x))
+    error ("dirac: X must not contain complex values");
+  endif
+
+  y = zeros (size (x));
+  y(x == 0) = inf;
+  y(isnan (x)) = nan;
+
+endfunction
+
+
+%!assert (isinf (dirac (0)))
+%!assert (dirac (1) == 0)
+%!assert (isnan (dirac (nan)))
+%!assert (isequaln (dirac ([-1 1 0 eps inf -inf nan]), [0 0 inf 0 0 0 nan]))
+
+%!error <complex values> dirac (1i)
diff -r 0e712de0af4d -r 1d17949618ac scripts/specfun/heaviside.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/specfun/heaviside.m	Tue Apr 07 16:24:36 2015 +0100
@@ -0,0 +1,68 @@
+## Copyright (C) 2015 Colin B. Macdonald
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {Function File} {@var{y} =} heaviside (@var{x})
+## Heaviside unit-step function.
+##
+## The Heaviside function is 0 for negative arguments and 1 for
+## positive arguments.
+##
+## Examples:
+## @example
+## @group
+## heaviside([-inf -3 -1 1 3 inf])
+## @result{} 0     0     0     1     1     1
+## @end group
+## @end example
+##
+## There are various conventions for @code{Heaviside(0)}; this
+## function uses:
+## @example
+## @group
+## heaviside(0)
+## @result{} 0.50000
+## @end group
+## @end example
+## @seealso{dirac}
+## @end deftypefn
+
+## Author: Colin B. Macdonald <cbm@m.fsf.org>
+
+function y = heaviside(x)
+
+  if (nargin != 1)
+    print_usage ();
+  endif
+
+  if (iscomplex (x))
+    error ("heaviside: X must not contain complex values");
+  endif
+
+  y = 1/2*(x == 0) + double (x > 0);
+  y(isnan (x)) = nan;
+
+endfunction
+
+
+%!assert (heaviside (0) == 0.5)
+%!assert (isnan (heaviside (nan)))
+%!assert (isequal (heaviside ([-inf -eps 0 eps inf]), [0 0 0.5 1 1]))
+%!assert (isequaln (heaviside ([-1 1 nan]), [0 1 nan]))
+
+%!error <complex values> heaviside (1i)
diff -r 0e712de0af4d -r 1d17949618ac scripts/specfun/module.mk
--- a/scripts/specfun/module.mk	Mon Apr 06 23:06:08 2015 -0400
+++ b/scripts/specfun/module.mk	Tue Apr 07 16:24:36 2015 +0100
@@ -4,10 +4,12 @@
   specfun/bessel.m \
   specfun/beta.m \
   specfun/betaln.m \
+  specfun/dirac.m \
   specfun/ellipke.m \
   specfun/expint.m \
   specfun/factor.m \
   specfun/factorial.m \
+  specfun/heaviside.m \
   specfun/isprime.m \
   specfun/lcm.m \
   specfun/legendre.m \
