function [ S ] = normalizeS(varargin)
% [ S ] = normalizeS(S, [options])
% Normalizes the values of the masks a in S.
%
%Input: 
%   S               Cell array of subdivision operators
%
% Options: 
%   'equal'         entries of the mask corresponding to representatives of a digit set are equal and sum up to one
%   'gaussian'      entries of the mask are preserved, but gaussian like scaled
%   'scale'         entris are just scaled (Default)
%   'verbose',val   sets the verbose level
%
% Output:
%   S               Cell array of subdivision operators with normalized masks
%
% E.g.: S=normalizeS({[1 4 6 4 1]',2}); vdisp(S);
%
% Written by: tommsch, 2017



S=varargin{1};
S=getS(S,'nocheck');
equal=parsem('equal',varargin);
gaussian=parsem('gaussian',varargin);
verbose=parsem('verbose',varargin,1);
scale=parsem('scale',varargin);


sizeS=size(S,1);
for i=1:sizeS
    a=shrink(S{i,1});
    M=S{i,2};
    suppa=a.supp; %kth support
    CENTER=sum(suppa,2)/size(suppa,2); %center of mass of all entries
    Cl=constructdigit(M,'ZZ',suppa,'classify','sym'); %Class
    [C, ~, ic] = unique(Cl','rows');
    C=C';
    if(size(C,2)~=round(abs(det(M)))); 
        vprintf('Cannot normalize mask, since the support of the mask is to small for index %i.\n',i,'cpr','err','imp', [1,verbose]); 
        S={};
        return;
    end;
    anew=zerosm(size(a)); %new mask
    MIN=min(suppa,[],2); %support of the new mask
    MAX=max(suppa,[],2);

    %weight matrix
    if(~equal); 
        L=cell(1,size(MIN,1));
        for j=1:numel(L);  
            L{j}=(MIN(j):MAX(j))-CENTER(j);
            L{j}=exp(-abs(L{j}));
        end
        W=convm(L{:},'outer');
    else
        W=onesm((MAX-MIN)'+1);
    end;

    for j=1:size(C,2) %go through all classes
        submask=suppa(:,ic==j);
        %num=size(submask,2);
        asub=characteristic(submask,'amin',MIN,'amax',MAX).*W;

        if(gaussian); asub=asub.*ak; 
        elseif(scale); asub(logical(asub))=ak(logical(asub));
        end
        asub=asub/sum(asub(:));
        if(anym(~isfinite(asub)))
            vprintf('normalizeS: Cant normalize mask for index %i.\n',i,'cpr','err','imp',[1,verbose]); 
            S={};
            return;
        end;

        if(any(size(anew)-size(asub)));
            vprintf('normalizeS: Wrong dimension for index %i:.\n',i,'cpr','err','imp',[1,verbose]); 
            S={};
            return;
        end;
        anew=anew+asub;
    end
    S{i,1}.c=anew; 
end


end

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