/*
Copyright (C) 2018 Sahil Yadav
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
.
*/
#include
#include
#include
#include "./libinterp/octave-value/ov.h"
#include "./libinterp/octave-value/ovl.h"
#include "./libinterp/corefcn/error.h"
octave_value_list
_xor_ (octave_value x, octave_value y)
{
octave_value_list ret;
octave_value_list inputFunction ;
inputFunction(0) = x;
inputFunction(1) = y;
if(x.is_scalar_type() && y.is_scalar_type())
ret = octave_value (x.bool_value() != y.bool_value());
else if (octave::feval("size_equal",inputFunction)(0).bool_value())
ret =( do_binary_op(octave_value::op_ne,octave::feval("logical",x)(0),\
octave::feval("logical",y)(0)));
else
{ octave_value_list val;
val(0) = "xor1";
val(1) = x.matrix_value();
val(2) = y.matrix_value();
try
{
ret = octave_value(octave::feval("bsxfun",val )(0));
}
catch(const octave::execution_exception&)
{
octave_value_list er;
er(0) = "xor1: X and Y must be of compatible size or scalars";
octave::feval ("error",er);
}
}
return ret;
}
DEFUN_DLD (xor1, args, ,
"-*- texinfo -*-\n\
@deftypefn {} {@var{z} =} xor1 (@var{x}, @var{y})\n\
@deftypefnx {} {@var{z} =} xor1 (@var{x1}, @var{x2}, @dots{})\n\
Return the @dfn{exclusive or} of @var{x} and @var{y}.\n\
For boolean expressions @var{x} and @var{y},\n\
@code{xor1 (@var{x}, @var{y})} is true if and only if one of @var{x} or\n\
@var{y} is true. Otherwise, if @var{x} and @var{y} are both true or both\n\
false, @code{xor1} returns false.\n\
The truth table for the xor1 operation is\n\
@multitable @columnfractions 0.44 .03 .05 .03 0.44\n\
@item @tab @var{x} @tab @var{y} @tab @var{z} @tab\n\
@item @tab - @tab - @tab - @tab\n\
@item @tab 0 @tab 0 @tab 0 @tab\n\
@item @tab 1 @tab 0 @tab 1 @tab\n\
@item @tab 0 @tab 1 @tab 1 @tab\n\
@item @tab 1 @tab 1 @tab 0 @tab\n\
@end multitable\n\
If more than two arguments are given the xor1 operation is applied\n\
cumulatively from left to right:\n\
@example\n\
(@dots{}((x1 xor1 x2) xor1 x3) xor1 @dots{})\n\
@end example\n\
@seealso{and, or, not}\n\
@end deftypefn")
{
int nargin = args.length ();
if (nargin < 2)
print_usage();
octave_value_list retval;
retval = _xor_ (args(0),args(1));
if(nargin > 2)
for (int i = 2; i < nargin; ++i)
{
retval = _xor_ (retval(0),args(i));
}
return retval;
}
/*
%!assert (xor1 ([1, 1, 0, 0], [0, 1, 0, 1]), logical ([1, 0, 0, 1]))
%!assert (xor1 ([i, i, 0, 0], [1, 0, 1, 0]), logical ([0, 1, 1, 0]))
%!assert (xor1 (eye (2), fliplr (eye (2))), logical (ones (2)))
%!assert (xor1 (speye (2), fliplr (speye (2))), sparse (logical (ones (2))))
%!assert (xor1 ([1 2 3 ; 0 0 0],[0 0 0 ; 1 1 1]), logical ([1 1 1; 1 1 1]))
%!assert (xor1 ([0 0 0 ; 0 0 0 ; 0 0 0],1),logical([1 1 1 ; 1 1 1; 1 1 1]))
%!assert (xor1 ([0 0 1 ; 0 0 0 ],1),logical([1 1 0 ; 1 1 1]))
## Test xor1 reduction
%!assert (xor1 ([1 0], [1 1], [0 0]), logical ([0 1]))
## Test input validation
%!error xor1 ()
%!error xor1 (1)
%!error xor1 (ones (3,2), ones (2,3))
*/