function tparameters=scalar_vt_vech_transform(parameters,p,o,q,kappa)
% SCALAR_VT_VECH(P,Q) parameter transformation.  Used to map parameters
% from a scalar MVGARCH process to the real line. Used in the estimation of SCALAR_VT_VECH.
%
% USAGE:
%   [TPARAMETERS]=scalar_vt_vech_transform(PARAMETERS,P,O,Q,KAPPA)
%
% INPUTS:
%   PARAMETERS       - Column parameter vector
%   P                - Positive, scalar integer representing the number of symmetric innovations
%   Q                - Non-negative, scalar integer representing the number of lags of conditional variance
%
% OUTPUTS:
%   TPARAMETERS      - A 1+p+q column vector of transformed parameters corresponding to
%                      [alpha(1),...,alpha(p), beta1 ... beta(q)]'
%
% COMMENTS:
%   Input parameters must satisfy:
%    (1) alpha(i) >= 0 for i = 1,2,...,p
%    (2) beta(i)  >= 0 for i = 1,2,...,q
%    (3) sum(alpha) + sum(beta) < 1
%
% See also SCALAR_VT_VECH

% Copyright: Kevin Sheppard
% kevin.sheppard@economics.ox.ac.uk
% Revision: 3    Date: 9/1/2005

if size(parameters,2)>size(parameters,1)
   parameters = parameters';
end
%Upper bound to keep it a bit away from 1
UB=.999998;

alpha=parameters(1:p);
gamma = parameters(p+1:p+o);
beta=parameters(p+o+1:p+o+q);
%Check that the parameters satisfy the necessary constraints
if  any(alpha<0) || any(beta<0) || any(gamma<0) || (sum(alpha)+sum(gamma)/kappa+sum(beta))>=UB
    error('These do not conform to the necessary set of restrictions to be transformed.')
end

%Alpha, beta cannot be exactly zero or there will be problems with log()
alpha(alpha<1e-8)=1e-8;
gamma(gamma<1e-8)=1e-8;
beta(beta<1e-8)=1e-8;
%Up the upper bound a small amount to make sure it is satisfied
UB=UB+1e-8*(p+o+q);

%Set the scale
scale=UB;
%Initialize the transformed parameters
parameters=[alpha;gamma;beta];
tparameters=[alpha;gamma;beta];
for i=1:(p)
    %Scale the parameters
    tparameters(i)=tparameters(i)/scale;
    %Use an inverse logistic
    tparameters(i)=log(tparameters(i)/(1-tparameters(i)));
    %Update the scale
    scale=scale-parameters(i);
end
for i=p+1:p+o
    %Scale the parameters
    tparameters(i)=tparameters(i)/scale/kappa;
    %Use an inverse logistic
    tparameters(i)=log(tparameters(i)/(1-tparameters(i)));
    %Update the scale
    scale=scale-parameters(i)/kappa;
end

for i=p+o+1:p+o+q
    %Scale the parameters
    tparameters(i)=tparameters(i)/scale;
    %Use an inverse logistic
    tparameters(i)=log(tparameters(i)/(1-tparameters(i)));
    %Update the scale
    scale=scale-parameters(i);
end


function fem2d_pack_test16 ( )

%*****************************************************************************80
%
%% TEST16 tests REFERENCE_TO_PHYSICAL_T6.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  n = 16;

  ref = [ ...
    0.00, 0.00; ...
    1.00, 0.00; ...
    0.00, 1.00; ...
    0.50, 0.00; ...
    0.50, 0.50; ...
    0.00, 0.50; ...
    0.25, 0.75; ...
    0.75, 0.25; ...
    0.40, 0.10; ...
    0.30, 0.20; ...
    0.20, 0.30; ...
    0.10, 0.40; ...
    0.10, 0.10; ...
    0.20, 0.20; ...
    0.30, 0.30; ...
    0.40, 0.40 ]';
  t = [ ...
    0.0, 0.0; ...
    2.0, 0.0; ...
    0.0, 4.0; ...
    1.0, 0.0; ...
    1.0, 1.0; ...
    0.0, 2.0 ]';

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST16\n' );
  fprintf ( 1, '  For an order 6 triangle,\n' );
  fprintf ( 1, '  REFERENCE_TO_PHYSICAL_T6 maps a reference point to\n' );
  fprintf ( 1, '    a physical point.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      XSI     ETA  ==>  X	Y\n' );
  fprintf ( 1, '\n' );

  phy = reference_to_physical_t6 ( t, n, ref );

  for j = 1 : n
    fprintf ( 1, '  %8f  %8f  %8f  %8f\n', ref(1:2,j), phy(1:2,j) );
  end

  return
end


function [X,Y,indsort] = grid_communities(c)
% GRID_COMMUNITIES       Outline communities along diagonal
%
%   [X Y INDSORT] = GRID_COMMUNITIES(C) takes a vector of community
%   assignments C and returns three output arguments for visualizing the
%   communities. The third is INDSORT, which is an ordering of the vertices
%   so that nodes with the same community assignment are next to one
%   another. The first two arguments are vectors that, when overlaid on the
%   adjacency matrix using the PLOT function, highlight the communities.
%
%   Example:
%
%   >> load AIJ;                                % load adjacency matrix
%   >> [C,Q] = modularity_louvain_und(AIJ);     % get community assignments
%   >> [X,Y,INDSORT] = fcn_grid_communities(C); % call function
%   >> imagesc(AIJ(INDSORT,INDSORT));           % plot ordered adjacency matrix
%   >> hold on;                                 % hold on to overlay community visualization
%   >> plot(X,Y,'r','linewidth',2);             % plot community boundaries
%
%   Inputs:     C,       community assignments
%
%   Outputs:    X,       x coor
%               Y,       y coor
%               INDSORT, indices
%
%   Richard Betzel, Indiana University, 2012
%

%#ok<*AGROW>

nc = max(c);
[c,indsort] = sort(c);

X = [];
Y = [];
for i = 1:nc
    ind = find(c == i);
    if ~isempty(ind)
        mn = min(ind) - 0.5;
        mx = max(ind) + 0.5;
        x = [mn mn mx mx mn NaN];
        y = [mn mx mx mn mn NaN];
        X = [X, x];
        Y = [Y, y];
    end
end


function denoise_PRL_2010_main
%
% This is a demo program of the paper J. Tian, W. Yu, and L. Ma, "AntShrink: Ant
% colony optimization for image shrinkage," Pattern Recognition Letters,
% Vol. 31, Oct. 2010, pp. 1751-1758.
%
% Contact: eejtian@gmail.com
%
% Note that the PSNR values could be slightly different with that reported
% in paper due to the random number generator used to generate noisy image.
%
% The input image should have a square size.
%
% Acknowledgement: This program needs Wavelet transform toolbox, which is
% downloaded from http://www-stat.stanford.edu/~wavelab/.

clear all; close all; clc;

% Load the ground truth image
img_truth = double(imread('barbara_truth.bmp'));
[nRow, nColumn] = size(img_truth);

% Use the ground truth image to generate the noisy image
noise_sig_truth = 10; % sigma_n used in the paper. This parameter is adjusted by the user.
noise_mu = 0;
img_noisy = img_truth + randn(size(img_truth)) .* noise_sig_truth + noise_mu;

% wavelet parameters
wbase = 'Daubechies';
mom = 8;
dwt_level = 5; %note that here, dwt_scale means the decomposition level of the DWT
[n,J] = func_quadlength(img_truth);
L = J-dwt_level;%here, L means the size of the coarsest level, 2^L

win_size = 2;
img_denoised = zeros(size(img_noisy));

% Since this is time consuming approach, divide the image into four parts, then
% conduct denoising for each part
for ii=1:4
    win_size=2;

    switch ii
        case 1
            img_denoised(1:end/2,1:end/2) = func_ACOShrink(img_noisy(1:end/2,1:end/2), wbase, mom, dwt_level, win_size);
        case 2
            img_denoised(end/2+1:end,1:end/2) = func_ACOShrink(img_noisy(end/2+1:end,1:end/2), wbase, mom, dwt_level, win_size);
        case 3
            img_denoised(1:end/2,end/2+1:end) = func_ACOShrink(img_noisy(1:end/2,end/2+1:end), wbase, mom, dwt_level, win_size);
        case 4
            img_denoised(end/2+1:end,end/2+1:end) = func_ACOShrink(img_noisy(end/2+1:end,end/2+1:end), wbase, mom, dwt_level, win_size);
    end

end

% Calculate the PSNR performance
fprintf('PSNR=%.2fdB\n', func_psnr_gray(img_truth, img_denoised));

% Write the output image
imwrite(uint8(img_denoised), 'barbara_denoised.bmp','bmp');

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
% Main algorithm of proposed AntShrink algorithm
function x_out= func_ACOShrink(x_in, wbase, mom, dwt_level, win_size)

[nrow, ncol] = size(x_in);
L = log2(size(x_in,2))-dwt_level;

% Estimate the noise_sigma from the noisy signal
qmf = func_MakeONFilter(wbase,mom);
[temp, coef] = func_NormNoise_2d(x_in, qmf);
noise_sigma = 1/coef;

wx  = func_FWT2_PO(x_in, L, qmf);
[n,J] = func_dyadlength(wx);
ws = wx;

rr = (meshgrid(1:nrow))'; cc = meshgrid(1:ncol);

ant_search_range_row_min = rr;
ant_search_range_row_max = rr;
ant_search_range_col_min = cc;
ant_search_range_col_max = cc;

for j=(J-1):-1:L
    [t1,t2] = func_dyad2HH(j);
    ant_search_range_row_min(t1,t2) = min2(t1);
    ant_search_range_row_max(t1,t2) = max2(t1);
    ant_search_range_col_min(t1,t2) = min2(t2);
    ant_search_range_col_max(t1,t2) = max2(t2);
    [t1,t2] = func_dyad2HL(j);
    ant_search_range_row_min(t1,t2) = min2(t1);
    ant_search_range_row_max(t1,t2) = max2(t1);
    ant_search_range_col_min(t1,t2) = min2(t2);
    ant_search_range_col_max(t1,t2) = max2(t2);
    [t1,t2] = func_dyad2LH(j);
    ant_search_range_row_min(t1,t2) = min2(t1);
    ant_search_range_row_max(t1,t2) = max2(t1);
    ant_search_range_col_min(t1,t2) = min2(t2);
    ant_search_range_col_max(t1,t2) = max2(t2);
end

data_var = func_signal_variance_estimation(wx, noise_sigma, win_size,ant_search_range_row_min,ant_search_range_row_max,ant_search_range_col_min,ant_search_range_col_max,J,L);

for j=(J-1):-1:L
    [t1,t2] = func_dyad2HH(j);
    ws(t1,t2) = wx(t1,t2) .* data_var(t1,t2) ./ (data_var(t1,t2) + noise_sigma.^2);
    [t1,t2] = func_dyad2HL(j);
    ws(t1,t2) = wx(t1,t2) .* data_var(t1,t2) ./ (data_var(t1,t2) + noise_sigma.^2);
    [t1,t2] = func_dyad2LH(j);
    ws(t1,t2) = wx(t1,t2) .* data_var(t1,t2) ./ (data_var(t1,t2) + noise_sigma.^2);
end

x_out  = func_IWT2_PO(ws, L, qmf);

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
% Estimate the signal variance value, which will be used for shrinkage
function result = func_signal_variance_estimation(wx, noise_sigma, win_size,ant_search_range_row_min,ant_search_range_row_max,ant_search_range_col_min,ant_search_range_col_max,J,L)

% System setup
ant_move_step_within_iteration = 5; % the numbe of iterations?
total_iteration_num = 5;
search_clique_mode = 8;

wx = abs(wx);
[nrow, ncol] = size(wx);
wx_1D = func_2D_LexicoOrder(wx);

% initialization
h = zeros(size(wx));
h_1D = func_2D_LexicoOrder(h);
p = 1./(wx.*(wx~=0)+1.*(wx==0)).*(wx~=0)+(wx==0);
p_1D = func_2D_LexicoOrder(p);

%paramete setting
alpha = 1;
beta = 2;
rho = 0.1;
w = 0.6;
A = 5000;
B = 10;

%use one ant for each pixel position
ant_total_num = nrow*ncol;
ant_current_row = zeros(ant_total_num, 1); % record the location of ant
ant_current_col = zeros(ant_total_num, 1); % record the location of ant
ant_current_val = zeros(ant_total_num, 1); % record the location of ant
rr = (meshgrid(1:nrow))'; cc = meshgrid(1:ncol);
ant_current_row = rr(:);
ant_current_col = cc(:);
ant_current_val = wx_1D((ant_current_row-1).*ncol+ant_current_col);

ant_search_range_row_min = ant_search_range_row_min(:);
ant_search_range_row_min = padarray(ant_search_range_row_min, [0 search_clique_mode-1],'replicate','post');
ant_search_range_row_max = ant_search_range_row_max(:);
ant_search_range_row_max = padarray(ant_search_range_row_max, [0 search_clique_mode-1],'replicate','post');
ant_search_range_col_min = ant_search_range_col_min(:);
ant_search_range_col_min = padarray(ant_search_range_col_min, [0 search_clique_mode-1],'replicate','post');
ant_search_range_col_max = ant_search_range_col_max(:);
ant_search_range_col_max = padarray(ant_search_range_col_max, [0 search_clique_mode-1],'replicate','post');

for nIteration = 1: total_iteration_num

        ant_current_path_val_mean = zeros(ant_total_num,1);
        ant_current_path_val_mean = ant_current_val;

        for nMoveStep = 1: ant_move_step_within_iteration-1

            if search_clique_mode == 4
                ant_search_range_row = [ant_current_row-1, ant_current_row, ant_current_row+1, ant_current_row];
                ant_search_range_col = [ant_current_col, ant_current_col+1, ant_current_col, ant_current_col-1];
            elseif search_clique_mode == 8
                ant_search_range_row = [ant_current_row-1, ant_current_row-1, ant_current_row-1, ant_current_row, ant_current_row,ant_current_row+1, ant_current_row+1, ant_current_row+1];
                ant_search_range_col = [ant_current_col-1, ant_current_col, ant_current_col+1, ant_current_col-1, ant_current_col+1, ant_current_col-1, ant_current_col, ant_current_col+1];
            end

            ant_current_row_extend = padarray(ant_current_row, [0 search_clique_mode-1],'replicate','post');
            ant_current_col_extend = padarray(ant_current_col, [0 search_clique_mode-1],'replicate','post');
            ant_search_range_val = zeros(ant_total_num,search_clique_mode);

            %replace the positions our of the image's range
            temp = (ant_search_range_row>=ant_search_range_row_min) & (ant_search_range_row<=ant_search_range_row_max) & (ant_search_range_col>=ant_search_range_col_min) & (ant_search_range_col<=ant_search_range_col_max);
            ant_search_range_row = temp.*ant_search_range_row + (~temp).*ant_current_row_extend;
            ant_search_range_col = temp.*ant_search_range_col + (~temp).*ant_current_col_extend;

            ant_search_range_transit_prob_h = zeros(size(ant_search_range_val));
            ant_search_range_transit_prob_p = zeros(size(ant_search_range_val));

            for ii=1:search_clique_mode
                ant_search_range_val(:,ii) = wx_1D((ant_search_range_row(:,ii)-1).*ncol+ant_search_range_col(:,ii));

                temp = ant_search_range_val(:,ii);
                temp = abs(temp - ant_current_path_val_mean);
                temp = 1./(temp.*(temp~=0)+1.*(temp==0)) .* (temp~=0)+(temp==0);

                ant_search_range_transit_prob_h(:,ii) = temp;
                ant_search_range_transit_prob_p(:,ii) = p_1D((ant_search_range_row(:,ii)-1).*ncol+ant_search_range_col(:,ii));

            end
            temp = (ant_search_range_transit_prob_h.^alpha) .* (ant_search_range_transit_prob_p.^beta);

            temp_sum = sum(temp,2);
            temp_sum = padarray(temp_sum, [0 search_clique_mode-1],'replicate','post');

            ant_search_range_transit_prob = temp ./ temp_sum;

            % generate a random number to determine the next position.
            rand('state', sum(100*clock));
            temp = rand(ant_total_num,1);
            temp = padarray(temp, [0 search_clique_mode-1],'replicate','post');
            temp = cumsum(ant_search_range_transit_prob,2)>=temp;
            temp = padarray(temp, [0 1],'pre');
            temp = (diff(temp,1,2)==1);

            temp_row = (ant_search_range_row .* temp)';
            [ii, jj, vv] = find(temp_row);
            ant_next_row = vv;
            temp_col = (ant_search_range_col .* temp)';
            [ii, jj, vv] = find(temp_col);
            ant_next_col = vv;

            ant_current_row = ant_next_row;
            ant_current_col = ant_next_col;
            ant_current_val = wx_1D((ant_current_row-1).*ncol+ant_current_col);
            ant_current_path_val_mean = (ant_current_path_val_mean.*nMoveStep + ant_current_val)/(nMoveStep+1);

            %update p;
            rr = ant_current_row;
            cc = ant_current_col;
            p_1D((rr-1).*ncol+cc,1) = w*p_1D((rr-1).*ncol+cc,1) + 1./(A+B.*ant_current_path_val_mean);
            p = func_LexicoOrder_2D(p_1D, nrow, ncol);

        end % end of nMoveStep

        p = (1-rho).*p;
        p_1D = func_2D_LexicoOrder(p);

end % end of nIteration

clear h h_1D temp
clear ant_current_row ant_current_col ant_current_val
clear ant_current_path_val_mean
clear ant_search_range_row search_range_path_col ant_search_range_val
clear ant_search_range_transit_prob_h ant_search_range_transit_prob_v ant_search_range_transit_prob
clear ant_search_range_row_min ant_search_range_row_max ant_search_range_col_min ant_search_range_col_max

result = wx.^2;

for j=(J-1):-1:L
    [t1,t2] = func_dyad2HH(j);
    result(t1,t2) = func_determine_class_fcm(wx(t1,t2), p(t1,t2), win_size, noise_sigma);
    [t1,t2] = func_dyad2HL(j);
    result(t1,t2) = func_determine_class_fcm(wx(t1,t2), p(t1,t2), win_size, noise_sigma);
    [t1,t2] = func_dyad2LH(j);
    result(t1,t2) = func_determine_class_fcm(wx(t1,t2), p(t1,t2), win_size, noise_sigma);
end

% ******************************************************************************
% **************************Inner Function *************************************
% ******************************************************************************
% Bi-class classification algorithm using Fuzzy C-means
function result = func_determine_class_fcm(wx, p, win_size, noise_sigma)

p_1D = func_2D_LexicoOrder(p);
wx_1D = func_2D_LexicoOrder(wx);
[nrow, ncol] = size(wx);

nFeature = p_1D(:)./(max(max(p_1D)));
[center,U,obj_fcn] = fcm(nFeature, 2,[2.0 100 1e-5 0]);

idx = zeros(nrow:ncol,1);
if sum(sum(wx_1D(U(1,:) >= U(2,:)))) >= sum(sum(wx_1D(U(1,:) < U(2,:))))
    idx = U(1,:) >= U(2,:);
else
    idx = U(1,:) < U(2,:);
end
idx = func_LexicoOrder_2D(idx, nrow, ncol);

center_class = idx(:);
padnum = win_size;
A = padarray(wx, [padnum padnum], 'replicate', 'bot');
B = padarray(idx, [padnum padnum], 'replicate', 'bot');

A = im2col(A, [win_size*2+1 win_size*2+1],'sliding');
A = A.^2;
B = im2col(B, [win_size*2+1 win_size*2+1],'sliding');

C = padarray(center_class', [size(A,1)-1, 0], 'replicate', 'post');

result = sum(A,1) ./ (win_size*2+1)./(win_size*2+1);
result = reshape(result, [nrow, ncol]);
result = (result>=noise_sigma^2).*(result-noise_sigma^2);

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
% Convert data from 2D format to 1D format
function result = func_2D_LexicoOrder(x)
temp = x';
result = temp(:);

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
% Convert data from 1D format to 2D format
function result = func_LexicoOrder_2D(x, nRow, nColumn)
result = reshape(x, nColumn, nRow)';

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
function result=min2(f)
%calculate minimum of 2D matrix
result=min(min(f));

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
function result=max2(f)
%calculate maximum of 2D matrix
result=max(max(f));

%-------------------------------------------------------------------------
%------------------------------Inner Function ----------------------------
%-------------------------------------------------------------------------
% Calculate the PSNR performance to two images
function result = func_psnr_gray(f, g)

f = double(f);
g = double(g);
Q=255; MSE=0;
[M,N]=size(f);
h = f - g;
MSE = sum(sum(h.*h));
MSE=MSE/M/N;
result=10*log10(Q*Q/MSE);


function a = distance_ld(A,B)

a = sqrt(log(det((A+B)/2))-0.5*log(det(A*B)));


function[varargout]=cellsum(varargin)
%CELLSUM  Sum of each element a cell array, possibly weighted.
%
%   S=CELLSUM(X) where X is a cell array of N arrays, is equivalent to
%
%      S(1,1)=SUM(X{1}(:)),  S(2,1)=SUM(X{2}(:)), ...,  S(N,1)=SUM(X{N}(:))
%
%   thus returning an N x 1 array containing the sum over all values of
%   each element in the cell array.
%
%   In taking the sum, non-finite values are ignored, as in VSUM.
%   S is a column vector of the same length as X.
%
%   [S1,S2,...,SP]=CELLSUM(X1,X2,...,XP) also works for P different
%   input arguments.
%
%   CELLSUM(X1,X2,...XP);  with no output arguments overwrites the
%   original input variables.
%   __________________________________________________________________
%
%   Weighted sums
%
%   CELLSUM(X,'weight',W) or CELLSUM(X1,X2,...,XP,'weight',W) where W is
%   a cell array of the same size as the other input variables, computes
%   the weighted sum, using the weighting factor ABS(W).^2.
%   __________________________________________________________________
%
%   Parallelization
%
%   CELLSUM(...,'parallel') parallelizes the computation using a PARFOR
%   loop.  This requires that Matlab's Parallel Computing Toolbox be
%   installed, and is useful for very large datasets.
%   __________________________________________________________________
%
%   See also CELLSTD, CELLMED, JCELL.
%
%   Usage: s=cellsum(x);
%          [s1,s2,s3]=cellsum(x1,x2,x3);
%          cellsum(x1,x2,x3,'weight',w);
%          cellsum(x1,x2,x3);
%          cellsum(x1,x2,x3,'parallel');
%   __________________________________________________________________
%   This is part of JLAB --- type 'help jlab' for more information
%   (C) 2014--2019 J.M. Lilly --- type 'help jlab_license' for details

if ~iscell(varargin{1})
    error('X must be a cell array.')
end

weight=[];
cores='serial';

if ischar(varargin{end})
    if strcmpi(varargin{end}(1:3),'par')||strcmpi(varargin{end}(1:3),'ser')
        cores=varargin{end};
    end
    varargin=varargin(1:end-1);
end

if length(varargin)>1
    if ischar(varargin{end-1})
        if strcmpi(varargin{end-1}(1:3),'wei')
            weight=varargin{end};
        end
        varargin=varargin(1:end-2);
    end
end

if strcmpi(cores(1:3),'par')
    if exist('parpool')~=2
        disp('Sorry, parallel algorithm requires the Parallel Computing Toolbox.')
        disp('Defaulting to the standard algorithm.')
        cores='serial';
    end
end

for i=1:length(varargin)
    if isempty(weight)
        varargout{i}=cellsum_one_unweighted(varargin{i},cores);
    else
        varargout{i}=cellsum_one_weighted(varargin{i},weight,cores);
    end
end

eval(to_overwrite(length(varargin)))

function[y]=cellsum_one_unweighted(x,cores)

y=nan*zeros(length(x),1);
if strcmpi(cores(1:3),'par')
    parfor i=1:length(x)
        y(i)=sum(x{i}(:),1);
        if ~isfinite(y(i))
            y(i)=vsum(x{i}(:),1);
        end
    end
else
    for i=1:length(x)
        y(i)=sum(x{i}(:),1);
        if ~isfinite(y(i))
            y(i)=vsum(x{i}(:),1);
        end
    end
end

function[y]=cellsum_one_weighted(x,weight,cores)

y=nan*zeros(length(x),1);
if strcmpi(cores(1:3),'par')
    parfor i=1:length(x)
        w=squared(weight{i}(:));
        y(i)=sum(w.*x{i}(:),1);
        if ~isfinite(y(i))
            y(i)=vsum(w.*x{i}(:),1);
        end
    end
else
    for i=1:length(x)
        w=squared(weight{i}(:));
        y(i)=sum(w.*x{i}(:),1);
        if ~isfinite(y(i))
            y(i)=vsum(w.*x{i}(:),1);
        end
    end
end


function ispy(A,sizeMultiplier)
%ISPY Visualize sparsity pattern and the size of the elements of a matrix.
%
%   ISPY(S) plots the sparsity pattern of the matrix S and visualizes the
%   size and sign of its elements. Positive elements are plotted in
%   red and negative elements in blue. Zero elements are not plotted.
%   The size of an element is scaled logarithmically.
%
%   The imaginary part of any number is discarded.
%   NaNs are plotted as red stars on a yellow background.
%   Infs are plotted as blue stars on a green background.
%
%   If the matrix is very big, try zooming in to get a more accurate view.
%
%   If the visualisation should rely on coloring instead of sizes, or if the
%   matrix is really big, then you should consider using imagesc instead.
%
%   sizeMultiplier allows for manual tuning of the visualized sizes.
%
%   Examples:
%      ispy(gallery('wathen',15,15))
%      ispy(gallery('orthog',50,5))
%
%   See also SPY and IMAGESC.

%   -- v1.0 / 2013-08-20 / Samu Alanko, Patrick N. Raanes

% Input checks
if nargin<2
    sizeMultiplier = 1;
end
if nnz(A) == 0
    fprintf('Matrix is empty! Exiting. \n');
    return;
end

% Sizes
[M,N] = size(A);

% We neglect the complex parts of the elements
A = real(sparse(A));

% All nonzero elements
[Xnz,Ynz,Snz] = find(A);
% Finite elems
inds = isfinite(Snz);
X = Xnz(inds); Y = Ynz(inds); S = Snz(inds);
% Infinite elems
inds = isinf(Snz);
Xinf = Xnz(inds); Yinf = Ynz(inds);
% NaN elems
inds = isnan(Snz);
Xnan = Xnz(inds); Ynan = Ynz(inds);

% Get indices of positive and negative elements
posind = S>0;
negind = S<0;

% Set up figure
fh = gcf; % fig handle
ah = gca; % axes handle
xlim([0 N+1]); ylim([0 M+1]);

% Main routine
drawelems();

% Beautify
title(inputname(1));
box on;
set(ah, 'YDir', 'reverse')

% Draws the elements that are inside the current view (makes zoom fast)
function drawelems()

    % Get axes size and multiply the sizeMultiplier by the
    % ratio (axes area)/(standard axes area)
    [axW,axH] = getAxesSize(ah);
    sm = sizeMultiplier * axW*axH/1.2e5;

    % Elements included in current zoom
    xlims = get(ah,'YLim');
    ylims = get(ah,'XLim');
    width = abs(xlims(2)-xlims(1));

    % Select the elements that are inside the current view
    ninds = negind & X>=xlims(1) & X<=xlims(2) & Y>=ylims(1) & Y<=ylims(2);
    pinds = posind & X>=xlims(1) & X<=xlims(2) & Y>=ylims(1) & Y<=ylims(2);

    nn = sum(ninds); % num of neg elems
    np = sum(pinds); % num of pos elems

    Xp = [X(ninds); X(pinds)];
    Yp = [Y(ninds); Y(pinds)];
    Sp = [S(ninds); S(pinds)];

    % Markersizes - constants
    minmarkersize = 4;              % Minimum marker size
    maxmarkersize = 40*sm;          % Maximum marker size
    markersize    = 12*40*sm/width; % Average size

    % Markersize of elements
    meanval = mean(abs(Sp));
    Sp = markersize.*(log(abs(Sp)./meanval+1)/log(2));
    Sp = min(max(Sp,minmarkersize*ones(size(Sp))),maxmarkersize*ones(size(Sp)));
    Sp = markersize*Sp/mean(abs(Sp));

    % Plot
    cla; hold on;
    scatter(Yp(1:nn),    Xp(1:nn),    Sp(1:nn),    'b','filled'); % neg elems
    scatter(Yp(nn+1:end),Xp(nn+1:end),Sp(nn+1:end),'r','filled'); % pos elems

    % NaN and Inf
    if ~isempty(Xnan)
        scatter(Ynan,Xnan,1000*sm,'y.');
        scatter(Ynan,Xnan,80*sm,'r*');
    end
    if ~isempty(Xinf)
        scatter(Yinf,Xinf,1000*sm,'g.');
        scatter(Yinf,Xinf,80*sm,'b*');
    end

    hold off;

    % Attach callback functions. Also make them detachable if the figure
    % is reused. But if it's reused in resizing/zooming, then we must attach
    % the callbacks again. That's why the callbacks are inside drawelems().
    zh = zoom(fh);
    ph = pan(fh);
    set(zh,'ActionPostCallback',@zoomcallback);
    set(ph,'ActionPostCallback',@pancallback);
    set(fh,'ResizeFcn',@resizecallback);
    set(fh,'NextPlot','Replace');

end

% Callback functions
function zoomcallback(~,~)
    drawelems();
end

function pancallback(~,~)
    drawelems();
end

function resizecallback(~,~)
    drawelems();
end

end

% Get axes size in "points" units
function [w,h] = getAxesSize(ah)
oldUnits = get(ah,'Units');
set(ah, 'Units', 'points');
axPos = get(ah,'Position');
set(ah,'Units',oldUnits);
w = axPos(3);
h = axPos(4);
end


function im=mat2im(mat,cmap,limits)
% mat2im - convert to rgb image
%
% function im=mat2im(mat,cmap,maxVal)
%
% PURPOSE
% Uses vectorized code to convert matrix "mat" to an m-by-n-by-3
% image matrix which can be handled by the Mathworks image-processing
% functions. The the image is created using a specified color-map
% and, optionally, a specified maximum value. Note that it discards
% negative values!
%
% INPUTS
% mat     - an m-by-n matrix
% cmap    - an m-by-3 color-map matrix. e.g. hot(100). If the colormap has
%           few rows (e.g. less than 20 or so) then the image will appear
%           contour-like.
% limits  - by default the image is normalised to it's max and min values
%           so as to use the full dynamic range of the
%           colormap. Alternatively, it may be normalised to between
%           limits(1) and limits(2). Nan values in limits are ignored. So
%           to clip the max alone you would do, for example, [nan, 2]
%
%
% OUTPUTS
% im - an m-by-n-by-3 image matrix
%
%
% Example 1 - combine multiple color maps on one figure
% clf, colormap jet, r=rand(40);
% subplot(1,3,1),imagesc(r), axis equal off , title('jet')
% subplot(1,3,2),imshow(mat2im(r,hot(100))) , title('hot')
% subplot(1,3,3),imshow(mat2im(r,summer(100))), title('summer')
% colormap winter %changes colormap in only the first panel
%
% Example 2 - clipping
% p=peaks(128); J=jet(100);
% subplot(2,2,1), imshow(mat2im(p,J)); title('Unclipped')
% subplot(2,2,2), imshow(mat2im(p,J,[0,nan])); title('Remove pixels <0')
% subplot(2,2,3), imshow(mat2im(p,J,[nan,0])); title('Remove pixels >0')
% subplot(2,2,4), imshow(mat2im(p,J,[-1,3])); title('Plot narrow pixel range')
%
% Rob Campbell - April 2009
%
% See Also: ind2rgb, imadjust

%Check input arguments
error(nargchk(2,3,nargin));

if ~isa(mat, 'double')
    mat = double(mat)+1;    % Switch to one based indexing
    limits = limits + 1;
end

if ~isnumeric(cmap)
    error('cmap must be a colormap, such as jet(100)')
end

%Clip if desired
L=length(cmap);
if nargin==3 && length(limits)==1
    warning('limits should be vector of length of 2. Assuming a max value was specified.')
    limits=[nan,limits];
end

if nargin==3
    minVal=limits(1);
    if isnan(minVal), minVal=min(mat(:)); end
    mat(mat<minVal)=minVal;

    maxVal=limits(2);
    if isnan(maxVal), maxVal=max(mat(:)); end
    mat(mat>maxVal)=maxVal;
else
minVal=min(mat(:));
maxVal=max(mat(:));
end

%Normalise
mat=mat-minVal;
mat=(mat/(maxVal-minVal))*(L-1);
mat=mat+1;

%convert to indecies
mat=round(mat);

%Vectorised way of making the image matrix
im=reshape(cmap(mat(:),:),[size(mat),3]);


%MDL_STANFORD_MDH Create model of Stanford arm using MDH conventions
%
%      mdl_stanford_mdh
%
% Script creates the workspace variable stanf which describes the
% kinematic and dynamic characteristics of the Stanford (Scheinman) arm
% using modified Denavit-Hartenberg parameters.
%
% Also defines the vectors:
%   qz   zero joint angle configuration.
%
% Notes::
% - SI units are used.
%
% References::
% - Kinematic data from "Modelling, Trajectory calculation and Servoing of
%   a computer controlled arm".  Stanford AIM-177.  Figure 2.3
% - Dynamic data from "Robot manipulators: mathematics, programming and control"
%   Paul 1981, Tables 6.5, 6.6
%
% See also SerialLink, mdl_puma560, mdl_puma560akb.

% MODEL: Stanford, Stanford arm, prismatic, 6DOF, modified_DH

% Copyright (C) 1993-2015, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: 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.
%
% RTB 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.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB.  If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com

clear L

qz = [0 0 0 0 0 0];

stanf = SerialLink([
            RevoluteMDH('d', 0.412)
            RevoluteMDH('d', 0.154, 'alpha', -pi/2)
            PrismaticMDH('alpha', pi/2, 'qlim', [0.2032 0.9144])
            RevoluteMDH()
            RevoluteMDH('alpha', -pi/2)
            RevoluteMDH('d', 0.263, 'alpha', pi/2)
        ], ...
    'name', 'Stanford arm MDH', ...
    'plotopt', {'workspace', [-2 2 -2 2 -2 2]} ...
    );


function[]=makefigs_periodize
%MAKEFIGS_PERIODIZE  Makes a sample figure for PERIODIZE.

load qgsnapshot
[xp,yp,fp]=periodize(100,200,qgsnapshot.x,qgsnapshot.y,qgsnapshot.psi);

figure
pcolor(xp,yp,fp),set(gca,'dataaspectratio',[1 1 1]),axis tight, shading interp
vlines(xp([100 end-100]),'w')
hlines(yp([200 end-200]),'w')
xtick([-5:1:5]*1000),ytick([-5:1:5]*1000)
title('Periodization of 1024x1024 QG turbulence with N=100 and M=200.')


%This Matlab script can be used to reproduce Figure 7.20 in the monograph:
%
%Emil Bjornson, Jakob Hoydis and Luca Sanguinetti (2017),
%"Massive MIMO Networks: Spectral, Energy, and Hardware Efficiency",
%Foundations and Trends in Signal Processing: Vol. 11, No. 3-4,
%pp. 154-655. DOI: 10.1561/2000000093.
%
%For further information, visit: https://www.massivemimobook.com
%
%This is version 1.0 (Last edited: 2017-11-04)
%
%License: This code is licensed under the GPLv2 license. If you in any way
%use this code for research that results in publications, please cite our
%monograph as described above.

%Empty workspace and close figures
close all;
clear;

%% Define parameters
M = 64; %Number of antennas
Mprime = 30; %Number of
beta = [0,10^(-5/10),1];
IT = 10000;

%Derived parameters
betad = (M-beta*Mprime)/(M-Mprime);

%% Simulate channels
correlationAA = zeros(numel(beta),IT);
correlationAB = zeros(numel(beta),IT);

%Go through all beta values
for b = 1:numel(beta)
    R_A = diag([beta(b)*ones(1,Mprime), betad(b)*ones(1,M-Mprime)]);
    R_B = diag([betad(b)*ones(1,M-Mprime), beta(b)*ones(1,Mprime)]);
    R_A2 = sqrt(R_A);
    R_B2 = sqrt(R_B);

    %Compute UE correlation metric in (7.22)
    for it = 1:IT

        hA1 = R_A2*1/sqrt(2)*(randn(M,1) + 1i*randn(M,1));
        hA2 = R_A2*1/sqrt(2)*(randn(M,1) + 1i*randn(M,1));
        hB1 = R_B2*1/sqrt(2)*(randn(M,1) + 1i*randn(M,1));
        correlationAA(b,it) = 10*log10((abs(hA1'*hA2)/(norm(hA1)*norm(hA2)))^2);
        correlationAB(b,it) = 10*log10((abs(hA1'*hB1)/(norm(hA1)*norm(hB1)))^2);

    end
end

%% Plot the simulation results
figure;
hold on; box on;
nth = 1000;
Colors = {'r','b'};
Markers = {'square', 'o'};

plot(-100,-100,'Color', 'k', 'LineStyle', '-');
plot(-100,-100,'Color', 'k', 'LineStyle', '--');
plot(-100,-100,'Color','k', 'LineStyle', 'none', 'Marker', '*');
plot(-100,-100,'Color', Colors{2}, 'LineStyle', 'none', 'Marker', Markers{2});
plot(-100,-100,'Color', Colors{1}, 'LineStyle', 'none', 'Marker', Markers{1});

[y,x] = ecdf(correlationAA(end,:));
plot(x,y,'Color','k', 'LineWidth', 1);
plot(x(1:nth:end),y(1:nth:end),'Color','k', 'LineStyle', 'none', 'Marker', '*');

for b = 1:numel(beta)-1
    [y,x] = ecdf(correlationAA(b,:));
    plot(x,y,'Color', Colors{b}, 'LineWidth', 1);
end

for b = 1:numel(beta)-1
    [y,x] = ecdf(correlationAB(b,:));
    plot(x,y,'--','Color', Colors{b}, 'LineWidth', 1)
end

for b = 1:numel(beta)-1
    [y,x] = ecdf(correlationAA(b,:));
    plot(x(1:nth:end),y(1:nth:end),'LineStyle', 'none','Color', Colors{b}, 'LineWidth', 1, 'Marker', Markers{b});
end

for b = 1:numel(beta)-1
    [y,x] = ecdf(correlationAB(b,:));
    plot(x(1:nth:end),y(1:nth:end),'LineStyle', 'none','Color', Colors{b}, 'LineWidth', 1, 'Marker', Markers{b})
end

xlim([-40,-10])
legend('Same region', 'Different regions','\beta=1', '\beta=-5 dB', '\beta=0','Location','NorthWest')
xlabel('Average UE correlation [dB]')
ylabel('CDF')


function [out] = evap_1(S,Ep,dt)
%evap_1

% Copyright (C) 2019, 2021 Wouter J.M. Knoben, Luca Trotter
% This file is part of the Modular Assessment of Rainfall-Runoff Models
% Toolbox (MARRMoT).
% MARRMoT is a free software (GNU GPL v3) and distributed WITHOUT ANY
% WARRANTY. See <https://www.gnu.org/licenses/> for details.

% Flux function
% ------------------
% Description:  Evaporation at the potential rate
% Constraints:  f <= S/dt
% @(Inputs):    S    - current storage [mm]
%               Ep   - potential evaporation rate [mm/d]
%               dt   - time step size

out = min(S/dt,Ep);

end


%TEST_bezierFunction.m
%
% This script tests the use of a bezier curve to represent a vector
% function, rather than an arbitrary space curve
%

order = 4;

p = rand(1,order+1);

tSpan = [1,4];
t = linspace(tSpan(1),tSpan(2),100);
pGrid = linspace(tSpan(1),tSpan(2),order+1);

tic
x = bezierCurve(p,t,tSpan);
toc

figure(1); clf;
plot(t,x); hold on;
plot(pGrid,p,'x');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inverse dynamics for the 1dof planar robot
%
%   tau = inversedynamics_1dofplanar(robot, q, qd, qdd, fext)
%
%   Where robot stores the kinematic and dynamic parameters for this robot.
%   q: joint positions.
%   qd: joint velocities.
%   qdd: joint accelerations.
%   fext: vector of external forces. Defined in the last reference system.
%
%   This function just executes the inverse dynamic model for this robot.
%   The equations to compute this dynamic model can be found in:
%   "ROBOT ANALYSIS. The mechanics of Serial and Parallel
%        manipulators". Lung Weng Tsai. John Wiley and Sons, inc. ISBN:
%        0-471-32593-7. page 405.
%
%
%   Author: Arturo Gil Aparicio arturo.gil@umh.es
%   Date: 23/11/2016
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Copyright (C) 2012, by Arturo Gil Aparicio
%
% This file is part of ARTE (A Robotics Toolbox for Education).
%
% ARTE is free software: 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.
%
% ARTE 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.
%
% You should have received a copy of the GNU Leser General Public License
% along with ARTE.  If not, see <http://www.gnu.org/licenses/>.
function tau = exercise_inv_dynamics_1dofplanar(robot, q, qd, qdd, g, fext)
a = eval(robot.DH.a);
a1=a(1);

g=abs(g);
m=robot.dynamics.masses(1);

%In this case we must define the Inertia with respect to the rotating axis.
J = (1/3)*m*a1^2;
tau = J*qdd + m*g*a1*cos(q(1))/2;


% Copyright 2011 Zdenek Kalal
%
% This file is part of TLD.
%
% TLD is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% TLD 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 General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with TLD.  If not, see <http://www.gnu.org/licenses/>.

function pt = bb_points(bb,numM,numN,margin)
% Generates numM x numN points on BBox.

bb(1:2) = bb(1:2)+margin;
bb(3:4) = bb(3:4)-margin;

if (numM == 1 && numN ==1)
    pt = bb_center(bb);
    return;
end

if (numM == 1 && numN > 1)
    c = bb_center(bb);
    stepW = (bb(3)-bb(1)) / (numN - 1);
    pt = ntuples(bb(1):stepW:bb(3),c(2));
    return;
end

if (numM > 1 && numN == 1)
    c = bb_center(bb);
    stepH = (bb(4)-bb(2)) / (numM - 1);
    pt = ntuples(c(1),(bb(2):stepH:bb(4)));
    return;
end

stepW = (bb(3)-bb(1)) / (numN - 1);
stepH = (bb(4)-bb(2)) / (numM - 1);

pt = ntuples(bb(1):stepW:bb(3),(bb(2):stepH:bb(4)));

if size(pt,2) < numM*numN
    count = numM * numN - size(pt, 2);
    app = repmat(pt(:,end), 1, count);
    pt = [pt, app];
end


function [mwcn,wpixc, globmax,edgedist]=localminW(w,nx,ny,psf)
% [mwcn,wpixc, globmax]=analyzeWconfPSF(w,nx,ny,psf)
% Finds local maxima in the results w (nx*ny,ncomp) convoluted with psf
% wpixc - w convolved with estimated PSF.
% mwcn = image of local maxima (non zero pixels) with values of hte convoluted image, nornalised to the maximum of each column.
% globmax = maximum of each column of W convolved with PSF. - PSF and W are L1 normalised - teh maximum can be used as a "quality measure."
%
% To get number of local maxima with relative strength < .5: sum(mwcn>.5)
ncomp=size(w,2);
autoconvmax=max(max(conv2(psf,psf,'same'))); % maximum of the "autoconvolution" - use to normalise the convolution with w.
wpix=reshape(w,nx, ny, ncomp); % each frame normalized to 1
wpixc=convstack(wpix,psf,'same');
edgedist=maxtoedgedist(wpixc);
wc=reshape(wpixc,nx*ny,ncomp);
mpix=maximastack(wpixc); % binary image of local maxima locations in each frame
m=reshape(mpix,nx*ny,ncomp);
mwc=m.*wc; % pixels indicates locations and value of hte local maxima
globmax=max(mwc)/autoconvmax; % The value of the global maximum for each column of W normalised with respect to the 'autoconvolution' of the PSF.
mwcn=normcMax(mwc); % normalised to the maximum of each column.


% pet_transmission_example.m
% Example of reconstructing a PET attenuation map via penalized-likelihood
% estimation from real 2D PET transmission data.
%
% Copyright 2002-12-19, Jeff Fessler, University of Michigan

% read raw data
if ~isvar('yi'), printm 'raw data'
    yi = ir_get_data(fullfile('pet_trans_2d_sino','phan_trans.mat'));
    bi = ir_get_data(fullfile('pet_trans_2d_sino','phan_blank.mat'));
	im plc 2 2, im(1, yi, 'yi: transmission scan')
	im(2, bi, 'bi: blank scan')

	% system model for ecat921 PET scanner geometry
	ig = image_geom('nx', 128, 'dx', 0.421875, 'dy', 0.421875); % cm
%		'center_x', 0.5, 'center_y', -0.5, ...
	sg = sino_geom('par', 'nb', size(yi,1), 'na', size(yi,2), ...
		'dr', 0.3375, 'offset_r', 0.5, 'strip_width', 'dr', ...
		'orbit_start', -15);
prompt
end

% FBP image
if ~isvar('xfbp'), printm 'do FBP'
	kernel = gaussian_kernel(3);
%	kernel = [1];
	xfbp = tr_fbp(sg, ig, max(yi,1), bi, 0*bi, 'kernel', kernel);
% fix: correct for backproject scaling problem; probably incorrect!
%	xfbp = xfbp * na/pi * f.pixel_size / f.ray_spacing;
	xfbp = max(xfbp,0);
	im(3, xfbp, 'fbp'), cbar

	ig.mask = ig.circ(26, 21, 0, 3) > 0;
	im(4, ig.mask+6*xfbp, 'mask check')
prompt
end

% strip integral system matrix with mask for iterative reconstruction.
if ~isvar('A2'), printm 'A2'
	if 0 && has_mex_jf
		A2 = Gtomo2_wtmex(sg, ig); % preferable for speed
	else
		A2 = Gtomo2_strip(sg, ig, 'strip_width', sg.dr); % slower but universal
	end
end

if ~isvar('Ab'), printm 'make Ab' % block system object for ordered-subsets
	f.nblock = 5;
	Ab = Gblock(A2, f.nblock);
end

if ~isvar('R'), printm 'make R' % regularizer object
	f.l2b = 10.5;
	f.delta = 0.03;
%	f.pot = 'huber';
	f.pot = 'hyper3';
	R = Reg1(ig.mask, 'type_denom', 'matlab', ...
		'beta', 2^f.l2b, 'pot_arg', {f.pot, f.delta});
end

% matlab iterations
if ~isvar('xmat'), printm 'matlab T-PL-OS-SPS'
	f.niter = 8+1;
	f.niter = 16+1;
	f.pixmax = 0.4;
	xinit = max(xfbp,0);

	xmat = tpl_os_sps(xinit(ig.mask), Ab, yi, bi, [], R, ...
		f.niter, f.pixmax, 'pc');
	xmat = ig.embed(xmat);

	im clf, im(xmat, 'T-PL-OSPS iterations (0th is FBP)')
prompt
end

% nice figure for book
if 1
	im clf
	im(211, yi, 'sinogram yi')
	clim = [0 0.2];
	im(223, xfbp, 'FBP', clim), cbar
	im(224, xmat(:,:,end), 'Statistical', clim), cbar
end


function [R, G, B] = Lab2RGB(L, a, b)
%LAB2RGB Convert an image from CIELAB to RGB
%
% function [R, G, B] = Lab2RGB(L, a, b)
% function [R, G, B] = Lab2RGB(I)
% function I = Lab2RGB(...)
%
% Lab2RGB takes L, a, and b double matrices, or an M x N x 3 double
% image, and returns an image in the RGB color space.  Values for L are in
% the range [0,100] while a* and b* are roughly in the range [-110,110].
% If 3 outputs are specified, the values will be returned as doubles in the
% range [0,1], otherwise the values will be uint8s in the range [0,255].
%
% This transform is based on ITU-R Recommendation BT.709 using the D65
% white point reference. The error in transforming RGB -> Lab -> RGB is
% approximately 10^-5.
%
% See also RGB2LAB.

% By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
% Updated for MATLAB 5 28 January 1998.
% Fixed a bug in conversion back to uint8 9 September 1999.
% Updated for MATLAB 7 30 March 2009.

if nargin == 1
  b = L(:,:,3);
  a = L(:,:,2);
  L = L(:,:,1);
end

if max(max(L)) < 1.1 || max(max(a)) < 1.1 || max(max(b)) < 1.1
  L = double(L) * 100;
  a = double(a) * 220;
  a = a - 110;
  b = double(b) * 220;
  b = b - 110;
end

% Thresholds
T1 = 0.008856;
T2 = 0.206893;

[M, N] = size(L);
s = M * N;
L = reshape(L, 1, s);
a = reshape(a, 1, s);
b = reshape(b, 1, s);

% Compute Y
fY = ((L + 16) / 116) .^ 3;
YT = fY > T1;
fY = (~YT) .* (L / 903.3) + YT .* fY;
Y = fY;

% Alter fY slightly for further calculations
fY = YT .* (fY .^ (1/3)) + (~YT) .* (7.787 .* fY + 16/116);

% Compute X
fX = a / 500 + fY;
XT = fX > T2;
X = (XT .* (fX .^ 3) + (~XT) .* ((fX - 16/116) / 7.787));

% Compute Z
fZ = fY - b / 200;
ZT = fZ > T2;
Z = (ZT .* (fZ .^ 3) + (~ZT) .* ((fZ - 16/116) / 7.787));

% Normalize for D65 white point
X = X * 0.950456;
Z = Z * 1.088754;

% XYZ to RGB
MAT = [ 3.240479 -1.537150 -0.498535;
       -0.969256  1.875992  0.041556;
        0.055648 -0.204043  1.057311];

RGB = max(min(MAT * [X; Y; Z], 1), 0);

R = reshape(RGB(1,:), M, N);
G = reshape(RGB(2,:), M, N);
B = reshape(RGB(3,:), M, N);

if nargout < 2
  R = uint8(round(cat(3,R,G,B) * 255));
end


function UNew = diffusionNeumann2D(varargin);
% diffusionNeumann2D: solve diffusion registraion in 2D with Neumann
%        boundary conditions
%
%
% author: Nathan D. Cahill
% email: nathan.cahill@rit.edu
% affiliation: Rochester Institute of Technology
% date: January 2014
% licence: GNU GPL v3
%
% Copyright Nathan D. Cahill
% Code available from https://github.com/tomdoel/npReg
%
%

% parse input arguments
[U,F,PixSize,NumPix,RegularizerFactor] = parse_inputs(varargin{:});

% divide by regularizer factor
Fnew = F/RegularizerFactor;

% compute dct of new force field
FnewF1 = real(fft(real(fft(Fnew(:,:,1),2*NumPix(1)-2,1)),2*NumPix(2)-2,2));
FnewF2 = real(fft(real(fft(Fnew(:,:,2),2*NumPix(1)-2,1)),2*NumPix(2)-2,2));
FnewF1 = FnewF1(1:NumPix(1),1:NumPix(2));
FnewF2 = FnewF2(1:NumPix(1),1:NumPix(2));

% construct images of coordinates scaled by pi/(N or M)
[alpha,beta] = ndgrid(pi*(0:(NumPix(1)-1))/(NumPix(1)-1),pi*(0:(NumPix(2)-1))/(NumPix(2)-1));

% construct LHS factor
LHSfactor = 2*cos(alpha) + 2*cos(beta) - 4;

% set origin term to 1, as DC term does not matter
LHSfactor(1,1) = 1;

% solve for FFT of U
UF1 = FnewF1./LHSfactor;
UF2 = FnewF2./LHSfactor;

% if gamma is zero, set DC term to 0
UF1(1,1) = 0;
UF2(1,1) = 0;

% perform inverse dct
U1 = real(ifft(real(ifft(UF1,2*NumPix(1)-2,1)),2*NumPix(2)-2,2));
U2 = real(ifft(real(ifft(UF2,2*NumPix(1)-2,1)),2*NumPix(2)-2,2));

% crop and concatenate
UNew = cat(3,U1(1:NumPix(1),1:NumPix(2)),U2(1:NumPix(1),1:NumPix(2)));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [U,F,PixSize,NumPix,RegularizerFactor] = parse_inputs(varargin);

% get displacement field and check size
U = varargin{1};
F = varargin{2};
PixSize = varargin{4}(1:2);
NumPix = [varargin{5} varargin{6}];
RegularizerFactor = varargin{10};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [CC, rhs, bb, gg, Px, Py, xsplit, ysplit] = discretize(N, f, m, n, flag)
%DISCRETIZE   Given a CHEBOP2, this function converts the problem to one of the form
%
%  sum_i  kron(A_i,B_i)
%
% and computes the discretisation as a cell array:
%
%  {{
%
%       A_1 , B_1
%       A_2 , B_2
%        .  ,  .
%        .  ,  .
%        .  ,  .
%       A_k , B_k
%
%                   }}
%
%    INPUTS:
%      N = PDE (CHEBOP2).
%      f = forcing term (CHEBFUN2).
%      m = discretization size in 1st variable.
%      n = discretization size in 2nd variable.
%      flag = 0 (default) means assigned boundary conditions, flag = 1
%      means do not assign boundary conditions.
%
%    OUTPUTS:
%      CC = cell array of matrices storing the terms in the matrix
%      equation.
%      rhs = matrix discretizing forcing term.
%      bb = cell array storing the discretized linear constraints.
%      gg = cell array storing the discretized nonhomogeneous part of the
%      constraints.
%      Px, Py = store permutation matrix to ensure bcs are linear
%      dependent.
%      XSPLIT, YSPLIT = 0 if subproblems cannot be formed. XSPLIT = 1 if
%      even and odd modes decouple in 1st variable. YSPLIT = 1 if even and
%      odd modes decouple in 2nd variable.
%
% Returns RHS with degrees of freedom removed and bb which stores
% elminated boundary conditions, gg eliminated boundary rows.

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

% Parse inputs.
if nargin < 5
    flag = 0;
end

%%
% Get information of PDE and pref.
A = N.coeffs;
rect = N.domain;
pref = chebfunpref();
tol = pref.cheb2Prefs.chebfun2eps;
xorder = N.xorder;
yorder = N.yorder;

%%
% Check if the PDO was given as a variable coefficient PDO with the notation
% @(x,y,u), but is actually a constant coefficient PDO.
if ( iscell( A ) )
    doesNotDependOnXorY = all(all(cellfun(@isnumeric, N.coeffs)));
    if ( doesNotDependOnXorY )
        A = cell2mat( A ).';
    end
end

%%
% Convert matrix of coefficients to a discretization for the PDE using the
% singular value decomposition.  We find the rank of the PDE operator and
% then use the optimal low rank expansion of the operator as a way to
% discretise the PDE.
if (  isempty(N.V) || isempty(N.U) )
    if ( iscell(A) )
        counter = 1;
        U = cell(size(A, 1), 1);
        V = cell(size(A, 2), 1);
        for jj = 1:size(A, 1)
            for kk = 1:size(A, 2)
                a = A{jj,kk};
                if ( isa(a, 'chebfun2') )
                    if ( abs(vscale(a)) > tol )
                        [C, D, R] = cdr(a);
                        for col = 1:size(C, 2)
                            U{jj,counter} = C(:,col)*D(col,col);
                            V{kk,counter} = R(:,col);
                            counter = counter + 1;
                        end
                    end
                elseif ( isa(a,'double') && ( isempty(a) || abs(a) > tol ) )
                    U{jj,counter} = a;
                    V{kk,counter} = 1;
                    counter = counter + 1;
                end
            end
        end
        rk = size(U, 2);
        S = diag(ones(rk, 1));
        na = size(U, 1);
        nb = size(V, 1);
    else
        % Compute the SVD of the coefficient matrix.
        [U, S, V] = svd(A.');
        % Find the rank of A, which is also the rank of the PDE operator and
        % construct the low rank expansion for A.
        rk = find(diag(S) > tol, 1, 'last');
        U = U(:,1:rk);
        S = S(1:rk,1:rk);
        V = V(:,1:rk);
        [na, nb] = size(A.');
    end
else
    rk = size(N.S, 2);
    U = N.U;
    V = N.V;
    S = N.S;
    na = size(U, 1);
    nb = size(V, 1);
end
% LEFT = zeros(m); RIGHT = zeros(n);

% Construct the discretisation in matrix equation form.
CC = cell(rk,2);

for jj = 1 : rk

    RIGHT = unconstrainedMatrixEquation(V, jj, n, xorder, rect(1:2)); % jjth term on the right.
    LEFT = unconstrainedMatrixEquation(U, jj, m, yorder, rect(3:4)); % jjth term on the left.

    % Balance out the scaling from the singular value. This does slightly
    % improve the accuracy.
    singvalue = sqrt(S(jj,jj));
    CC{jj,2} = singvalue * RIGHT;
    CC{jj,1} = singvalue * LEFT;

end

%%
% Test to see if we can solve subproblems. This checks if the PDE operator
% contains differential terms of the same parity.
ysplit = 0; xsplit=0;
if ~iscell(U) && ~iscell(V)
    emask = 1:2:na;
    omask = 2:2:na;
    if ( min( norm(U(emask,:)), norm(U(omask,:)) ) < 10*tol )
        ysplit = 1;
    end
    emask = 1:2:nb;
    omask = 2:2:nb;
    if ( min( norm(V(emask,:)), norm(V(omask,:)) ) < 10*tol )
        xsplit = 1;
    end
end

%%
% We have a discretisation for the PDE operator, now let's find a
% discretisation for the boundary conditions.

% If no boundary conditions is prescribed then make it empty.
bcLeft = [];
leftVal = [];
bcRight = [];
rightVal = [];
bcUp = [];
upVal = [];
bcDown = [];
downVal = [];

if ( ~isempty(N.lbc) ) % Left boundary conditions.
    [bcLeft, leftVal] = chebop2.constructBC(N.lbc, -1, m, n, rect(3:4), rect(1:2), xorder);
end
if ( ~isempty(N.rbc) ) % Right boundary conditions.
    [bcRight, rightVal] = chebop2.constructBC(N.rbc, 1, m, n, rect(3:4), rect(1:2), xorder);
end
if ( ~isempty(N.ubc) ) % Top boundary conditions.
    [bcUp, upVal] = chebop2.constructBC(N.ubc, 1, n, m, rect(1:2), rect(3:4), yorder);
end
if ( ~isempty(N.dbc) ) % Bottom boundary conditions.
    [bcDown, downVal] = chebop2.constructBC(N.dbc, -1, n, m, rect(1:2), rect(3:4), yorder);
end

%%

% For the down and up BCs we have B^TX = g^T.
By = [ bcUp.'; bcDown.' ];
Gy = [ upVal.'; downVal.' ];
[By, Gy, Py] = canonicalBC(By, Gy);

% For the left and right BCs we have X*B = g. We do the LU to B^T.
Bx = [ bcLeft.'; bcRight.' ];
Gx = [ leftVal.'; rightVal.' ];
[Bx, Gx, Px] = canonicalBC(Bx, Gx);
Bx = Bx.';
Gx = Gx.'; % Now transpose so that X*B = g;

%%
% Construct the RHS of the Sylvester matrix equation.

% Complete the RHS (part of the RHS could have been in the operator):
if ( ~isempty(N.rhs) && ( isa(N.rhs, 'chebfun2') || isa(N.rhs, 'double') ) )
    f = f + N.rhs;
end
E = zeros(m, n);
[n2, n1] = length(f);
F = chebcoeffs2(f);

% Map the RHS to the right ultraspherical space.
lmap = ultraS.convertmat(n1, 0, yorder-1);
rmap = ultraS.convertmat(n2, 0, xorder-1);
F = lmap * F * rmap.';

% Place those coefficients of the forcing function onto the RHS.
n1 = min(n1, m);
n2 = min(n2, n);
E(1:n1,1:n2) = F(1:n1,1:n2);

if ( ~flag ) % Impose boundary conditions.

    % Use the eliminated boundary condition to place zeros in the columns of
    % the matrix equation discretization. There are rk columns to zero out.

    for jj = 1:rk  % For term in the matrix equation.
        [C, E] = zeroDOF(CC{jj,1}, CC{jj,2}, E, By, Gy);
        CC{jj,1} = C;
        [C, E] = zeroDOF(CC{jj,2}, CC{jj,1}, E.', Bx.', Gx.');
        CC{jj,2} = C;
        E = E.';
    end

    % Remove degrees of freedom.
    nn = n - max(xorder, yorder);
    mm = m - max(xorder, yorder);
    df1 = max(0, xorder - yorder);
    df2 = max(0, yorder - xorder);
    for jj = 1:rk
        CC{jj,1} = CC{jj,1}(1:mm, yorder+1:m-df1);
        CC{jj,2} = CC{jj,2}(1:nn, xorder+1:n-df2);
    end
    % Truncation of righthand side.
    rhs = E(1:mm, 1:nn);

else
    rhs = E;
end

% Pass back the eliminated boundary conditions.
bb = {bcLeft bcRight bcUp bcDown};
gg = {leftVal rightVal upVal downVal};

%%
% Check boundary continunity conditions.

% Check BCs at corners:
allbc = 0;
if ( ~isempty(bcUp) && ~isempty(upVal) && ~isempty(bcRight) && ~isempty(rightVal) )
    if ( norm(rightVal(end-4:end),inf) < sqrt(tol) && norm(upVal(end-4:end),inf) < sqrt(tol) )
        allbc = allbc + norm(upVal.'*bcRight - bcUp.'*rightVal);
    end
end
if ( ~isempty(bcUp) && ~isempty(upVal) && ~isempty(bcLeft) && ~isempty(leftVal) )
    if ( norm(leftVal(end-4:end),inf) < sqrt(tol) && norm(upVal(end-4:end),inf) < sqrt(tol) )
        allbc = allbc + norm(upVal.'*bcLeft - bcUp.'*leftVal);
    end
end
if ( ~isempty(bcDown) && ~isempty(downVal) && ~isempty(bcRight) && ~isempty(rightVal) )
    if ( norm(rightVal(end-4:end),inf) < sqrt(tol) && norm(downVal(end-4:end),inf) < sqrt(tol) )
        allbc = allbc + norm(downVal.'*bcRight - bcDown.'*rightVal);
    end
end
if ( ~isempty(bcDown) && ~isempty(downVal) && ~isempty(bcLeft) && ~isempty(leftVal) )
    if ( norm(leftVal(end-4:end),inf)<sqrt(tol) && norm(downVal(end-4:end),inf)<sqrt(tol) )
        allbc = allbc + norm(downVal.'*bcLeft - bcDown.'*leftVal);
    end
end

% [TODO]: Should there be an error if the compatibility conditions do not
% match?
%
% if allbc >= 100*sqrt(tol)
%     s = sprintf('Boundary conditions differ by %1.4f', allbc');
%     warning('CHEBFUN:CHEBOP2:discretize:BCs', s)
% end

end

function B = unconstrainedMatrixEquation(ODE, jj, n, order, dom)
% Construct the unconstrained Matix Equation. Adding in the constraints later.

B = spalloc(n, n, 3*n);
for kk = 1:size(ODE, 1)

    % Get conversion and differentiation matrices:
    S = ultraS.convertmat(n, kk-1, order-1);
    D = ((2./diff(dom))^(kk-1)) * ultraS.diffmat(n, kk-1);

    if ( iscell(ODE(kk,jj)) && isa(ODE{kk,jj}, 'chebfun') )
        % Variable coefficient term:
        c = ODE{kk,jj}.coeffs;
        M = ultraS.multmat(n, c, kk-1);
        A = S * M * D;

    elseif ( iscell(ODE(kk,jj)) && ~isempty(ODE{kk,jj}) )
        % Constant coefficient term in a variable coefficient ODE:
        A = ODE{kk,jj}.* S * D;

    elseif ( isa(ODE(kk,jj),'double') )
        % Constant coefficient term in a constant coefficient ODE:
        A = ODE(kk,jj).* S * D;

    else
        % Empty cell in array, so no term:
        A = zeros(n);

    end

    % Form the ODE operator:
    B = B + A;

end

end

function [B, G, P] = canonicalBC(B, G)
%CANONICALBC   Form a linear combintation of the boundary conditions
%so that they can be used for imposing on the PDE.

P = nonsingularPermute(B);
B = B*P;
[L, B] = lu(B);
G = L \ G;

% Scale so that B is unit upper triangular.
if ( min(size(B)) > 1 )
    D = diag(1./diag(B));
elseif ( ~isempty(B) )
    D = 1./B(1,1);
else
    D = []; % No boundary conditions.
end
B = D*B;
G = D*G;

end

function P = nonsingularPermute(B)
%NONSINGULARPERMUTE   Permute the columns of B to ensure that the principal
%m*m submatrix of B is nonsingular, where m = size(B, 1).
%
% Note: This is needed for solving the matrix equations with linear
% constraints, see DPhil thesis of Alex Townsend (section 6.5).

m = size(B, 1);
k = 1;

% [TODO]: improve this check.
% Try each mxm block in a linear fashion:
while ( rank(B(:,k:m+k-1)) < m )
    k = k+1;
    if ( m+k > size(B, 2) )
        error('CHEBFUN:CHEBOP2:discretize:nonsingularPermute:BCs', ...
            'Boundary conditions are linearly dependent.');
    end
end

P = speye(size(B, 2));
P = P(:,[k:m+k-1, 1:k-1, m+k:end]);

end

function [C1, E] = zeroDOF(C1, C2, E, B, G)
%ZERODOF   Eliminate so degrees of freedom in the matrix equation can be
%removed.

for ii = 1:size(B, 1) % For each boundary condition, zero a column.
    for kk = 1:size(C1, 1)
        if ( abs(C1(kk,ii)) > 10*eps )
            c = C1(kk, ii); % Constant required to zero entry out.
            C1(kk,:) = C1(kk,:) - c*B(ii,:);
            E(kk,:) = E(kk,:) - c*G(ii,:)*C2.';
        end
    end
end

end


function Q = StructureMeasure(prediction,GT)
% StructureMeasure computes the similarity between the foreground map and
% ground truth(as proposed in "Structure-measure: A new way to evaluate
% foreground maps" [Deng-Ping Fan et. al - ICCV 2017])
% Usage:
%   Q = StructureMeasure(prediction,GT)
% Input:
%   prediction - Binary/Non binary foreground map with values in the range
%                [0 1]. Type: double.
%   GT - Binary ground truth. Type: logical.
% Output:
%   Q - The computed similarity score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Check input
if (~isa(prediction,'double'))
    error('The prediction should be double type...');
end
if ((max(prediction(:))>1) || min(prediction(:))<0)
    error('The prediction should be in the range of [0 1]...');
end
if (~islogical(GT))
    error('GT should be logical type...');
end

y = mean2(GT);

if (y==0)% if the GT is completely black
    x = mean2(prediction);
    Q = 1.0 - x; %only calculate the area of intersection
elseif(y==1)%if the GT is completely white
    x = mean2(prediction);
    Q = x; %only calcualte the area of intersection
else
    alpha = 0.5;
    Q = alpha*S_object(prediction,GT)+(1-alpha)*S_region(prediction,GT);
    if (Q<0)
      Q=0;
    end
end

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%  This script is used to test whether the functions in this  %%%%%%%%%%
%%%%%%%%%%  package can run smoothly.                                  %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%  H.D. Li, lhdcsu@gmail.com                                  %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%+++ Import data;
load DM2;
%+++ Cross validation
A=6;
K=5;
method='autoscaling';
N=500;
Nmcs=50;
CV=plsldacv(Xcal,ycal,A,K,method)
MCCV=plsldamccv(Xcal,ycal,A,method,N)
DCV=plsldardcv(Xcal,ycal,A,K,method,Nmcs)

%+++ Build a PLS-LDA model
nLV=3;
LDA=plslda(Xcal,ycal,nLV);
[ScoresTest]=plsldaproj(LDA,Xcal(1:3,:))

%+++ Scores plot
plotlda(LDA,1,0,[2 3 1]);
figure;
plotlda(LDA,1,1,[1  2 3]);
figure;
plotlda(LDA,0,0,[1 2]);
figure;
plotlda(LDA,1,1,[2 3 ]);
figure;
plotlda(LDA,2,1,[3 1]);

%+++ CARS-PLSLDA for variable selection
CARS=carsplslda(Xcal,ycal,A,K,method,50);
figure;
plotcars(CARS);
%+++ simplified version of CARS-PLSLDA for variable selection
sCARS=scarsplslda(Xcal,ycal,A,K,method,50);
figure;
plotcars(sCARS);

%+++ SPA for vairable selection: based on Model Population Analysis
SPA=spa(Xcal,ycal,A,K,method,N,0.7,15);
figure;
bar(SPA.COSS,'b','edgecolor','w');
figure;
plotspa(SPA,SPA.RankedVariable(1));
p=SPA.p(SPA.RankedVariable(1))
%+++ Test ended


function rule_num = keast_rule_num ( DUMMY )

%*****************************************************************************80
%
%% KEAST_RULE_NUM returns the number of Keast rules for the tetrahedron.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 June 2007
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Patrick Keast,
%    Moderate Degree Tetrahedral Quadrature Formulas,
%    Computer Methods in Applied Mechanics and Engineering,
%    Volume 55, Number 3, May 1986, pages 339-348.
%
%  Parameters:
%
%    Output, integer RULE_NUM, the number of rules.
%
  rule_num = 10;

  return
end


%ISINT Test on number(s) on integer >= 0
% $Id: isint.m,v 1.2 2006/03/08 22:06:58 duin Exp $

function n = isint(m)

	if all(all(m == round(m) & m >= 0.5))
		n = 1;
	else
		n = 0;
	end

	return


[out, th_value] = threshold(rcc,'isodata',1);
siz = size(out);
% out=fliplr(out);
a=zeros(siz(1:2));
num=1;
for ind=1:siz(1)*siz(2)
    [i,j] = ind2sub(siz(1:2),ind);
    if a(ind) == 0
        indexlabel = intersect(find(out(:,:,ind-1)>0),find(a==0));
        if ~isempty(indexlabel)
            a([indexlabel; ind]) = num;

            num=num+1;
        end
    end
end


function [ xmin, xmax ] = r8_gaml ( )

%*****************************************************************************80
%
%% R8_GAML evaluates bounds for an R8 argument of the gamma function.
%
%  Discussion:
%
%    This function calculates the minimum and maximum legal bounds
%    for X in the evaluation of GAMMA ( X ).
%
%    XMIN and XMAX are not the only bounds, but they are the only
%    non-trivial ones to calculate.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 September 2011
%
%  Author:
%
%    Original FORTRAN77 version by Wayne Fullerton.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Wayne Fullerton,
%    Portable Special Function Routines,
%    in Portability of Numerical Software,
%    edited by Wayne Cowell,
%    Lecture Notes in Computer Science, Volume 57,
%    Springer 1977,
%    ISBN: 978-3-540-08446-4,
%    LC: QA297.W65.
%
%  Parameters:
%
%    Output, real XMIN, XMAX, the bounds.
%
  alnsml = log ( r8_mach ( 1 ) );
  xmin = - alnsml;

  for i = 1 : 10

    xold = xmin;
    xln = log ( xmin );
    xmin = xmin - xmin * ( ( xmin + 0.5 ) * xln - xmin ...
      - 0.2258 + alnsml ) / ( xmin * xln + 0.5 );

    if ( abs ( xmin - xold ) < 0.005 )

      xmin = - xmin + 0.01;

      alnbig = log ( r8_mach ( 2 ) );
      xmax = alnbig;

      for j = 1 : 10

        xold = xmax;
        xln = log ( xmax );
        xmax = xmax - xmax * ( ( xmax - 0.5 ) * xln - xmax ...
          + 0.9189 - alnbig ) / ( xmax * xln - 0.5 );

        if ( abs ( xmax - xold ) < 0.005 )
          xmax = xmax - 0.01;
          xmin = max ( xmin, - xmax + 1.0 );
          return
        end

      end

      fprintf ( 1, '\n' );
      fprintf ( 1, 'R8_GAML - Fatal error!\n' );
      fprintf ( 1, '  Unable to find XMAX.\n' );
      error ( 'R8_GAML - Fatal error!' )

    end

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, 'R8_GAML - Fatal error!\n' );
  fprintf ( 1, '  Unable to find XMIN.\n' );

  error ( 'R8_GAML - Fatal error!' )
end


clear, clc;

% This is an example for running the function eplb
%
%  Problem:
%
%  min  1/2 || x - y||^2
%  s.t. \|x\|_1 <= z
%
% For detailed description of the function, please refer to the Manual.
%
%% ------------   History --------------------
% First version on August 10, 2008.
%
%
% For any problem, please contact Jun Liu (j.liu@asu.edu)

cd ..
cd ..

root=cd;
addpath(genpath([root '/SLEP']));
                     % add the functions in the folder SLEP to the path

% change to the original folder
cd Examples/L1;

n=1000;
v=randn(n, 1);
z=20;
lambda0=0;

% run the function epp
[x, lambda, iter_step]=eplb(v, n, z, lambda0);


function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure
%   PLOTDATA(x,y) plots the data points with + for the positive examples
%   and o for the negative examples. X is assumed to be a Mx2 matrix.

% Create New Figure
figure; hold on;

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%

% Find Indices of Positive and Negative Examples
pos = find(y==1);
neg = find(y == 0);

plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);

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

hold off;

end


function mono_total_next_grevlex_test ( )

%*****************************************************************************80
%
%% MONO_TOTAL_NEXT_GREVLEX_TEST tests MONO_TOTAL_NEXT_GREVLEX.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 December 2013
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'MONO_TOTAL_NEXT_GREVLEX_TEST\n' );
  fprintf ( 1, '  MONO_TOTAL_NEXT_GREVLEX can list the monomials\n' );
  fprintf ( 1, '  in M variables, of total degree N,\n' );
  fprintf ( 1, '  one at a time, in graded reverse lexicographic order.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  We start the process with (0,0,...,0,N).\n' );
  fprintf ( 1, '  The process ends with (N,0,...,0,0)\n' );

  n = 3;
  m = 3;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Let M = %d\n', m );
  fprintf ( 1, '      N = %d\n', n );
  fprintf ( 1, '\n' );

  x = [ 0, 0, n ];
  i = 1;

  while ( 1 )

    fprintf ( 1, '  %2d:', i );
    for j = 1 : m
      fprintf ( 1, '  %1d', x(j) );
    end
    fprintf ( 1, '\n' );

    if ( x(1) == n )
      break
    end

    x = mono_total_next_grevlex ( m, n, x );
    i = i + 1;

  end

  return
end


function linpack_c_test29 ( )

%*****************************************************************************80
%
%% TEST29 tests CSIFA and CSISL.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 June 2009
%
%  Author:
%
%    John Burkardt
%
  n = 3;
  lda = n;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST29\n' );
  fprintf ( 1, '  For a single precision complex (C)\n' );
  fprintf ( 1, '  symmetric matrix (SI):\n' );
  fprintf ( 1, '  CSIFA factors the matrix.\n' );
  fprintf ( 1, '  CSISL solves a linear system.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The matrix order is N = %d\n', n );
%
%  Set the values of the matrix A.
%
  seed = 123456789;

  for i = 1 : n
    [ a(i,i), seed ] = c4_uniform_01 ( seed );
    for j = i+1 : n
      [ a(i,j), seed ] = c4_uniform_01 ( seed );
      a(j,i) = a(i,j);
    end
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The matrix A is\n' );
  fprintf ( 1, '\n' );

  for i = 1 : n
    for j = 1 : n
      fprintf ( 1, '  (%8f  %8f)', real ( a(i,j) ), imag ( a(i,j) ) );
    end
    fprintf ( 1, '\n' );
  end
%
%  Set the values of the right hand side vector B.
%
  [ x, seed ] = c4vec_uniform_01 ( n, seed );

  b(1:n) = a(1:n,1:n) * transpose ( x(1:n) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The right hand side B is\n' );
  fprintf ( 1, '\n' );
  for i = 1 : n
    fprintf ( 1, '  (%8f  %8f)\n', real ( b(i) ), imag ( b(i) ) );
  end
%
%  Factor the matrix A.
%
  [ a, ipvt, info ] = csifa ( a, lda, n );

  if ( info ~= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, '  CSIFA returned an error flag INFO = %d\n', info );
    return
  end
%
%  Solve the system.
%
  b = csisl ( a, lda, n, ipvt, b );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Computed                     Exact\n' );
  fprintf ( 1, '  Solution                     Solution\n' );
  fprintf ( 1, '\n' );

  for i = 1 : n
    fprintf ( 1, '  (%8f  %8f)  (%8f  %8f)\n', ...
      real ( b(i) ), imag ( b(i) ), real ( x(i) ), imag ( x(i) ) );
  end

  return
end


  function sino = fbp2_sino_weight(sg, sino, varargin)
%|function sino = fbp2_sino_weight(sg, sino, varargin)
%|
%| Apply sinogram weighting for first step of 2D fan-beam FBP.
%| This matlab version is the backup alternative for users lacking mex routine.
%|
%| in
%|	sg			sino_geom()
%|	ig			image_geom()
%|	sino	[nb,na,(L)]	fan-beam sinogram(s) (line integrals)
%| out
%|	sino	[nb,na,(L)]	weighted sinogram
%|
%| Copyright 2006-4-19 by Jeff Fessler, University of Michigan

if nargin == 1 && streq(sg, 'test'), fbp2_sino_weight_test, return, end
if nargin < 2, ir_usage, end

arg.chat = 0;
arg = vararg_pair(arg, varargin);

idim = size(sino);
sino = reshape(sino, idim(1), idim(2), []); % [nb,na,*L]
sino = fbp2_sino_weight_do(sino, sg);
sino = reshape(sino, idim); % [nb,na,(L)]

%
% fbp2_sino_weight_do()
%
function sino = fbp2_sino_weight_do(sino, sg)

if isinf(sg.dfs)
	sino = fbp2_sino_weight_flat(sino, ...
		sg.s, sg.dsd, sg.dso, sg.source_offset);
elseif sg.dfs == 0
	sino = fbp2_sino_weight_arc(sino, ...
		sg.s, sg.dsd, sg.dso, sg.source_offset);
else
	error 'only flat and arc done'
end

%
% fbp2_sino_weight_arc()
%
function sino = fbp2_sino_weight_arc(sino, ss, dsd, dso, source_offset);
na = size(sino,2);
nz = size(sino,3);
gam = ss / dsd;
w1 = abs(dso * cos(gam) - source_offset * sin(gam)) / dsd; % 1D weighting
sino = sino .* repmat(w1, [1 na nz]);

%
% fbp2_sino_weight_flat()
%
function sino = fbp2_sino_weight_flat(sino, ss, dsd, dso, source_offset);
na = size(sino,2);
nz = size(sino,3);
gam = atan(ss / dsd);
w1 = abs(dso * cos(gam) - source_offset * sin(gam)) / dsd; % 1D weighting
sino = sino .* repmat(w1, [1 na nz]);

%
% fbp2_sino_weight_test()
%
function fbp2_sino_weight_test
sg = sino_geom('ge1', 'down', 4);
sino = sg.ones;
s1 = fbp2_sino_weight(sg, sino);

im pl 1 2
im(1, s1), cbar
%max_percent_diff(s1,s2)


function pos2 = aberat (pos1, ve, tlight)

% this function corrects position vector for aberration of light.
% algorithm includes relativistic terms.  adapted from murray (1981)
% mon. notices royal ast. society 195, 639-648.

% input

%  pos1   = position vector of observed object, with reespect to
%           origin at observer (or the geocenter), components in au

%  ve     = velocity vector of observer (or the geocenter),
%           with respect to origin at solar system barycenter,
%           components in au/day (in)

%  tlight = light time from body to observer (or the geocenter) in days

% output

%  pos2 = position vector of observed object, with respect to
%         origin at observer (or the geocenter), corrected
%         for aberration, components in au

% ported from NOVAS 3.1

%%%%%%%%%%%%%%%%%%%%%%%

% light-time for one astronomical unit in seconds, from de-405

ausec = 499.0047838061d0;

% speed of light in au/day

c = 86400.0d0 / ausec;

tl = tlight;

p1mag = tl * c;

if (tl == 0.0d0)

    p1mag = sqrt(pos1(1)^2 + pos1(2)^2 + pos1(3)^2);

    tl = p1mag / c;
end

vemag = sqrt(ve(1)^2 + ve(2)^2 + ve(3)^2);

beta = vemag / c;

rdotv = pos1(1) * ve(1) + pos1(2) * ve(2) + pos1(3) * ve(3);

cosd = rdotv / (p1mag * vemag);

gammai = sqrt(1.0d0 - beta^2);

p = beta * cosd;

q = (1.0d0 + p / (1.0d0 + gammai)) * tl;

r = 1.0d0 + p;

for j = 1:1:3

    pos2(j) = (gammai * pos1(j) + q * ve(j)) / r;

end


%IMM_FILTER  Interacting Multiple Model (IMM) Filter prediction and update steps
%
% Syntax:
%   [X_i,P_i,MU,X,P] = IMM_FILTER(X_ip,P_ip,MU_ip,p_ij,ind,dims,A,Q,Y,H,R)
%
% In:
%   X_ip  - Cell array containing N^j x 1 mean state estimate vector for
%           each model j after update step of previous time step
%   P_ip  - Cell array containing N^j x N^j state covariance matrix for
%           each model j after update step of previous time step
%   MU_ip - Vector containing the model probabilities at previous time step
%   p_ij  - Model transition matrix
%   ind   - Indices of state components for each model as a cell array
%   dims  - Total number of different state components in the combined system
%   A     - State transition matrices for each model as a cell array.
%   Q     - Process noise matrices for each model as a cell array.
%   Y    - Dx1 measurement vector.
%   H    - Measurement matrices for each model as a cell array.
%   R    - Measurement noise covariances for each model as a cell array.
%
%
% Out:
%   X_p  - Updated state mean for each model as a cell array
%   P_p  - Updated state covariance for each model as a cell array
%   MU   - Model probabilities as vector
%   X    - Combined state mean estimate
%   P    - Combined state covariance estimate
%
% Description:
%   IMM filter prediction and update steps. Use this instead
%   of separate prediction and update functions, if you don't need
%   the prediction estimates.
%
% See also:
%   IMM_UPDATE, IMM_SMOOTH, IMM_FILTER

% History:
%   01.11.2007 JH The first official version.
%
% Copyright (C) 2007 Jouni Hartikainen
%
% $Id: imm_update.m 111 2007-11-01 12:09:23Z jmjharti $
%
% This software is distributed under the GNU General Public
% Licence (version 2 or later); please refer to the file
% Licence.txt, included with the software, for details.

function [X_i,P_i,MU,X,P] = imm_filter(X_ip,P_ip,MU_ip,p_ij,ind,dims,A,Q,Y,H,R)
    % Number of models
    m = length(X_ip);

    % Default values for state mean and covariance
    MM_def = zeros(dims,1);
    PP_def = diag(20*ones(dims,1));

    % Normalizing factors for mixing probabilities
    c_j = zeros(1,m);
    for j = 1:m
        for i = 1:m
            c_j(j) = c_j(j) + p_ij(i,j).*MU_ip(i);
        end
    end

    % Mixing probabilities
    MU_ij = zeros(m,m);
    for i = 1:m
        for j = 1:m
            MU_ij(i,j) = p_ij(i,j) * MU_ip(i) / c_j(j);
        end
    end

    % Calculate the mixed state mean for each filter
    X_0j = cell(1,m);
    for j = 1:m
        X_0j{j} = zeros(dims,1);
        for i = 1:m
            X_0j{j}(ind{i}) = X_0j{j}(ind{i}) + X_ip{i}*MU_ij(i,j);
        end
    end

    % Calculate the mixed state covariance for each filter
    P_0j = cell(1,m);
    for j = 1:m
        P_0j{j} = zeros(dims,dims);
        for i = 1:m
            P_0j{j}(ind{i},ind{i}) = P_0j{j}(ind{i},ind{i}) + MU_ij(i,j)*(P_ip{i} + (X_ip{i}-X_0j{j}(ind{i}))*(X_ip{i}-X_0j{j}(ind{i}))');
        end
    end

    % Space for estimates
    X_p = cell(1,m);
    P_p = cell(1,m);
    X_i = cell(1,m);
    P_i = cell(1,m);
    lambda = zeros(1,m);

    % Filter the estimates for each model
    for i = 1:m
        % Predict the estimates
        [X_p{i}, P_p{i}] = kf_predict(X_0j{i}(ind{i}),P_0j{i}(ind{i},ind{i}),A{i},Q{i});
        % Update the estimates
        [X_i{i}, P_i{i}, K, IM, IS] = kf_update(X_p{i},P_p{i},Y,H{i},R{i});

        % Calculate likelihoods
        lambda(i) = kf_lhood(X_p{i},P_p{i},Y,H{i},R{i});
    end

     % Calculate the model probabilities
    MU = zeros(1,m);
    c = sum(lambda.*c_j);
    MU = c_j.*lambda/c;

    % Output the combined updated state mean and covariance, if wanted.
    if nargout > 3
        % Space for estimates
        X = zeros(dims,1);
        P = zeros(dims,dims);
        % Updated state mean
        for i = 1:m
            X(ind{i}) = X(ind{i}) + MU(i)*X_i{i};
        end
        % Updated state covariance
        for i = 1:m
            P(ind{i},ind{i}) = P(ind{i},ind{i}) + MU(i)*(P_i{i} + (X_i{i}-X(ind{i}))*(X_i{i}-X(ind{i}))');
        end
    end


function dx = dynamics(x,u,p)
% dx = dynamics(x,u,p)
%
% Computes the dynamics for the simple pendulum
%

q = x(1,:);
dq = x(2,:);

k = p.k;    c = p.c;
ddq = -c*dq - k*sin(q) + u;
dx = [dq;ddq];

end


function [traj_ok] = checkTrajectory(Trajectory, drag_coefficient, roh_air, vehiclemass_kg, EmergencyLine_b, P_VDC_FinalSpeedEmergency_mps)
%_________________________________________________________________
%% Documentation
%
% Authors:      Alexander Wischnewski (alexander.wischnewski@tum.de)
%
% Start Date:   01.07.2019
%
% Description:  checks whether a given trajectory complies with the
%               specified acceleration limits and (optional) if it is a
%               valid emergency trajectory.
%
% Inputs:
%   Trajectory                  Trajectory to be checked (see tests for format)
%   drag_coefficient            Vehicle drag coefficient
%   roh_air                     air density
%   vehiclemass_kg              vehicle mass
%   P_VDC_MinVelSlipCalc_mps    Speed above which slip calculation works
%   EmergencyLine_b             Set to true if it should be verified that this
%                               trajectory comes to a full stop.
%
% Outputs:
%   traj_ok             True if trajectory complies with all specs

% Parameters (these are *not* tunable by intentation and therefore set here)
AccCheckTolerance = 1.025;
AccelerationErrorTolerance_mps2 = 2;

% basic checks if a valid trajectory has been send
if(Trajectory.TrajCnt == 0)
    traj_ok = false;
    return
end

% check if trajectory increases strictly monotonic
if(any(diff(Trajectory.s_loc_m) <= 0))
    traj_ok = false;
    return
end

% calculate acceleration requested by a driving force free vehicle to compensate for tire
% force influence
ax_mps2 = Trajectory.ax_mps2 + (0.5.*drag_coefficient.*roh_air.*Trajectory.v_mps.^2)./vehiclemass_kg;

% calculate required lateral acceleration for every point
ay_mps2 = Trajectory.kappa_radpm.*Trajectory.v_mps.^2;
% check if every point complies with the acceleration limits
acc_ok = abs(ax_mps2./Trajectory.ax_lim_mps2) + abs(ay_mps2./Trajectory.ay_lim_mps2) <= AccCheckTolerance;
if(any(~acc_ok))
    traj_ok = false;
    return
end

% if it is an emergency line, it must end with very low velocity
if(EmergencyLine_b && Trajectory.v_mps(end) > P_VDC_FinalSpeedEmergency_mps)
    traj_ok = false;
    return
end

% verfiy if velocity profile matches velocity where velocity is larger than minimum slip speed
ax_mps2_recalc = calcPathAx(Trajectory.s_loc_m, Trajectory.v_mps);
ax_mps2_error = ax_mps2_recalc - Trajectory.ax_mps2;
for i = 1:1:(length(Trajectory.ax_mps2)-1)
    % only consider indices where the next index is still above zero
    if(Trajectory.v_mps(i+1) > 0)
        % perform actual check
        if(abs(ax_mps2_error) > AccelerationErrorTolerance_mps2)
            traj_ok = false;
            return
        end
    % if speed is zero only check that acceleration is negative
    else
        if(Trajectory.ax_mps2 > 0)
            traj_ok = false;
            return
        end
    end
end

% everything is ok
traj_ok = true;
end


%% Experiment with the cnn_mnist_fc_bnorm

[net_bn, info_bn] = cnn_mnist(...
  'expDir', 'data/mnist-bnorm', 'batchNormalization', true);

[net_fc, info_fc] = cnn_mnist(...
  'expDir', 'data/mnist-baseline', 'batchNormalization', false);

figure(1) ; clf ;
subplot(1,2,1) ;
semilogy(info_fc.val.objective', 'o-') ; hold all ;
semilogy(info_bn.val.objective', '+--') ;
xlabel('Training samples [x 10^3]'); ylabel('energy') ;
grid on ;
h=legend('BSLN', 'BNORM') ;
set(h,'color','none');
title('objective') ;
subplot(1,2,2) ;
plot(info_fc.val.error', 'o-') ; hold all ;
plot(info_bn.val.error', '+--') ;
h=legend('BSLN-val','BSLN-val-5','BNORM-val','BNORM-val-5') ;
grid on ;
xlabel('Training samples [x 10^3]'); ylabel('error') ;
set(h,'color','none') ;
title('error') ;
drawnow ;


function sF = dtheta(sF)
% first derivative in direction theta

s = size(sF);
sF = reshape(sF, []);

sF.fhat(1) = 0; % exclude some special cases
fhat = zeros((sF.bandwidth+2)^2, length(sF));
for m = 0:sF.bandwidth+1
  if 0 <= m-1
    fhat(m^2+1:(m+1)^2, :) = (m-1)*sqrt((m^2-(-m:m)'.^2)/((2*m-1)*(2*m+1))).*[zeros(1, length(sF)); sF.fhat((m-1)^2+1:m^2, :); zeros(1, length(sF))];
  end
  if m+1 <= sF.bandwidth
    fhat(m^2+1:(m+1)^2, :) = fhat(m^2+1:(m+1)^2, :)-(m+2)*sqrt(((m+1)^2-(-m:m)'.^2)/((2*m+1)*(2*m+3))).*sF.fhat((m+1)^2+2:(m+2)^2-1, :);
  end
end

sF = S2FunHarmonic(fhat);
f = @(v) sF.eval(v)./max(sin(v.theta), eps);
sF = S2FunHarmonic.quadrature(f, 'bandwidth', sF.bandwidth);
sF = reshape(sF, s);

end


function [params, result] = ortho_quasi(params, W, w)
% Quasi-orthogonalization
%   W = orthof(params, W)     for symmetric dss
%   w = orthof(params, W, w)  for deflation dss
%     params.alpha  For deflation...
%     W Matrix with projection vectors as rows. For deflation
%       algorithm only previously calculated projections are given.
%     w Currently iterated projection. Only for deflation algorithm.

% Copyright (C) 2004, 2005 DSS MATLAB package team (dss@cis.hut.fi).
% Distributed by Laboratory of Computer and Information Science,
% Helsinki University of Technology. http://www.cis.hut.fi/projects/dss/.
% $Id$

if nargin<2
    params.name = 'Quasi-orthogonalization';
    params.description = 'Description of this function.';
    params.param = {'alpha'};
    params.param_type = {'scalar'};
    params.param_value = {0.1};
    params.param_desc = {'For deflation...'};
    return;
end

if ~isfield(params, 'alpha')
  % TODO: set alpha based on data dimension
  params.alpha = 0.1;
end

if nargin>2
  % per component quasi-orthogonalization
  w = w - params.alpha * W' * W * w;
  result = w / norm(w);
else
  % symmetric quasi-orthogonalization
  W = 3/2*W - W'*W*W'/2;
  result = W.*(sum(W.^2,2).^(-1/2)*ones(1,size(W,2)));
end


function [r,u,t,p] = macroproperties1d(n,j_x,E,nx,nv,theta)
%% Recover Macroscopic Properties
% compute back, fugacity, macroscopic velocities, temperature and pressure.
    % Computing first velocite,s from the momentum:
    u = j_x./n;

% to compute fugacity, temperature and pressure, we need to rely on the
% distribution fucntion that we where are using: MB, FD, BE.

switch theta
    case{-1} % BE
    % If BE: we apply bisection method to the approx BE distribution Eq.
        r_a = 0.001; r_b = 0.99; tol = 1e-7;
        for i = 1:nx
        psi = @(r_x) 2*E(i)- BE(r_x,1.5)*(n(i)/BE(r_x,0.5))^3/(2*pi) ...
        - n(i)*(u(i)^2);
        r_p = bisection(psi,r_a,r_b,tol);
        r(i) = r_p;
        t(i) = n(i)^2/(pi*(BE(r_p,0.5))^2);
        p(i) = E(i) - 1/2*n(i)*(u(i)^2);
        end

    case{1} % FD
    % if FD: we apply bisection method to the approx FD distribution Eq.
        r_a = 0.001; r_b = 0.99; tol = 1e-7;
        for i = 1:nx
        psi = @(r_x) 2*E(i)- FD(r_x,1.5)*(n(i)/FD(r_x,0.5))^3/(2*pi) ...
        - n(i)*(u(i)^2);
        r_p = bisection(psi,r_a,r_b,tol);
        r(i) = r_p;
        t(i) = n(i)^2/(pi*(FD(r_p,0.5))^2);
        p(i) = E(i) - 1/2*n(i)*(u(i)^2);
        end

    case{0} % MB
    % IF MB: the task is much simple.
        t = 4*E./n - 2*u.^2;
        r = n./sqrt(pi.*t);
        p = E - 1/2.*n.*u.^2;
    otherwise
        error('theta can only be: -1, 0, +1 ');
end

% Using Discrete Ordinate Method:
%     r = repmat(r,nv,1);     u = repmat(u,nv,1);
%     t = repmat(t,nv,1);     p = repmat(p,nv,1);
%     n = repmat(n,nv,1);


function loglik = compute_log_lik(P, S_bar, V, E_z, E_zz, RO, Tr, sigma_sq)

[K, T] = size(E_z);
J = size(S_bar, 2);

M_t = zeros(2*J, K);

loglik = - 0.5*T * (2*J*log(sigma_sq));
for t = 1:T,
   R_t = RO{t};

   Sdef = S_bar;
   for kk = 1:K,
      Sdef = Sdef + E_z(kk,t)*V((kk-1)*3+[1:3],:);

      M_t(1:J, kk) = (R_t(1,:)*V((kk-1)*3+[1:3],:))';
      M_t(J+1:end, kk) = (R_t(2,:)*V((kk-1)*3+[1:3], :))';
   end;

   invSigmaSq_p = eye(2*J)./sigma_sq;

   f_bar_t = R_t(1:2,:)*S_bar;
   f_bar_t = [f_bar_t(1,:) f_bar_t(2,:)]';

   f_t = [P(t, :) P(t+T, :)]';
   t_vect_t = [Tr(t,1)*ones(J,1); Tr(t,2)*ones(J,1)];

   covZ_t = E_zz((t-1)*K+1:t*K,:) - E_z(:,t)*E_z(:,t)';
   loglik = loglik - 0.5*(((f_t-f_bar_t-t_vect_t)./sigma_sq)'*(f_t-f_bar_t-t_vect_t)) + (((f_t-f_bar_t-t_vect_t)'./sigma_sq)*M_t*E_z(:,t)) ...
      - 0.5*trace(((M_t./sigma_sq)'*M_t) * E_zz((t-1)*K+1:t*K,:)) - 0.5*log(det(covZ_t));
end


% Data file PILZ1
% Forced damped vibrations of a mechanical
% system with one degree of freedom, contained
% a reverse pendulum.
  Ek   = '3.6666*qt^2'; % Kinetic energy
  N    = '(10*sin(p*t)-c*q-k*qt+19.62*sin(10*q))*qt'; % power
  Tend = 10;    % upper bound of integration
  q0   = '0.1'; % initial coordinate
  qt0  = '0';   % initial velocity
  eps  = 1e-10; % desirable accuracy
  np   = 3;     % number of parameters
  P{1} = 'c';   % spring stiffness
  P{2} = 'k';   % coefficient of damping
  P{3} = 'p';   % disturbance frequency


function result = calcPreRecRadiusLabel(queryLabel, retrievalLabel, qB, rB)
%% Function: calcPreRecRadiusLabel
%   calculate precision and recall within different radius based on Label.
% Input:
%   queryLabel: 0-1 label matrix (numQuery * numLabel) for query set.
%   retrievalLabel: 0-1 label matrix (numQuery * numLabel) for retrieval set.
%   qB: compressed binary code for query set.
%   rB: compressed binary code for retrieval set.
% Output:
%   result.Pre: maxR-dims vector. Precision within different hamming radius.
%   result.Rec: maxR-dims vector. Recall within different hamming radius.
Wtrue = queryLabel * retrievalLabel' > 0;
Dhamm = hammingDist(qB, rB);

maxHamm = max(Dhamm(:));
totalGoodPairs = sum(Wtrue(:));

% find pairs with similar codes
precision = zeros(maxHamm, 1);
recall = zeros(maxHamm, 1);
for n = 1: length(precision)
    j = (Dhamm <= ((n-1) + 00.001));
    retrievalGoodPairs = sum(Wtrue(j));

    retrievalPairs = sum(j(:));
    precision(n) = retrievalGoodPairs / (retrievalPairs + eps);
    recall(n) = retrievalGoodPairs / totalGoodPairs;
end

result.Pre = precision;
result.Rec = recall;
end


function [relaxRxnBool, solutionRelax] = minCardinalityConservationRelaxationVector(S, param, printLevel)
% DC programming for solving the cardinality optimization problem
%
% .. math::
%
%    min  ~& \lambda ||x||_0 \\
%    s.t. ~& x + S^T z = 0 \\
%         ~& -\infty \leq x \leq \infty, \\
%         ~& 1 \leq z \leq 1 / \epsilon
%
% USAGE:
%
%    [relaxRxnBool, solutionRelax] = minCardinalityConservationRelaxationVector(S, param, printLevel)
%
% INPUT:
%    S:                `m` x `n` stoichiometric matrix
%
% OPTIONAL INPUTS:
%    param:           structure with:
%
%                        * param.epsilon - (getCobraSolverParams('LP', 'feasTol')*100) 1/epsilon is the largest flux expected
%                        * param.eta - (`feasTol` * 100), cutoff for mass leak/siphon
%                        * param.nonRelaxBool - (false(n, 1)), `n` x 1 boolean vector for reactions not to relax
%    printLevel:       verbose level
%
% OUTPUTS:
%    relaxRxnBool:     `n` x 1 boolean vector where true correspond to relaxation
%    solutionRelax:    structure with:
%
%                        * solutionRelax.stat - solution status
%                        * solutionRelax.x - `n` x 1 vector where nonzeros>eta correspond to relaxations
%                        * solutionRelax.z - `m` x 1 vector where positives correspond to molecular mass

[mlt,nlt]=size(S');

if ~exist('param','var') || isempty(param)
    param.epsilon=getCobraSolverParams('LP', 'feasTol')*100;
    feasTol = getCobraSolverParams('LP', 'feasTol');
    param.eta=feasTol*100;
    param.nonRelaxBool=false(mlt,1);
    param.checkConsistency=0;
else
    if ~isfield(param,'epsilon')
        param.epsilon=getCobraSolverParams('LP', 'feasTol')*100;
    end
    if ~isfield(param,'eta')
        feasTol = getCobraSolverParams('LP', 'feasTol');
        param.eta=feasTol*100;
    end
    if ~isfield(param,'nonRelaxBool')
        param.nonRelaxBool=false(mlt,1);
    end
    if ~isfield(param,'checkConsistency')
        param.checkConsistency=1;
    end
end

if ~exist('printLevel','var')
    printLevel =0;
end

if param.checkConsistency
    % Check the stoichiometric consistency of the network without relaxation by
    % solving the following linear problem
    %       min sum(m_i)
    %           s.t     S'*m = 0
    %                   m >= 1
    % where l  is is a  mx1 vector of the molecular mass of m molecular species
    cardProblem.A=S';
    cardProblem.b=zeros(size(cardProblem.A,1),1);
    cardProblem.lb=ones(size(cardProblem.A,2),1);
    cardProblem.ub=inf*ones(size(cardProblem.A,2),1);
    cardProblem.c=1*ones(size(cardProblem.A,2),1);
    cardProblem.osense=1;
    cardProblem.csense(1:size(cardProblem.A,1),1)='E';

    solutionRelax = solveCobraLP(cardProblem,'printLevel',printLevel);
    if solutionRelax.stat==1
        solutionRelax.z = solutionRelax.full;
        solutionRelax.x = S'*solutionRelax.z;
    end
    done=1;
else
    done=0;
end

%relaxation problem
if done==0
    cardProblem.p=mlt;
    cardProblem.q=0;
    cardProblem.r=nlt;
    cardProblem.c=zeros(nlt+mlt,1);
    if 1
        cardProblem.A=[speye(mlt,mlt),S'];
    else
        cardProblem.A=[sparse(mlt,mlt),S'];
    end
    cardProblem.b=zeros(mlt,1);
    cardProblem.lb=[-inf*ones(mlt,1);ones(nlt,1)];
    %cardProblem.lb=[zeros(mlt,1);epsilon*ones(nlt,1)];
    cardProblem.ub=[inf*ones(nlt,1);(1/param.epsilon)*ones(mlt,1)];
    %omits flux from this reaction - perhaps not a good way to do it.
    if any(param.nonRelaxBool)
        %prevent relaxation of specified reactions
        cardProblem.lb([param.nonRelaxBool;false(mlt,1)])=0;
        cardProblem.ub([param.nonRelaxBool;false(mlt,1)])=0;
    end
    cardProblem.csense(1:mlt,1)='E';
    cardProblem.lambda0=1;
    cardProblem.lambda1=getCobraSolverParams('LP', 'feasTol')*100;% sensitive to this value, 1e-4 works for Recon3Model.
    cardProblem.delta=0;
    solutionRelax = optimizeCardinality(cardProblem,param);
    %  problem                  Structure containing the following fields describing the problem
    %       p                   size of vector x
    %       q                   size of vector y
    %       r                   size of vector z
    %       c                   (p+q+r) x 1 linear objective function vector
    %       lambda              trade-off parameter of ||x||_0
    %       delta               trade-off parameter of ||y||_0
    %       A                   s x (p+q+r) LHS matrix
    %       b                   s x 1 RHS vector
    %       csense              s x 1 Constraint senses, a string containting the constraint sense for
    %                           each row in A ('E', equality, 'G' greater than, 'L' less than).
    %       lb                  (p+q+r) x 1 Lower bound vector
    %       ub                  (p+q+r) x 1 Upper bound vector
    %
    % OPTIONAL INPUTS
    % param                    parameters structure
    %       nbMaxIteration      stopping criteria - number maximal of iteration (Defaut value = 1000)
    %       epsilon             stopping criteria - (Defaut value = 10e-6)
    %       theta               parameter of the approximation (Defaut value = 2)
    %
    % OUTPUT
    % solution                  Structure containing the following fields
    %       x                   p x 1 solution vector
    %       y                   q x 1 solution vector
    %       z                   r x 1 solution vector
    %       stat                status
    %                           1 =  Solution found
    %                           2 =  Unbounded
    %                           0 =  Infeasible
    %                           -1=  Invalid input
end

%check optimality
if printLevel>2
    fprintf('%g%s\n',norm(solutionRelax.x + S'*solutionRelax.z),' = ||x + S''*z||')
    fprintf('%g%s\n',min(solutionRelax.z),' = min(z_i)')
    fprintf('%g%s\n',max(solutionRelax.z),' = min(z_i)')
    fprintf('%g%s\n',min(solutionRelax.x),' = min(x_i)')
    fprintf('%g%s\n',max(solutionRelax.x),' = max(x_i)')
end

if solutionRelax.stat==1
    %conserved if relaxation is below epsilon
    relaxRxnBool=abs(solutionRelax.x)>=param.eta;
    if printLevel>1
        fprintf('%g%s\n',norm(S(:,~relaxRxnBool)'*solutionRelax.z),' = ||N''*z|| (should be zero)')
    end
    if printLevel>1
        fprintf('%s\n',[int2str(nnz(relaxRxnBool)) '/' int2str(length(relaxRxnBool)) ' reactions relaxed.'])
    end
else
    disp(solutionRelax)
    error('solve for minimum cardinality of conservation relaxation vector failed')
end


function [N,E] = rentian_scaling(A,XYZ,n)
%RENTIAN_SCALING    Physical Rentian scaling
%
% [N E] = rentian_scaling(A,XYZ,n)
%
% Physical Rentian scaling (or more simply Rentian scaling) is a property
% of systems that are cost-efficiently embedded into physical space. It is
% what is called a "topo-physical" property because it combines information
% regarding the topological organization of the graph with information
% about the physical placement of connections. Rentian scaling is present
% in very large scale integrated circuits, the C. elegans neuronal network,
% and morphometric and diffusion-based graphs of human anatomical networks.
% Rentian scaling is determined by partitioning the system into cubes,
% counting the number of nodes inside of each cube (N), and the number of
% edges traversing the boundary of each cube (E). If the system displays
% Rentian scaling, these two variables N and E will scale with one another
% in loglog space. The Rent's exponent is given by the slope of log10(E)
% vs. log10(N), and can be reported alone or can be compared to the
% theoretical minimum Rent's exponent to determine how cost efficiently the
% network has been embedded into physical space. Note: if a system displays
% Rentian scaling, it does not automatically mean that the system is
% cost-efficiently embedded (although it does suggest that). Validation
% occurs when comparing to the theoretical minimum Rent's exponent for that
% system.
%
% Inputs:
% 	A       MxM adjacency matrix
%           must be unweighted, binary, and symmetric.
% 	XYZ     Vector of node placement coordinates
%           must be Mx3 matrix, where M is the number of nodes.
% 	n       Number of partitions to compute. Each partition is a data
%           point. You want a large enough number to adequately estimate
%           the Rent's exponent.
%
% Outputs:
%	N       nx1 vector of the number of nodes in each of the n partitions.
%	E       nx1 vector of the number of edges crossing the boundary of each
%           partition.
%
% Subsequent Analysis:
%   Rentian scaling plots are then created by: figure; loglog(E,N,'*');
%
%	To determine the Rent's exponent, p, it is important not to use
%	partitions which may be affected by boundary conditions. In Bassett et
%	al. 2010 PLoS CB, only partitions with N<M/2 were used in the
%	estimation of the Rent's exponent. Thus, we can define N_prime =
%	N(find(N<M/2)) and E_prime = E(find(N<M/2)). Next we need to determine
%	the slope of Eprime vs. Nprime in loglog space, which is the Rent's
%	exponent. There are many ways of doing this with more or less
%	statistical rigor. Robustfit in MATLAB is one such option:
%       [b,stats] = robustfit(log10(N_prime),log10(E_prime))
%
%   Then the Rent's exponent is b(1,2) and the standard error of the
%   estimation is given by stats.se(1,2).
%
% Note: n=5000 was used in Bassett et al. 2010 in PLoS CB.
%
%
% Reference:
%   Danielle S. Bassett, Daniel L. Greenfield, Andreas Meyer-Lindenberg,
%   Daniel R. Weinberger, Simon W. Moore, Edward T. Bullmore. Efficient
%   physical embedding of topologically complex information processing
%   networks in brains and computer circuits. PLoS Comput Biol, 2010,
%   6(4):e1000748.
%
%
% Danielle Bassett, UCSB, 2010

% determine the number of nodes in the system
M = numel(XYZ(:,1));
% rescale coordinates so that they are all greater than unity
XYZn = XYZ-repmat(min(XYZ)-1,M,1);
% find the absolute minimum and maximum over all directions
nmax = max(max(XYZn));
nmin = min(min(XYZn));

% initialize variables
count = 0;
N = zeros(n,1);
E = zeros(n,1);

% create partitions, and count the number of nodes inside the partition (N) and the number of % edges traversing the boundary of the partition  (E)
while count<(n+1);
    % define cube end points
    randx = sort((1+nmax-nmin).*rand(2,1),'ascend');
    % find nodes in cube
    L = find(XYZn(:,1)>randx(1) & XYZn(:,1)<randx(2) & XYZn(:,2)>randx(1) & XYZn(:,2)<randx(2) & XYZn(:,3)>randx(1) & XYZn(:,3)<randx(2));
    if ~isempty(L)
        count = count+1;
        % count edges crossing the boundary of the cube
        E(count,1) = sum(sum(A(L,setdiff(1:M,L))));
        % count nodes inside of the cube
        N(count,1) = numel(L);
    end
end


% RES = reconLpyr(PYR, INDICES, LEVS, FILT2, EDGES)
%
% Reconstruct image from Laplacian pyramid, as created by buildLpyr.
%
% PYR is a vector containing the N pyramid subbands, ordered from fine
% to coarse.  INDICES is an Nx2 matrix containing the sizes of
% each subband.  This is compatible with the MatLab Wavelet toolbox.
%
% LEVS (optional) should be a list of levels to include, or the string
% 'all' (default).  The finest scale is number 1.  The lowpass band
% corresponds to lpyrHt(INDICES)+1.
%
% FILT2 (optional) can be a string naming a standard filter (see
% namedFilter), or a vector which will be used for (separable)
% convolution.  Default = 'binom5'.  EDGES specifies edge-handling,
% and defaults to 'reflect1' (see corrDn).

% Eero Simoncelli, 6/96

function res = reconLpyr(pyr, ind, levs, filt2, edges)

if (nargin < 2)
  error('First two arguments (PYR, INDICES) are required');
end

%%------------------------------------------------------------
%% DEFAULTS:

if (exist('levs') ~= 1)
  levs = 'all';
end

if (exist('filt2') ~= 1)
  filt2 = 'binom5';
end

if (exist('edges') ~= 1)
  edges= 'reflect1';
end
%%------------------------------------------------------------

maxLev =  1+lpyrHt(ind);
if strcmp(levs,'all')
  levs = [1:maxLev]';
else
  if (any(levs > maxLev))
    error(sprintf('Level numbers must be in the range [1, %d].', maxLev));
  end
  levs = levs(:);
end

if isstr(filt2)
  filt2 = namedFilter(filt2);
end

filt2 = filt2(:);
res_sz = ind(1,:);

if any(levs > 1)

  int_sz = [ind(1,1), ind(2,2)];

  nres = reconLpyr( pyr(prod(res_sz)+1:size(pyr,1)), ...
      ind(2:size(ind,1),:), levs-1, filt2, edges);

  if (res_sz(1) == 1)
    res = upConv(nres, filt2', edges, [1 2], [1 1], res_sz);
  elseif (res_sz(2) == 1)
    res = upConv(nres, filt2, edges, [2 1], [1 1], res_sz);
  else
    hi = upConv(nres, filt2, edges, [2 1], [1 1], int_sz);
    res = upConv(hi, filt2', edges, [1 2], [1 1], res_sz);
  end

else

  res = zeros(res_sz);

end

if any(levs == 1)
  res = res + pyrBand(pyr,ind,1);
end