Mercurial > hg > octave-lyh
changeset 6422:9b982dd07654
[project @ 2007-03-20 21:26:12 by dbateman]
author | dbateman |
---|---|
date | Tue, 20 Mar 2007 21:26:13 +0000 |
parents | cac156381f81 |
children | a90b54016b12 |
files | scripts/ChangeLog scripts/general/Makefile.in scripts/general/arrayfun.m src/ChangeLog src/DLD-FUNCTIONS/cellfun.cc |
diffstat | 5 files changed, 89 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,11 @@ +2007-03-20 David Bateman <dbateman@free.fr> + + * general/Makefile.in: Include arrayfun.m in SOURCES. + +2007-03-20 Bill Denney <denney@seas.upenn.edu> + + * general/arrayfun.m: New function. + 2007-03-20 John W. Eaton <jwe@octave.org> * plot/newplot.m: Call __request_drawnow__ after initializing axes.
--- a/scripts/general/Makefile.in +++ b/scripts/general/Makefile.in @@ -20,8 +20,8 @@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_DATA = @INSTALL_DATA@ -SOURCES = __isequal__.m bicubic.m bitcmp.m bitget.m bitset.m \ - blkdiag.m cart2pol.m cart2sph.m cell2mat.m circshift.m \ +SOURCES = __isequal__.m arrayfun.m bicubic.m bitcmp.m bitget.m \ + bitset.m blkdiag.m cart2pol.m cart2sph.m cell2mat.m circshift.m \ common_size.m cplxpair.m cumtrapz.m deal.m diff.m flipdim.m \ fliplr.m flipud.m gradient.m ind2sub.m int2str.m interp1.m \ interp2.m interpft.m is_duplicate_entry.m isa.m isdefinite.m \
new file mode 100644 --- /dev/null +++ b/scripts/general/arrayfun.m @@ -0,0 +1,71 @@ +## 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 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. +## +## 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 + +## -*- 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 +## 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) + + if (nargin < 2) + 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}); + 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)) + 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 + ## size as the other input. + stillmatches = false; + else + cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:}); + endif + else + stillmatches = false; + endif + endwhile + + varargout = cell (max ([nargout, 1]), 1); + [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:length(varargin)}); +endfunction + +%!test +%! 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,8 @@ +2007-03-20 David Bateman <dbateman@free.fr> + + * DLD-FUNCTIONS/cellfun.cc (Fcellfun): Correct for shape of return + matrix for the case of UniformOutput being true. + 2007-03-20 John W. Eaton <jwe@octave.org> * sysdep.cc (Fpause): Call drawnow.
--- a/src/DLD-FUNCTIONS/cellfun.cc +++ b/src/DLD-FUNCTIONS/cellfun.cc @@ -401,8 +401,7 @@ if (error_state) goto cellfun_err; - val.resize(f_args.dims()); - retval(j) = val; + retval(j) = val.resize(f_args.dims()); } } else @@ -521,6 +520,8 @@ %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) +%!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4])) +%!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2])) %!error(cellfun(@factorial,{-1,3})) %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) %!test