Mercurial > hg > octave-terminal
changeset 9850:85f6c386f2bb
implement built-in logical
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 23 Nov 2009 06:55:30 +0100 |
parents | 87fd803c583b |
children | 1fac51c5f83f |
files | scripts/general/logical.m scripts/general/module.mk src/ov-bool-mat.cc |
diffstat | 3 files changed, 32 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
deleted file mode 100644 --- a/scripts/general/logical.m +++ /dev/null @@ -1,68 +0,0 @@ -## Copyright (C) 1996, 1997, 1998, 2000, 2005, 2006, 2007, 2008 John W. Eaton -## -## 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 -## <http://www.gnu.org/licenses/>. - -## -*- texinfo -*- -## @deftypefn {Function File} {} logical (@var{arg}) -## Convert @var{arg} to a logical value. For example, -## -## @example -## logical ([-1, 0, 1]) -## @end example -## -## @noindent -## is equivalent to -## -## @example -## [-1, 0, 1] != 0 -## @end example -## @end deftypefn - -## Author: jwe - -function y = logical (x) - - if (nargin == 1) - if (islogical (x)) - y = x; - elseif (isempty (x)) - y = zeros (size (x), "logical"); - elseif (isnumeric (x)) - if (any (isnan (x(:)))) - error ("invalid conversion from NaN to logical"); - else - y = x != 0; - endif - else - error ("logical not defined for type `%s'", typeinfo (x)); - endif - else - print_usage (); - endif - -endfunction - -%!assert (logical ([]), zeros ([0, 0], "logical")); -%!assert (logical (zeros (2, 0)), zeros ([2, 0], "logical")); -%!assert (logical (0), false); -%!assert (logical (13), true); -%!assert (logical (-13), true); -%!assert (logical (int8 (13)), true); -%!assert (logical (int8 (-13)), true); -%!assert (logical ([-1, 0, 1, Inf]), [-1, 0, 1, Inf] != 0); -%!error (logical ([-1, 0, 1, NaN, Inf])) -%!error (logical (NaN))
--- a/scripts/general/module.mk +++ b/scripts/general/module.mk @@ -48,7 +48,6 @@ general/issymmetric.m \ general/isvector.m \ general/loadobj.m \ - general/logical.m \ general/logspace.m \ general/mod.m \ general/nargchk.m \
--- a/src/ov-bool-mat.cc +++ b/src/ov-bool-mat.cc @@ -536,6 +536,38 @@ return retval; } +DEFUN (logical, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} logical (@var{x})\n\ +Convert @var{x} to logical type.\n\ +@seealso{double, single, char}\n\ +@end deftypefn") +{ + octave_value retval; + + if (args.length () == 1) + { + octave_value arg = args(0); + if (arg.is_bool_type ()) + retval = arg; + else if (arg.is_numeric_type ()) + { + if (arg.is_sparse_type ()) + retval = arg.sparse_bool_matrix_value (); + else if (arg.is_scalar_type ()) + retval = arg.bool_value (); + else + retval = arg.bool_array_value (); + } + else + gripe_wrong_type_arg ("logical", arg); + } + else + print_usage (); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***