annotate scripts/general/logical.m @ 9141:c1fff751b5a8

Update section 17.1 (Utility Functions) of arith.txi Split section into "Exponents and Logarithms" and "Utility Functions" Use Tex in many more of the doc strings for pretty printing in pdf format.
author Rik <rdrider0-list@yahoo.com>
date Mon, 20 Apr 2009 17:16:09 -0700
parents eb63fbe60fab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7922
diff changeset
1 ## Copyright (C) 1996, 1997, 1998, 2000, 2005, 2006, 2007, 2008 John W. Eaton
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
2 ##
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
4 ##
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
9 ##
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
14 ##
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
15 ## 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: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
18
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
19 ## -*- texinfo -*-
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
20 ## @deftypefn {Function File} {} logical (@var{arg})
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
21 ## Convert @var{arg} to a logical value. For example,
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
22 ##
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
23 ## @example
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
24 ## logical ([-1, 0, 1])
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
25 ## @end example
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
26 ##
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
27 ## @noindent
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
28 ## is equivalent to
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
29 ##
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
30 ## @example
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
31 ## [-1, 0, 1] != 0
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
32 ## @end example
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3225
diff changeset
33 ## @end deftypefn
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
34
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
35 ## Author: jwe
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
36
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
37 function y = logical (x)
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
38
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
39 if (nargin == 1)
7652
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
40 if (islogical (x))
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3449
diff changeset
41 y = x;
7652
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
42 elseif (isempty (x))
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
43 y = zeros (size (x), "logical");
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3449
diff changeset
44 elseif (isnumeric (x))
7922
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
45 if (any (isnan (x(:))))
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
46 error ("invalid conversion from NaN to logical");
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
47 else
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
48 y = x != 0;
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
49 endif
3225
7aae2c3636a7 [project @ 1998-12-04 23:20:12 by jwe]
jwe
parents: 2847
diff changeset
50 else
3674
ef883684e58e [project @ 2000-06-07 17:11:33 by jwe]
jwe
parents: 3449
diff changeset
51 error ("logical not defined for type `%s'", typeinfo (x));
3225
7aae2c3636a7 [project @ 1998-12-04 23:20:12 by jwe]
jwe
parents: 2847
diff changeset
52 endif
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
53 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
54 print_usage ();
2823
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
55 endif
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
56
7ffbb34efd2d [project @ 1997-03-24 22:50:14 by jwe]
jwe
parents:
diff changeset
57 endfunction
7652
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
58
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
59 %!assert (logical ([]), zeros ([0, 0], "logical"));
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
60 %!assert (logical (zeros (2, 0)), zeros ([2, 0], "logical"));
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
61 %!assert (logical (0), false);
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
62 %!assert (logical (13), true);
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
63 %!assert (logical (-13), true);
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
64 %!assert (logical (int8 (13)), true);
b5731e43283a ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
65 %!assert (logical (int8 (-13)), true);
7922
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
66 %!assert (logical ([-1, 0, 1, Inf]), [-1, 0, 1, Inf] != 0);
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
67 %!error (logical ([-1, 0, 1, NaN, Inf]))
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7652
diff changeset
68 %!error (logical (NaN))