annotate scripts/general/xor.m @ 33623:4517f929c59d bytecode-interpreter tip

maint: Merge default to bytecode-interpreter
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 May 2024 16:22:41 -0400
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 1995-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3321
diff changeset
7 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
8 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24490
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24490
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3321
diff changeset
14 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3321
diff changeset
19 ##
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24490
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
25
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 2870
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## @deftypefn {} {@var{z} =} xor (@var{x}, @var{y})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## @deftypefnx {} {@var{z} =} xor (@var{x1}, @var{x2}, @dots{})
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## 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
30 ##
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 2870
diff changeset
31 ## 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
32 ## @code{xor (@var{x}, @var{y})} is true if and only if one of @var{x} or
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
33 ## @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
34 ## false, @code{xor} returns false.
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
35 ##
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
36 ## 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
37 ##
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
38 ## @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
39 ## @item @tab @var{x} @tab @var{y} @tab @var{z} @tab
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## @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
41 ## @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
42 ## @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
43 ## @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
44 ## @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
45 ## @end multitable
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
46 ##
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
47 ## 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
48 ## 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
49 ##
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
50 ## @example
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
51 ## (@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
52 ## @end example
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ##
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
54 ## @seealso{and, or, not}
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 2870
diff changeset
55 ## @end deftypefn
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
56
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 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
58
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
59 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
60 print_usage ();
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
61 endif
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
62
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
63 z = __xor__ (x, y);
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
64
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
65 ## Slow expansion to multiple arguments.
23090
8cb4a2c07bce doc: Fix '##' in middle of docstring/comment lines (bug #50145).50145).50145).50145).50145).
Rik <rik@octave.org>
parents: 23083
diff changeset
66 ## Probably okay number of elements will be small.
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
67 if (! isempty (varargin))
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19193
diff changeset
68 for i = 1:numel (varargin)
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
69 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
70 endfor
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
71 endif
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
72
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
73 endfunction
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
74
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 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
76
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 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
78 ## 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
79 z = logical (x) != logical (y);
2621
337a09dd1c06 [project @ 1997-01-24 21:49:41 by jwe]
jwe
parents: 2538
diff changeset
80 else
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 try
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
82 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
83 catch
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
84 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
85 end_try_catch
2538
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
86 endif
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
87
6f71af650490 [project @ 1996-11-19 21:41:48 by jwe]
jwe
parents:
diff changeset
88 endfunction
7385
8b7b4f58199f [project @ 2008-01-15 20:49:09 by jwe]
jwe
parents: 7017
diff changeset
89
8b7b4f58199f [project @ 2008-01-15 20:49:09 by jwe]
jwe
parents: 7017
diff changeset
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 ([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
92 %!assert (xor ([i, i, 0, 0], [1, 0, 1, 0]), logical ([0, 1, 1, 0]))
7385
8b7b4f58199f [project @ 2008-01-15 20:49:09 by jwe]
jwe
parents: 7017
diff changeset
93
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
94 %!assert (xor (eye (2), fliplr (eye (2))), logical (ones (2)))
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
95 %!assert (xor (speye (2), fliplr (speye (2))), sparse (logical (ones (2))))
7385
8b7b4f58199f [project @ 2008-01-15 20:49:09 by jwe]
jwe
parents: 7017
diff changeset
96
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
97 ## Test XOR reduction
19193
8661727db27a xor.m: Fix failing BIST test.
Rik <rik@octave.org>
parents: 19188
diff changeset
98 %!assert (xor ([1 0], [1 1], [0 0]), logical ([0 1]))
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
99
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
100 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27985
diff changeset
101 %!error <Invalid call> xor ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27985
diff changeset
102 %!error <Invalid call> xor (1)
19188
c3611856cdd4 xor.m: Extend to handle more than 2 arguments as a reduction operator.
Rik <rik@octave.org>
parents: 17744
diff changeset
103 %!error <X and Y must be of compatible size> xor (ones (3,2), ones (2,3))