## Copyright (C) 2019 Michele Ginesi
##
## 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
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {} {} subplus (@var{x})
## Compute the positive part function of @var{x}.
##
## The positive part function is defined as
## @tex
## $$
##  (x)_+ = \begin{cases} x & \text{if } x \ge 0 \\ 0 & \text{otherwise} \end{cases}
## $$
## @end tex
## @ifnottex
##
## @example
## @group
##
##            / x   if x >= 0
##   (x)_+ = <
##            \ 0   otherwise
##
## @end group
## @end example
##
## @end ifnottex
## @end deftypefn

## Author: Michele Ginesi <michele.ginesi@gmail.com>

function [y] = subplus (x)

  if (nargin != 1)
    print_usage ();
  endif

  y = max (x, 0);

endfunction

%!test
%! assert (subplus (1), 1)
%! assert (subplus (2), 2)
%! assert (subplus (- 1), 0)
%! assert (subplus ([1, -1]), [1, 0])

## Tests on conservation of input class
%!test
%! assert (class (subplus (1)), 'double')
%! assert (class (subplus (single (1))), 'single')
%! assert (class (subplus (int8 (1))), 'int8')
