Mercurial > hg > octave-lyh
changeset 6439:a37c427ae9d1
[project @ 2007-03-23 15:04:01 by jwe]
author | jwe |
---|---|
date | Fri, 23 Mar 2007 15:05:01 +0000 |
parents | 14e5882b6269 |
children | 98ee80702bca |
files | scripts/general/arrayfun.m src/ChangeLog src/bitfcns.cc |
diffstat | 3 files changed, 55 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/arrayfun.m +++ b/scripts/general/arrayfun.m @@ -1,56 +1,59 @@ ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> ## -## This program 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 2 of the License, or -## (at your option) any later version. +## This file is part of Octave. ## -## This program 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. +## 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 2, 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 this program; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. ## -*- texinfo -*- -## @deftypefn {Command} @var{a} = arrayfun(@var{name}, @var{c}) -## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}) -## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{d}) -## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{options}) -## @deftypefnx {Command} [@var{a}, @var{b}, @dots{}] = arrayfun(@var{func}, @var{c}, @dots{}) -## Execute a function on each element of an array. This is useful for -## functions that do not accept array arguments. If the function does +## @deftypefn {Command} {@var{a} =} arrayfun (@var{name}, @var{c}) +## @deftypefnx {Command} {@var{a} =} arrayfun (@var{func}, @var{c}) +## @deftypefnx {Command} {@var{a} =} arrayfun (@var{func}, @var{c}, @var{d}) +## @deftypefnx {Command} {@var{a} =} arrayfun (@var{func}, @var{c}, @var{options}) +## @deftypefnx {Command} {[@var{a}, @var{b}, @dots{}] =} arrayfun (@var{func}, @var{c}, @dots{}) +## Execute a function on each element of an array. This is useful for +## functions that do not accept array arguments. If the function does ## accept array arguments it is better to call the function directly. ## ## See @code{cellfun} for complete usage instructions. ## @seealso{cellfun} ## @end deftypefn -function [varargout] = arrayfun(func, varargin) +function varargout = arrayfun (func, varargin) if (nargin < 2) - print_usage(); + print_usage (); endif - ## convert everything to cells and call cellfun (let cellfun error - ## check the options in case more options come available) - sizetomatch = size(varargin{1}); + ## Convert everything to cells and call cellfun (let cellfun error + ## check the options in case more options come available). + sizetomatch = size (varargin{1}); m2cargs{1} = ones (size (varargin{1}, 1), 1); m2cargs{2} = ones (size (varargin{1}, 2), 1); cfarg{1} = mat2cell (varargin{1}, m2cargs{:}); stillmatches = true; idx = 1; - while stillmatches && (idx < length(varargin)) + while (stillmatches && idx < numel (varargin)) idx++; thissize = size (varargin{idx}); - if (length (thissize) == length (sizetomatch)) && \ - all (thissize == sizetomatch) - if ischar (varargin{idx}) && \ - (strcmpi (varargin{idx}, "UniformOutput") || \ - strcmpi (varargin{idx}, "ErrorHandler")) - ## catch these strings just in case they happen to be the same + if (numel (thissize) == numel (sizetomatch) + && all (thissize == sizetomatch)) + if (ischar (varargin{idx}) + && (strcmpi (varargin{idx}, "UniformOutput") + || strcmpi (varargin{idx}, "ErrorHandler"))) + ## Catch these strings just in case they happen to be the same ## size as the other input. stillmatches = false; else @@ -62,10 +65,10 @@ endwhile varargout = cell (max ([nargout, 1]), 1); - [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:length(varargin)}); + [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:numel(varargin)}); endfunction %!test -%! fun = @(x,y) 2*x+y +%! fun = @(x,y) 2*x+y; %! A = [1,2;3,4]; %! assert(arrayfun(fun,A,A),fun(A,A))
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2007-03-23 John W. Eaton <jwe@octave.org> + + * bitfcns.cc (Fbitshift): Error if third argument is not a scalar. + 2007-03-23 David Bateman <dbateman@free.fr> * DLD-FUNCTIONS/rand.cc: Make statistical tests optional and
--- a/src/bitfcns.cc +++ b/src/bitfcns.cc @@ -404,9 +404,11 @@ \n\ bitshift (10, [-2, -1, 0, 1, 2])\n\ @result{} 2 5 10 20 40\n\ -\n\ -bitshift ([1, 10], 2, [3,4])\n\ -@result{} 4 8\n\ +@c FIXME -- restore this example when third arg is allowed to be an array.\n\ +@c \n\ +@c \n\ +@c bitshift ([1, 10], 2, [3,4])\n\ +@c @result{} 4 8\n\ @end example\n\ @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}\n\ @end deftypefn") @@ -427,12 +429,19 @@ { if (nargin == 3) { - nbits = args(2).int_value (); + // FIXME -- for compatibility, we should accept an array + // or a scalar as the third argument. + if (args(2).numel () > 1) + error ("bitshift: expecting scalar integer as third argument"); + else + { + nbits = args(2).int_value (); - if (error_state) - error ("bitshift: expecting integer as third argument"); - else if (nbits < 0) - error ("bitshift: number of bits to mask must be positive"); + if (error_state) + error ("bitshift: expecting integer as third argument"); + else if (nbits < 0) + error ("bitshift: number of bits to mask must be positive"); + } } }