Mercurial > octave-nkf
annotate scripts/miscellaneous/xor.m @ 20595:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | 9fc020886ae9 |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19631
diff
changeset
|
1 ## Copyright (C) 1995-2015 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
2538 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
2538 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2538 | 18 |
3321 | 19 ## -*- texinfo -*- |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
20 ## @deftypefn {Function File} {@var{z} =} xor (@var{x}, @var{y}) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{z} =} xor (@var{x1}, @var{x2}, @dots{}) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## Return the @dfn{exclusive or} of @var{x} and @var{y}. |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## |
3321 | 24 ## For boolean expressions @var{x} and @var{y}, |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## @code{xor (@var{x}, @var{y})} is true if and only if one of @var{x} or |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
26 ## @var{y} is true. Otherwise, if @var{x} and @var{y} are both true or both |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
27 ## false, @code{xor} returns false. |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 ## |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## The truth table for the xor operation is |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
30 ## |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
31 ## @multitable @columnfractions 0.44 .03 .05 .03 0.44 |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
32 ## @item @tab @var{x} @tab @var{y} @tab @var{z} @tab |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## @item @tab - @tab - @tab - @tab |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
34 ## @item @tab 0 @tab 0 @tab 0 @tab |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 ## @item @tab 1 @tab 0 @tab 1 @tab |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
36 ## @item @tab 0 @tab 1 @tab 1 @tab |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
37 ## @item @tab 1 @tab 1 @tab 0 @tab |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
38 ## @end multitable |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
39 ## |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 ## If more than two arguments are given the xor operation is applied |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 ## cumulatively from left to right: |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 ## |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 ## @example |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 ## (@dots{}((x1 XOR x2) XOR x3) XOR @dots{}) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 ## @end example |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
46 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 ## @seealso{and, or, not} |
3321 | 48 ## @end deftypefn |
2538 | 49 |
5428 | 50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2538 | 51 ## Created: 16 September 1994 |
52 ## Adapted-By: jwe | |
53 | |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 function z = xor (x, y, varargin) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 if (nargin < 2) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
57 print_usage (); |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 endif |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
60 z = __xor__ (x, y); |
2538 | 61 |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
62 ## Slow expansion to multiple arguments. |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 ## Probably okay number of elements ## will be small. |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 if (! isempty (varargin)) |
19631
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19227
diff
changeset
|
65 for i = 1:numel (varargin) |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 z = __xor__ (z, varargin{i}); |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 endfor |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
68 endif |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 endfunction |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 function z = __xor__ (x, y) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
73 |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
74 if (isscalar (x) || isscalar (y) || size_equal (x, y)) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
75 ## Typecast to logicals is necessary for other numeric types. |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
76 z = logical (x) != logical (y); |
2621 | 77 else |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 try |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
79 z = bsxfun (@xor, x, y); |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
80 catch |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
81 error ("xor: X and Y must be of compatible size or scalars"); |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 end_try_catch |
2538 | 83 endif |
84 | |
85 endfunction | |
7385 | 86 |
87 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!assert (xor ([1, 1, 0, 0], [0, 1, 0, 1]), logical ([1, 0, 0, 1])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!assert (xor ([i, i, 0, 0], [1, 0, 1, 0]), logical ([0, 1, 1, 0])) |
7385 | 90 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %!assert (xor (eye (2), fliplr (eye (2))), logical (ones (2))) |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
92 %!assert (xor (speye (2), fliplr (speye (2))), sparse (logical (ones (2)))) |
7385 | 93 |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
94 ## Test XOR reduction |
19227 | 95 %!assert (xor ([1 0], [1 1], [0 0]), logical ([0 1])) |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
97 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!error xor () |
19222
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 %!error xor (1) |
c3611856cdd4
xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 %!error <X and Y must be of compatible size> xor (ones (3,2), ones (2,3)) |
7385 | 101 |