function [x, c, funVal, ValueL]=sgLogisticR(A, y, z, opts)
%
%%
% Function sgLogisticR
%      Logistic Loss with the
%           sparse group Lasso Regularization
%
%% Problem
%
%  min  f(x,c) = - sum_i weight_i * log (p_i)
%                + z_1 \|x\|_1 + z_2 * sum_j w_j ||x_{G_j}||
%
%  a_i denotes a training sample,
%      and a_i' corresponds to the i-th row of the data matrix A
%
%  y_i (either 1 or -1) is the response
%
%  p_i= 1/ (1+ exp(-y_i (x' * a_i + c) ) ) denotes the probability
%
%  G_j's are nodes with tree structure
%
%  For this special case,
%    we have L1 for each element
%    and the L2 for the non-overlapping group
%
%  The tree overlapping group information is contained in
%  opts.ind, which is a 3 x nodes matrix, where nodes denotes the number of
%  nodes of the tree.
%  opts.ind(1,:) contains the starting index
%  opts.ind(2,:) contains the ending index
%  opts.ind(3,:) contains the corresponding weight (w_j)
%
%% Input parameters:
%
%  A-         Matrix of size m x n
%                A can be a dense matrix
%                         a sparse matrix
%                         or a DCT matrix
%  y -        Response vector (of size mx1)
%  z -        Regularization parameter (z >=0)
%  opts-      Optional inputs (default value: opts=[])
%             !!We require that opts.ind is specified.!!
%
%% Output parameters:
%  x-         The obtained weight of size n x 1
%  c-         The obtained intercept (scalar)
%  funVal-    Function value during iterations
%
%% Copyright (C) 2009-2010 Jun Liu, and Jieping Ye
%
% You are suggested to first read the Manual.
%
% For any problem, please contact with Jun Liu via j.liu@asu.edu
%
% Last modified on February 19, 2010.
%
%% Related papers
%
% [1] Jun Liu and Jieping Ye, Moreau-Yosida Regularization for
%     Grouped Tree Structure Learning, NIPS 2010
%
%% Related functions:
%
%  sll_opts
%
%%

%% Verify and initialize the parameters
%%
if (nargin <4)
    error('\n Inputs: A, y, z, and opts (.ind) should be specified!\n');
end

[m,n]=size(A);

if (length(y) ~=m)
    error('\n Check the length of y!\n');
end

if (z(1)<0 || z(2)<0)
    error('\n z should be nonnegative!\n');
end

lambda1=z(1);
lambda2=z(2);

opts=sll_opts(opts); % run sll_opts to set default values (flags)

% restart the program for better efficiency
%  this is a newly added function
if (~isfield(opts,'rStartNum'))
    opts.rStartNum=opts.maxIter;
else
    if (opts.rStartNum<=0)
        opts.rStartNum=opts.maxIter;
    end
end

%% Detailed initialization
%% Normalization

% Please refer to the function 'sll_opts'
%                 for the definitions of mu, nu and nFlag
%
% If .nFlag =1, the input matrix A is normalized to
%                     A= ( A- repmat(mu, m,1) ) * diag(nu)^{-1}
%
% If .nFlag =2, the input matrix A is normalized to
%                     A= diag(nu)^{-1} * ( A- repmat(mu, m,1) )
%
% Such normalization is done implicitly.
%     This implicit normalization is suggested for the sparse matrix,
%                                    but not for the dense matrix.
%

if (opts.nFlag~=0)
    if (isfield(opts,'mu'))
        mu=opts.mu;
        if(size(mu,2)~=n)
            error('\n Check the input .mu');
        end
    else
        mu=mean(A,1);
    end

    if (opts.nFlag==1)
        if (isfield(opts,'nu'))
            nu=opts.nu;
            if(size(nu,1)~=n)
                error('\n Check the input .nu!');
            end
        else
            nu=(sum(A.^2,1)/m).^(0.5); nu=nu';
        end
    else % .nFlag=2
        if (isfield(opts,'nu'))
            nu=opts.nu;
            if(size(nu,1)~=m)
                error('\n Check the input .nu!');
            end
        else
            nu=(sum(A.^2,2)/n).^(0.5);
        end
    end

    ind_zero=find(abs(nu)<= 1e-10);    nu(ind_zero)=1;
    % If some values in nu is typically small, it might be that,
    % the entries in a given row or column in A are all close to zero.
    % For numerical stability, we set the corresponding value to 1.
end

if (~issparse(A)) && (opts.nFlag~=0)
    fprintf('\n -----------------------------------------------------');
    fprintf('\n The data is not sparse or not stored in sparse format');
    fprintf('\n The code still works.');
    fprintf('\n But we suggest you to normalize the data directly,');
    fprintf('\n for achieving better efficiency.');
    fprintf('\n -----------------------------------------------------');
end

%% Group & Others

% Initialize ind
if (~isfield(opts,'ind'))
    error('\n In sgLogisticR, the field .ind should be specified');
else
    ind=opts.ind;

    if (size(ind,1)~=3)
        error('\n Check opts.ind');
    end
end

% The parameter 'weight' contains the weight for each training sample.
% See the definition of the problem above.
% The summation of the weights for all the samples equals to 1.
if (isfield(opts,'sWeight'))
    sWeight=opts.sWeight;

    if ( length(sWeight)~=2 || sWeight(1) <=0 || sWeight(2) <= 0)
        error('\n Check opts.sWeight, which contains two positive values');
    end

    % we process the weight, so that the summation of the weights for all
    % the samples is 1.

    p_flag=(y==1);                  % the indices of the postive samples
    m1=sum(p_flag) * sWeight(1);    % the total weight for the positive samples
    m2=sum(~p_flag) * sWeight(2);   % the total weight for the positive samples

    weight(p_flag,1)=sWeight(1)/(m1+m2);
    weight(~p_flag,1)=sWeight(2)/(m1+m2);
else
    weight=ones(m,1)/m;             % if not specified, we apply equal weight
end

%% Starting point initialization

p_flag=(y==1);                  % the indices of the postive samples
m1=sum(weight(p_flag));         % the total weight for the positive samples
m2=1-m1;                        % the total weight for the positive samples

% process the regularization parameter
if (opts.rFlag==0)
    lambda=z;
else % z here is the scaling factor lying in [0,1]
    if (lambda1<0 || lambda1>1 || lambda2<0 || lambda2>1)
        error('\n opts.rFlag=1, and z should be in [0,1]');
    end

    % we compute ATb for computing lambda_max, when the input z is a ratio

    b(p_flag,1)=m2;  b(~p_flag,1)=-m1;
    b=b.*weight;

    % compute AT b
    if (opts.nFlag==0)
        ATb =A'*b;
    elseif (opts.nFlag==1)
        ATb= A'*b - sum(b) * mu';  ATb=ATb./nu;
    else
        invNu=b./nu;               ATb=A'*invNu-sum(invNu)*mu';
    end

    % compute lambda1_max
    temp=abs(ATb);
    lambda1_max=max(temp);

    lambda1=lambda1*lambda1_max;

    % compute lambda2_max(lambda_1)
    temp=max(temp-lambda1,0);

    if ( min(ind(3,:))<=0 )
        error('\n In this case, lambda2_max = inf!');
    end
    lambda2_max=computeLambda2Max(temp,n,ind,size(ind,2));

    lambda2=lambda2*lambda2_max;
end

% initialize a starting point
if opts.init==2
    x=zeros(n,1); c=log(m1/m2);
else
    if isfield(opts,'x0')
        x=opts.x0;
        if (length(x)~=n)
            error('\n Check the input .x0');
        end
    else
        x=zeros(n,1);
    end

    if isfield(opts,'c0')
        c=opts.c0;
    else
        c=log(m1/m2);
    end
end

% compute A x
if (opts.nFlag==0)
    Ax=A* x;
elseif (opts.nFlag==1)
    invNu=x./nu; mu_invNu=mu * invNu;
    Ax=A*invNu -repmat(mu_invNu, m, 1);
else
    Ax=A*x-repmat(mu*x, m, 1);    Ax=Ax./nu;
end

%% The main program

if (opts.mFlag==0 && opts.lFlag==0)

    bFlag=0; % this flag tests whether the gradient step only changes a little

    L=1/m; % the intial guess of the Lipschitz continuous gradient

    weighty=weight.*y;
    % the product between weight and y

    % assign xp with x, and Axp with Ax
    xp=x; Axp=Ax; xxp=zeros(n,1);
    cp=c;         ccp=0;

    %% The Armijo Goldstein line search schemes + accelearted gradient descent

    alphap=0; alpha=1;

    for iterStep=1:opts.maxIter
        % --------------------------- step 1 ---------------------------
        % compute search point s based on xp and x (with beta)
        beta=(alphap-1)/alpha;    s=x + beta* xxp;  sc=c + beta* ccp;

        % --------------------------- step 2 ---------------------------
        % line search for L and compute the new approximate solution x

        % compute As=A*s
        As=Ax + beta* (Ax-Axp);

        % aa= - diag(y) * (A * s + sc)
        aa=- y.*(As+ sc);

        % fun_s is the logistic loss at the search point
        bb=max(aa,0);
        fun_s= weight' * ( log( exp(-bb) +  exp(aa-bb) ) + bb );

        % compute prob=[p_1;p_2;...;p_m]
        prob=1./( 1+ exp(aa) );

        % b= - diag(y.* weight) * (1 - prob)
        b= -weighty.*(1-prob);

        gc=sum(b); % the gradient of c

        % compute g= AT b, the gradient of x
        if (opts.nFlag==0)
            g=A'*b;
        elseif (opts.nFlag==1)
            g=A'*b - sum(b) * mu';  g=g./nu;
        else
            invNu=b./nu;              g=A'*invNu-sum(invNu)*mu';
        end

        % copy x and Ax to xp and Axp
        xp=x;    Axp=Ax;
        cp=c;

        while (1)
            % let s walk in a step in the antigradient of s to get v
            % and then do the L1/Lq-norm regularized projection
            v=s-g/L;  c= sc- gc/L;

            % tree overlapping group Lasso projection
            ind_work(1:2,:)=[ [-1, -1]', ind(1:2,:) ];
            ind_work(3,:)=[ lambda1/L, ind(3,:) * (lambda2 / L) ];

            x=altra(v, n, ind_work, size(ind_work,2));

            v=x-s;  % the difference between the new approximate solution x
            % and the search point s

            % compute A x
            if (opts.nFlag==0)
                Ax=A* x;
            elseif (opts.nFlag==1)
                invNu=x./nu; mu_invNu=mu * invNu;
                Ax=A*invNu -repmat(mu_invNu, m, 1);
            else
                Ax=A*x-repmat(mu*x, m, 1);     Ax=Ax./nu;
            end

            % aa= - diag(y) * (A * x + c)
            aa=- y.*(Ax+ c);

            % fun_x is the logistic loss at the new approximate solution
            bb=max(aa,0);
            fun_x= weight'* ( log( exp(-bb) +  exp(aa-bb) ) + bb );

            r_sum= (v'*v + (c-sc)^2) / 2;
            l_sum=fun_x - fun_s - v'* g - (c-sc)* gc;

            if (r_sum <=1e-20)
                bFlag=1; % this shows that, the gradient step makes little improvement
                break;
            end

            % the condition is fun_x <= fun_s + v'* g + c * gc
            %                           + L/2 * (v'*v + (c-sc)^2 )
            if(l_sum <= r_sum * L)
                break;
            else
                L=max(2*L, l_sum/r_sum);
                %fprintf('\n L=%e, r_sum=%e',L, r_sum);
            end
        end

        % --------------------------- step 3 ---------------------------
        % update alpha and alphap, and check whether converge
        alphap=alpha; alpha= (1+ sqrt(4*alpha*alpha +1))/2;

        ValueL(iterStep)=L;
        % store values for L

        xxp=x-xp;    ccp=c-cp;
        funVal(iterStep)=fun_x;

        % compute the regularization part
        ind_work(1:2,:)=[ [-1, -1]', ind(1:2,:) ];
        ind_work(3,:)=[ lambda1, ind(3,:) * lambda2 ];
        tree_norm=treeNorm(x, n, ind_work, size(ind_work,2));

        % function value = loss + regularizatioin
        funVal(iterStep)=fun_x + tree_norm;

        if (bFlag)
            % fprintf('\n The program terminates as the gradient step changes the solution very small.');
            break;
        end

        switch(opts.tFlag)
            case 0
                if iterStep>=2
                    if (abs( funVal(iterStep) - funVal(iterStep-1) ) <= opts.tol)
                        break;
                    end
                end
            case 1
                if iterStep>=2
                    if (abs( funVal(iterStep) - funVal(iterStep-1) ) <=...
                            opts.tol* funVal(iterStep-1))
                        break;
                    end
                end
            case 2
                if ( funVal(iterStep)<= opts.tol)
                    break;
                end
            case 3
                norm_xxp=sqrt(xxp'*xxp);
                if ( norm_xxp <=opts.tol)
                    break;
                end
            case 4
                norm_xp=sqrt(xp'*xp);    norm_xxp=sqrt(xxp'*xxp);
                if ( norm_xxp <=opts.tol * max(norm_xp,1))
                    break;
                end
            case 5
                if iterStep>=opts.maxIter
                    break;
                end
        end

        % restart the program every opts.rStartNum
        if (~mod(iterStep, opts.rStartNum))
            alphap=0; alpha=1;
            xp=x; Axp=Ax; xxp=zeros(n,1); L =L/2;
        end
    end
else
    error('\n The function does not support opts.mFlag neq 0 & opts.lFlag neq 0!');
end


function [DisS, dis] = DistanceSum_MPP(X, Y, configure)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Define the weighted distance of two point processes' instances
%
% Reference:
% Iwayama, Koji, Yoshito Hirata, and Kazuyuki Aihara.
% "Definition of distance for nonlinear time series analysis
% of marked point process data."
% Physics Letters A 381.4 (2017): 257-262.
%
% Provider:
% Hongteng Xu @ Georgia Tech
% June 13, 2017
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dis = zeros(length(configure.weight),1);
DisS = 0;
for i = 1:length(configure.weight)
    dis(i) = Distance_MPP(X, Y, configure.M, configure.id{i});
    DisS = DisS + configure.weight(i) * dis(i);
end


function emap = snakeMap(f,varargin)
%SNAKEMAP Computes an edge map for use in the snake iterative algorithm.
%   EMAP = SNAKEMAP(F,T,SIG,NSIG,ORDER) computes the edge map, EMAP, of
%	 input image F. If only F is provided in the input, EMAP will equal
%	 the magnitude of the gradient of F (see Chpapter 3) without
%	 thresholding. If T is also provided, EMAP is thresholded so that, at
%	 any point, EMAP = 1 if EMAP > T; it is zero otherwise. If T is the
%	 string 'auto', thresholding is done automatically by function edge.
%	 Otherwise, T is expected to be a number in the range [0,1]. No
%	 smoothing filter is applied. If all inputs are provided, the
%	 magnitude of the gradient is thresholded and filtered using a
%	 Gaussian kernel of size NSIG*SIG-by-NSIG*SIG where SIG is the
%	 standard deviation and NISG is an integer giving the number of
%	 standard deviations we want the kernel size to be. The function
%	 adjusts the kernel size to be the odd size closest to the specified
%	 size. ORDER is a character string with the following possible
%	 values: If order = 'before' then image F is filtered before the map
%	 (magnitude of the gradient) is computed. If order = 'after'
%	 filtering is performed on the edge map after it is computed. If
%	 ORDER = 'both' filtering is performed on the image first, and then
%	 after the edge map is computed. If ORDER = 'none', no filtering is
%	 performed. This is the default. Gradients are computed using the
%	 Sobel kernels.
%
%   Copyright 2002-2020 Gatesmark
%
%   This function, and other functions in the DIPUM Toolbox, are based
%   on the theoretical and practical foundations established in the
%   book Digital Image Processing Using MATLAB, 3rd ed., Gatesmark
%   Press, 2020.
%
%   Book website: http://www.imageprocessingplace.com
%   License: https://github.com/dipum/dipum-toolbox/blob/master/LICENSE.txt

% PRELIMINARIES.
% Scale image to the floating point intensity range [0,1].
f = im2double(f);
% Set defaults.
thresholding = true;
filtering = true;
if nargin == 1
	% Only one input. No thresholding.
	thresholding = false;
	% No filtering will be done.
	filtering = false;
elseif numel(varargin) == 1
	% Only two inputs.
	T = varargin{1};
	filtering = false;
elseif numel(varargin) == 4
	T = varargin{1};
	sig = varargin{2};
	nsig = varargin{3};
	order = varargin{4};
else
	% Filtering is expected, but not enough inputs were provided.
	error('No enough inputs to define the filtering operation.')
end

% FILTER THE INPUT IMAGE.
% If filtering is called for, form the Gaussian lowpass kernel.
if filtering
	% Form Gaussian kernel using function fspecial.
	kernelSize = floor(nsig*sig);
	% Find the closest odd number of kernelSize.
	if iseven(kernelSize)
      kernelSize = kernelSize + 1;
	end
	% Gaussian kernel.
	w = fspecial('gaussian',kernelSize,sig);
end
% Check to see if filtering is to be done here.
if filtering
	if isequal(order,'before') || isequal(order,'both')
      f = im2double(imfilter(f,w,'conv','symmetric'));
	end
end

% COMPUTE THE EDGE MAP. We use function edge to compute the gradient
% image. Because thresholding is done as part of the function, we have
% to determine at this point if thresholding is to be done and, if so,
% whether 'auto' was specified or not. Either way, the resulting map is
% thinned automatically by function edge.
if thresholding
	if isequal(T,'auto')
      emap = double(edge(f,'sobel'));
   else
      emap = double(edge(f,'sobel',T));
	end
else
   % No thresholding; use edge with a 0 threshold.
	emap = double(edge(f,'sobel',0));
end
if min(emap(:)) == max(emap(:))
   error('Threshold is too low/high. All values of emap are equal')
end

% FILTER EDGE MAP.
% Check to see if filtering was specified.
if filtering
	% If order is 'after', or 'both' then filter the edge map now.
	if isequal(order,'after') || isequal(order,'both')
      emap = imfilter(im2double(intensityScaling(emap)),...
         w,'conv','symmetric');
	end
end

% SCALE THE EDGE MAP TO THE FULL RANGE [0,1].
emap = intensityScaling(emap);


%
% Multiply polynomials, polynomial matrices and numeric matrices
%             h = f1 * f2 * ... * fk
%
% Syntax:   (ptimes is the shortened alias for PolynomialTimes)
%            >> p = PolynomialTimes(f,g)
%            >> p = PolynomialTimes(f, g, h)  %
%   Input:    f, g, h, ...  --- (string/numeric/cell/matrix)
%                 polynomials, numeric matrices and/or polynomial cells,
%                 so that the multiplication is well-defined.
%
%  Output:    p --- (string) product of the input polynomials
%
%  Example:
%
%     >> PolynomialTimes(3,[1 5; -2 4],'x+y',{'y-1';'x+z'})
%
%     ans =
%
%    '-3*x + 15*x^2 - 3*y + 18*x*y + 3*y^2 + 15*x*z + 15*y*z'
%    '6*x + 12*x^2 + 6*y + 6*x*y - 6*y^2 + 12*x*z + 12*y*z'
%


% Minimum-phase spectrum of a given spectrum using the complex cepstrum
%
% Description
%  Compute the minimum-phase spectrum of a given spectrum X by dropping the
%  negative quefrencies of X.
%
% Input
%  X   : The spectrum to convert (full DFT length)
%
% Output
%  M   : The corresponding minimum-phase spectrum
%  lM  : The log minimum-phase spectrum
%
% References
%  [1] Alan V. Oppenheim and Ronald W. Schafer, "Digital Signal Processing",
%      Prentice-Hall, 2nd edition, 1978.
%      Note that this 2nd edition contains a chapter about complex cepstrum which
%      has been removed in the 3rd edition (and possibly came back in the 4th ed.)
%
% Copyright (c) 2011 University of Crete - Computer Science Department (UOC-CSD)
%
% License
%  This file is under the LGPL license,  you can
%  redistribute it and/or modify it under the terms of the GNU Lesser General
%  Public License as published by the Free Software Foundation, either version 3
%  of the License, or (at your option) any later version. This file is
%  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
%  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
%  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
%  details.
%
% This function is part of the Covarep project: http://covarep.github.io/covarep
%
% Author
%  Gilles Degottex <degottex@csd.uoc.gr>
%

function [M lM] = spec2minphasespec(X)

    X = X(:).';

    rcc = ifft(log(abs(X))); % Compute the real cepstrum

    if mod(length(X),2)==0
        % For even DFT length
        lM = fft([rcc(1),2*rcc(2:end/2),rcc(end/2+1)], length(X));% [1]
    else
        % For odd DFT length
        lM = fft([rcc(1),2*rcc(2:(end-1)/2+1)], length(X));       % [1]
    end

    M = exp(lM);

return


function pers=ordinal_persistence(S)
% ORDINAL_PERSISTENCE computes the persistence of an ordered multilayer partition
%
% Version: 2.2.0
% Date: Thu 11 Jul 2019 12:25:42 CEST
%
% pers = ORDINAL_PERSISTENCE(S) with a single multilayer partition or a
% cell of multilayer partitions S (with S{i} the ith multilayer partition
% of cell S) computes the "persistence" of ordered multilayer partitions. The
% output pers is a vector such that pers(i) is the persistence of multilayer
% partition S{i} (stored as an N*T matrix where N is the number of nodes in
% each layer and T is the number of layers). For a multilayer partition with
% ordered layers, the value of persistence is the number of nodes that do
% not change community assignments between consecutive layers (see Bazzi
% et al. 2016 for more detail). Ordinal persistence varies between 0 and 1.
% Ordinal persistence is related to the multilayer quality function
% developed in Mucha et al. 2010 when one uses ordinal interlayer coupling.
%
%   References:
%
%     Mucha, Peter J., Thomas Richardson, Kevin Macon, Mason A. Porter, and
%     Jukka-Pekka Onnela. "Community Structure in Time-Dependent,
%     Multiscale, and Multiplex Networks," Science 328, 876-878 (2010).
%
%     Bazzi, Marya, Mason A. Porter, Stacy Williams, Mark McDonald, Daniel
%     J. Fenn, and Sam D. Howison. "Community Detection in Temporal Multilayer
%     Networks, with an Application to Correlation Networks", MMS: A SIAM
%     Interdisciplinary Journal 14, 1-41 (2016).

if ~iscell(S)
    S={S};
end

[N,T]=size(S{1});
pers=zeros(length(S),1);
for i=1:length(S)
    pers(i)=sum(sum(S{i}(:,1:end-1)==S{i}(:,2:end)))/(N*(T-1));
end

end


%%***********************************************************
%% lmiexamp2: generate SDP data for the following LMI problem
%%
%%  max  -Tr(P)
%%  s.t. A1*P + P*A1' + B*diag(d)*B' <= 0
%%       A2*P + P*A2' + B*diag(d)*B' <= 0
%%                                -d <= 0
%%                            sum(d)  = 1
%%***********************************************************
%% Here is an example on how to use this function to
%% find an optimal P.
%%
%% A1 = [-1  0  0; 0 -2  0; 1  1 -1];
%% A2 = A1 + 0.1*A1';
%% B  = [1 2; 3 4; 5 6];
%%
%% [blk,Avec,C,b] = lmiexamp2(A1,A2,B);
%% [obj,X,y,Z] = sqlp(blk,Avec,C,b);
%% n = size(A1,2); N = n*(n+1)/2; dlen = size(B,2);
%% P = smat(blk(1,:),y(1:N));
%% d = y(N+[1:dlen]);
%%*****************************************************************
%% SDPT3: version 4.0
%% Copyright (c) 1997 by
%% Kim-Chuan Toh, Michael J. Todd, Reha H. Tutuncu
%% Last Modified: 16 Sep 2004
%%*****************************************************************

   function [blk,Avec,C,b] = lmiexamp2(A1,A2,B);

%%
   n1 = size(A1,2);  n2 = size(A2,2);
   if (n1 ~= n2); error('lmiexamp2: A1, A2 not compatible'); end;
%%
   n = n1;
   I = speye(n);
   blk{1,1}  = 's'; blk{1,2} = n;
   Avec{1,1} = lmifun(A1,I,B);
   C{1,1} = sparse(n,n);
%%
   blk{2,1}  = 's'; blk{2,2} = n;
   Avec{2,1} = lmifun(A2,I,B);
   C{2,1} = sparse(n,n);
%%
%% and constraints:  -d <= 0
%%
   N = n*(n+1)/2; dlen = size(B,2);
   blk{3,1}  = 'l'; blk{3,2} = dlen;
   Avec{3,1} = [sparse(dlen,N)    -speye(dlen,dlen)];
   C{3,1} = zeros(dlen,1);
%%
%% add in the constraint:  sum(d) = 1
%%
   blk{4,1}  = 'u'; blk{4,2} = 1;
   Avec{4,1} = sparse([zeros(1,N) ones(1,dlen)]);
   C{4,1} = 1;
%%
   blktmp{1,1} = 's'; blktmp{1,2} = n;
   b = [-svec(blktmp,I); zeros(dlen,1)];
%%**********************************************************


function lambdamax = tvdiplmax(y)
% Calculate the value of lambda so that if lambda >= lambdamax, the TVD
% functional solved by TVDIP is minimized by the trivial constant
% solution x = mean(y). This can then be used to determine a useful range
% of values of lambda, for example.
%
% Usage:
% lambdamax = tvdiplmax(y)
%
% Input arguments:
% - y          Original signal to denoise, size N x 1.
%
% Output arguments:
% - lambdamax  Value of at which x = mean(y) is the output of the TVDIP
%              function.
%
% (c) Max Little, 2010. Based around code originally written by
% S.J. Kim, K. Koh, S. Boyd and D. Gorinevsky. If you use this code for
% your research, please cite:
% M.A. Little, Nick S. Jones (2010)
% "Sparse Bayesian Step-Filtering for High-Throughput Analysis of Molecular
% Machine Dynamics", in 2010 IEEE International Conference on Acoustics,
% Speech and Signal Processing, 2010, ICASSP 2010 Proceedings.
%
% This code is released under the terms of GNU General Public License as
% published by the Free Software Foundation; version 2 or later.

error(nargchk(1,1,nargin));
y = y(:);
N = length(y);
M = N - 1;

% Construct sparse operator matrices
I1 = speye(M,M);
O1 = spalloc(M,1,M);
D = [I1 O1]-[O1 I1];

DDT = D*D';
Dy  = D*y;

lambdamax = max(abs(DDT\Dy));


function [ x, w ] = legendre_ek_compute ( n )

%*****************************************************************************80
%
%% LEGENDRE_EK_COMPUTE: Legendre quadrature rule by the Elhay-Kautsky method.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 April 2011
%
%  Author:
%
%    Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Sylvan Elhay, Jaroslav Kautsky,
%    Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
%    Interpolatory Quadrature,
%    ACM Transactions on Mathematical Software,
%    Volume 13, Number 4, December 1987, pages 399-415.
%
%  Parameters:
%
%    Input, integer N, the order.
%
%    Output, real X(N), the abscissas.
%
%    Output, real W(N), the weights.
%

%
%  Define the zero-th moment.
%
  zemu = 2.0;
%
%  Define the Jacobi matrix.
%
  bj = zeros ( n, 1 );

  for i = 1 : n
    bj(i) = ( i * i ) / ( 4 * i * i - 1 );
  end
  bj(1:n) = sqrt ( bj(1:n) );

  x = zeros ( n, 1 );

  w = zeros ( n, 1 );
  w(1) = sqrt ( zemu );
%
%  Diagonalize the Jacobi matrix.
%
  [ x, w ] = imtqlx ( n, x, bj, w );

  w(1:n) = w(1:n).^2;

  return
end


function b = r8ge_ml ( n, a_lu, pivot, x, job )

%*****************************************************************************80
%
%% R8GE_ML computes A * x or A' * x, using R8GE_FA factors.
%
%  Discussion:
%
%    The R8GE storage format is used for a general M by N matrix.  A storage
%    space is made for each logical entry.  The two dimensional logical
%    array is mapped to a vector, in which storage is by columns.
%
%    It is assumed that R8GE_FA has overwritten the original matrix
%    information by LU factors.  R8GE_ML is able to reconstruct the
%    original matrix from the LU factor data.
%
%    R8GE_ML allows the user to check that the solution of a linear
%    system is correct, without having to save an unfactored copy
%    of the matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    27 March 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%    N must be positive.
%
%    Input, real A_LU(N,N), the LU factors from R8GE_FA.
%
%    Input, integer PIVOT(N), the pivot vector computed by R8GE_FA.
%
%    Input, real X(N), the vector to be multiplied.
%
%    Input, integer JOB, specifies the operation to be done:
%    JOB = 0, compute A * x.
%    JOB nonzero, compute A' * X.
%
%    Output, real B(N), the result of the multiplication.
%
  b(1:n) = x(1:n);

  if ( job == 0 )
%
%  Y = U * X.
%
    for j = 1 : n
      b(1:j-1) = b(1:j-1) + a_lu(1:j-1,j)' * b(j);
      b(j) = a_lu(j,j) * b(j);
    end
%
%  B = PL * Y = PL * U * X = A * x.
%
    for j = n-1 : -1 : 1

      b(j+1:n) = b(j+1:n) - a_lu(j+1:n,j)' * b(j);
      k = pivot(j);

      if ( k ~= j )
        t    = b(k);
        b(k) = b(j);
        b(j) = t;
      end

    end

  else
%
%  Y = (PL)' * X:
%
    for j = 1 : n-1

      k = pivot(j);

      if ( k ~= j )
        t    = b(k);
        b(k) = b(j);
        b(j) = t;
      end

      b(j) = b(j) - sum ( b(j+1:n) * a_lu(j+1:n,j) );

    end
%
%  B = U' * Y = ( PL * U )' * X = A' * X.
%
    for i = n : -1 : 1
      b(i+1:n) = b(i+1:n) + b(i) * a_lu(i,i+1:n);
      b(i) = b(i) * a_lu(i,i);
    end

  end

  return
end


function [z,e]=exactPairMult(xIn,yIn)
%%EXACTPAIRMULT Calculate the product of the floating point double values
%               x and y such that the result z+e (if added with infinite
%               precision) is exactly the product x*y, where z is the
%               floating point product and e is the part of the result that
%               cannot be expressed exactly. This is implemented for x and
%               y being floating point doubles.
%
%INPUTS xIn, yIn Two real arrays of sizes that can be added.
%
%OUTPUTS: z The real product x.*y as limited by floating point precision.
%         e The real error such that if done with inifite precision
%           (z+e)==x*y. This does not necessarily hold when numbers become
%           denormalized.
%
%This function implements Dekker's algorithm, which is given as Mul12 in
%[1]. Note that additional scaling is performed for the instance where
%Dekker's algorithm might result in an overflow. That is when x or y is
%above 2^(1023 (maximum exponent) -53 (mantissa length))=2^970.
%
%The output pair (z,e) can be considered to be a "doublelength" floating
%point number, as defined in [1]. This means that
%abs(e)<=abs(z+e)*2^(-t)/(1+2^(-t)) (considering exact arithmetic), where t
%is the number of bits in the mantissa of the floating point number. Here,
%that is 53.
%
%Note that this assumes that the processor rounding mode has been set to
%round to "nearest," which is the default in Matlab, but which can be
%changed using the function setProcRoundingMode.
%
%EXAMPLE:
%Consider the product
% [z,e]=exactPairMult(1+2^50,1+2^(-15))
%One will get z=1.125934266580993e+15 and e=3.051757812500000e-05.
%The exact value of the product can be found to be
%1125934266580993.000030517578125
%If one adds z and e exactly, one gets precisely that result.
%
%REFERENCES:
%[1] T. J. Dekker, "A Floating Point Technique for Extending the Available
%    Precision," Numerische Mathematik, vol. 18, no. 3, Jun. 1971, pp.
%    224-242.
%
%November 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(~isa(xIn,'double')||~isa(yIn,'double'))
    error('This function is only implemented for x and y being double floating point values.')
end

if(isempty(xIn)||isempty(yIn))
    %Returning empty matrices if one input is an empty matrix is consistent
    %with what Matlab does when trying to multiply an empty matrix with
    %something.
    z=[];
    e=[];
    return
end

%This is 2^(1023 (maximum exponent) -53)=2^970
maxUnscaled=9.9792015476735990582818635651842e291;
two53=9007199254740992;%2^53;
twom53=1.1102230246251565404236316680908e-16;%2^(-53)

isScalX=isscalar(xIn);
isScalY=isscalar(yIn);

if(xor(isScalX,isScalY))
    %If one is scalar and the other is not.
    if(isScalX)
        dims=size(yIn);
        xIn=repmat(xIn,dims);
    else
        dims=size(xIn);
        yIn=repmat(yIn,dims);
    end
else
    %If neither is scalar or both are scalar, assume that they are the same
    %size.
    dims=size(xIn);
end

z=zeros(dims);
e=zeros(dims);
numEl=numel(z);

for curEl=1:numEl
    x=xIn(curEl);
    y=yIn(curEl);
    if(x>maxUnscaled)
        x=x*twom53;
        invScalFactX=two53;
    else
        invScalFactX=1;
    end

    if(y>maxUnscaled)
        y=y*twom53;
        invScalFactY=two53;
    else
        invScalFactY=1;
    end
    invScalFact=invScalFactX*invScalFactY;

    [zCur,eCur]=exactPairMultPrescaled(x,y);

    z(curEl)=zCur*invScalFact;
    e(curEl)=eCur*invScalFact;
end
end

function [z,e]=exactPairMultPrescaled(x,y)
    %The double mantissa is 53 bits (including the implicit one).
    const=134217729;%This is 2^ceil(53/2)+1=2^27+1

    p=x*const;
    hx=(x-p)+p;
    tx=x-hx;
    p=y*const;
    hy=(y-p)+p;
    ty=y-hy;
    p=hx*hy;
    q=(hx*ty)+(tx*hy);
    z=p+q;
    e=p-z+q+tx*ty;

end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function [ bool ] = gsp_test_duality_coefficient( gcoeff,hcoeff,tol )
%GSP_TEST_DUALITY_COEFFICIENT Test if the coefficient are from dual filters
%   Usage:  bool = gsp_test_duality_coefficient( gcoeff,hcoeff );
%           bool = gsp_test_duality_coefficient( gcoeff,hcoeff,tol );
%
%   Input parameters:
%       gcoeff  : coefficient of the filter 1 (matrix $N$ x $M$ )
%       hcoeff  : coefficinet of the filter 2 (matrix $N$ x $M$ )
%       tol     : tolerance for the test (default 1e-5)
%
%   Ouput paramters:
%       bool    : boolean
%
%   This function test if two discrete filterbanks are dual. Each filter is
%   a column in the matrix *gcoeff* or *hcoeff*. $M$ is the number of
%   filters and $N$ the number of coefficients (size of the graph signal).
%

% Author: Nathanael Perraudin
% Date  : 13 july 2014
% testing: test_dual

if nargin<3
    tol = 1e-5;
end

v = sum(gcoeff.*hcoeff,2);
A = min(v);
B = max(v);
bool = ( (A-B) / ((A+B)/2) ) < tol;

end


function prob_test026 ( )

%*****************************************************************************80
%
%% TEST026 tests BURR_CDF, BURR_CDF_INV, BURR_PDF.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 April 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST026\n' );
  fprintf ( 1, '  For the Burr PDF:\n' );
  fprintf ( 1, '  BURR_CDF evaluates the CDF;\n' );
  fprintf ( 1, '  BURR_CDF_INV inverts the CDF.\n' );
  fprintf ( 1, '  BURR_PDF evaluates the PDF;\n' );

  a = 1.0;
  b = 2.0;
  c = 3.0;
  d = 2.0;

  check = burr_check ( a, b, c, d );

  if ( ~check );
    fprintf ( 1, '\n' );
    fprintf ( 1, 'TEST026 - Fatal error!\n' );
    fprintf ( 1, '  The parameters are not legal.\n' );
    return
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  PDF parameter A = %14f\n', a );
  fprintf ( 1, '  PDF parameter B = %14f\n', b );
  fprintf ( 1, '  PDF parameter C = %14f\n', c );
  fprintf ( 1, '  PDF parameter D = %14f\n', d );

  seed = 123456789;

  fprintf ( 1, '\n' );
  fprintf ( 1, '       X            PDF           CDF            CDF_INV\n' );
  fprintf ( 1, '\n' );

  for i = 1 : 10

    [ x, seed ] = burr_sample ( a, b, c, d, seed );

    pdf = burr_pdf ( x, a, b, c, d );

    cdf = burr_cdf ( x, a, b, c, d );

    x2 = burr_cdf_inv ( cdf, a, b, c, d );

    fprintf ( 1, ' %14f  %14f  %14f  %14f\n', x, pdf, cdf, x2 );

  end

  return
end


function value = problem4 ( x, h, epsilon, o )

%*****************************************************************************80
%
%% PROBLEM4 evaluates problem data for problem 4.
%
%  Discussion:
%
%    case # 4: u_anl  =   x-1/4.......................................x<0.5
%                         x-1/2.......................................x>0.5
%
%              w_anl  =   0...................................x<0.5-epsilon
%                        -2/epsilon^2(-1/4(log(epsilon)-
%                         log(1/2-v)))....................0.5-epsilon<x<0.5
%                         2/epsilon^2( 1/4*(log(v-1/2)-
%                         log(epsilon)))..................0.5<x<0.5+epsilon
%                         0...................................x>0.5+epsilon
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 February 2012
%
%  Parameters:
%
%    Input, real X(:), the evaluation points.
%
%    Input, real H, the discretization parameter.
%
%    Input, real EPSILON, the nonlocalization parameter.
%
%    Input, string O, the option:
%    'U', evaluate the exact solution.
%    'D', evaluate the derivative of the exact solution.
%    'F', evaluate the right hand side.
%    'L', evaluate the lifting function.
%
  if ( o == 'u' )
    value = (x-1/4).*(x<0.5) + (x-1/2).*(x>=0.5);
  elseif ( o == 'd' )
    value = zeros ( size ( x ) );
  elseif ( o == 'f' )
    epsilon2 = epsilon * epsilon;
    value = (x<0.5-epsilon).*0 + ...
            (x>=0.5-epsilon & x<0.5).*(-2)/epsilon2.*(-1/4* (log(epsilon)-log(1/2-x))) + ...
            (x>0.5 & x<0.5+epsilon).*2/epsilon2.*(1/4*(log(x-1/2)-log(epsilon))) + ...
            (x>=0.5+epsilon).*0;
  elseif ( o == 'l' )
    value = (x<=0).*(x-1/4) + ...
            (x>0 & x<=h).* (1/4).*(x./h-1)+ ...
            (x>h & x< 1-h).* 0 * epsilon + ...
            (x>=1-h & x<=1).* (1/2).*(1. + (x-1)./h)  + ...
            (x>1).*(x-1/2);
  else
    fprintf ( 1, '\n' );
    fprintf ( 1, 'PROBLEM4 - Fatal error!\n' );
    fprintf ( 1, '  Unrecognized option O = "%s"\n', o );
    error ( 'PROBLEM4 - Fatal error!\n' );
  end

  return
end


function spline_test17 ( )

%*****************************************************************************80
%
%% TEST17 tests SPLINE_CUBIC_SET, SPLINE_CUBIC_VAL.
%
%  Discussion:
%
%    For boundary condition 0, the spline should come very close within
%    the interpolation interval.
%
%    For conditions 1 and 2, the spline should be essentially exactly equal
%    to the data, inside and outside the interpolation interval.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 February 2009
%
%  Author
%
%    John Burkardt
%
  n = 11;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST17\n' );
  fprintf ( 1, '  SPLINE_CUBIC_SET sets up a cubic spline;\n' );
  fprintf ( 1, '  SPLINE_CUBIC_VAL evaluates it.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Cubic data, unevenly spaced knots.\n' );

  for i = 1 : n
    t(i) = ( ( i - 1 ) / ( n - 1 ) )^2;
  end

  for i = 1 : n
    y(i) =  fcube ( t(i) );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The data to be interpolated:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of data values = %d\n', n );
  fprintf ( 1, '\n' );
  fprintf ( 1, '       T             Y\n' );
  fprintf ( 1, '\n' );

  for i = 1 : n
    fprintf ( 1, '%14f  %14f\n', t(i), y(i) );
  end
%
%  Try boundary condition types 0, 1 and 2.
%
  for k = 0 : 2

    if ( k == 0 )

      ibcbeg = 0;
      ybcbeg = 0.0E+00;

      ibcend = 0;
      ybcend = 0.0E+00;

      fprintf ( 1, '\n' );
      fprintf ( 1, '  Boundary condition 0 at both ends:\n' );
      fprintf ( 1, '  Spline is quadratic in boundary intervals.\n' );

    elseif ( k == 1 )

      ibcbeg = 1;
      ybcbeg = fpcube ( t(1) );

      ibcend = 1;
      ybcend = fpcube ( t(n) );

      fprintf ( 1, '\n' );
      fprintf ( 1, '  Boundary condition 1 at both ends:\n' );
      fprintf ( 1, '  Y''(left) =  %f\n', ybcbeg );
      fprintf ( 1, '  Y''(right) = %f\n', ybcend );

    elseif ( k == 2 )

      ibcbeg = 2;
      ybcbeg = fppcube ( t(1) );

      ibcend = 2;
      ybcend = fppcube ( t(n) );

      fprintf ( 1, '\n' );
      fprintf ( 1, '  Boundary condition 2 at both ends:\n' );
      fprintf ( 1, '  YP"(left) =  %f\n', ybcbeg );
      fprintf ( 1, '  YP"(right) = %f\n', ybcend );

    end

    ypp = spline_cubic_set ( n, t, y, ibcbeg, ybcbeg, ibcend, ybcend );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  SPLINE"(T), F"(T):\n' );
    fprintf ( 1, '\n' );
    for i = 1: n
      fprintf ( 1, '%14f  %14f\n', ypp(i), fppcube(t(i)) );
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  T, SPLINE(T), F(T)\n' );
    fprintf ( 1, '\n' );

    for i = 0 : n

      if ( i == 0 )
        jhi = 1;
      elseif ( i < n )
        jhi = 2;
      else
        jhi = 2;
      end

      for j = 1 : jhi

        if ( i == 0 )
          tval = t(1) - 1.0E+00;
        elseif ( i < n )
          tval = ( ( jhi - j + 1 ) * t(i)     ...
                 + (       j - 1 ) * t(i+1) ) ...
                 / ( jhi );
        else
          if ( j == 1 )
            tval = t(n);
          else
            tval = t(n) + 1.0E+00;
          end
        end

        [ yval, ypval, yppval ] = spline_cubic_val ( n, t, y, ypp, tval );

        fprintf ( 1, '%14f  %14f  %14f\n', tval, yval, fcube ( tval ) );

      end
    end

  end

  return
end


function chebyshev_polynomial_test17 ( )

%*****************************************************************************80
%
%% TEST17 tests T_TRIPLE_PRODUCT_INTEGRAL.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 April 2012
%
%  Author:
%
%    John Burkardt
%
  test_num = 20;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'CHEBYSHEV_POLYNOMIAL_TEST17:\n' );
  fprintf ( 1, '  T_TRIPLE_PRODUCT_INTEGRAL computes the triple integral\n' );
  fprintf ( 1, '  Tijk = integral ( -1 <= x <= 1 ) T(i,x) T(j,x) T(k,x) / sqrt ( 1-x^2) dx\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '   I   J   K     Tijk           Tijk\n' );
  fprintf ( 1, '                 computed       exact\n' );
  fprintf ( 1, '\n' );

  n = 15;
  [ x, w ] = t_quadrature_rule ( n );

  seed = 123456789;

  for test = 1 : test_num
    [ i, seed ] = i4_uniform ( 2, 6, seed );
    [ j, seed ] = i4_uniform ( 1, 3, seed );
    [ k, seed ] = i4_uniform ( 0, 4, seed );
    fx1 = t_triple_product_integral ( i, j, k );
    fx2 = 0.0;
    for l = 1 : n
      ti = t_polynomial_value ( i, x(l) );
      tj = t_polynomial_value ( j, x(l) );
      tk = t_polynomial_value ( k, x(l) );
      fx2 = fx2 + w(l) * ti * tj * tk;
    end
    fprintf ( 1, '  %2d  %2d  %2d  %14.6g  %14.6g\n', i, j, k, fx1, fx2 );
  end

  return
end


function [ num1, num2 ] = myconvert( str )
% Function to convert the vector string to numbers
% By Kyriakos Tsourapas
% You may contact me through the Mathworks site
% University of Essex 2002

% CREATE THE NUMBERS FROM THE STRING
sign1    = str(1);
intpart1 = sprintf('%d%d', str(2:3));
decpart1 = sprintf('%d%d%d%d%d%d%d%d%d%d', str(4:13));
sign2    = str(14);
intpart2 = sprintf('%d%d', str(15:16));
decpart2 = sprintf('%d%d%d%d%d%d%d%d%d%d', str(17:26));

% CONVERT THEM TO DECIMAL FROM BINARY
num1 = bin2dec(intpart1) + bin2dec(decpart1)/1000;
num2 = bin2dec(intpart2) + bin2dec(decpart2)/1000;

if sign1 == 1
    num1 = - num1;
end

if sign2 == 1
    num2 = - num2;
end


function hypercube_grid_test02 ( )

%*****************************************************************************80
%
%% HYPERCUBE_GRID_TEST02 tests HYPERCUBE_GRID on a five dimensional example.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 August 2014
%
%  Author:
%
%    John Burkardt
%
  m = 5;

  a = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
  b = [ 1.0, 1.0, 1.0, 1.0, 1.0 ];
  c = [ 1, 2, 3, 4, 5 ];
  ns = [ 2, 2, 2, 2, 2 ];

  n = prod ( ns(1:m) );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'HYPERCUBE_GRID_TEST02\n' );
  fprintf ( 1, '  Create a grid using HYPERCUBE_GRID.\n' );
  fprintf ( 1, '  Use a two point grid in each dimension.\n' );
  fprintf ( 1, '  Use a different centering option in each dimension.\n' );
  fprintf ( 1, '  Spatial dimension M = %d\n', m );
  fprintf ( 1, '  Number of grid points N = %d\n', n );
  fprintf ( 1, '\n' );
  fprintf ( 1, '     I    NS     C      A         B\n' );
  fprintf ( 1, '\n' );
  for i = 1 : m
    fprintf ( 1, '  %4d  %4d  %4d  %8.4f  %8.4f\n', i, ns(i), c(i), a(i), b(i) );
  end

  x = hypercube_grid ( m, n, ns, a, b, c );
  r8mat_transpose_print ( m, n, x, '  Grid points:' );

  return
end


function fd1d_advection_diffusion_steady ( )

%*****************************************************************************80
%
%% FD1D_ADVECTION_DIFFUSION_STEADY solves steady advection diffusion equation.
%
%  Discussion:
%
%    The steady advection diffusion equation has the form:
%
%      v ux - k * uxx = 0
%
%    where V (the advection velocity) and K (the diffusivity) are positive
%    constants, posed in the region
%
%      a = 0 < x < 1 = b
%
%    with boundary conditions
%
%      u(0) = 0, u(1) = 1.
%
%    The discrete solution is unreliable when dx > 2 * k / v / ( b - a ).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 May 2014
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Charles Hall, Thomas Porsching,
%    Numerical Analysis of Partial Differential Equations,
%    Prentice-Hall, 1990,
%    ISBN: 013626557X,
%    LC: QA374.H29.
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'FD1D_ADVECTION_DIFFUSION_STEADY:\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Solve the 1D steady advection diffusion equation:\n' );
  fprintf ( 1, '    v du/dx - k d2u/dx2 = 0\n' );
  fprintf ( 1, '  with constant, positive velocity V and diffusivity K,\n' );
  fprintf ( 1, '  over the interval:\n' );
  fprintf ( 1, '    0.0 <= x <= 1.0\n' );
  fprintf ( 1, '  with boundary conditions:\n' );
  fprintf ( 1, '    u(0) = 0, u(1) = 1.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Use finite differences:\n' );
  fprintf ( 1, '    d u/dx  = (u(x+dx)-u(x-dx))/2/dx\n' );
  fprintf ( 1, '    d2u/dx2 = (u(x+dx)-2u(x)+u(x-dx))/dx^2\n' );
%
%  Physical constants.
%
  v = 1.0;
  k = 0.05;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Diffusivity K = %g\n', k );
  fprintf ( 1, '  Constant velocity V = %g\n', v );
%
%  Spatial discretization.
%
  nx = 41;
  a = 0.0;
  b = 1.0;
  dx = ( b - a ) / ( nx - 1 );
  x = ( linspace ( a, b, nx ) )';

  fprintf ( 1, '  Number of nodes NX = %d\n', nx );
  fprintf ( 1, '  DX = %g\n', dx );
  fprintf ( 1, '  Maximum safe DX is %g\n', 2 * k / v / ( b - a ) );
%
%  Set up linear system corresponding to boundary conditions and
%  advection-diffusion equation.
%
  A = sparse ( nx, nx );
  f = zeros ( nx, 1 );

  A(1,1) = 1.0;
  f(1) = 0.0;

  for i = 2 : nx - 1
    A(i,i-1) = - v / dx / 2.0 -       k / dx / dx;
    A(i,i)   =                + 2.0 * k / dx / dx;
    A(i,i+1) = + v / dx / 2.0 -       k / dx / dx;
    f(i) = 0.0;
  end

  A(nx,nx) = 1.0;
  f(nx) = 1.0;
%
%  Solve linear system for U.
%
  u = A \ f;
%
%  The exact solution to the differential equation is known.
%
  r = v * ( b - a ) / k;
  w = ( 1.0 - exp ( r * x ) ) / ( 1.0 - exp ( r ) );
%
%  Plot discrete solution U (dots) and exact solution W (line).
%
  hold on
  plot ( x, w, 'b-', 'LineWidth', 2 );
  plot ( x, u, 'r.', 'LineWidth', 2, 'Markersize', 20 );

  grid on
  xlabel ( '<--X-->', 'Fontsize', 16 );
  ylabel ( '<--U(X)-->', 'Fontsize', 16 );
  title ( 'Solution of Steady Advection-Diffusion Equation', 'Fontsize', 24);
  hold off

  filename = 'fd1d_advection_diffusion_steady.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Saving plot as "%s".\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'FD1D_ADVECTION_DIFFUSION_STEADY\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% TIMESTAMP prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end


function lp=dir_lpdf(x,a)
%DIR_LPDF   Log probability density function of uniform Dirichlet
%           distribution
%
%       Description:
%       LP = DIR_LPDF(X, A) returns the lpdf of Dirichlet distribution
%       with A at X
%
% Copyright (c) 2000 Aki Vehtari

% This software is distributed under the GNU General Public
% License (version 3 or later); please refer to the file
% License.txt, included with the software, for details.

lp=gammaln(sum(a))-sum(gammaln)+sum(log(x).^(a-1));


% EX_LAPLACE_ISO_PLATE: solve the Poisson problem in one quarter of a plate with a hole, discretized with NURBS (isoparametric approach).

% 1) PHYSICAL DATA OF THE PROBLEM
clear problem_data
% Physical domain, defined as NURBS map given in a text file
problem_data.geo_name = 'geo_plate_with_hole.txt';

% Type of boundary conditions for each side of the domain
problem_data.nmnn_sides   = [3 4];
problem_data.drchlt_sides = [1 2];

% Physical parameters
problem_data.c_diff  = @(x, y) ones(size(x));

% Source and boundary terms
problem_data.f = @(x, y) zeros(size(x));
problem_data.g = @test_plate_mixed_bc_g_nmnn;
problem_data.h = @(x, y, ind)  exp(x).*sin(y);

% Exact solution (optional)
problem_data.uex     = @(x, y) exp(x).*sin (y);
problem_data.graduex = @(x, y) cat (1, ...
                       reshape (exp(x).*sin(y), [1, size(x)]), ...
                       reshape (exp(x).*cos(y), [1, size(x)]));

% 2) CHOICE OF THE DISCRETIZATION PARAMETERS
clear method_data
method_data.degree     = [3 3];       % Degree of the splines
method_data.regularity = [2 2];       % Regularity of the splines
method_data.nsub       = [8 8];       % Number of subdivisions
method_data.nquad      = [4 4];       % Points for the Gaussian quadrature rule

% 3) CALL TO THE SOLVER
[geometry, msh, space, u] = solve_laplace_iso (problem_data, method_data);

% 4) POST-PROCESSING
% 4.1) EXPORT TO PARAVIEW

output_file = 'Plate_NRB_Deg3_Reg2_Sub8';

vtk_pts = {linspace(0, 1, 21), linspace(0, 1, 21)};
fprintf ('The result is saved in the file %s \n \n', output_file);
sp_to_vtk (u, space, geometry, vtk_pts, output_file, 'u')

% 4.2) PLOT IN MATLAB. COMPARISON WITH THE EXACT SOLUTION

[eu, F] = sp_eval (u, space, geometry, vtk_pts);
[X, Y]  = deal (squeeze(F(1,:,:)), squeeze(F(2,:,:)));
subplot (1,2,1)
surf (X, Y, eu)
title ('Numerical solution'), axis tight
subplot (1,2,2)
surf (X, Y, problem_data.uex (X,Y))
title ('Exact solution'), axis tight

% Display errors of the computed solution in the L2 and H1 norm
[error_h1, error_l2] = ...
           sp_h1_error (space, msh, u, problem_data.uex, problem_data.graduex)

%!demo
%! ex_laplace_iso_plate

%!test
%! problem_data.geo_name = 'geo_plate_with_hole.txt';
%! problem_data.nmnn_sides   = [3 4];
%! problem_data.drchlt_sides = [1 2];
%! problem_data.c_diff  = @(x, y) ones(size(x));
%! problem_data.f = @(x, y) zeros(size(x));
%! problem_data.g = @test_plate_mixed_bc_g_nmnn;
%! problem_data.h = @(x, y, ind)  exp(x).*sin(y);
%! problem_data.uex     = @(x, y) exp(x).*sin (y);
%! problem_data.graduex = @(x, y) cat (1, ...
%!                        reshape (exp(x).*sin(y), [1, size(x)]), ...
%!                        reshape (exp(x).*cos(y), [1, size(x)]));
%! method_data.degree     = [3 3];       % Degree of the splines
%! method_data.regularity = [2 2];       % Regularity of the splines
%! method_data.nsub       = [8 8];       % Number of subdivisions
%! method_data.nquad      = [4 4];       % Points for the Gaussian quadrature rule
%! [geometry, msh, space, u] = solve_laplace_iso (problem_data, method_data);
%! [error_h1, error_l2] = sp_h1_error (space, msh, u, problem_data.uex, problem_data.graduex);
%! assert (msh.nel, 128)
%! assert (space.ndof, 231)
%! assert (error_h1, 6.16178098362953e-04, 1e-16)
%! assert (error_l2, 4.44336164898602e-05, 1e-16)


function D=distMat(P1, P2)
%
% Euclidian distances between vectors
% each vector is one row

if nargin == 2
    P1 = double(P1);
    P2 = double(P2);

    X1=repmat(sum(P1.^2,2),[1 size(P2,1)]);
    X2=repmat(sum(P2.^2,2),[1 size(P1,1)]);
    R=P1*P2';
    D=real(sqrt(X1+X2'-2*R));
else
    P1 = double(P1);

    % each vector is one row
    X1=repmat(sum(P1.^2,2),[1 size(P1,1)]);
    R=P1*P1';
    D=X1+X1'-2*R;
    D = real(sqrt(D));
end


function [y,L] = mdwt(x,h,L);
%    [y,L] = mdwt(x,h,L);
%
%    Function computes the discrete wavelet transform y for a 1D or 2D input
%    signal x using the scaling filter h.
%
%    Input:
%	x : finite length 1D or 2D signal (implicitly periodized)
%       h : scaling filter
%       L : number of levels. In the case of a 1D signal, length(x) must be
%           divisible by 2^L; in the case of a 2D signal, the row and the
%           column dimension must be divisible by 2^L. If no argument is
%           specified, a full DWT is returned for maximal possible L.
%
%    Output:
%       y : the wavelet transform of the signal
%           (see example to understand the coefficients)
%       L : number of decomposition levels
%
%    1D Example:
%       x = makesig('LinChirp',8);
%       h = daubcqf(4,'min');
%       L = 2;
%       [y,L] = mdwt(x,h,L)
%
%    1D Example's  output and explanation:
%
%       y = [1.1097 0.8767 0.8204 -0.5201 -0.0339 0.1001 0.2201 -0.1401]
%       L = 2
%
%    The coefficients in output y are arranged as follows
%
%       y(1) and y(2) : Scaling coefficients (lowest frequency)
%       y(3) and y(4) : Band pass wavelet coefficients
%       y(5) to y(8)  : Finest scale wavelet coefficients (highest frequency)
%
%    2D Example:
%
%       load test_image
%       h = daubcqf(4,'min');
%       L = 1;
%       [y,L] = mdwt(test_image,h,L);
%
%    2D Example's  output and explanation:
%
%       The coefficients in y are arranged as follows.
%
%              .------------------.
%              |         |        |
%              |    4    |   2    |
%              |         |        |
%              |   L,L   |   H,L  |
%              |         |        |
%              --------------------
%              |         |        |
%              |    3    |   1    |
%              |         |        |
%              |   L,H   |  H,H   |
%              |         |        |
%              `------------------'
%
%       where
%            1 : High pass vertically and high pass horizontally
%            2 : Low pass vertically and high pass horizontally
%            3 : High pass vertically and low  pass horizontally
%            4 : Low pass vertically and Low pass horizontally
%                (scaling coefficients)
%
%
%
%
%    See also: midwt, mrdwt, mirdwt
%

%File Name: mdwt.m
%Last Modification Date: 08/07/95	15:13:25
%Current Version: mdwt.m	2.4
%File Creation Date: Wed Oct 19 10:51:58 1994
%Author: Markus Lang  <lang@jazz.rice.edu>
%
%Copyright (c) 2000 RICE UNIVERSITY. All rights reserved.
%Created by Markus Lang, Department of ECE, Rice University.
%
%This software is distributed and licensed to you on a non-exclusive
%basis, free-of-charge. Redistribution and use in source and binary forms,
%with or without modification, are permitted provided that the following
%conditions are met:
%
%1. Redistribution of source code must retain the above copyright notice,
%   this list of conditions and the following disclaimer.
%2. Redistribution in binary form must reproduce the above copyright notice,
%   this list of conditions and the following disclaimer in the
%   documentation and/or other materials provided with the distribution.
%3. All advertising materials mentioning features or use of this software
%   must display the following acknowledgment: This product includes
%   software developed by Rice University, Houston, Texas and its contributors.
%4. Neither the name of the University nor the names of its contributors
%   may be used to endorse or promote products derived from this software
%   without specific prior written permission.
%
%THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, TEXAS,
%AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
%BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RICE UNIVERSITY
%OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
%EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
%PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
%OR BUSINESS INTERRUPTIONS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
%OTHERWISE), PRODUCT LIABILITY, OR OTHERWISE ARISING IN ANY WAY OUT OF THE
%USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
%For information on commercial licenses, contact Rice University's Office of
%Technology Transfer at techtran@rice.edu or (713) 348-6173
%
%Change History:
%
%Modification #1
%Mon Aug  7 11:42:11 CDT 1995
%Rebecca Hindman <hindman@ece.rice.edu>
%Added L to function line so that it can be displayed as an output
%
%Change History:
%
%Modification #1
%Thu Mar  2 13:07:11 CDT 2000
%Ramesh Neelamani<neelsh@ece.rice.edu>
%Revamped the help file
%


clear all, close all, clc

n = 1000;
q = n/4;
X = zeros(n,n);
X(q:(n/2)+q,q:(n/2)+q) = 1;

subplot(2,2,1), imshow(X); colormap gray, axis off

Y = imrotate(X,10,'bicubic');  % rotate 10 degrees
Y = Y - Y(1,1);
nY = size(Y,1);
startind = floor((nY-n)/2);
Xrot = Y(startind:startind+n-1, startind:startind+n-1);
subplot(2,2,2), imshow(Xrot); colormap gray, axis off

[U,S,V] = svd(X);  % svd well-aligned square
[U,S,V] = svd(Xrot);  % svd rotated square

subplot(2,2,3), semilogy(diag(S),'-ko')
ylim([1.e-16 1.e4]), grid on
set(gca,'YTick',[1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4])
set(gca,'XTick',[0 250 500 750 1000])

subplot(2,2,4), semilogy(diag(S),'-ko')
ylim([1.e-16 1.e4]), grid on
set(gca,'YTick',[1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4])
set(gca,'XTick',[0 250 500 750 1000])


function h = histc2( A, edges, wtMask )
% Multidimensional histogram count with weighted values.
%
% Creates a histogram h of the values in A [n x nd], with edges as
% specified. If nd==1, that is when A is a vector, the resulting histogram
% h is a vector of length nBins, where nBins=length(edges)-1. h(q) contains
% the weighted count of values v in A such that edges(q) <= v < edges(q+1).
% h(nBins) additionally contains the weighted count of values in A such
% that v==edges(nBins+1) -- which is different then how histc treates the
% boundary condition. Finally, h is normalized so that sum(h(:))==1.
%
% It usually makes sense to specify edges explicitly, especially if
% different histograms are going to be compared.  In general, edges must
% have monotonically non-decreasing values.  Also, if the exact bounds are
% unknown then it is convenient to set the first element in edges to -inf
% and the last to inf.  If h = histc2( A, nBins, ...), edges are
% automatically generated and have bins equally spaced between min(A) and
% max(A). That is the edges vector is generated by:
%  edges = linspace( min(A)-eps, max(A)+eps, nBins+1 );
%
% If nd>1, that is when A is a 2d matrix instead of a vector, the created
% histogram is multi-dimensional with dimensions nBins^nd, where each bin
% h(q1,...,qnd) contains the the weighted count of vectors v in A such that
% edges{k}(qk) <= v(k) < edges{k}(qk+1), for k=1,...,nd.  Note that if nd>1
% edges may be a cell vector where each element is a vector of edges or a
% scalar nBins as before.
%
% Each value in A may optionally have an associated weight given by wtMask,
% which should have the same number of elements as A. If not specified, the
% default is wtMask=ones(n,1).
%
% USAGE
%  h = histc2( A, edges, [wtMask] )
%
% INPUTS
%  A           - [n x nd] 2D numeric array
%  edges       - quantization bounds, see above
%  wtMask      - [] length [n] vector of weights
%
% OUTPUTS
%  h           - nd histogram [nBins^nd]
%
% EXAMPLE - 1D histograms
%  A=filterGauss([1000 1000],[],[],0); A=A(:); n=length(A);
%  h1 = histc2( A, 25 );              figure(1); bar(h1);
%  h2 = histc2( A, 25, ones(n,1) );   figure(2); bar(h2);
%  h3 = histc2( A, 25, A );           figure(3); bar(h3);
%
% EXAMPLE - 2D histograms
%  A=filterGauss([1000 1000],[],[],0); A=A(:); n=length(A);
%  h=histc2( [A A], 25 );    figure(1); im(h);  % decreasing along diag
%  h=histc2( [A A], 25, A ); figure(2); im(h);  % constant along diag
%
% See also HISTC, ASSIGNTOBINS, BAR
%
% Piotr's Computer Vision Matlab Toolbox      Version 2.0
% Copyright 2014 Piotr Dollar.  [pdollar-at-gmail.com]
% Licensed under the Simplified BSD License [see external/bsd.txt]

if( nargin<3 ); wtMask=[]; end;
if( ~isa(A,'double') ); A=double(A); end;
if( ~ismatrix(A) ); error('A must be a 2 dim array'); end;
[n,nd] = size(A);
if( ~isempty(wtMask) && n~=numel(wtMask) )
  error( 'wtMask must have n elements (A is nxnd)' ); end

if( nd==1 )
  % if nBins given instead of edges calculate edges
  if(length(edges)==1)
    edges = linspace(min(A)-eps,max(A)+eps,edges+1);
  end

  % create 1d histogram
  if(isempty(wtMask))
    h = histc( A, edges );
    h(end-1) = h(end-1)+h(end);
    h = h(1:end-1); h = h / sum(h);
  else
    h = histc2c( A, wtMask, edges );
    h = h / sum(h);
  end

else
  % if nBins given instead of edges calculate edges per dimension
  if( ~iscell(edges ) )
    edges=repmat({edges},[1 nd]);
  elseif( length(edges)~=nd )
    error( 'Illegal dimensions for edges' );
  end
  for i=1:length( edges );
    if(length(edges{i})==1)
      edges{i}=linspace(min(A(:,i))-eps,max(A(:,i))+eps,edges{i}+1);
    end
  end

  % create multidimensional histogram
  if( isempty(wtMask) ); wtMask=ones(1,n); end;
  h = histc2c( A, wtMask, edges{:} );
  h = h / sum(h(:));
end


function [E,v] = km_kpca(X,m,ktype,kpar)
% KM_KPCA performs kernel principal component analysis (KPCA) on a data set
% X.
% Input:	- X: data matrix in column format (each data point is a row)
%			- m: the number of principal components to return. If m is
%			smaller than 1, it is interpreted as the fraction of the signal
%			energy that is to be contained within the returned principal
%			components.
%			- ktype: string representing kernel type.
%			- kpar: vector containing the kernel parameters.
% Output:	- E: matrix containing the principal components.
%			- v: array containing the eigenvalues.
%			- Xep: projections of Xe on principal directions
% USAGE: [E,v] = km_kpca(X,m,ktype,kpar)
%
% Author: Steven Van Vaerenbergh (steven *at* gtas.dicom.unican.es), 2010.
%
% This file is part of the Kernel Methods Toolbox for MATLAB.
% https://github.com/steven2358/kmbox

n = size(X,1);

K = km_kernel(X,X,ktype,kpar);
[E,V] = eig(K);
v = diag(V);	% eigenvalues
[v,ind] = sort(v,'descend');
v = v(1:m);
E = E(:,ind(1:m));	% principal components
for i=1:m
	E(:,i) = E(:,i)/sqrt(n*v(i));	% normalization
end


function dz = dynamics_3_link(~,z,P)
%DZ = DYNAMICS_3_LINK(T,Z,P)
%
%FUNCTION:  This function computes the dynamics of a 3
%    link pendulum, and is designed to be called from ode45.
%    The model allows for arbitrary mass and inertia for each
%    link, but no friction or actuation
%
%INPUTS:
%    t = time. Dummy input for ode45. Not used.
%    z = [6 X nTime]  matrix of states
%    P = struct of parameters
%OUTPUTS:
%    dz = [6 X nTime]  matrix of state derivatives
%
%NOTES:
%    This file was automatically generated by writeDynamics.m

g  = P.g ; %gravity
m1 = P.m(1); % Link 1 mass
m2 = P.m(2); % Link 2 mass
m3 = P.m(3); % Link 3 mass
l1 = P.l(1); % Link 1 length
l2 = P.l(2); % Link 2 length
l3 = P.l(3); % Link 3 length
I1 = P.I(1); % Link 1 moment of inertia about its center of mass
I2 = P.I(2); % Link 2 moment of inertia about its center of mass
I3 = P.I(3); % Link 3 moment of inertia about its center of mass
d1 = P.d(1); % Link 1 distance between center of mass and parent joint
d2 = P.d(2); % Link 2 distance between center of mass and parent joint
d3 = P.d(3); % Link 3 distance between center of mass and parent joint

nTime = size(z,2);
dz = zeros(size(z));
M = zeros(3,3,nTime);
f = zeros(3,nTime);

th1 = z(1,:);
th2 = z(2,:);
th3 = z(3,:);

dth1 = z(4,:);
dth2 = z(5,:);
dth3 = z(6,:);

dz(1,:) = dth1;
dz(2,:) = dth2;
dz(3,:) = dth3;

M(1,1,:) = - I1 - d1.^2.*m1 - l1.^2.*m2 - l1.^2.*m3;
M(1,2,:) = - d2.*l1.*m2.*cos(th1 - th2) - l1.*l2.*m3.*cos(th1 - th2);
M(1,3,:) = -d3.*l1.*m3.*cos(th1 - th3);
M(2,1,:) = - d2.*l1.*m2.*cos(th1 - th2) - l1.*l2.*m3.*cos(th1 - th2);
M(2,2,:) = - I2 - d2.^2.*m2 - l2.^2.*m3;
M(2,3,:) = -d3.*l2.*m3.*cos(th2 - th3);
M(3,1,:) = -d3.*l1.*m3.*cos(th1 - th3);
M(3,2,:) = -d3.*l2.*m3.*cos(th2 - th3);
M(3,3,:) = - I3 - d3.^2.*m3;

f(1,:) = - d1.*g.*m1.*cos(th1) - g.*l1.*m2.*cos(th1) - g.*l1.*m3.*cos(th1) - d2.*dth2.^2.*l1.*m2.*sin(th1 - th2) - d3.*dth3.^2.*l1.*m3.*sin(th1 - th3) - dth2.^2.*l1.*l2.*m3.*sin(th1 - th2);
f(2,:) = d2.*dth1.^2.*l1.*m2.*sin(th1 - th2) - g.*l2.*m3.*cos(th2) - d2.*g.*m2.*cos(th2) - d3.*dth3.^2.*l2.*m3.*sin(th2 - th3) + dth1.^2.*l1.*l2.*m3.*sin(th1 - th2);
f(3,:) = d3.*dth1.^2.*l1.*m3.*sin(th1 - th3) - d3.*g.*m3.*cos(th3) + d3.*dth2.^2.*l2.*m3.*sin(th2 - th3);

for i=1:nTime
    MM = M(:,:,i);  ff = f(:,i);
    dz(4:6,i) = -MM \ ff;
end

end


function beampattern(N,d,w,steer_angle)
% beampattern(N,d,w,steer_angle)
% where:
% N = NO. OF ELEMENTS
% d = INTER-ELEMENT SPACINGS B/W ELEMENTS
% w = WEIGHTINGS
% steer_angle = STEERING ANGLE FOR BEAM PATTERN
% By: Muhammad Fahad
% 2005-MS-E-EE-38
if nargin < 4, steer_angle = 0; end    % for no steering direction
if nargin < 3, w = 1/N*ones(1,N); end   % for uniform linear arrays
if nargin < 2, d = 1/2; end   % for standard linear array

n = (-(N-1)/2:(N-1)/2).';
theta = pi*(-1:0.001:1);
u = cos(theta);
vv = exp(j*2*pi*d*n*u);
theta_T = steer_angle/180*pi;
ut=cos(theta_T);
W = w.*exp(j*n*pi*cos(theta_T))';
B = W*vv;
B = 10*log10(abs(B).^2);
figure
polardb(theta,B,-40)
return


% This function returns the oblique shock wave angle (beta) for a given
% deflection angle (theta) in degrees and ratio of specific heats (gamma).
% and Mach number M. Specify 0 for the weak oblique shock
% or 1 for the strong shock.
%
% Syntax:
% beta(M,theta,gamma,n)     where n specifies weak or strong shock returned
%
% NOTE: Angles supplied and returned from this function are in DEGREES.
%
% Based on an analytical solution to the theta-beta-Mach relation given in
% the following reference:  Rudd, L., and Lewis, M. J., "Comparison of
% Shock Calculation Methods", AIAA Journal of Aircraft, Vol. 35, No. 4,
% July-August, 1998, pp. 647-649.
%
% By Chris Plumley, undergraduate, University of Maryland.

function Beta=beta(M,theta,gamma,n)

theta=theta*pi/180;             % convert to radians
mu=asin(1/M);                   % Mach wave angle
c=tan(mu)^2;
a=((gamma-1)/2+(gamma+1)*c/2)*tan(theta);
b=((gamma+1)/2+(gamma+3)*c/2)*tan(theta);
d=sqrt(4*(1-3*a*b)^3/((27*a^2*c+9*a*b-2)^2)-1);
Beta=atan((b+9*a*c)/(2*(1-3*a*b))-(d*(27*a^2*c+9*a*b-2))/(6*a*(1-3*a*b))*tan(n*pi/3+1/3*atan(1/d)))*180/pi;


function [y,deriv] = sums_of_squares(w,m)
% This is a MV2DF. See MV2DF_API_DEFINITION.readme.
% Does:
% (i) Reshapes w to m-by-n.
% (ii) Computes sum of squares of each of n columns.
% (iii) Transposes to output n-vector.

if nargin==0
    test_this();
    return;
end

if isempty(w)
    y = @(w)sums_of_squares(w,m);
    return;
end

if isa(w,'function_handle')
    outer = sums_of_squares([],m);
    y = compose_mv(outer,w,[]);
    return;
end

M = reshape(w,m,[]);
y = sum(M.^2,1);
y = y(:);

deriv = @(g2) deriv_this(g2,M);

function [g,hess,linear] = deriv_this(g2,M)

g = 2*bsxfun(@times,M,g2.');
g = g(:);
linear = false;
hess = @(d) hess_this(d,g2,M);

function [h,Jv] = hess_this(d,g2,M)
h = deriv_this(g2,reshape(d,size(M)));
if nargout>1
    Jv = 2*sum(reshape(d,size(M)).*M,1);
    Jv = Jv(:);
end

function test_this()
f = sums_of_squares([],10);
test_MV2DF(f,randn(10*4,1));


function y = wshift(type,x,p)
%WSHIFT Shift Vector or Matrix.
%   Y = WSHIFT(TYPE,X,P) with TYPE = {1,'1','1d' or '1D'}
%   performs a P-circular shift of vector X.
%   The shift P must be an integer, positive for right to left
%   shift and negative for left to right shift.
%
%   Y = WSHIFT(TYPE,X,P) with TYPE = {2,'2','2d' or '2D'}
%   performs a P-circular shift of matrix X.
%   The shifts P must be integers. P(1) is the shift for rows
%   and P(2) is the shift for columns.
%
%   WSHIFT('1D',X) is equivalent to WSHIFT('1D',X,1)
%   WSHIFT('2D',X) is equivalent to WSHIFT('2D',X,[1 1])
%
%   Example 1D:
%     x = [1 2 3 4 5]
%     wshift('1D',x,1)  = [2 3 4 5 1]
%     wshift('1D',x,-1) = [5 1 2 3 4]
%
%   Example 2D:
%     x = [1 2 3;5 6 7]
%     wshift('2D',x,[1 1])  = [6 7 5;2 3 1]
%     wshift('2D',x,[-1,0]) = [5 6 7;1 2 3]

%   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 01-Dec-97.
%   Last Revision: 01-May-1998.
%   Copyright (c) 1995-98 by The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 1998/07/16 14:33:59 $

if isempty(x) | all(p==0) , y = x; return; end
switch type
  case {1,'1','1d','1D'}
    if nargin<3 , p = 1; end
    L = length(x);
    p = rem(p,L);
    if p<0 , p = L+p; end
    y = x([p+1:L,1:p]);

  case {2,'2','2d','2D'}
    if nargin<3 , p = [1 1]; end
    S = size(x);
    p = rem(p,S);
    k = (p<0);
    p(k) = S(k)+p(k);
    y = x([p(1)+1:S(1),1:p(1)],[p(2)+1:S(2),1:p(2)]);
end


function len = edgeLength(varargin)
%EDGELENGTH Return length of an edge
%
%   L = edgeLength(EDGE);
%   Returns the length of an edge, with parametric representation:
%   [x1 y1 x2 y2].
%
%   The function also works for several edges, in this case input is a
%   N-by-4 array, containing parametric representation of each edge, and
%   output is a N-by-1 array containing length of each edge.
%
%   See also:
%   edges2d, edgeAngle
%
%   ---------
%
%   author : David Legland
%   INRA - TPV URPOI - BIA IMASTE
%   created the 19/02/2004
%

%   HISTORY
%   15/04/2005 changes definition for edge, uses [x1 y1 x2 y2] instead of
%       [x0 y0 dx dy].

%   TODO : specify norm (euclidian, taxi, ...).

if nargin == 1
    % input is an edge [X1 Y1 X2 Y2]
    edge = varargin{1};
    len = hypot(edge(:,3)-edge(:,1), edge(:,4)-edge(:,2));

elseif nargin == 2
    % input are two points [X1 Y1] and [X2 Y2]
    p1 = varargin{1};
    p2 = varargin{2};
    len = hypot(p2(:,1)-p1(:,1), p2(:,2)-p1(:,2));

end


function g = std(f, varargin)
%STD   Standard deviation of a CHEBFUN3 along one variable.
%   G = STD(F) returns the standard deviation of F in the x-variable
%   (default). If F is defined on the cuboid [a, b] x [c, d] x [e, g] then
%
%                      b
%                     /
%     G.^2 = 1/(b-a)  | (F(x,y,z) - mean(F, 1))^2 dx
%                     /
%                     a
%
%   The output G is a CHEBFUN2 object over the rectangle [c, d] x [e, g].
%
%   G = STD(F, FLAG, DIM) takes the standard deviation along the x, y, or
%   z directions if DIM = 1, 2, or 3, respectively.  FLAG is ignored and
%   kept in this function so the syntax agrees with the Matlab STD command.
%
% See also CHEBFUN/STD, CHEBFUN2/STD, CHEBFUN2/STD2, CHEBFUN3/STD2 and
% CHEBFUN3/STD3.

% Copyright 2017 by The University of Oxford and The Chebfun Developers.
% See http://www.chebfun.org/ for Chebfun information.

% Empty check:
if ( isempty(f) )
    g = chebfun2();
    return
end

dom = f.domain;
if ( nargin < 3 )
    dim = 1;   % default to std over the x-variable.
elseif ( nargin == 3 )
    dim = varargin{ 2 };
else
    error( 'CHEBFUN:CHEBFUN3:std:nargin', 'Too many input arguments.' );
end

%   std(X) = sqrt( mean( (X - mean(X) )^2 ) ).
if ( dim == 1 )          % x-variable.
    mx = chebfun3(@(x,y,z) feval(mean(f, 1), y, z), dom);
    g = sqrt(1/(diff(dom(1:2))) * sum((f - mx).^2, 1)) ;

elseif ( dim == 2 )      % y-variable.
    my = chebfun3(@(x,y,z) feval(mean(f, 2), x, z), dom);
    g = sqrt(1/(diff(dom(3:4))) * sum((f - my).^2, 2));

elseif ( dim == 3 )      % z-variable.
    mz = chebfun3(@(x,y,z) feval(mean(f, 3), x, y), dom);
    g = sqrt(1/(diff(dom(5:6))) * sum((f - mz).^2, 3));

else
    error('CHEBFUN:CHEBFUN3:std:dim', ...
        'Third argument should have value 1, 2 or 3.');
end

end


function M = multinomialdoublystochasticfactory(n)
% Manifold of n-by-n doubly-stochastic matrices with positive entries.
%
% function M = multinomialdoublystochasticfactory(n)
%
% M is a Manopt manifold structure to optimize over the set of n-by-n
% matrices with (strictly) positive entries and such that the entries of
% each column and each row sum to one.
%
% Points on the manifold and tangent vectors are represented naturally as
% matrices of size n. The Riemannian metric imposed on the manifold is the
% Fisher metric, that is, if X is a point on the manifold and U, V are two
% tangent vectors:
%
%     M.inner(X, U, V) = <U, V>_X = sum(sum(U.*V./X)).
%
% The retraction here provided is only first order. Consequently, the
% slope test in the checkhessian tool is only valid at points X where the
% gradient is zero. Furthermore, if some entries of X are very close to
% zero, this may cause numerical difficulties that can also lead to a
% failed slope test. More generally, it is important that the solution of
% the optimization problem should have positive entries, sufficiently far
% away from zero to avoid numerical issues.
%
% The file is based on developments in the research paper
% A. Douik and B. Hassibi, "Manifold Optimization Over the Set
% of Doubly Stochastic Matrices: A Second-Order Geometry"
% ArXiv:1802.02628, 2018.
%
% Link to the paper: https://arxiv.org/abs/1802.02628.
%
% Please cite the Manopt paper as well as the research paper:
% @Techreport{Douik2018Manifold,
%   Title   = {Manifold Optimization Over the Set of Doubly Stochastic
%              Matrices: {A} Second-Order Geometry},
%   Author  = {Douik, A. and Hassibi, B.},
%   Journal = {Arxiv preprint ArXiv:1802.02628},
%   Year    = {2018}
% }
%
% See also: multinomialsymmetricfactory multinomialfactory

% This file is part of Manopt: www.manopt.org.
% Original author: Ahmed Douik, March 6, 2018.
% Contributors: Nicolas Boumal
% Change log:
%
%    Apr. 24, 2018 (AD):
%        Changed pinv() to a particular solution to the equation.
%
%    July 24, 2018 (AD):
%        A bugfix related to the pinv() change, with effects in many places.
%
%    Sep.  6, 2018 (NB):
%        Removed M.exp() as it was not implemented.
%
%    Aug. 19, 2019 (AD, BM, NB):
%        Fixed typos in comments; replaced some linear solves with pcg
%        for efficiency when n is large. By default, pcg is used:
%        comments in the code indicate other possibilities and how they
%        differ. Added maxDSiters to doubly_stochastic argument.
%        The main change has been to factor out these linear solves.

    e = ones(n, 1);

    maxDSiters = 100 + 2*n;

    function [alpha, beta] = mylinearsolve(X, b)
        % zeta = sparse(A)\b; % sparse might not be better perf.-wise.
        % where A = [eye(n) X ; X' eye(n)];
        %
        % For large n use the pcg solver instead of \.
        % [zeta, ~, ~, iter] = pcg(sparse(A), b, 1e-6, 100);
        %
        % Even faster is to create a function handle
        % computing A*x (x is a given vector).
        % Make sure that A is not created, and X is only
        % passed with mylinearsolve and not A.
        [zeta, ~, ~, iter] = pcg(@mycompute, b, 1e-6, 100);

        function Ax = mycompute(x)
            xtop = x(1:n,1);
            xbottom = x(n+1:end,1);
            Axtop = xtop + X*xbottom;
            Axbottom = X'*xtop + xbottom;
            Ax = [Axtop; Axbottom];
        end

        alpha = zeta(1:n, 1);
        beta = zeta(n+1:end, 1);
    end

    M.name = @() sprintf('%dx%d doubly-stochastic matrices with positive entries', n, n);

    M.dim = @() (n-1)^2;

    % Fisher metric
    M.inner = @iproduct;
    function ip = iproduct(X, eta, zeta)
        ip = sum((eta(:).*zeta(:))./X(:));
    end

    M.norm = @(X, eta) sqrt(M.inner(X, eta, eta));

    M.dist = @(X, Y) error('multinomialdoublystochasticfactory.dist not implemented yet.');

    % The manifold is not compact as a result of the choice of the metric,
    % thus any choice here is arbitrary. This is notably used to pick
    % default values of initial and maximal trust-region radius in the
    % trustregions solver.
    M.typicaldist = @() n;

    % Pick a random point on the manifold
    M.rand = @random;
    function X = random()
        Z = abs(randn(n, n));     % Random point in the ambient space
        X = doubly_stochastic(Z, maxDSiters); % Projection on the Manifold
    end

    % Pick a random vector in the tangent space at X.
    M.randvec = @randomvec;
    function eta = randomvec(X) % A random vector in the tangent space
        % A random vector in the ambient space
        Z = randn(n, n);
        % Projection of the vector onto the tangent space
        b = [sum(Z, 2) ; sum(Z, 1)'];
        [alpha, beta] = mylinearsolve(X, b);
        eta = Z - (alpha*e' + e*beta').*X;
        % Normalizing the vector
        nrm = M.norm(X, eta);
        eta = eta / nrm;
    end

    % Projection of vector eta in the ambient space to the tangent space.
    M.proj = @projection;
    function etaproj = projection(X, eta) % Projection of the vector eta in the ambeint space onto the tangent space
        b = [sum(eta, 2) ; sum(eta, 1)'];
        [alpha, beta] = mylinearsolve(X, b);
        etaproj = eta - (alpha*e' + e*beta').*X;
    end

    M.tangent = M.proj;
    M.tangent2ambient = @(X, eta) eta;

    % Conversion of Euclidean to Riemannian gradient
    M.egrad2rgrad = @egrad2rgrad;
    function rgrad = egrad2rgrad(X, egrad) % projection of the euclidean gradient
        mu = (X.*egrad);
        b = [sum(mu, 2) ; sum(mu, 1)'];
        [alpha, beta] = mylinearsolve(X, b);
        rgrad = mu - (alpha*e' + e*beta').*X;
    end

    % First-order retraction
    M.retr = @retraction;
    function Y = retraction(X, eta, t)
        if nargin < 3
            t = 1.0;
        end
        Y = X.*exp(t*(eta./X));
        Y = doubly_stochastic(Y, maxDSiters);
        Y = max(Y, eps);
    end

    % Conversion of Euclidean to Riemannian Hessian
    M.ehess2rhess = @ehess2rhess;
    function rhess = ehess2rhess(X, egrad, ehess, eta)

        % computing the directional derivative of the Riemannian
        % gradient
        gamma = egrad.*X;
        gammadot = ehess.*X + egrad.*eta;

        b = [sum(gamma, 2) ; sum(gamma, 1)'];
        bdot = [sum(gammadot, 2) ; sum(gammadot, 1)'];
        [alpha, beta] = mylinearsolve(X, b);
        [alphadot, betadot] = mylinearsolve(X, bdot- [eta*beta; eta'*alpha]);

        S = (alpha*e' + e*beta');
        deltadot = gammadot - (alphadot*e' + e*betadot').*X- S.*eta;

        % projecting gamma
        delta = gamma - S.*X;

        % computing and projecting nabla
        nabla = deltadot - 0.5*(delta.*eta)./X;
        rhess = projection(X, nabla);
    end

    % Miscellaneous manifold functions
    M.hash = @(X) ['z' hashmd5(X(:))];
    M.lincomb = @matrixlincomb;
    M.zerovec = @(X) zeros(n, n);
    M.transp = @(X1, X2, d) projection(X2, d);
    M.vec = @(X, U) U(:);
    M.mat = @(X, u) reshape(u, n, n);
    M.vecmatareisometries = @() false;

end


function [data clustPoints idx centers slopes lengths] = generateData( ...
    slope, ...
    slopeStd, ...
    numClusts, ...
    xClustAvgSep, ...
    yClustAvgSep, ...
    lengthAvg, ...
    lengthStd, ...
    lateralStd, ...
    totalPoints ...
    )
% generateData Generates 2D data for clustering; data is created along
%              straight lines, which can be more or less parallel depending
%              on slopeStd argument.
%
% Inputs:
%        slope - Base direction of the lines on which clusters are based.
%     slopeStd - Standard deviation of the slope; used to obtain a random
%                slope variation from the normal distribution, which is
%                added to the base slope in order to obtain the final slope
%                of each cluster.
%    numClusts - Number of clusters (and therefore of lines) to generate.
% xClustAvgSep - Average separation of line centers along the X axis.
% yClustAvgSep - Average separation of line centers along the Y axis.
%                line centers along each dimension.
%    lengthAvg - The base length of lines on which clusters are based.
%    lengthStd - Standard deviation of line length; used to obtain a random
%                length variation from the normal distribution, which is
%                added to the base length in order to obtain the final
%                length of each line.
%   lateralStd - "Cluster fatness", i.e., the standard deviation of the
%                distance from each point to the respective line, in both x
%                and y directions; this distance is obtained from the
%                normal distribution.
%  totalPoints - Total points in generated data (will be
%                randomly divided among clusters).
%
% Output:
%         data - Matrix (totalPoints x 2) with the generated data
%  clustPoints - Vector (numClusts x 1) containing number of points in each
%                cluster
%          idx - Vector (totalPoints x 1) containing the cluster indices of
%                each point
%      centers - Matrix (numClusts x 2) containing centers from where
%                clusters were generated
%       slopes - Vector (numClusts x 1) containing the effective slopes
%                used to generate clusters
%      lengths - Vector (numClusts x 1) containing the effective lengths
%                used to generate clusters
%
% ----------------------------------------------------------
% Usage example:
%
%   [data cp idx] = generateData(1, 0.5, 5, 15, 15, 5, 1, 2, 200);
%
% This creates 5 clusters with a total of 200 points, with a base slope
% of 1 (std=0.5), separated in average by 15 units in both x and y
% directions, with average length of 5 units (std=1) and a "fatness" or
% spread of 2 units.
%
% To take a quick look at the clusters just do:
%
%   scatter(data(:,1), data(:,2), 8, idx);
% ----------------------------------------------------------
%
%  N. Fachada
%  Instituto Superior Técnico
%  Aug 10, 2010
%  Updated: Aug 2, 2012

% Make sure totalPoints >= numClusts
if totalPoints < numClusts
    error('Number of points must be equal or larger than the number of clusters.');
end;

% Determine number of points in each cluster
clustPoints = abs(randn(numClusts, 1));
clustPoints = clustPoints / sum(clustPoints);
clustPoints = round(clustPoints * totalPoints);

% Make sure totalPoints is respected
while sum(clustPoints) < totalPoints
    % If one point is missing add it to the smaller cluster
    [C,I] = min(clustPoints);
    clustPoints(I(1)) = C + 1;
end;
while sum(clustPoints) > totalPoints
    % If there is one extra point, remove it from larger cluster
    [C,I] = max(clustPoints);
    clustPoints(I(1)) = C - 1;
end;

% Make sure there are no empty clusters
emptyClusts = find(clustPoints == 0);
if ~isempty(emptyClusts)
    % If there are empty clusters...
    numEmptyClusts = size(emptyClusts, 1);
    for i=1:numEmptyClusts
        % ...get a point from the largest cluster and assign it to the
        % empty cluster
        [C,I] = max(clustPoints);
        clustPoints(I(1)) = C - 1;
        clustPoints(emptyClusts(i)) = 1;
    end;
end;

% Initialize data matrix
data = zeros(sum(clustPoints), 2);

% Initialize idx (vector containing the cluster indices of each point)
idx = zeros(totalPoints, 1);

% Initialize lengths vector
lengths = zeros(numClusts, 1);

% Determine cluster centers
xCenters = xClustAvgSep * numClusts * (rand(numClusts, 1) - 0.5);
yCenters = yClustAvgSep * numClusts * (rand(numClusts, 1) - 0.5);
centers = [xCenters yCenters];

% Determine cluster slopes
slopes = slope + slopeStd * randn(numClusts, 1);

% Create clusters
for i=1:numClusts
    % Determine length of line where this cluster will be based
    lengths(i) = abs(lengthAvg + lengthStd*randn);
    % Determine how many points have been assigned to previous clusters
    sumClustPoints = 0;
    if i > 1
        sumClustPoints = sum(clustPoints(1:(i - 1)));
    end;
    % Create points for this cluster
    for j=1:clustPoints(i)
        % Determine where in the line the next point will be projected
        position = lengths(i) * rand - lengths(i) / 2;
        % Determine x coordinate of point projection
        delta_x = cos(atan(slopes(i))) * position;
        % Determine y coordinate of point projection
        delta_y = delta_x * slopes(i);
        % Get point distance from line in x coordinate
        delta_x = delta_x + lateralStd * randn;
        % Get point distance from line in y coordinate
        delta_y = delta_y + lateralStd * randn;
        % Determine the actual point
        data(sumClustPoints + j, :) = [(xCenters(i) + delta_x) (yCenters(i) + delta_y)];
    end;
    % Update idx
    idx(sumClustPoints + 1 : sumClustPoints + clustPoints(i)) = i;
end;


function [y Bitrate MSE Stepsize QNoise]=pcm(A,fm,fs,n)
%A=amplitute of cosine signal
%fm=frequency of cosine signal
%fs=sampling frequency
%n= number of bits per sample
%MSE=Mean Squar error, QNoise=Quantization Noise
%Example [y Bitrate MSE Stepsize QNoise]=pcm(2,3,20,3)
%If you have any problem or feedback please contact me @
%%===============================================
% NIKESH BAJAJ
% Asst. Prof., Lovely Professional University, India
% Almameter: Aligarh Muslim University, India
% +919915522564, bajaj.nikkey@gmail.com
%%===============================================

t=0:1/(100*fm):1;
x=A*cos(2*pi*fm*t);
%---Sampling-----
ts=0:1/fs:1;
xs=A*cos(2*pi*fm*ts);
%xs Sampled signal

%--Quantization---
x1=xs+A;
x1=x1/(2*A);
L=(-1+2^n); % Levels
x1=L*x1;
xq=round(x1);
r=xq/L;
r=2*A*r;
r=r-A;
%r quantized signal

%----Encoding---
y=[];
for i=1:length(xq)
    d=dec2bin(xq(i),n);
    y=[y double(d)-48];
end
%Calculations
MSE=sum((xs-r).^2)/length(x);
Bitrate=n*fs;
Stepsize=2*A/L;
QNoise=((Stepsize)^2)/12;

figure(1)
plot(t,x,'linewidth',2)
title('Sampling')
ylabel('Amplitute')
xlabel('Time t(in sec)')
hold on
stem(ts,xs,'r','linewidth',2)
hold off
legend('Original Signal','Sampled Signal');

figure(2)
stem(ts,x1,'linewidth',2)
title('Quantization')
ylabel('Levels L')
hold on
stem(ts,xq,'r','linewidth',2)
plot(ts,xq,'--r')
plot(t,(x+A)*L/(2*A),'--b')
grid
hold off
legend('Sampled Signal','Quantized Signal');

figure(3)
stairs([y y(length(y))],'linewidth',2)
title('Encoding')
ylabel('Binary Signal')
xlabel('bits')
axis([0 length(y) -1 2])
grid


function v = rowNorm(A)
%ROWNORM  norm of each row
%   V = ROWNORM(A) returns the Euclidean norm of each row of A. When A is
%   an M*N matrix, the output, V, will be a M*1 vector.
%
%   Babak taati May 4, 2005
%   revised May 19, 2009

if nargin ~= 1
    error('Requires one input arguments.')
end

v = sqrt(sum(A.*A, 2));


function bti_test02 ( )

%*****************************************************************************80
%
%% BTI_TEST02 tests BURGERS_TIME_INVISCID.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 April 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'BTI_TEST02\n' );
  fprintf ( 1, '  Test BURGERS_TIME_INVISCID\n' );

  method = 2;
  nx = 81;
  nt = 200;
  t_max = 2.0;
  bc = 4;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Method: 2, Upwind conservative.\n' );
  fprintf ( 1, '  Initial condition: expansion\n' );
  fprintf ( 1, '  Number of space nodes = %d\n', nx );
  fprintf ( 1, '  Number of time steps = %d\n', nt );
  fprintf ( 1, '  Final time T_MAX = %g\n', t_max );
  fprintf ( 1, '  Boundary condition = %d\n', bc );

  U = burgers_time_inviscid ( method, @ic_expansion, nx, nt, t_max, bc );

  x = linspace ( -1.0, +1.0, nx );

  figure ( 2 )

  plot ( x, U(1:50:(nt+1),:), 'Linewidth', 3 )
  grid on
  xlabel ( '<-- X -->' )
  ylabel ( '<-- U(X,T) -->' )
  title ( 'Burgers, inviscid, initial expansion, upwind conservative' )

  filename = 'bti_test02.png';
  print ( '-dpng', filename )
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Saved plot as "%s"\n', filename );

  return
end


%% ODF Estimation from EBSD data
%
%%
% In order to discuss ODF estimation from individual orientation data we
% start by loading an EBSD data set

mtexdata copper

plot(ebsd,ebsd.orientations)

%%
% of copper. The orientation distribution function can now be computed by

odf = calcDensity(ebsd('copper').orientations)

plotSection(odf,'contourf')
mtexColorMap LaboTeX
mtexColorbar

%%
% The function <orientation.calcODF.html calcODF> implements the ODF
% estimation from EBSD data in MTEX. The underlying statistical method is
% called kernel density estimation, which can be seen as a generalized
% histogram. To be more precise, let's $\psi : SO(3) \to R$ be a radially
% symmetric, unimodal model ODF. Then the kernel density estimator for the
% individual orientation data $o_1,o_2,\ldots,o_M$ is defined as
%
% $$f(o) = \frac{1}{M} \sum_{i=1}^{M} \psi(o o_i^{-1})$$
%
% The choice of the model ODF $\psi$ and in particular its halfwidth has a
% great impact in the resulting ODF. If no halfwidth is specified the
% default halfwidth of 10 degrees is selected.
%
%% Automatic halfwidth selection
%
% MTEX includes an automatic halfwidth selection algorithm which is called
% by the command <orientation.calcKernel.html calcKernel>. To work
% properly, this algorithm needs spatially independent EBSD data as in the
% case of this dataset of very rough EBSD measurements (only one
% measurement per grain).

% try to compute an optimal kernel
psi = calcKernel(ebsd.orientations)

%%
% In the above example, the EBSD measurements are spatial dependent and the
% resulting halfwidth is too small. To avoid this problem we have to perform
% grain reconstruction first and then estimate the halfwidth from the
% grains.

% grains reconstruction
grains = calcGrains(ebsd);

% correct for to small grains
grains = grains(grains.grainSize>5);

% compute optimal halfwidth from the meanorientations of grains
psi = calcKernel(grains('co').meanOrientation)

% compute the ODF with the kernel psi
odf = calcDensity(ebsd('co').orientations,'kernel',psi)

%%
% Once an ODF is estimated all the functionality MTEX offers for
% <ODFCharacteristics.html ODF analysis> and <ODFPlot.html ODF
% visualization> is available.

h = [Miller(1,0,0,odf.CS),Miller(1,1,0,odf.CS),Miller(1,1,1,odf.CS)];
plotPDF(odf,h,'antipodal','silent')

%% Effect of halfwidth selection
%
% As mentioned above a proper halfwidth selection is crucial for ODF
% estimation. The following simple numerical experiment illustrates the
% dependency between the kernel halfwidth and the estimated error.
%
% Let's start with a model ODF and simulate some individual orientation data.

modelODF = fibreODF(Miller(1,1,1,crystalSymmetry('cubic')),xvector);
ori = discreteSample(modelODF,10000)

%%
% Next we define a list of kernel halfwidth ,

hw = [1*degree, 2*degree, 4*degree, 8*degree, 16*degree, 32*degree];

%%
% estimate for each halfwidth an ODF and compare it to the original ODF.

e = zeros(size(hw));
for i = 1:length(hw)

  odf = calcDensity(ori,'halfwidth',hw(i),'silent');
  e(i) = calcError(modelODF, odf);

end

%%
% After visualizing the estimation error we observe that its value is large
% either if we choose a very small or a very large halfwidth.
% In this specific example, the optimal halfwidth seems to be about 4
% degrees.

close all
plot(hw/degree,e)
xlabel('halfwidth in degree')
ylabel('esimation error')


%  Figure 5.45  Feedback Control of Dynamic Systems, 6e
%                   Franklin, Powell, Emami

clf
numG=[1 0];
denG=[1 1 4];
K1=0:.1:8;   % K = 2 at breakin point
K2=100;
K=[K1 K2];
axis('square')
r=rlocus(numG,denG,K);    % roots vs. K
plot(r,'-'),nicegrid;
axis([-6 2 -4 4])
hold on
p=roots(denG);             % find and plot OL poles
z=roots(numG);             % find and plot OL zeros
plot(z(1),0,'o')
plot(real(p(1)),imag(p(1)),'x',real(p(2)),imag(p(2)),'x')
title('Fig. 5.45  Root Locus vs. K_T')
xlabel('Re(s)')
ylabel('Im(s)')
hold off
axis('normal')


function [s,lon,lat] = m_geodesic(lon1,lat1,lon2,lat2,Npoints,varargin)
% M_GEODESIC - Compute points along geodesics of an ellipsoidal earth.
%
%       [S,LON,LAT] = m_geodesic(lon1,lat1,lon2,lat2,Npoints,spheroid)
%
% lat1 = GEODETIC latitude of first point (degrees)
% lon1 = longitude of first point (degrees)
% lat2, lon2 = second point (degrees)
% Npoints = number of points along geodesic
% spheroid = (Optional) spheroid, defaults to 'wgs84'
% S = distance in meters
% LON,LAT = points on geodesics.
%
% Note that inputs can be the same size, or a mixture of scalars and matrices.
% However, output curves will be on columns of a matrix (i.e. form of
% inputs will not be preserved).
%
% See M_FDIST, M_IDIST, M_LLDIST

% R. pawlowicz (rich@ocgy.ubc.ca) 10/Jan/2005
%
%
% This software is provided "as is" without warranty of any kind. But
% it's mine, so you can't sell it.

% Do the equivalent of meshgrid-type calls to make all
% matrix sizes match. To do this we can have no more than
% two different numbers in any particular dimension, and one
% of these has to be a '1'.
allsize=[size(lon1);size(lat1);size(lon2);size(lat2)];
i1=ones(1,size(allsize,2));
for k=1:size(allsize,2)
 rs=unique(allsize(:,k));
 if length(rs)==2 && rs(1)==1
   j1=i1;j1(k)=rs(2);
   if allsize(1,k)==1,lon1=repmat(lon1,j1); end
   if allsize(2,k)==1,lat1=repmat(lat1,j1); end
   if allsize(3,k)==1,lon2=repmat(lon2,j1); end
   if allsize(4,k)==1,lat2=repmat(lat2,j1); end
 elseif length(rs)>2
  error('incompatible array sizes!');
 end
end

% Get the distances and bearings

[S,A12,A21]=m_idist(lon1,lat1,lon2,lat2,varargin{:});

s=[0:Npoints-1]'/(Npoints-1)*S(:)';

[lon,lat]=m_fdist(lon1(:)',lat1(:)',A12(:)',s,varargin{:});


% This code just simply run the SVM on the example data set "heart_scale",
% which is scaled properly. The code divides the data into 2 parts
% train: 1 to 200
% test: 201:270
% Then plot the results vs their true class. In order to visualize the high
% dimensional data, we apply MDS to the 13D data and reduce the dimension
% to 2D

clear
clc
close all

% read the data set
[heart_scale_label, heart_scale_inst] = libsvmread('heart_scale');
[N,D] = size(heart_scale_inst);

% Determine the train and test index
trainIndex = zeros(N,1); trainIndex(1:200) = 1;
testIndex = zeros(N,1); testIndex(201:N) = 1;
trainData = heart_scale_inst(trainIndex==1,:);
trainLabel = heart_scale_label(trainIndex==1,:);
testData = heart_scale_inst(testIndex==1,:);
testLabel = heart_scale_label(testIndex==1,:);

% Train the SVM
model = svmtrain(trainLabel, trainData, '-c 1 -g 0.07 -b 1');
% Use the SVM model to classify the data
[predict_label, accuracy, prob_values] = svmpredict(testLabel, testData, model, '-b 1'); % run the SVM model on the test data

% ================================
% ===== Showing the results ======
% ================================

% Assign color for each class
% colorList = generateColorList(2); % This is my own way to assign the color...don't worry about it
colorList = prism(100);

% true (ground truth) class
trueClassIndex = zeros(N,1);
trueClassIndex(heart_scale_label==1) = 1;
trueClassIndex(heart_scale_label==-1) = 2;
colorTrueClass = colorList(trueClassIndex,:);
% result Class
resultClassIndex = zeros(length(predict_label),1);
resultClassIndex(predict_label==1) = 1;
resultClassIndex(predict_label==-1) = 2;
colorResultClass = colorList(resultClassIndex,:);

% Reduce the dimension from 13D to 2D
distanceMatrix = pdist(heart_scale_inst,'euclidean');
newCoor = mdscale(distanceMatrix,2);

% Plot the whole data set
x = newCoor(:,1);
y = newCoor(:,2);
patchSize = 30; %max(prob_values,[],2);
colorTrueClassPlot = colorTrueClass;
figure; scatter(x,y,patchSize,colorTrueClassPlot,'filled');
title('whole data set');

% Plot the test data
x = newCoor(testIndex==1,1);
y = newCoor(testIndex==1,2);
patchSize = 80*max(prob_values,[],2);
colorTrueClassPlot = colorTrueClass(testIndex==1,:);
figure; hold on;
scatter(x,y,2*patchSize,colorTrueClassPlot,'o','filled');
scatter(x,y,patchSize,colorResultClass,'o','filled');
% Plot the training set
x = newCoor(trainIndex==1,1);
y = newCoor(trainIndex==1,2);
patchSize = 30;
colorTrueClassPlot = colorTrueClass(trainIndex==1,:);
scatter(x,y,patchSize,colorTrueClassPlot,'o');
title('classification results');


function geometry_test0415 ( )

%*****************************************************************************80
%
%% TEST0415 tests LINES_IMP_INT_2D.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    08 April 2009
%
%  Author:
%
%    John Burkardt
%
  dim_num = 2;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST0415\n' );
  fprintf ( 1, '  LINES_IMP_INT_2D finds the intersection of\n' );
  fprintf ( 1, '  two lines written in implicit form.\n' );
  fprintf ( 1, '\n' );
%
%  x + 2y - 4 = 0
%
  a1 =  1.0;
  b1 =  2.0;
  c1 = -4.0;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Line 1 coefficients:  %8f  %8f  %8f\n', a1, b1, c1 );
%
%  x - y - 1 = 0
%
  a2 =  1.0;
  b2 = -1.0;
  c2 = -1.0;
  fprintf ( 1, '  Line 2 coefficients:  %8f  %8f  %8f\n', a2, b2, c2 );

  [ ival, p ] = lines_imp_int_2d ( a1, b1, c1, a2, b2, c2 );

  if ( ival == 1 )
    fprintf ( 1, '  Intersection at %8f  %8f\n', p(1:dim_num) )
  elseif ( ival == 0 )
    fprintf ( 1, '  Lines are parallel, no intersection.\n' );
  elseif ( ival == 2 )
    fprintf ( 1, '  Lines are coincident.\n' );
  else
    fprintf ( 1, '  Unknown return value of ival = %d\n', ival );
  end
%
%  2x + 4y - 1 = 0
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Line 1 coefficients:  %8f  %8f  %8f\n', a1, b1, c1 );

  a2 =  2.0;
  b2 = +4.0;
  c2 = -1.0;
  fprintf ( 1, '  Line 2 coefficients:  %8f  %8f  %8f\n', a2, b2, c2 );

  [ ival, p ] = lines_imp_int_2d (a1, b1, c1, a2, b2, c2 );

  if ( ival == 1 )
    fprintf ( 1, '  Intersection at %8f  %8f\n', p(1:dim_num) );
  elseif ( ival == 0 )
    fprintf ( 1, '  Lines are parallel, no intersection.\n' );
  elseif ( ival == 2 )
    fprintf ( 1, '  Lines are coincident.\n' );
  else
    fprintf ( 1, '  Unknown return value of ival = %d\n', ival );
  end
%
%  -3x - 6y +12 = 0
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Line 1 coefficients:  %8f  %8f  %8f\n', a1, b1, c1 );

  a2 =  -3.0;
  b2 =  -6.0;
  c2 = +12.0;
  fprintf ( 1, '  Line 2 coefficients:  %8f  %8f  %8f\n', a2, b2, c2 );

  [ ival, p ] = lines_imp_int_2d  ( a1, b1, c1, a2, b2, c2 );

  if ( ival == 1 )
    fprintf ( 1, '  Intersection at %8f  %8f\n', p(1:dim_num) );
  elseif ( ival == 0 )
    fprintf ( 1, '  Lines are parallel, no intersection.\n' );
  elseif ( ival == 2 )
    fprintf ( 1, '  Lines are coincident.\n' );
  else
    fprintf ( 1, '  Unknown return value of ival = %d\n', ival );
  end

  return
end