]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/common_size.m
update packages
[CreaPhase.git] / octave_packages / m / general / common_size.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ## Copyright (C) 2009 VZLU Prague
3 ## Copyright (C) 2009 Jaroslav Hajek
4 ##
5 ## This file is part of Octave.
6 ##
7 ## Octave is free software; you can redistribute it and/or modify it
8 ## under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
11 ##
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ## General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with Octave; see the file COPYING.  If not, see
19 ## <http://www.gnu.org/licenses/>.
20
21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{})
23 ## Determine if all input arguments are either scalar or of common
24 ## size.  If so, @var{err} is zero, and @var{yi} is a matrix of the
25 ## common size with all entries equal to @var{xi} if this is a scalar or
26 ## @var{xi} otherwise.  If the inputs cannot be brought to a common size,
27 ## @var{err} is 1, and @var{yi} is @var{xi}.  For example:
28 ##
29 ## @example
30 ## @group
31 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5)
32 ##      @result{} errorcode = 0
33 ##      @result{} a = [ 1, 2; 3, 4 ]
34 ##      @result{} b = [ 5, 5; 5, 5 ]
35 ## @end group
36 ## @end example
37 ##
38 ## @noindent
39 ## This is useful for implementing functions where arguments can either
40 ## be scalars or of common size.
41 ## @end deftypefn
42
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Created: 15 October 1994
45 ## Adapted-By: jwe
46 ## Optimized-By: Jaroslav Hajek
47
48 function [errorcode, varargout] = common_size (varargin)
49
50   if (nargin < 2)
51     error ("common_size: only makes sense if nargin >= 2");
52   endif
53
54   ## Find scalar args.
55   nscal = cellfun ("numel", varargin) != 1;
56
57   i = find (nscal, 1);
58
59   if (isempty (i))
60     errorcode = 0;
61     varargout = varargin;
62   else
63     match = cellfun ("size_equal", varargin, varargin(i));
64     if (any (nscal &! match))
65       errorcode = 1;
66       varargout = varargin;
67     else
68       errorcode = 0;
69       if (nargout > 1)
70         scal = !nscal;
71         varargout = varargin;
72         if (any (nscal))
73           dims = size (varargin{find (nscal, 1)});
74           subs = arrayfun (@ones, 1, dims, "uniformoutput", false);
75           varargout(scal) = cellindexmat (varargin(scal), subs{:});
76         endif
77       endif
78     endif
79   endif
80 endfunction
81
82 %!error common_size ();
83
84 %!test
85 %! m = [1,2;3,4];
86 %! [err, a, b, c] = common_size (m, 3, 5);
87 %! assert (err, 0);
88 %! assert (a, m);
89 %! assert (b, [3,3;3,3]);
90 %! assert (c, [5,5;5,5]);