Skip to content

Commit d4f9ad7

Browse files
committed
Various improvements
* Correction in `gcd` and `lcm`: they weren't working correctly with a single input * `conv` automatically converts its first two inputs to `double` * `Z}` with two inputs splits along the dimension indicated by second input * `isempty` function has been removed (can be done easily with `numel`) * added Octave compatibility with `spiral` function * extended `image` to include `imagesc`. This is controlled by last input. * Functions `bitand`, `bitor`, `bitxor`, `bitget`, `bitset` now automatically convert `char` first input to `double` * Function `bitset` now has 3 inputs by default
1 parent 690f1ce commit d4f9ad7

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

MATL_spec.pdf

1.1 KB
Binary file not shown.

funDef.mat

932 Bytes
Binary file not shown.

funDef.txt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ Z% 2 2 2 1 1 1 true true out{1} = typecast(in{:}); convert datatypes without cha
5252
& 1 inf 2 1 1 1 true true y = in{1}; for n=2:numel(in), y = bsxfun(@and, y, in{n}); end; if numel(in)==1, y = logical(y); end; out{1} = y; clear y n; logical 'and' (element-wise, singleton expansion) \matlab+&+ (\matlab+and+), element-wise with singleton expansion
5353
X& 2 4 2 1 3 1 true true [out{:}] = intersect(in{:}); set intersection \matlab+intersect+
5454
Y&
55-
Z& 2 3 2 1 1 1 true true if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2})) bit-wise logical 'and' (element-wise, singleton expansion) \matlab+bitand+, element-wise with singleton expansion
55+
Z& 2 3 2 1 1 1 true true if ischar(in{1}), in{1} = double(in{1}); end bit-wise logical 'and' (element-wise, singleton expansion) \matlab+bitand+, element-wise with singleton expansion. If first input is \matlab+char+ it is automatically converted to \matlab+double+
56+
if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2}))
5657
out{1} = bitand(in{:});
5758
else
5859
nd = max(ndims(in{1}), ndims(in{2})); sz1 = arrayfun(@(n)size(in{1},n), 1:nd); sz2 = arrayfun(@(n)size(in{2},n), 1:nd);
@@ -147,7 +148,7 @@ Z* 1 inf 2 1 1 1 true true n = numel(in); combs = cell(1,n); Cartesian product C
147148
combs = cat(n+1, combs{:}); combs = reshape(combs,[],n);
148149
out{1} = combs; clear combs n
149150
+ 1 inf 2 1 1 1 true true y = in{1}; for n=2:numel(in), y = bsxfun(@plus, y, in{n}); end; out{1} = y; clear y n; addition (element-wise, singleton expansion) \matlab|+| (\matlab+plus+), element-wise with singleton expansion
150-
X+ 2 3 2 1 1 1 true true out{1} = conv(in{:}); convolution \matlab+conv+
151+
X+ 2 3 2 1 1 1 true true out{1} = conv(double(in{1}), double(in{2}), in{3:end}); convolution \matlab+conv+. Converts first two inputs to \matlab+double+.
151152
Y+ 2 4 2 1 1 1 true true out{1} = conv2(in{:}); two-dimensional convolution \matlab+conv2+
152153
Z+ 2 2 2 1 1 1 true true out{1} = convmtx(in{:}); convolution matrix \matlab+convmtx+
153154
,
@@ -161,7 +162,8 @@ Z-
161162
.
162163
X.
163164
Y. 0 1 1 0 0 0 true true pause(in{:}) pause \matlab+pause+ (without outputs)
164-
Z. 2 3 2 1 1 1 true true out{1} = bitget(in{:}); get bit \matlab+bitget+
165+
Z. 2 3 2 1 1 1 true true if ischar(in{1}), in{1} = double(in{1}); end get bit \matlab+bitget+. If first input is \matlab+char+ it is automatically converted to \matlab+double+
166+
out{1} = bitget(in{:});
165167
/ 2 2 2 1 1 1 true true out{1} = bsxfun(@rdivide, in{1}, in{2}); array right division (element-wise, singleton expansion) \matlab+./+ (\matlab+rdivide+), element-wise with singleton expansion
166168
X/ 1 1 1 1 1 1 true true out{1} = angle(in{1}); phase angle (radians) \matlab+angle+
167169
Y/ 2 2 2 1 1 1 true true out{1} = in{1}/in{2}; right matrix division right matrix division, \matlab+/+ (\matlab+mrdivide+)
@@ -245,7 +247,8 @@ Y: 1 inf 2 1 inf 1 true true switch in{1} Higham test matrices and other matrice
245247
otherwise
246248
[out{:}] = gallery(in{:});
247249
end
248-
Z: 2 3 2 1 1 1 true true out{1} = bitset(in{:}); set bit \matlab+bitset+
250+
Z: 2 4 3 1 1 1 true true if ischar(in{1}), in{1} = double(in{1}); end set bit \matlab+bitset+. If first input is \matlab+char+ it is automatically converted to \matlab+double+
251+
out{1} = bitset(in{:});
249252
;
250253
X; 1 1 1 1 1 1 true true out{1} = acos(in{1}); inverse cosine (radians) \matlab+acos+
251254
Y; 1 1 1 1 1 1 true true out{1} = asin(in{1}); inverse sine (radians) \matlab+asin+
@@ -343,7 +346,10 @@ YF 0 1 1 0 0 0 true true format(in{:}) set output format \matlab+format+
343346
ZF
344347
G
345348
XG 1 inf 1 0 0 0 true true plot(in{:}); drawnow plot \matlab+plot+. Calls \matlab+drawnow+ to update figure immediately
346-
YG 0 3 1 0 0 0 true true image(in{:}); axis ij, axis image, drawnow display image \matlab+image(...), axis ij, axis image+. Calls \matlab+drawnow+ to update figure immediately
349+
YG 2 inf 2 0 0 0 true true if isnumeric(in{end})&&numel(in{end})~=1, imagesc(in{:}); display image \matlab+imagesc+ or \matlab+image+. If last input is a scalar that evaluates to \matlab+true+ / \matlab+false+: \matlab+imagesc+ / \matlab+image+ is respectively called with remaining inputs. If last input is not a scalar: \matlab+imagesc+ is called with all inputs. Afterwards, in all cases, this sets \matlab+axis ij, axis image+ and calls \matlab+drawnow+ to update figure immediately
350+
elseif in{end}, imagesc(in{1:end-1});
351+
else image(in{1:end-1}); end
352+
axis ij, axis image, drawnow
347353
ZG 1 1 1 0 1 0 true true [out{:}] = colormap(in{:}); if isempty(out), drawnow, end color look-up table \matlab+colormap+. With $0$ outputs, calls \matlab+drawnow+ to update figure immediately
348354
H 0 0 0 0 inf numel(CB_H) true true out = CB_H(1:nout); paste from clipboard H paste from clipboard H
349355
XH 0 inf 1 0 0 0 false true CB_H = in; copy to clipboard H copy to clipboard H
@@ -466,7 +472,7 @@ Zc 1 2 1 1 1 1 true true out{1} = strjoin(in{:}); join cell array of strings int
466472
d 1 3 1 1 1 1 true true out{1} = diff(in{:}); difference \matlab+diff+
467473
Xd 1 2 1 1 1 1 true true out{1} = diag(in{:}); diagonal matrices and diagonals of a matrix \matlab+diag+
468474
Yd 1 inf 2 1 1 1 true true out{1} = blkdiag(in{:}); block diagonal concatenation \matlab+blkdiag+
469-
Zd 1 2 2 1 3 1 true true if numel(in)==1 && numel(out)==1, x=in{1}(1); for t=in{1}, x=gcd(x,t); end; out{1}=x; greatest common divisor (element-wise, singleton expansion) \matlab+gcd+, element-wise with singleton expansion. With $1$ input and $1$ output, computes the greatest common divisor of all elements of the input
475+
Zd 1 2 2 1 3 1 true true if numel(in)==1 && numel(out)==1, x=in{1}(1); for t=in{1}(:).', x=gcd(x,t); end; out{1}=x; greatest common divisor (element-wise, singleton expansion) \matlab+gcd+, element-wise with singleton expansion. With $1$ input and $1$ output, computes the greatest common divisor of all elements of the input
470476
else
471477
if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2}))
472478
[out{:}] = gcd(in{:});
@@ -520,7 +526,7 @@ Zl 1 1 1 1 2 1 true true [out{:}] = log2(in{:}); base 2 logarithm \matlab+log2+
520526
m 2 4 2 1 2 1 true true [out{:}] = ismember(in{:}); true for set member \matlab+ismember+. \sa \matl+Xm+
521527
Xm 2 3 2 1 2 1 true true [out{:}] = ismember(in{1},in{2},'rows',in{3:end}); true for set member, row-wise \matlab+ismember(..., 'rows', ...)+. \sa \matl+m+
522528
Ym 1 4 1 1 1 1 true true out{1} = mean(in{:}); mean value \matlab+mean+
523-
Zm 1 2 2 1 1 1 true true if numel(in)==1, x=1; for t=in{1}, x=lcm(x,t); end; out{1}=x; least common multiple (element-wise, singleton expansion) \matlab+lcm+, element-wise with singleton expansion. With $1$ input, computes the least common multiple of all elements of the input
529+
Zm 1 2 2 1 1 1 true true if numel(in)==1, x=1; for t=in{1}(:).', x=lcm(x,t); end; out{1}=x; least common multiple (element-wise, singleton expansion) \matlab+lcm+, element-wise with singleton expansion. With $1$ input, computes the least common multiple of all elements of the input
524530
else
525531
if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2}))
526532
out{1} = lcm(in{:});
@@ -584,7 +590,7 @@ Yy 2 2 2 1 1 1 true true out{1} = bsxfun(@hypot, in{1}, in{2}); hypotenuse (elem
584590
Zy
585591
z 1 1 1 1 1 1 true true out{1} = nnz(in{:}); number of nonzero elements \matlab+nnz+
586592
Xz 1 1 1 1 1 1 true true out{1} = nonzeros(in{:}); nonzero elements \matlab+nonzeros+
587-
Yz 1 1 1 1 1 1 true true out{1} = isempty(in{:}); true for empty array \matlab+isempty+
593+
Yz
588594
Zz
589595
{
590596
X{ 1 2 1 1 1 1 true true out{1} = num2cell(in{:}); convert numeric array into cell array \matlab+num2cell+
@@ -593,7 +599,8 @@ Z{ 1 1 1 1 1 1 true true sz = num2cell(size(in{1})); out{1} = mat2cell(in{1}, on
593599
| 1 inf 2 1 1 1 true true y = in{1}; for n=2:numel(in), y = bsxfun(@or, y, in{n}); end; if numel(in)==1, y = logical(y); end; out{1} = y; clear y n; logical 'or' (element-wise, singleton expansion) \matlab+|+ (\matlab+or+), element-wise with singleton expansion
594600
X| 2 4 2 1 3 1 true true [out{:}] = union(in{:}); set union \matlab+union+
595601
Y| 1 2 1 1 1 1 true true out{1} = norm(in{:}); matrix or vector norm \matlab+norm+
596-
Z| 2 3 2 1 1 1 true true if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2})) bit-wise logical 'or' (element-wise, singleton expansion) \matlab+bitor+, element-wise with singleton expansion
602+
Z| 2 3 2 1 1 1 true true if ischar(in{1}), in{1} = double(in{1}); end bit-wise logical 'or' (element-wise, singleton expansion) \matlab+bitor+, element-wise with singleton expansion. If first input is \matlab+char+ it is automatically converted to \matlab+double+
603+
if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2}))
597604
out{1} = bitor(in{:});
598605
else
599606
nd = max(ndims(in{1}), ndims(in{2})); sz1 = arrayfun(@(n)size(in{1},n), 1:nd); sz2 = arrayfun(@(n)size(in{2},n), 1:nd);
@@ -605,12 +612,17 @@ Z| 2 3 2 1 1 1 true true if max(size(in{1}))==1 || max(size(in{2}))==1 || isequa
605612
}
606613
X}
607614
Y} 1 1 1 1 1 1 true true out{1} = cell2mat(in{:}); convert contents of cell array into single array \matlab+cell2mat+
608-
Z} 1 1 1 0 inf numel(in{1}) true true outall = mat2cell(in{1}(:), ones(1,numel(in{1}))).'; split array into elements split array into its elements in linear order
615+
Z} 1 2 1 0 inf prod(size(in{:})) true true if numel(in)==1, outall = mat2cell(in{1}(:), ones(1,numel(in{1}))).'; split array along a dimension split array into elements in linear order. With $2$ inputs: split into subarrays along the dimension indicated by the second input
616+
elseif numel(in)==2, d = num2cell(size(in{1})); d{in{2}} = ones(1,size(in{1},in{2})); outall = mat2cell(in{1}, d{:}); outall = outall(:).'; clear d
617+
else error('MATL:runtime', 'MATL run-time error: too many inputs'); end
609618
out = outall(1:nout);
619+
% The default nout, prod(size(in{:})), is either numel(in{1}) o size(in{1}, in{2})
620+
% I considered defining Z} such that with 1 input it split along the first non-singleton dimension. But that gave me trouble. : The default nout should have been the size along the first non-singleton dimension or along the dimension indicated by the first output. : I didn't see how to define the default nout in a single statement. I tried size(in{1}, (numel(in)>1)*in{end} + (numel(in)==1)*[find(size(in{1})-1,1) repmat(1,1,numel(in{1})==1)]); but it didn't work when the first input was a cell array and there was not second input: in that case in{end} was not a number
610621
~ 1 1 1 1 1 1 true true out{1} = ~in{1}; logical 'not' (element-wise) \matlab+~+ (\matlab+not+)
611622
X~ 2 4 2 1 3 1 true true [out{:}] = setxor(in{:}); set exclusive-or \matlab+setxor+
612623
Y~ 2 2 2 1 1 1 true true out{1} = bsxfun(@xor, in{1}, in{2}); logical 'xor' (element-wise, singleton expansion) \matlab+xor+, element-wise with singleton expansion
613-
Z~ 1 3 2 1 1 1 true true if numel(in)>=2 && isnumeric(in{2}) bit-wise logical 'xor' (element-wise, singleton expanstion) or complement bits \matlab+bitxor+, element-wise with singleton expansion. With $1$ numeric input (and optionally a second string input): \matlab+bitcmp+
624+
Z~ 1 3 2 1 1 1 true true if ischar(in{1}), in{1} = double(in{1}); end bit-wise logical 'xor' (element-wise, singleton expanstion) or complement bits \matlab+bitxor+, element-wise with singleton expansion. With $1$ numeric input (and optionally a second string input): \matlab+bitcmp+. In both cases, if first input is \matlab+char+ it is automatically converted to \matlab+double+
625+
if numel(in)>=2 && isnumeric(in{2})
614626
if max(size(in{1}))==1 || max(size(in{2}))==1 || isequal(size(in{1}), size(in{2}))
615627
out{1} = bitxor(in{:});
616628
else

genHelp.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
maxOut = str2double(F(n).maxOut);
133133
defOut = str2double(F(n).defOut);
134134

135+
% Changes done here should be done in MATL_spec.tex too;
135136
if isnan(defIn) && ~isempty(F(n).defIn) % F(n).defIn contains a string that couldn't be converted to a number
136137
switch F(n).defIn
137138
case 'numel(STACK)'
@@ -158,7 +159,7 @@
158159
inFormatted{n} = sprintf('%i', defIn);
159160
end
160161

161-
% Format output spec:
162+
% Format output spec. Changes done here should be done in MATL_spec.tex too
162163
if isnan(defOut) && ~isempty(F(n).defOut) % F(n).defOut contains a string that couldn't be converted to a number
163164
switch F(n).defOut
164165
case {'numel(CB_H)' 'numel(CB_I)' 'numel(CB_J)' 'numel(CB_K)'}
@@ -167,6 +168,8 @@
167168
defOutStr = 'number of elements in clipboard level';
168169
case 'numel(in{1})'
169170
defOutStr = 'number of elements of first input';
171+
case 'prod(size(in{:}))' % Z}
172+
defOutStr = 'number of elements or subarrays that will be produced';
170173
otherwise
171174
error('Unrecognized default number of outputs')
172175
end

help.mat

1.09 KB
Binary file not shown.

matl_compile.m

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
appendLines('% Set initial conditions', 0)
6969
appendLines('warningState = warning;', 0);
7070
appendLines('format compact; format long; warning(''off'',''all'');', 0) % clc
71+
appendLines('defaultColorMap = get(0, ''DefaultFigureColormap'');', 0)
72+
appendLines('set(0, ''DefaultFigureColormap'', gray(256));', 0)
7173
if isMatlab && exist('rng', 'file') % recent Matlab version
7274
appendLines('rng(''shuffle'')', 0)
7375
elseif isMatlab % old Matlab version
74-
appendLines('rand(''seed'',sum(clock)); randn(''seed'',sum(clock))');
76+
appendLines('rand(''seed'',sum(clock)); randn(''seed'',sum(clock))', 0);
7577
% else % Octave: seeds are set randomly automatically by Octave
7678
end
7779
appendLines('diary off; delete defout; diary defout', 0)
@@ -230,14 +232,15 @@
230232
appendLines('', 0)
231233
appendLines('% Set final conditions', 0)
232234
appendLines('diary off; warning(warningState);', 0);
235+
appendLines('set(0, ''DefaultFigureColormap'', defaultColorMap);', 0)
233236
appendLines('', 0)
234237
appendLines('end', 0) % close function, in case there are subfunctions
235238

236239
% Define subfunctions for compatibility with Octave
237240
if ~isMatlab
238-
% num2str
239-
appendLines('% Define subfunctions', 0)
240241
appendLines('', 0)
242+
appendLines('% Define subfunctions', 0)
243+
% num2str. Fixes alignment in certain cases
241244
appendLines({...
242245
'function y = num2str(varargin)' ...
243246
'x = varargin{1}; x = reshape(x, size(x,1),[]);' ...
@@ -249,13 +252,31 @@
249252
'y = reshape(y.'',[],size(x,1)).''; y = strtrim(y);' ...
250253
'end' ...
251254
'end'}, 0)
252-
% im2col
253-
appendLines('', 0)
254-
appendLines({...
255-
'function y = im2col(varargin)' ...
256-
'argin1 = varargin{1}; argin1 = reshape(argin1, size(argin1,1), []);' ...
257-
'y = builtin(''im2col'', argin1, varargin{2:end});' ...
258-
'end'}, 0)
255+
% im2col. Fixes behaviour
256+
fname = 'im2col';
257+
if any(~cellfun(@isempty,strfind(C,fname)))
258+
appendLines('', 0)
259+
appendLines({...
260+
'function y = im2col(varargin)' ...
261+
'argin1 = varargin{1}; argin1 = reshape(argin1, size(argin1,1), []);' ...
262+
'y = builtin(''im2col'', argin1, varargin{2:end});' ...
263+
'end'}, 0)
264+
end
265+
% spiral. Non-existent in Octave
266+
fname = 'spiral';
267+
if any(~cellfun(@isempty,strfind(C,fname)))
268+
appendLines('', 0)
269+
appendLines({...
270+
'function s = spiral(n)' ...
271+
's = zeros(n,n); i = ceil(n/2); j = ceil(n/2);' ...
272+
's(i,j) = 1; if n == 1, return, end' ...
273+
'k = 1; d = 1;' ...
274+
'for p = 1:n' ...
275+
'q = 1:min(p,n-1); j = j+d*q; k = k+q; s(i,j) = k; if (p == n), return, end' ...
276+
'j = j(p); k = k(p); i = i+d*q''; k = k+q''; s(i,j) = k; i = i(p); k = k(p); d = -d;' ...
277+
'end' ...
278+
'end'}, 0)
279+
end
259280
end
260281

261282
if verbose

0 commit comments

Comments
 (0)