function S = getS(varargin)
% [ S ] = getS(dim || cellarray || 'name', ['special'],['help'],[options])
% Returns subdivision operators in this packages format.
% Format: { mask, dilation-matrix,  digit-set, name}
%
% Input:
%       dim                     return all unnamed subdiv operators with the given dimension
%       cellarray               returns the given subdivision operators
%       name,value list         List of names plus corresponding values. Eg.: (< 'a',1/2*[1 2 1]','M',2 >) 
%       '<dim>_all'             returns all subdivision operators of dimension n (e.g. '1_all')
%       {names}                 names of subdivision operator in a cell array
%
% Options:  
%       'supp'                  returns not the digit sets D, but the support of the masks as the digit sets.        
%       'Omega'                 returns not the digit sets D, but the support of the masks minus the digit sets as the digit sets.
%       'characteristic'        returns not the masks a, but the characteristic function of the digit sets as masks.
%       'nocheck'               does not make basic checks whether the subdivision operators are correct or not.
%       'bigcheck'              checks some more stuff, but this is slower
%       'verbose',val           verbose level
%
% Info:
%       More elements (than: a,M,D, name) may be added to a subdivision scheme later. The name is always the last element in each row
%
% Output
%       S                       the subdivision operaotrs
%
% E.g.: getS(2)
%       getS('a',1/2*[1 2 1]','M',2)
%       getS('2_butterfly')
%       getS('1_all')   
%
% See also: blf, tile, transitionmatrix, constructdigit, characteristic, supp, normalizeS, 
%
% Written by: tommsch, 2016

% XX bigcheck-sum-rules not tested

%#ok<*AGROW>

[bigcheck,varargin]=parsem('bigcheck',varargin);
[nocheck,varargin]=parsem('nocheck',varargin);
[verbose,varargin]=parsem('verbose',varargin,1);
[suppflag,varargin]=parsem('supp',varargin);
[Omegaflag,varargin]=parsem('Omega',varargin);
[characteristicflag,varargin]=parsem('characteristic',varargin);

    
    NUMELEMENTS=4 ;%a subdivision scheme consists of 4 elements
    S=cell(0,NUMELEMENTS);  
    if(parsem('M',varargin) || parsem('dilation',varargin)); [S,varargin]=getS_namevalue(S, varargin{:});  
    elseif(isscalar(varargin{1})); 
        dim=varargin{1}; 
        if dim==1;        
            S{end+1,2}=2; S{end,1}=[1 0 0 1]'; S{end,3}=[0 3]; S{end,end}='1_Om';
            S{end+1,2}=2; S{end,1}=[1 0 0 1]'; S{end,3}=[0 1]; S{end,end}='1_Om';
            %S{end+1,2}=2; S{end,1}=1/2*[1 2 1]'; S{end,end}='1_unconnected';
        elseif dim==2;
            
            S{end+1,2}=[1 1; 1 -1];
            S{end,3}=[1 0; 0 0]';
            %S{end,1}=[1 0 1 0 1 0;
            %            0 1 0 1 0 1;
            %            0 0 1 1 1 0;
            %            0 0 0 1 0 0];
                    
        elseif dim==3;            
            S{end+1,2}=diag([3 2 2]); 
            a1=1/9*[1 3 6 7 6 3 1]'; 
            a2=1/8*[1 4 6 4 1]'; 
            a3=1/2*[1 2 1]'; 
            S{end,1}=convm(a1,a2,a3,'outer');
            S{end,end}='tensor3';
        end; 
        
    elseif(iscell(varargin{1}));    S=varargin{1}; 
    else;                           S=getS_named(S, varargin{:});
    end;
    
%Post-processing
if(numel(varargin)>1); vprintf('There are some unkown options among these: \n %v\n',varargin,'cpr','err','imp',[0 verbose]); end;    

if(~isempty(S));
    dim=size(S{1,2},2);
else
    vprintf('getS: No data returned.\n','imp',[1 verbose]);
end;
sizeS=size(S,1);
    
    for i=1:sizeS;
        if(size(S,2)<NUMELEMENTS); S{1,NUMELEMENTS}=[]; end;
        if(isempty(S{i,2})); error('Missing dilation matrix for entry= %i, name: %s\n',i,S{i,end});       end; %2==M
        if(isempty(S{i,3})); S{i,3}=constructdigit(S{i,2}); end;
        if(isempty(S{i,1}) || characteristicflag); S{i,1}={}; [val1,val2]=characteristic(S{i,3}); S{i,1}=sequence(val1,val2); end;
        if(~isempty(S{i,1}) && ~isa(S{i,1},'sequence')); S{i,1}=sequence(S{i,1});end;  % make mask to svm if it is not a svm        
        if(suppflag); S{i,3}=S{i,1}.supp; end;
        if(Omegaflag); S{:,3}=setplus(S{i,1}.support,-S{i,3}); end; 
        if(isempty(S{i,end})); S{i,end}='unnamed'; end;
    end;
    
    %Do Checks
    if(~nocheck && sizeS>0);  
        for i=1:sizeS;
            if(min(abs(eig(S{i,2})))<=1+eps); vprintf('Dilation matrix not expanding for entry: %i, name: %s\n',i,S{i,end},'imp',[1,verbose]);  end;
            val=summ(S{i,1}.c,[]);
            detval=abs(det(S{i,2}));
            if(abs(val-detval) > 100 * eps) %%XX Aendern zu Berechnung fuer eps_\mathcal A
                vprintf('Mask sums up to %f, instead to determinant=%i for entry: %i, name: %s\n', val, detval, i, S{i,end},'imp',[1,verbose]); 
            end; 
            
             if (isequal(sizem(S{i,2},1),1) && size(S{i,1}.c,2)>1 && size(S{i,1}.c,1)==1); 
                     vprintf('Seems like you forgot to transpose the mask sequence for the entry: %i, name: %s\n',i,S{i,end},'imp',[1,verbose],'cpr','err');
             end;
            if(~isequal(size(S{i,2}),[dim dim])); vprintf('Dilation Matrix has the wrong size for the entry: %i, name: %s\n',i,S{i,end},'imp',[1,verbose]); end;
            
            if(bigcheck)                
                %test digit set
                Cl=constructdigit(S{i,2},'ZZ',S{i,3},'classify','sym'); %Classify entries in digit set
                Cl=unique(Cl);
                if(size(Cl,2)~=abs(det(S{i,2}))); 
                    vprintf('Digits are no digit set for entry: %i, name: %s\n',i,S{i,end},'imp',[1,verbose]);                
                end; 
                
                
                %test sum rules
                SUPP=supp(S{i,1});
                Cl=constructdigit(S{i,2},'ZZ',SUPP,'classify','sym'); %Classify entries in mask
                Dtemp=unique(Cl);
                val=zeros(1,numel(Dtemp));
                for j=1:numel(Dtemp)
                    val(j)=summ(S{i,1}.c(SUPP(:,Cl==Dtemp(j))+S{i,1}.idx+1));
                end
                if(any(diff(val)));
                    vprintf('Sum rules not fulfiled.\n','imp',[1,verbose]);                
                end; 
                
            end;    
            
            
        end    
    end;
    
end

%=============================================================================================================================
%=============================================================================================================================
%=============================================================================================================================


function S = getS_named(S,varargin)
        [val,varargin]=parsem('1_all',varargin); if(val); [~,varargin]=parsem('parse_one',varargin,1,'set'); end;
        [val,varargin]=parsem('1_rand',varargin); if(val);                  S(end+1,:)=randS(1); end;
        [val,varargin]=parsem('1_143',varargin); if(val);                   S{end+1,2}=2; S{end,1}=1/4*[1 4 3]'; S{end,3}=[0 1]; S{end,end}='1_143_scaled'; end; %just for testing the construction of matrices
        [val,varargin]=parsem('1_1133',varargin); if(val);                  S{end+1,2}=2; S{end,1}=1/4*[1 1 3 3]'; S{end,end}='1_1133'; end; %dim U \neq dim V_0
        [val,varargin]=parsem('1_ternary_C1',varargin); if(val);            S{end+1,2}=3; S{end,1}=1/9*[1 3 6 7 6 3 1]'; S{end,end}='1_ternary_C1'; end; %ternary, C1
        [val,varargin]=parsem('1_unconnected_support',varargin); if(val);   S{end+1,2}=2; S{end,1}=1/2*[1  2 1  0 -1 -2 -1 0 1 2 1]'; S{end,end}='1_unconnected_support'; end;%closure(support) is not connected
        [val,varargin]=parsem('1_singular_scheme',varargin); if(val);       S{end+1,2}=2; S{end,1}=[0.5 0.5 0.5 0.5]';  S{end,end}='1_singular_scheme'; end;%Singular Scheme to starting seq [-1 1 -1 1 -1 1 -1 1]
        [val,varargin]=parsem('1_4point',varargin); if(val);                S{end+1,2}=2; S{end,1}=1/16*[-1 0 9 16 9 0 -1]'; S{end,end}='1_4point'; end;%interpolatory, n>1, n==4: 4point scheme   
        [val,varargin]=parsem('1_DD',varargin); if(val);                    S{end+1,2}=2; S{end,1}=1/256*[3 0 -25 0 150 256 150 0 -25 0 3]'; S{end,end}='1_DD'; end;% n>3, n=6: Dubuc Deslauriers
        [val,varargin]=parsem('1_strange_interpolatory',varargin); if(val); %#ok<ALIGN>
            z=-1/32; %strange interpolatory, -1<z<0
            S{end+1,2}=4; 
            S{end,1}=1/128*[8*z, 72*z, -72*z-15, -8*z-7, 9-24*z, 33-216*z, 216*z+110, 24*z+126, 24*z+126, 216*z+110, 33-216*z, 9-24*z, -8*z-7, -72*z-15, 72*z, 8*z]'; %strange interpolatory
            S{end,end}='1_strange_interpolatory';
            clear z;
        end;
        if(parsem('1_Hassan_Dodgson_3point',varargin));              S{end+1,2}=2; S{end,1}=1/16*[1 5 10 10 5 1]'; S{end,end}='1_Hassan_Dodgson_3point'; end; 
        
        if(parsem('1_devil_stairs',varargin));           S{end+1,2}=3; S{end,1}=1/2*[1 1 2 1 1]'; S{end,end}='1_devil_stairs'; end;
        if(parsem('1_balanced_ternary',varargin));       S{end+1,2}=3; S{end,3}=[-1 0 1]; S{end,end}='1_balanced_ternary'; end;
        
        
        if(parsem('1_Hassan-Dodgson-balanced_ternary',varargin));     S{end+1,2}=3; S{end,1}= 1/9*[-1 0 2 8 9 8 2 0 -1]'; S{end,end}='1_HD_ternarybalanced_ternary';  end;         
        
        if(parsem('1_spline_binary-0',varargin) || parsem('1_11',varargin));    S{end+1,2}=2; S{end,1}=[1 1]'; S{end,end}='1_spline_binary_0'; end;
        if(parsem('1_spline_binary-1',varargin) || parsem('1_121',varargin));    S{end+1,2}=2; S{end,1}=[1/2 1 1/2]'; S{end,end}='1_spline_binary_1'; end;
        if(parsem('1_spline_binary-2',varargin) || parsem('1_1331',varargin));    S{end+1,2}=2; S{end,1}=1/4*[1 3 3 1]'; S{end,end}='1_spline_binary_2'; end;
        if(parsem('1_spline_binary-3',varargin) || parsem('1_14641',varargin));    S{end+1,2}=2; S{end,1}=1/8*[1 4 6 4 1]'; S{end,end}='1_spline_binary_3'; end;

        if(parsem('1_lizhang_combined_ternary_5point',varargin));       
            [param,varargin]=parsem('1_lizhang',varargin,[]);
            if(isempty(param)); a=-1/243; b=-7/243; c=-2/81; d=-1/81; 
            elseif(size(param,2)==1); d=param(1); b=-7*d/3; c=2*d; a=d/3; 
            elseif(size(param,2)==2); b=param(1); d=param(2); c=2*d; a=(-b+c+d)/2;
            elseif(size(param,2)==3); b=param(1); c=param(2); d=param(3); a=(-b+c+d)/2;
            elseif(size(param,2)==4); a=param(1); b=param(2); c=param(3); d=param(4);            
            else; error('getS: ''1_lizhang'' needs a vector of zero to 4 entries as second argument'); 
            end 
            S{end+1,2}=3; 
            S{end,1}=1/9*convm([1 1 1],[1 1 1],[-9*a   1/9-9*d+18*a   1/3-9*a-9*c+18*d   2/3-9*b+18*c+9*d   7/9+18*b-18*c-36*d   2/3-9*b+18*c+9*d   1/3-9*a-9*c+18*d   1/9-9*d+18*a   -9*a])';
            S{end,end}='1_lizhang_combined_ternary_5point'; 
        end;
        
        
        if(parsem('1_paper1',varargin)); S{end+1,2}=2; S{end,1}=[3/8 1 3/4 0 -1/8]'; S{end,3}=[0 1]; S{end,end}='1_paper1'; end; %fuer example in paper
        if(parsem('1_three_disjoint_Om',varargin)); S{end+1,2}=-2; S{end,1}=[1 0 0 1]'; S{end,3}=[0 3]; S{end,end}='1_three_disjoint_Om'; end % Example where three smallest disjoint invariant sets Om exist: Om1=[0], Om2=[-1 2]; Om3=[ -2 1];
        if(parsem('1_two_disjoint_Om',varargin)); S{end+1,2}=2; S{end,1}=sequence([1/2 0 0   1  0 0   1/2]',0); S{end,3}=[ 0 3]; S{end,end}='1_two_disjoint_Om'; end; % Example where two smallest disjoint invariant sets Om exist: Om1=[0 3], Om2=[-2 -1 1 2 4 5]; Furthermore, JSR(T(Om1)|V_0)=1/2, JSR(T(Om2)|V_0)=1
        if(parsem('1_daubechies',varargin)); N=parsem('1_daubechies',varargin,0); S{end+1,2}=2; S{end,1}=double(daubechiesmask(N)).'; S{end,3}=[ 0 1]; end; %Daubechies wavelet
        if(parsem('1_unbounded_tile',varargin)); for i=1:9;  S{end+1,2}=3; S{end,3}=[0 3*i^i-1]; S{end,1}=[1 1 1]'; S{end,end}=['1_unbounded_tile (' num2str(i) ')']; end; end; %unbounded tile, no digit set
        if(parsem('1_exponential',varargin)); for i=0:15; S{end+1,2}=2; S{end,1}=[1 exp(2^(-i-1))]'; S{end,end}=['1_exponential (' num2str(i) ')']; end; end; %blf(1:15,'1_exp','nocheck','start',[exp(0) exp(1) -exp(2)]'); %nice example %asymptotic sum-rules
        if(parsem('1_cantor',varargin)); S{end+1,2}=3; S{end,3}=[0 2]; S{end,end}='1_cantor'; end;   %Tile: Cantor set %no full masks sets
        
        %no sum-rules
        if(parsem('1_cex_uniquesol',varargin)); 
            %two non-zero limit functions. the second consists again of two functions, which converge in the C_1 Norm. 
            %c=blf(15,'1_cex_uniquesol'); plot(c(1:2:end)); plot(c(2:2:end)); 
            %plot(diff(c(1:2:end),1)); plot(diff(c(2:2:end),1)); 
            %plot(diff(c(2:4:end),1)); plot(diff(c(4:4:end),1));             
            S{end+1,2}=2;  S{end,1}= 2/12*[1 1 2 1 2 1 2 1 1]'; S{end,3}=[0 1]; S{end,end}='1_cex_uniquesol';
        end;   
        
        %=====================================================
        %Dimension 2
        %=====================================================
        [~,varargin]=parsem('parse_one',varargin); 
        if(parsem('2_all',varargin)); [~,varargin]=parsem('parse_one',varargin,1,'set'); end;
        if(parsem('2_rand',varargin)); S(end+1,:)=randS(2); end;
        
        if(parsem('2_exlang',varargin));      S{end+1,2}=[2 0; 0 3]; S{end,1}=1/6*[1 1 3 2 1; 2 4 6 4 2; 1 3 3 2 1]; S{end,end}='2_exlang'; end;
        
        if(parsem('2_sierp',varargin));      S{end+1,2}=[2 0; 0 2]; S{end,1}=[0 1 0;1 1 0;0 0 1]; S{end,3}=[0 0; 1 0; 0 1; 1 1]';  S{end,end}='2_sierp'; end; %sum-rules, but irreducible symbol %blf is Sierpinski Triangle
        if(parsem('2_rqj43',varargin));      S{end+1,2}=[1 -1; 1 1]; S{end,1}=1/32*[0 -1 0 -1 0; -1 0 10 0 -1; 0 10 32 10 0; -1 0 10 0 -1; 0 -1 0 -1 0]; S{end,end}='2_rqj43'; end;%Rong Qing Jia p656, Example 4.3
        if(parsem('2_maria',varargin));      S{end+1,2}=[1 3; 1 0]; S{end,3}=[0 0; 1 0; 2 0]'; S{end,end}='2_maria'; end; %Tile: von Example von Maria
        if(parsem('2_M2102',varargin));   S{end+1,2}=[2  1;  0 2]; S{end,1}=1/4*[0 0 0 0; -2 -1 4 3; -4 -2 8 6; -2 -1 4 3]; S{end,end}='2_M2102'; end; %support phi: for example in appendix
        if(parsem('2_not_jointly_expanding',varargin));     
            S{end+1,2}=[2 -1; 2 0]; S{end,1}=1/6*[0 1;0 3;2 2;3 0;1 0]; S{end,end}='2_not_jointly_expanding (1)'; 
            S{end+1,2}=[0 -1;2 2];  S{end,1}=1/4*[1 1 0 0;0 2 2 0;0 0 1 1];  S{end,end}='2_not_jointly_expanding (2)'; 
        end;  %two matrices which are not jointly expanding
        if(parsem('2_2_times_2',varargin));       S{end+1,2}=[2  0;  0  2]; S{end,3}=[0 0; 1 0; 0 1; 1 1]'; S{end,end}='2_2_times_2'; end;   %Tile: 2x2 
        if(parsem('2_3_times_3',varargin)); S{end+1,2}=[3  0;  0  3]; S{end,3}=[0 0; 1 0; 2 0; 0 1; 1 1; 2 1 ; 0 2; 1 2; 2 2;]'; S{end,end}='2_3_times_3'; end;  %Tile: 3x3
        if(parsem('2_Cha2017',varargin));    S{end+1,2}=[2 1; 1 -1]; S{end,3}=[0 0; 1 0; 2 0]'; S{end,1}=1/4*[0 2 0; 2 4 1; 0 3 0]; S{end,end}='2_Cha2017'; end; %Example from Cha2017
        if(parsem('2_frayed_squares',varargin));   i=1; S{end+1,2}=[3 0; 0 3]; S{end,3}=[0 0; 1 0; 2 0; 0 1; 0 2; 1 2; 2 1; 2 2; 3*i+1 3*i+1]'; S{end,end}='2_frayed_squares';   end; %Ausgefranste Quadrate, unbounded
        if(parsem('2_frayed_squares_5',varargin));   i=5; S{end+1,2}=[3 0; 0 3]; S{end,3}=[0 0; 1 0; 2 0; 0 1; 0 2; 1 2; 2 1; 2 2; 3*i+1 3*i+1]'; S{end,end}='2_frayed_squares_5'; end; %Ausgefranste Quadrate, unbounded
        if(parsem('2_frayed_squares_n',varargin));   n=10; for i=1:n; S{end+1,2}=[3 0; 0 3]; S{end,3}=[0 0; 1 0; 2 0; 0 1; 0 2; 1 2; 2 1; 2 2; 3*i+1 3*i+1]'; S{end,end}=['2_frayed_squares_n (' num2str(i) ')']; end;  end; %Ausgefranste Quadrate, unbounded
        if(parsem('2_unbd_dilation',varargin));
            %Example for unbounded matrices with bounded digits
            %============================================================
            %Diese Matrizen sind nicht jointly expanding. Alle Matrizen haben DET=2, und das gleiche Digit set, Eigenwerte fuer alle Matrizen: I/Sqrt[DET], 
            %Es scheint als wuerden sie zumindest in der Reihenfolge M1,M2,M3,M4,... expanding sein, %fuer DET=2 
            %Attractor is immer unabhaengig von DET
            %Digit set sollte fuer DET=2 stimmen, fuer hoehere DET wahrhscheinlich
            %auskommentierter Code dient zum Testen der Matrizen
                  MAX=10; DET=2; 
                  for k=0:MAX; 
                      %k=k*0.1;
                      S{end+1,2}=[k -k^2-DET; 1 -k]; 
                      if(DET>0); S{end,3}=[0:-1:-DET+1;zeros(1,DET)]; 
                      else; S{end,3}=[0:-DET-1;zeros(1,-DET)];
                      end;
                      %S{end,1}=1/2*[1 1; 1 1];
                      S{end,end}=['2_unbd_dilation (' num2str(k) ')'];
                  end; %
            %for k=1:MAX; M{k}=inv(M{k}); end;
            %BEGIN=4; PROD=eye(2);
            %for k=BEGIN:MAX; PROD=PROD*M{k}; end;
            %max(abs(eig(PROD)))
            %clear MAX DET BEGIN PROD;
        end;
        
        if(parsem('2_loop',varargin));       S{end+1,2}=[1 2; -2 -1];  S{end,3}=[0 0; 1 0; 1 -1]'; S{end,end}='2_loop';  end;  %Tile: loop
        if(parsem('2_butterfly',varargin));  S{end+1,2}=[2 0; 0 2]; S{end,1}=1/16*[0 0 0 0 -1 -1 0;0 0 -1 0 2 0 -1;0 -1 2 8 8 2 -1;0 0 8 16 8 0 0;-1 2 8 8 2 -1 0; -1 0 2 0 -1 0 0;0 -1 -1 0 0 0 0];  S{end,end}='2_butterfly'; end;%butterfly
        if(parsem('2_twindragon',varargin)); S{end+1,2}=[1  1; -1  1]; S{end,3}=[0 0; 1 0]';  S{end,end}='2_twindragon'; end;  %Tile: TwinDragon
        if(parsem('2_flash',varargin));              S{end+1,2}=[1  3;  1  0]; S{end,3}=[0 0; 1 0; 2 0]';  S{end,end}='2_flash'; end;   %Tile: Flash 
        if(parsem('2_flflash_ru',varargin));         S{end+1,2}=[1  2;  1 -1]; S{end,3}=[0 0; 1 0; 2 0]';  S{end,end}='2_flflash_ru';end;   %Tile: fractalless Flash/Paralellogram: right up
        if(parsem('2_fltwindragon_ru',varargin));    S{end+1,2}=[1  1;  1 -1]; S{end,3}=[0 0; 1 0]';  S{end,end}='2_fltwindragon_ru'; end;   %Tile: fractalless TwinDragon/Paralellogram: right up
        if(parsem('2_fldragon_rd',varargin));        S{end+1,2}=[1  1;  1 -1]; S{end,3}=[0 0; 0 1]';  S{end,end}='2_fldragon_ru'; end;   %Tile: Paralellogram: right down
        if(parsem('2_fldragon_ld',varargin));        S{end+1,2}=[1  1;  1 -1]; S{end,3}=[0 0; -1 0]'; S{end,end}='2_fldragon_ld'; end;   %Tile: Paralellogram: left down 
        if(parsem('2_spline_binary_0',varargin));    S{end+1,2}=[2 0; 0 2]; S{end,1}= [1 1;1 1];  S{end,end}='2_spline_binary_0'; end;
        if(parsem('2_spline_binary_1',varargin));    S{end+1,2}=[2 0; 0 2]; S{end,1}= 1/2*[0 1 1;1 2 1; 1 1 0];  S{end,end}='2_spline_binary_1'; end;
        if(parsem('2_spline_binary_2',varargin));    S{end+1,2}=[2 0; 0 2]; S{end,1}= 1/8*[0 1 2 1; 1 4 5 2; 2 5 4 1; 1 2 1 0];  S{end,end}='2_spline_binary_2'; end;
        if(parsem('2_spline_binary_3',varargin));    S{end+1,2}=[2 0; 0 2]; S{end,1}= 1/16*[0 0 1 2 1;0 2 6 6 2;1 6 10 6 1;2 6 6 2 0;1 2 1 0 0];  S{end,end}='2_spline_binary_3'; end;
        if(parsem('2_spline_binary_5',varargin));    S{end+1,2}=[2 0; 0 2]; S{end,1}= 1/1024*[0 0 0 0 1 4 6 4 1;0 0 0 4 20 40 40 20 4;0 0 6 40 106 144 106 40 6;0 4 40 144 260 260 144 40 4;1 20 106 260 346 260 106 20 1;4 40 144 260 260 144 40 4 0;6 40 106 144 106 40 6 0 0;4 20 40 40 20 4 0 0 0;1 4 6 4 1 0 0 0 0];    S{end,end}='2_spline_binary_4'; end;  
        if(parsem('2_cex_tilearea',varargin));       S{end+1,2}=[-4 -2;5 1]; S{end,3}=[-5 5;-4 4;-3 3;-2 2;-1 1;0 0]';   S{end,end}='2_cex_tilearea'; end; %tile with standard digit set, but tile has area 2???
        
        %examples from papers
        %======================================
        
        if(parsem('2_V0neqV0bar_1',varargin));
            S{end+1,2}=[-3 -4; 4 4];
            S{end,1}=[0 0 0 1 0; 1/3 0 1 0 1/3; 0 1/2 0 0 0; 0 1/2 0 0 1/3];
            S{end,3}=[-3 3; -2 2 ; -1 1 ; 0 0]';
            %convergent scheme, where V0 ~= V0bar
        end;

        if(parsem('2_superomega',varargin));  %example from the thesis     
            S{end+1,2}=[1 -1; 1 1];
            S{end,1}=[0 1/4 0;1/4 1 1/4; 0 1/4 0]; S{end,3}=[0 0; 0 1]';             
            S{end,end}='2_superomega (1)';
            
            S{end+1,2}=[2 0; 0 2];            
            S{end,1}=[1/2 1/2 ;1 1; 1/2 1/2 ]; S{end,3}=[0 0; 0 1; 1 0; 1 1]'; 
            S{end,end}='2_superomega (2)';
        end;
        
        if(parsem('2_CDRT_ex531',varargin)); 
            S{end+1,2}=[2 0; 0 3]; 
            S{end,1}=[1/6 1/3 1/2 1/3 1/6; 1/3 2/3 1 2/3 1/3; 1/6 1/3 1/2 1/3 1/6]; 
            S{end,end}='2_CDRT_ex531'; 
        end;
        if(parsem('2_CDRT_ex532',varargin)); 
            S{end+1,2}=[2 0; 0 3]; 
            S{end,1}=[0 0 0 -1/48 -1/24 -1/16 -1/24 -1/48 0 0 0; 0 0 0 0 0 0 0 0 0 0 0;-2/81 -5/162 0 89/432 89/216 9/16 89/216 89/432 0 -5/162 -2/81;-4/81 -5/81 0 10/27 20/27 1 20/27 10/27 0 -5/81 -4/81;-2/81 -5/162 0 89/432 89/216 9/16 89/216 89/432 0 -5/162 -2/81;0 0 0 0 0 0 0 0 0 0 0;0 0 0 -1/48 -1/24 -1/16 -1/24 -1/48 0 0 0]; 
            S{end,end}='2_CDRT_ex532'; 
        end;
        if(parsem('2_CDRT_ex533',varargin)); S{end+1,2}=[2 0; 0 3]; 
            AA=[0 0 0 0 0 0 1/256 1/128; 0 0 0 0 0 0 0 0;0 0 0 1/324 5/1296 0 -241/6912 -241/3456;0 0 0 0 0 0 0 0;7/1458 4/729 0 -121/2916 -605/11664 0 20809/93312 20809/46656];
            BB=[7/729 8/729 0 -56/729 -70/729 0 280/729 560/729];
            CC=[3/256 0 -25/256 0 75/128 1 75/128 0 -25/256 0 3/256]';
            AA=[AA; BB; flipud(AA)];
            AA=[AA CC fliplr(AA)];
            S{end,1}=AA;
            S{end,end}='2_CDRT_ex533'; 
        end;    
        if(parsem('2_CDRT_ex541',varargin)); 
            S{end+1,2}=[2 0; 0 5]; 
            S{end,1}=1/10*[1 2 3 4 5 4 3 2 1; 2 4 6 8 10 8 6 4 2; 1 2 3 4 5 4 3 2 1];  
            S{end,end}='2_CDRT_ex541'; 
        end;
        if(parsem('2_CGRS',varargin)); 
            S{end+1,2}=[1 1; 1 -2];  S{end,1}=sequence(1/3*[1 2 3 2 1],[0; -2]); S{end,end}='2_CGRS (1)'; 
            S{end+1,2}=[2 -1; 1 -2]; S{end,1}=sequence(1/3*[1 2 3 2 1],[0; -2]); S{end,end}='2_CGRS (2)';             
        end;
        
        if(parsem('2_cool_1',varargin)); S{end+1,2}=[-1 -1;4 -1];  S{end,end}='2_cool_1'; end; %cool looking stuff
        if(parsem('2_identity',varargin)); S{end+1,2}=eye(2); S{end,end}='2_identity';end; %no dilation matrices
        if(parsem('2_cantor',varargin));         S{end+1,2}=[3 0; 0 3]; S{end,3}=[0 0 ; 2 0; 0 2; 2 2]'; end;   %Tile: Cantor set %no full masks sets
        if(parsem('2_McLure',varargin));     S{end+1,2}=[1 -sqrt(3); sqrt(3) 1]; S{end,3}=[0 0; 1 0; -1/2 sqrt(3)/2; -1/2 -sqrt(3)/2]';  S{end,end}='2_McLure';      end; %%non-integer scheme, self affine 4-tile, Mark McLure - Self affine tiles, p1
        if(parsem('2_tridragon',varargin));  S{end+1,2}=[3/2 -sqrt(3)/2; sqrt(3)/2 3/2]; S{end,3}=[0 0; 1 0; 1/2 sqrt(3)/2]';  S{end,end}='2_tridragon';  end;   %%non-integer scheme, Tridragon for hexagonal lattice
 
        %=====================================================
        [~,varargin]=parsem('parse_one',varargin); 
        if(parsem('4_all',varargin)); [~,varargin]=parsem('parse_one',varargin,1,'set'); end;
        if(parsem('4_cex_Pot97',varargin)); S{end+1,2}=[0 1 0 0; 0 0 1 0; 0 0 -1 2;-1 0 -1 1];  S{end,end}='4_cex_Pot97'; end; %dilation matrix which has no digit set which generates a tile [Pot97]

end

function Srand = randS(dim)
        MM=0;
        while(true);
            while(ceil(min(abs(eig(MM))))<=1.1 || abs(det(MM))>6 || abs(det(MM))<=1.5); 
                MM=randi(6,dim)-3; 
            end; 
            aa=randi(5,[5*ones(1,dim) 1])-1;
            Srand=normalizeS({aa,MM,[],[num2str(dim) '_rand']},'verbose',0);         
            if(~isempty(Srand)); 
                break
            end;
        end
        2;
end

function [S,varargin] = getS_namevalue(S, varargin)
    [a,varargin]=parsem('a',varargin,[]); [a,varargin]=parsem('mask',varargin,a);     S{1,1}=a;
    [M,varargin]=parsem('M',varargin,[]); [M,varargin]=parsem('dilation',varargin,M); S{1,2}=M;
    [D,varargin]=parsem('D',varargin,[]); [D,varargin]=parsem('digit',varargin,D);    S{1,3}=D;
    [n,varargin]=parsem('n',varargin,[]); [n,varargin]=parsem('name',varargin,n);     S{1,end}=n;
end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 