function Aout = Mini_Gallery(type,n)
% MiniGallery. Sampler of test matrices.
% MiniGallery, with no arguments, displays twelve matrices.
% A = MiniGallery('type') returns a 15-by-15 matrix of type 'type'.
% A = MiniGallery('type',n) returns an n-by-n matrix of type 'type'.
% ex. W = MiniGallery('wilkinson',21).
% See also gallery. Higham's test matrices.
% https://blogs.mathworks.com/cleve/2023/06/28/minigallery-sampler-of-matlab-test-matrices
%

% Copyright 2023 Cleve Moler

    if nargin < 2
        n = 15;
    end
    if nargin < 1
        type = ' ';
    end
    B = bucky;
    A = (B(1:n,1:n) + B(2:n+1,1:n) + B(1:n,2:n+1))/3;
    A(n,n) = 1/3;

    switch lower(type)
        case 'wilkinson', A = wilkinson(n);
        case 'bucky', A = B;
        case 'band', A = triu(tril(A,2),-2);
        case 'triangular', A = triu(A);
        case 'hessenberg', A = triu(A,-1);
        case 'random', A = sprand(n,n,0.25);
        case 'magic', A = magic(n);
        case 'toeplitz', A = gallery('toeppd',n);
        case 'hankel', A = flip(gallery('toeppd',n));
        case 'permutation', A = sparse(randperm(n),1:n,1);
        case 'companion', A = [-(1:n);eye(n-1,n)];
        case 'fiedler', A = fiedler(1:n);
        otherwise
            initgcf('MiniGallery')
            tiledlayout('flow') 

            cspy(7*sprand(n,n,0.25),'Random')
            cspy(triu(tril(A,2),-2),'Band')
            cspy(sparse(randperm(n),1:n,1/3),'Permutation')
            cspy(flip(gallery('toeppd',n)),'Hankel')

            cspy(3*B,'Bucky')
            cspy(tril(A),'Triangular')
            cspy(rot90([-(1:n);eye(n-1,n)],2),'Companion')
            cspy(gallery('toeppd',n),'Toeplitz')

            cspy(wilkinson(n),'Wilkinson')
            cspy(tril(A)+diag(ones(n-1,1),1),'Hessenberg')
            cspy(fiedler(1:n),'Fiedler')
            cspy(magic(n)/16,'Magic')
    end

    if nargout > 0
        Aout = A;
    end
    
    function cspy(A,type)
        nexttile
        ms = 14;
        co = get(gca,'colororder');
        [m,n] = size(A);
        axis([0 n+1 0 m+1])
        axis square
        box on
        set(gca,'xtick',[],'ytick',[])
        for k = 1:m
            for j = 1:n
                if A(k,j) ~= 0
                    line(n+1-j,k, ...
                        'linestyle','none', ...
                        'marker','.', ...
                        'markersize',ms, ...
                        'color',co(mod(floor(A(k,j)),7)+1,:))
                end
            end
        end
        title(type)
    end

    function F = fiedler(a)
        n = length(a);
        b = ones(n-2,1); b(1:2:n-2) = 0;
        c = -a(2:n); c(1:2:n-1) = 0; c(1) = 1;
        d = -a(2:n); d(2:2:n-1) = 0;
        e = ones(n-2,1); e(2:2:n-2) = 0;
        F = diag(b,-2) + diag(c,-1) + diag(d,1) + diag(e,2); F(1,1) = -a(1);
    end

    function initgcf(name)
        clf
        shg
        set(gcf,'name',name, ...
            'numbertitle','off', ...
            'toolbar','none', ...
            'menubar','none')
    end
end