function setuptjsr(flag)
% setuptjsr( [flag] )
% Demonstrates how to use the tjsr-packge and tests if the tjsr-package works correctly
% if flag is 1, then the functions in the package are tested.
%
% See also: testsubdivision, tjsr
%
% Written by: tommsch, 2018

%#ok<*NOPRT>
%#ok<*ASGLU>
%#ok<*NASGU>

if(nargin>=1 && flag); selftesttjsr(0); return; end;

errorstring=vprintf('Either start the function with argument <1> or read the source code of this file.\n'); 
return; 
2; %#ok<UNRCH>

%To see how the tjsr package works, see also testsubdivision

end

function errornum = selftesttjsr(verbose)
    if(nargin==0); verbose=0; end;
    errornum=0;
    errorstring='';
    try
        vprintf('The tjsr-package will be tested.\n','cpr',[.1 .4 .1]);
        vprintf('If there is some error or un-commented output, the test fails.\n\n','cpr',[.1 .4 .1]);
    catch
        fprintf('''vprintf'' broken.\n'); 
        errornum=errornum+1;
        return;
    end;
    
    vprintf('addcombination\n');
    val=addcombination({[1 1 2],[1]});
    if(~isequal(val,{[1 1 2],[1],[1 2],[2]})); errornum=errornum+1; errorstring=vprintf('ERROR: addcombination() may be broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('binarymatrix\n');
    val=binarymatrix(2, 2, 2^5+2^3+2^2+2^1);
    if(~isequal(val,{[0 1; 0 0],[1 1; 1 0]})); errornum=errornum+1; errorstring=vprintf('ERROR: binarymatrix() may be broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('blockjsr\n');
    if(~isequal(blockjsr([1 0],[.5 2]),[.5 2])); errornum=errornum+1; errorstring=vprintf('ERROR: blockjsr() is broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('chooseval\n');
    if(~islogical(chooseval(randn(1,100),[10 50]))); errornum=errornum+1; errorstring=vprintf('ERROR: chooseval() is broken.\n\n','cpr','err','str',errorstring); end;
    if(~isequal([1 0 0],chooseval([inf 0 nan],1))); errornum=errornum+1; errorstring=vprintf('ERROR: chooseval() is broken.\n\n','cpr','err','str',errorstring); end;
    if(~islogical(chooseval(randn(1,100),[10 50]))); errornum=errornum+1; errorstring=vprintf('ERROR: chooseval() is broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('codecapacity\n');
    figure;
    try
        codecapacity({[1 1 0],[1 -1 0]},'verbose',verbose,'plot',1);
        C=codecapacity({[1 1],[-1 -1]},'verbose',verbose);
    if(~isequal(C,{[0 1; 1 1],[1 1; 1 0]})); errornum=errornum+1; errorstring=vprintf('ERROR: codecapacity() may be broken.\n\n','cpr','err','str',errorstring); end;
    catch
        errornum=errornum+1; errorstring=vprintf('ERROR: codecapacity() is broken.\n\n','cpr','err','str',errorstring);
    end
    
    vprintf('computepolytopenorm\n');
    val=computepolytopenorm([1 1; 3 0]', [1 0; 0 1]', 1, 0, 2);
    if(norm(val-[2 3])>10e-12); errornum=errornum+1; errorstring=vprintf('ERROR: computepolytopenorm() is broken.\n\n','cpr','err','str',errorstring); end;    
    
    try
        vprintf('daubechiesmatrix\n');
        D21=daubechiesmatrix(2,'gugl','verbose',verbose); val1=max(cellfun(@(x) max(abs(eig(double(x)))),D21));
        D22=daubechiesmatrix(2,'jung','verbose',verbose); val2=max(cellfun(@(x) max(abs(eig(double(x)))),D22));
        if(norm(val1-val2)>10e-10); errornum=errornum+1; errorstring=vprintf('ERROR: daubechiesmatrix() is broken.\n\n','cpr','err','str',errorstring); end;
    catch
        errorstring=vprintf('ERROR: daubechiesmatrix() is broken. Probably the subdivision-package is not installed.\n\n','cpr','err','str',errorstring);
    end
    %vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    
    vprintf('estimatejsr\n');
    TT={[1 2; 0 1],[1 2; 1 0]};
    try
        val=estimatejsr(TT);
        if(val(1)-10e-9>2 || val(2)+10e-9<2); 
            errornum=errornum+1; 
            errorstring=vprintf('ERROR: estimatejsr() is broken.\n\n','cpr','err','str',errorstring);
        end
    catch
        errornum=errornum+1; errorstring=vprintf('ERROR: estimatejsr() is broken.\n\n','cpr','err','str',errorstring);
    end
    %vprintf('There should be two numbers text output.\n','cpr',[.1 .4 .1]);
    
    try
        vprintf('estimatepolytopenorm\n');
        VV=randn(3,10); pp=randn(3,10);
        [normval1,bd1]=estimatepolytopenorm(VV, VV, [], 11);
        [normval2,bd2]=estimatepolytopenorm(pp, pp, [], 1);
    catch
        errornum=errornum+1; errorstring=vprintf('ERROR: estimatepolytopenorm() is broken.\n\n','cpr','err','str',errorstring);
    end

    vprintf('findsmp\n');
    c1=findsmp({[1 -1; 3 -2], [1 3; -1 -1]},'bruteforce','maxsmpdepth',5,'verbose',verbose);
    c2=findsmp({[1 -1; 3 -2], [1 3; -1 -1]},'gripenberg','maxsmpdepth',5,'verbose',verbose);
    c3=findsmp({[1 -1; 3 -2], [1 3; -1 -1]},'genetic','maxsmpdepth',5,'verbose',verbose-1);
    [c4,nc,info]=findsmp({[1 -1; 3 -2], [1 3; -1 -1]},'maxsmpdepth',15,'verbose',verbose);
    %vprintf('There should be text output.\n','cpr',[.1 .4 .1]);
    if(~isequal(c4{1}, [1;2]) && ~isequal(c4{1}, [2;1])); errorstring=vprintf('ERROR: findsmp() broken.\n\n','cpr','err','str',errorstring); end;    
    
    vprintf('intersectinterval\n');
    val=intersectinterval([4 5],[1 4],[-inf 4]);
    if(~isequal(val,[4 4])); errorstring=vprintf('ERROR: intersectinterval() broken.\n\n','cpr','err','str',errorstring); end;    
    
    vprintf('invariantsubspace\n');
    [M,B]=invariantsubspace({[1 1 1; 1 0 1; 0 1 0], [1 -1 0; 0 2 0; 1 1 2]},'verbose',verbose);
    if(size(M,2)~=3); errorstring=vprintf('ERROR: invariantsubspace() broken.\n\n','cpr','err','str',errorstring); end;    
    
    vprintf('leadingeigenvector\n');
    [M,v0,v0s,mult,oo]=leadingeigenvector({[1 1 ; 0 1],[0 0; 1 0]},{[1 2]',[1]'},'verbose',verbose);
    
    %this tests makeorderinggraph
    vprintf('makeorderinggraph\n');
    figure;
    [~,~,finfo]=findsmp(tgallery('rand_gauss',2,2,'seed',10,'rho'),'maxsmpdepth',10,'plot','tree','N',2,'verbose',verbose);
    if(~isfield(finfo,'G'));  errorstring=vprintf('ERROR: findsmp() is broken.\n\n','cpr','err','str',errorstring); end;    
    vprintf('FIGURE.\n','cpr',[.4 .2 0]);
    figure;
    oo=[1 1 1 2 1 2 2 2 1 1;0 2 1 0 2 1 2 1 1 2;0 0 0 0 1 0 0 1 2 1]; makeorderinggraph(oo,'lableedge',0,'verbose',verbose);
    title('Ordering graph');
    vprintf('FIGURE.\n','cpr',[.4 .2 0]);
    
    vprintf('makepositive\n');
    val=makepositive([ 0 -1 2]);
    if(~isequal(val, [0 1 -2])); errorstring=vprintf('ERROR: makepositive() is broken.\n\n','cpr','err','str',errorstring); end;    
    
    vprintf('partitionatepolytope\n');
    figure;
    [chain, MST]=partitionatepolytope(randn(2,100),1,0); 
    title('Partition of points in the plane');
    %vprintf('There should be text output.\n','cpr',[.1 .4 .1]);
    vprintf('FIGURE.\n');

    vprintf('preprocessmatrix\n');
    preprocessmatrix({[-1 2; 2 3],[-1 2; 2 3]},'verbose',verbose);
    if(~isequal(preprocessmatrix({-1},'verbose',verbose),{1})); errorstring=vprintf('ERROR: preprocessmatrix() is broken.\n\n','cpr','err','str',errorstring); end;  
    
    vprintf('reducelength\n');
    val=reducelength({[2 1 2 2 1 2],[3 1]});
    if(~isequal(val,{[1 2 2],[1 3]})); errorstring=vprintf('ERROR: reducelength() broken.\n\n','cpr','err','str',errorstring); end;    
    
    vprintf('removecombination\n');
    val=removecombination({[1 1 2],[1]});
    if(size(val,2)~=2 || ~searchincellarray(2,val,1) || ~searchincellarray(1,val,1)); errorstring=vprintf('ERROR: removecombination() is broken.\n\n','cpr','err','str',errorstring); end;    
   
    vprintf('tbuildproduct_fast\n');
    val=tbuildproduct_fast({2 4 5},[1 0 3]);
    if(~isequal(val,10)); errorstring=vprintf('ERROR: tbuildproduct_fast() is broken.\n\n','cpr','err','str',errorstring); end;
    
    %testtjsr %this file
    
    vprintf('tgallery\n');
    
    
    vprintf('tjsr\n');
    tjsrflag=true;
    tjsr(tgallery('rand_pm1',3,2,'seed',100),'ordering',{[1 2 2 2 2 2 2 2]'},'plot','info_rho','verbose',verbose);
    if(~isequal(tjsr({[1],[2]},'verbose',verbose),2)); errornum=errornum+1; tjsrflag=false; errorstring=vprintf('ERROR: tjsr() is broken.\n','cpr','err','str',errorstring); end;
    if(norm(tjsr({[1 -1; 0 1],[0 0; 1 0]},'verbose',verbose)-1.31950791077)>10e-10);  errornum=errornum+1; tjsrflag=false; errorstring=vprintf('ERROR: tjsr() is broken.\n','cpr','err','str',errorstring); end;
    figure;
    AA={[1 1 0; 1 0 1; 1 0 0],[1 0 1; 1 1 0; 0 1 0]};
    [JSR,info]=tjsr(AA,'plot','polytope','proof',1,'verbose',verbose);
    if(norm(JSR-1.83928675521)>1e-10); errornum=errornum+1; tjsrflag=false; errorstring=vprintf('ERROR: tjsr() is broken.\n','cpr','err','str',errorstring); end;
    vprintf('FIGURE,\n','cpr',[.4 .2 0]);
    if(info.info.errorcode~=-10 || numel(JSR)>1); errornum=errornum+1; tjsrflag=false; errorstring=vprintf('ERROR: tjsr() may be broken. Test tjsr() again yourself.\n\n','cpr','err','str',errorstring); end;
    B={[1 1 0; -1 0 -1; 1 0 0],[1 0 -1; 1 -1 0; 0 1 0]};
    [JSR,info]=tjsr(B,'verbose',verbose);
    if(norm(JSR-1.39406936116)>1e-10); errornum=errornum+1; tjsrflag=false; errorstring=vprintf('ERROR: tjsr() is broken.\n','cpr','err','str',errorstring); end;    
    
    %tjsr_ %these files are not tested
    
    vprintf('tliftproduct\n');
    val=unique(cell2mat(tliftproduct({2 3 5}, 2)));
    if(~isequal(val, [1 2 3 4 5 6 9 10 15 25])); errorstring=vprintf('ERROR: tliftproduct() us broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('tliftsemidefinite\n');
    val=tliftsemidefinite({[1 1;0 1] [1 0; 1 0]}, 2);
    if(~isequal(val, {[1 4 4 2 4 1;0 1 2 1 3 1;0 0 1 0 2 1;0 0 0 1 2 1;0 0 0 0 1 1;0 0 0 0 0 1]; [ones(6,1) zeros(6,5)]})); errorstring=vprintf('ERROR: tliftsemidefinite() broken.\n\n','cpr','err','str',errorstring); end;
    
    vprintf('trho\n');
    MM=tgallery('rand_gauss',10,2,'rho');
    if(norm(trho(MM{1})-1)>10e-12); errorstring=vprintf('ERROR: trho() may be broken.\n\n','cpr','err','str',errorstring); end;
    
    %Gurobi functions
    vprintf('Gurobi\n');
    try      
        evalc('gurobi_setup');
        model.A = sparse([1 1 0; 0 1 1]);
        model.obj = [1 2 3];
        model.modelsense = 'Max';
        model.rhs = [1 1];
        model.sense = [ '<' '<'];
        params.outputflag=0;

        gurobiresult = gurobi(model,params);
        if(~isequal(gurobiresult.x,[ 1 0 1].')); 
            errorstring=vprintf('ERROR: Gurobi seems to be installed, but does not work.\n','cpr','err','str',errorstring);
            errorstring=vprintf('ERROR: The package will work nevertheless, but magnitudes slower. It is strongly recommended to fix this issue.\n\n','cpr','err','str',errorstring);
        end
       % vprintf('There should be a lot of text output.\n\n','cpr',[.1 .4 .1]);
    catch
        errorstring=vprintf('ERROR: Gurobi is broken / not installed.\n','cpr','err','str',errorstring);
        errorstring=vprintf('ERROR: The package will work nevertheless, but magnitudes slower. It is strongly recommended to install Gurobi.\n\n','cpr','err','str',errorstring);
    end
    
    %Sedumi functions
    vprintf('sedumi\n');
    try
        load arch0
        pars.fid=0;
        [x,y,info] = sedumi(At,b,c,K,pars);
        %vprintf('There should be a lot of text output.\n\n','cpr',[.1 .4 .1]);
    catch        
        errorstring=vprintf('ERROR: SeDuMi is broken / not installed.\n','cpr','err','str',errorstring);
        errorstring=vprintf('ERROR: This does not mean, that the packages do not work, since the packages use only a subset of functions of SeDuMi.\n\n','cpr','err','str',errorstring);
    end
    
    %JSR-toolbox functions
    vprintf('JSR-toolbox\n');
    try
        AA{1}=[1 2; -2 1];AA{2}=[1 2; -1 1];
        opts = jsrsettings('logfile',0,'verbose',verbose);
        valjsr=jsr(AA,opts);
        valtjsr=tjsr(AA,'verbose',verbose);
        if(norm(min(valjsr-valtjsr))>0.1); 
            errorstring=vprintf('ERROR: JSR-toolbox may be broken.\n','cpr','err','str',errorstring);
        end
        %vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    catch
        errorstring=vprintf('ERROR: The JSR toolbox is broken / not installed.\n','cpr','err','str',errorstring);
        errorstring=vprintf('ERROR: This does not mean, that the packages do not work, since the packages use only a subset of functions of The JSR toolbox.\n\n','cpr','err','str',errorstring);
    end
    
    if(~isempty(errorstring));
        vprintf(errorstring,'cpr','err');
    end
    if(tjsrflag)
        vprintf('GOOD NEWS: The most important function tjsr should work.\n','cpr',[0 .4 0]);
    end
end

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

