
function [lebs,dets,conds,errs,lagrs,l2opts]=do_stats_sh(deg,pts,timing,print_stats)

if nargin < 3
    timing=0;
end

if nargin < 4
    print_stats=1;
end

if nargin < 5
    t=[];
end

if nargin < 6
    table=[];
end

errs=0;

%-------------------------------------------------
% CHECKING IF THE POINTS ARE IN THE UNIT DISK.
% SOME MINIMAL CORRECTIONS.
%-------------------------------------------------

if print_stats == 1
    fprintf('\n \n \t case %2.0f ',deg);
end
x=pts(:,1); y=pts(:,2);


s = (x >= -1) +  (y >= -1) + (x <= 1) +  (y <= 1) -4;

if norm(s) == 0
    if print_stats == 1
        fprintf('\n \n \t %% PASSED (I).');
    end
else
    fprintf('\n \n \t %% CHECKING MORE, SOME FAILURE.');

    ix=find(x < -1);
    pts(ix,1)=-1.000000000000000000000000000000e+00;

    ix=find(x > 1);
    pts(ix,1)=1.000000000000000000000000000000e+00;

    iy=find(y < -1);
    pts(iy,2)=-1.000000000000000000000000000000e+00;

    iy=find(y > 1);
    pts(iy,2)=1.000000000000000000000000000000e+00;

    xp=pts(:,1); yp=pts(:,2);

%    ss=find(xp + yp > 1);
%    for index=1:length(ss)
%        ssloc=ss(index); xx=xp(ssloc); yy=yp(ssloc);
%        if xx > yy
%            pts(ssloc,2)=1-xx;
%        else
%            pts(ssloc,1)=1-yy;
%        end
%    end
    xp=pts(:,1); yp=pts(:,2);

    sp = (xp >= -1) +  (yp >= -1) + (xp <= 1) +  (yp <= 1) -4;
    if norm(sp) == 0
        if print_stats == 1
            fprintf('\n \n \t %% PASSED (II).');
        end
    else
        if print_stats == 1
            fprintf('\n \n \t %% CHECK AFTER POSTPRODUCTION');
        end
        errs=1;
        % [(1:length(x))' (xp >= 0)  (yp >= 0) (xp <= 1)  (yp <= 1) (xp + yp <= 1)]
    end
end

%---------------------------
% COMPUTING BASIC CONSTANTS.
%---------------------------

V_pts=chebvand2d(deg,pts);
dets=det(V_pts);

pts_leb=tens_cub_sq(250);
V_leb= chebvand2d(deg,pts_leb);
VV=V_pts'\V_leb';
lebs=norm(VV,1);
conds=condest(V_pts);
lagrs=max(max(abs(VV)));
l2opts=sum(diag(inv(V_pts'*V_pts)));

%-----------------------
% DISPLAYING CONSTANTS.
%-----------------------
if print_stats == 1
    fprintf('\n \n \t %% DEGREE: %3.0f',deg);
    fprintf('\n \t %% POINTS: %6.0f',size(pts,1));
    fprintf('\n \t %% LEBESGUE CONSTANT: %2.5e',lebs);
    fprintf('\n \t %% DETERMINANT: %2.5e',dets);
    fprintf('\n \t %% CONDITIONING: %2.5e',conds);
    fprintf('\n \t %% MAX LAGRANGE POLYNOMIALS: %2.5e',lagrs);
    fprintf('\n \t %% CHEN-BABUSKA L2 OPTIMALITY: %2.5e',l2opts);
    fprintf('\n \t %% VANDERMONDE: ORTHONORMAL DUBINER LEGENDRE.');
    if timing > 0
        minuti=floor(timing/60);
        secondi=floor(timing-minuti*60);
        fprintf('\n \t %% CPUTIME (MINUTES:SECONDS): %4.0f : %2.0f.\n',minuti,...
            secondi);
    else
        fprintf('\n');
    end
end

%-----------------------
% DISPLAYING POINTS.
%-----------------------
if print_stats == 1

%    if length(table) >0
%
%        fprintf('\n \t table=[ \n');
%        for kk=1:length(table)
%            fprintf('\t %2.0f',table(kk));
%        end
%        fprintf('\n \t ]; \n ');
%    end


%    fprintf('\n \t t=[');
%    if length(t) >0
%        for kk=1:size(t,1)
%            fprintf('\n \t %1.30e',t(kk,1));
%        end
%    end
%    fprintf('\n \t ]; \n ');


%    if size(pts,2) < 3
%        zz=1-pts(:,1)-pts(:,2);
%
%        pts=[pts zz];
%    end
    fprintf('\n \t pts=[');
    for kk=1:size(pts,1)
        fprintf('\n \t %1.15e %1.15e',pts(kk,1),pts(kk,2));
    end
    fprintf('\n \t ]; \n ');
end
