Mercurial > hg > octave-nkf
annotate scripts/general/common_size.m @ 10635:d1978e7364ad
Print name of function in error() string messages.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 16 May 2010 22:26:54 -0700 |
parents | 7fbdeaa9b0e0 |
children | 3140cb7a05a1 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2007 |
2 ## Kurt Hornik | |
9477 | 3 ## Copyright (C) 2009 VZLU Prague |
10034 | 4 ## Copyright (C) 2009 Jaroslav Hajek |
3426 | 5 ## |
3922 | 6 ## This file is part of Octave. |
7 ## | |
8 ## Octave is free software; you can redistribute it and/or modify it | |
9 ## under the terms of the GNU General Public License as published by | |
7016 | 10 ## the Free Software Foundation; either version 3 of the License, or (at |
11 ## your option) any later version. | |
3426 | 12 ## |
3922 | 13 ## Octave is distributed in the hope that it will be useful, but |
2539 | 14 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 16 ## General Public License for more details. |
17 ## | |
2539 | 18 ## You should have received a copy of the GNU General Public License |
7016 | 19 ## along with Octave; see the file COPYING. If not, see |
20 ## <http://www.gnu.org/licenses/>. | |
2539 | 21 |
3369 | 22 ## -*- texinfo -*- |
6547 | 23 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{}) |
2539 | 24 ## Determine if all input arguments are either scalar or of common |
3369 | 25 ## size. If so, @var{err} is zero, and @var{yi} is a matrix of the |
26 ## common size with all entries equal to @var{xi} if this is a scalar or | |
27 ## @var{xi} otherwise. If the inputs cannot be brought to a common size, | |
28 ## errorcode is 1, and @var{yi} is @var{xi}. For example, | |
3426 | 29 ## |
3369 | 30 ## @example |
31 ## @group | |
32 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) | |
33 ## @result{} errorcode = 0 | |
34 ## @result{} a = [ 1, 2; 3, 4 ] | |
35 ## @result{} b = [ 5, 5; 5, 5 ] | |
36 ## @end group | |
37 ## @end example | |
3426 | 38 ## |
3369 | 39 ## @noindent |
2539 | 40 ## This is useful for implementing functions where arguments can either |
41 ## be scalars or of common size. | |
3369 | 42 ## @end deftypefn |
2539 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 45 ## Created: 15 October 1994 |
46 ## Adapted-By: jwe | |
9477 | 47 ## Optimized-By: Jaroslav Hajek |
2539 | 48 |
3979 | 49 function [errorcode, varargout] = common_size (varargin) |
3426 | 50 |
2539 | 51 if (nargin < 2) |
52 error ("common_size: only makes sense if nargin >= 2"); | |
53 endif | |
54 | |
9477 | 55 ## Find scalar args. |
56 nscal = cellfun ("numel", varargin) != 1; | |
3426 | 57 |
9477 | 58 i = find (nscal, 1); |
59 | |
60 if (isempty (i)) | |
61 errorcode = 0; | |
3979 | 62 varargout = varargin; |
2539 | 63 else |
9477 | 64 match = cellfun (@size_equal, varargin, varargin(i)); |
65 if (any (nscal &! match)) | |
66 errorcode = 1; | |
67 varargout = varargin; | |
68 else | |
69 errorcode = 0; | |
70 if (nargout > 1) | |
71 scal = !nscal; | |
72 varargout = varargin; | |
10034 | 73 if (any (nscal)) |
74 ## This could also be achieved by cellfun (@repmat, ...), but repmat is an | |
75 ## m-function and hence still carries interpreter overhead. Further, | |
76 ## cellfun is slightly optimized for the case used below. | |
77 uo = "uniformoutput"; | |
78 dims = size (varargin{find (nscal, 1)}); | |
79 subs = substruct ("()", arrayfun (@ones, 1, dims, uo, false)); | |
10043
7fbdeaa9b0e0
fix common_size optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
10034
diff
changeset
|
80 varargout(scal) = cellfun ("subsref", varargin(scal), {subs}, uo, false); |
10034 | 81 endif |
2539 | 82 endif |
9477 | 83 endif |
2539 | 84 endif |
85 endfunction |