]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/pararrayfun.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / pararrayfun.m
1 ## Copyright (C) 2009 Jaroslav Hajek <highegg@gmail.com>
2 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
3 ## Copyright (C) 2009 Travis Collier <travcollier@gmail.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify it under
6 ## the terms of the GNU General Public License as published by the Free Software
7 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## version.
9 ##
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## details.
14 ##
15 ## You should have received a copy of the GNU General Public License along with
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn{Function File} {[@var{o1}, @var{o2}, @dots{}] =} pararrayfun (@var{nproc}, @var{fun}, @var{a1}, @var{a2}, @dots{})
20 ## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "UniformOutput", @var{val})
21 ## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "ErrorHandler", @var{errfunc})
22 ## Evaluates a function for corresponding elements of an array. 
23 ## Argument and options handling is analogical to @code{parcellfun}, except that
24 ## arguments are arrays rather than cells. If cells occur as arguments, they are treated
25 ## as arrays of singleton cells.
26 ## Arrayfun supports one extra option compared to parcellfun: "Vectorized".
27 ## This option must be given together with "ChunksPerProc" and it indicates
28 ## that @var{fun} is able to operate on vectors rather than just scalars, and returns
29 ## a vector. The same must be true for @var{errfunc}, if given.
30 ## In this case, the array is split into chunks which are then directly served to @var{func}
31 ## for evaluation, and the results are concatenated to output arrays.
32 ## @seealso{parcellfun, arrayfun}
33 ## @end deftypefn
34
35 function varargout = pararrayfun (nproc, func, varargin)
36
37   if (nargin < 3)
38     print_usage ();
39   endif
40
41   [nargs, uniform_output, error_handler, ...
42   verbose_level, chunks_per_proc, vectorized] = parcellfun_opts (varargin); 
43
44   args = varargin(1:nargs);
45   opts = varargin(nargs+1:end);
46   if (nargs == 0)
47     print_usage ();
48   elseif (nargs > 1)
49     [err, args{:}] = common_size (args{:});
50     if (err)
51       error ("pararrayfun: arguments size must match");
52     endif
53   endif
54
55   njobs = numel (args{1});
56
57   if (vectorized && chunks_per_proc > 0 && chunks_per_proc < njobs / nproc)
58     ## If "Vectorized" is on, we apply the function directly on chunks of
59     ## arrays.
60     [varargout{1:nargout}] = chunk_parcellfun (nproc, chunks_per_proc, ...
61     func, error_handler, verbose_level, args{:});
62   else
63     args = cellfun (@num2cell, args, "UniformOutput", false,
64     "ErrorHandler",  @arg_class_error);
65
66     [varargout{1:nargout}] = parcellfun (nproc, func, args{:}, opts{:});
67   endif
68
69 endfunction
70
71 function arg_class_error (S, X)
72   error ("arrayfun: invalid argument of class %s", class (X))
73 endfunction