function [ n_data, x, fx ] = bessel_j1_values ( n_data )

%*****************************************************************************80
%
%% BESSEL_J1_VALUES returns some values of the J1 Bessel function.
%
%  Discussion:
%
%    In Mathematica, the function can be evaluated by:
%
%      BesselJ[1,x]
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    12 August 2004
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Milton Abramowitz and Irene Stegun,
%    Handbook of Mathematical Functions,
%    US Department of Commerce, 1964.
%
%    Stephen Wolfram,
%    The Mathematica Book,
%    Fourth Edition,
%    Wolfram Media / Cambridge University Press, 1999.
%
%  Parameters:
%
%    Input/output, integer N_DATA.  The user sets N_DATA to 0 before the
%    first call.  On each call, the routine increments N_DATA by 1, and
%    returns the corresponding data; when there is no more data, the
%    output value of N_DATA will be 0 again.
%
%    Output, real X, the argument of the function.
%
%    Output, real FX, the value of the function.
%
  n_max = 21;

  fx_vec = [ ...
      0.3275791375914652E+00, ...
      0.6604332802354914E-01, ...
     -0.3390589585259365E+00, ...
     -0.5767248077568734E+00, ...
     -0.4400505857449335E+00, ...
      0.0000000000000000E+00, ...
      0.4400505857449335E+00, ...
      0.5767248077568734E+00, ...
      0.3390589585259365E+00, ...
     -0.6604332802354914E-01, ...
     -0.3275791375914652E+00, ...
     -0.2766838581275656E+00, ...
     -0.4682823482345833E-02, ...
      0.2346363468539146E+00, ...
      0.2453117865733253E+00, ...
      0.4347274616886144E-01, ...
     -0.1767852989567215E+00, ...
     -0.2234471044906276E+00, ...
     -0.7031805212177837E-01, ...
      0.1333751546987933E+00, ...
      0.2051040386135228E+00 ];

  x_vec = [ ...
     -5.0E+00, ...
     -4.0E+00, ...
     -3.0E+00, ...
     -2.0E+00, ...
     -1.0E+00, ...
      0.0E+00, ...
      1.0E+00, ...
      2.0E+00, ...
      3.0E+00, ...
      4.0E+00, ...
      5.0E+00, ...
      6.0E+00, ...
      7.0E+00, ...
      8.0E+00, ...
      9.0E+00, ...
     10.0E+00, ...
     11.0E+00, ...
     12.0E+00, ...
     13.0E+00, ...
     14.0E+00, ...
     15.0E+00 ];

  if ( n_data < 0 )
    n_data = 0;
  end

  n_data = n_data + 1;

  if ( n_max < n_data )
    n_data = 0;
    x = 0.0;
    fx = 0.0;
  else
    x = x_vec(n_data);
    fx = fx_vec(n_data);
  end

  return
end


function [sampleLabels, groupCodingLengths] = coding_seg(W, epsilon, affine )

% coding_seg.m
%
% Implements an agglomerative segmentation algorithm which attempts to
% minimize the number of bits needed to code the segmented vectors upto
% distortion epsilon^2. This method is described in detail in Ma, et. al.,
% Segmentation of Multivariate Mixed Data via Lossy Coding and Compression,
% PAMI 2007.
%
% Run test_coding_seg.m, included in this package, for a demonstration.
%
% Inputs:
%   W       - [ w1 | w2 | ... | wm ], the data matrix.
%               The columns are the input data vectors.
%
%   epsilon - sqrt of the allowable distortion,
%               E[ \| \hat{w_i} - w_i \|^2 ]
%
%   affine  - boolean, if true the algorithm uses the coding length for
%               nonzero-mean data, derived in Appendix II of the paper.
%
% Outputs:
%   sampleLabels - 1 x m list whose i-th entry is the group assignment of
%                   the i-th data vector w_i. Groups are indexed
%                   sequentially, starting from 1.
%
% Dependencies:
%   coding_length.m
%
% Feb '06, last revision Jan '07
%   Questions? John Wright -- jnwright@uiuc.edu

% Copyright 2007, University of Illinois. All rights reserved.

    VERBOSE = 0;

    epsilon2 = epsilon^2;

    % n - dimension
    % m - number of samples
    [n,m] = size(W);

    % initially treat each sample as its own group
    sampleLabels = 1:m;
    curGroupCount = m;

    % compute the coding length of each of the initial groups
    squaredNormW = sum(W.^2,1);
    if affine
        groupCodingLengths = n * log2(1 + (squaredNormW/epsilon2))/ 2 + log2(m);
    else
        groupCodingLengths = (n + 1) * log2(1 + (n / epsilon2)*squaredNormW)/ 2 + log2(m);
    end

    if VERBOSE
        disp('   Computing initial table.');
    end

    % entropyChange is a matrix whose ij-th element is the entropy lost by
    % merging groups i and j into one group.
    delta_L_matrix = ones(m);

    for groupIndex1 = 1 : m-1
        for groupIndex2 = groupIndex1+1 : m
            L1 = groupCodingLengths(groupIndex1);
            L2 = groupCodingLengths(groupIndex2);
            L_union = coding_length(W(:, [groupIndex1 groupIndex2]),epsilon2,m,affine);
            delta_L_matrix(groupIndex1, groupIndex2) = L_union - L1 - L2;
        end;
    end;

    if VERBOSE
        disp('   Starting merging process');
    end

    while curGroupCount > 1

        if mod( curGroupCount, 50 ) == 0 && VERBOSE
            disp(['   Group count: ' num2str(curGroupCount)]);
        end

        % Find the best two groups to merge together
        [temp minIndex]=min(delta_L_matrix);
        [min_delta_L minIndex2]=min(temp);
        minIndex1=minIndex(minIndex2);

        % If this does not decrease the coding length, we are done
        if ( min_delta_L > 0 )
            break;
        end;

        % Merging groups with labels minIndex1 and minIndex2
        if (minIndex1 > minIndex2)
            temp = minIndex1;
            minIndex1 = minIndex2;
            minIndex2 = temp;
        end

        if (minIndex2 ~= curGroupCount)
            % First make the group to merge have the last label

            % Change the sample labels of the group minIndex2 to be the last
            % label.
            sampleLabels(sampleLabels == curGroupCount) = -1;
            sampleLabels(sampleLabels == minIndex2) = curGroupCount;
            sampleLabels(sampleLabels == -1) = minIndex2;

            % Swap the entropy of groups minIndex2 and the last group.
            [groupCodingLengths(minIndex2), groupCodingLengths(curGroupCount)] = ...
                swap(groupCodingLengths(minIndex2), groupCodingLengths(curGroupCount));

            delta_L_matrix = swap_matrix(delta_L_matrix, minIndex2, curGroupCount);

        end

        % Now we can assume the last group is the group to be merged
        sampleLabels(sampleLabels == curGroupCount) = minIndex1;
        groupCodingLengths(curGroupCount) = [];
        groupCodingLengths(minIndex1) = coding_length(W(:, sampleLabels == minIndex1),epsilon2,m,affine);
        delta_L_matrix = delta_L_matrix(1:end-1, 1:end-1);

        curGroupCount = curGroupCount - 1;

        for groupIndex2 = [1:minIndex1-1 minIndex1+1:curGroupCount],
            i = min(minIndex1, groupIndex2); j = max(minIndex1, groupIndex2);
                L1 = groupCodingLengths(i);
                L2 = groupCodingLengths(j);
                L_union = coding_length(W(:, (sampleLabels == i) | (sampleLabels == j)) ,epsilon2,m,affine);
                delta_L_matrix(i, j) = L_union - L1 - L2;
        end;
    end;

    %codingLength = sum(groupCodingLengths);

    if VERBOSE
        disp(['   Final group count: ' num2str(curGroupCount)]);
    end

end
% If A is an upper triangular matrix where A(m,n) = f(x_m,x_n) for m < n
% relabel so that the roles of x_i and x_j are swapped.
function A = swap_matrix(A, i, j)
[A(1:i-1, i), A(1:i-1, j)] = swap(A(1:i-1, i), A(1:i-1, j));
[A(i, i+1:j-1), A(i+1:j-1, j)] = swap(A(i, i+1:j-1), A(i+1:j-1, j));
[A(i, j+1:end), A(j, j+1:end)] = swap(A(i, j+1:end), A(j, j+1:end));
end

% swap the values of x and y
function [y, x] = swap(x,y)
end


function out = proj_simplex(x,r,eq_flag)
%PROJ_SIMPLEX computes the orthogonal projection onto the r-simplex {x:sum(x)=r,x>=0}
%                                                or r-full simplex {x:sum(x)<=r,x>=0}
%
%  Usage:
%  out = PROJ_SIMPLEX(x,[r],[eq_flag])
%  =============================================================
%  Input:
%  x - point to be projected (vector/matrix)
%  r - a positive scalar [default: 1]
%  eq_flag - a flag that determines whether the projection is onto the r-simplex ('eq') or
%                 the r-full simplex ('ineq') [defualt: 'eq']
%  ==============================================================
%  Assumptions:
%  r > 0
%  ==============================================================
%  Output:
%  out - projection vector

% This file is part of the FOM package - a collection of first order methods for solving convex optimization problems
% Copyright (C) 2017 Amir and Nili Beck
%
% This program 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.
%
% This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

%reading the user x and setting defalut values when reqired.
if (nargin < 1)
    error ('usage: proj_simplex(x,r,[eq_flag])') ;
end

if (( nargin < 3) || (isempty(eq_flag)) )
    %eq_flag is not given, setting to default value: true
    eq_flag = 'eq' ;
end

if ((nargin < 2) || (isempty(r)))
    %r is not given, setting to default value: 1
    r = 1 ;
end

if (r < 0)
    error('Set is infeasible') ;
end

if (strcmp(eq_flag,'eq') == 1)
    %call proj_hyperplane_box
   out = proj_hyperplane_box(x,ones(size(x)),r,0) ;
else
    if (strcmp(eq_flag,'ineq') == 1)
        %call proj_halfspace_box
        out = proj_halfspace_box(x,ones(size(x)),r,0) ;
    else
        error('usage: proj_simplex(x,r,[eq_flag]) - eq_flag should be either eq or ineq') ;
    end
end


% Convert a stroke [x,y,t] such that it is uniformly sampled
% in time. This is done by linear interpolation in time
%
% Input
%  stk:  [n x 2] for x and y
%  time: [n x 1] for time coordinate
%  tint: [scalar] time interval (in milliseconds)
%
% Output
%  unif_stk: [k x 2] new stroke x and y
%  unif_t: [k x 1] uniform time interval
%
function [unif_stk,unif_t] = uniform_time_lerp(stk,time,tint)

    mint = min(time);
    maxt = max(time);

    unif_t = mint:tint:maxt;
    % make sure we don't leave out the last point
    if unif_t(end) ~= maxt
       unif_t = [unif_t maxt];
    end
    nt = length(unif_t);

    unif_stk = zeros(length(unif_t),2);
    for i=1:nt
        ti = unif_t(i);

        diff = time-ti;
        if any(diff==0)
            sel = stk(diff==0,:);
            unif_stk(i,:) = mean(sel,1);
            continue
        end

        % find the point before and after in time
        indx_gt = find(diff>0,1,'first');
        indx_lt = find(diff<0,1,'last');

        x_gt = stk(indx_gt,:);
        x_lt = stk(indx_lt,:);
        t_gt = time(indx_gt);
        t_lt = time(indx_lt);

        % Compute the linear interpolation
        frac = (ti - t_lt) ./ (t_gt - t_lt);
        assert(frac<=1);
        unif_stk(i,:) = (1-frac).*x_lt + frac.*x_gt;
    end

end


function tanh_prime = tanh_prime(a)
    %a = tanh(input);
    tanh_prime = (1-a.^2);
end


function pass = test_nonlinSys1Breaks_C1(pref)
% Test 2x2 system (sin/cos). This is pecewiseificaion of the test
%       test_nonlinearSystem1
%
% Asgeir Birkisson, April 2014.

if ( nargin == 0 )
    pref = cheboppref;
end

tol = 1e-10;

d = [-pi 0 pi];
x = chebfun('x',d);
f = [ 0*x ; 0*x ];

%% Piecewise (chebcolloc1):
pref.discretization = @chebcolloc1;

A = chebop(@(x,u,v) [u - diff(v,2) + u.^2; diff(u) + sin(v)],d);
A.lbc = @(u,v) u-1;
A.rbc = @(u,v) [v-1/2; diff(v)];

u = mldivide(A, f, pref);
u1 = u{1}; u2 = u{2};

% Want to check BCs as well.
bcFunLeft = A.lbc(u1,u2);
bcFunRight = chebfun(A.rbc(u1,u2));

% And check that we're continuous over breakpoint
u1jump = jump(u1, 0);
u2jump = jump(u2, 0);

pass(1) = norm( chebfun(A(x, u1, u2))) < tol;
pass(2) = norm(bcFunLeft(d(1))) < tol && norm(bcFunRight(d(end))) < tol;
pass(3) = norm(u1jump) < tol && norm(u2jump) < tol;

end


%GRAD_AND_DIV
% Generates pressure gradient matrices Pu, Pv and divergence matrix D
% Vectorized matrix generation by evaluation of stencil coefficients
% Output: Pu, Pv, Du, Dv

J= length(dx); K = length(dy);

%.................Pressure gradient matrix Pu ....................

%        	  |    0    |
% Stencil: [Pu] = |p1 p2  0 |
%        	  |    0    |

% Indexing convention in staggered grid:
%  +--k+1--+
%  |       |
%  j  jk  j+1
%  |       |
%  +---k---+
tic
p2 = 1./DXU(1,:)'; p1 = zeros(size(p2)); p1(1:J) = -p2(2:J+1);
Puu = spdiags([p1  p2],[-1;0], J+1, J); Pu = kron(speye(K),Puu);

%......................Boundary corrections....................................
for seg = 1:length(ybc(1,:))
  if (ybc(1,seg) == 1)|(ybc(1,seg) == 2)  % No-slip or inflow at left boundary
    k = 1+kseg(seg):kseg(seg+1); Pu(1+(k-1)*(J+1),:) = 0;
  end
  if (ybc(2,seg) == 1)|(ybc(2,seg) == 2)  % No-slip or inflow at right boundary
    k = 1+kseg(seg):kseg(seg+1);  Pu(k*(J+1),:) = 0;
  end
end
tijd = toc; disp(['Breakdown of grad_and_div time'])
disp(['  Pu time = ',num2str(tijd)])

%.................Pressure gradient matrix Pv ....................

%        	  |     0   |
% Stencil: [Pv] = |0   p2  0|
%        	  |    p1   |

tic
pp1 = zeros(size(XV)); pp2 = pp1;    		% Diagonals of Pv
pp1(2:K+1,:) = -1./DYV(2:K+1,:); 	pp2 = -pp1;
pp2(1,:) = 1./DYV(1,:); 		pp2(K+1,:) = 0;

%......................Boundary corrections....................................
for seg = 1:length(xbc(1,:))
  if (xbc(1,seg) == 1)|(xbc(1,seg) == 2)  % No-slip or inflow at lower boundary
    j = 1+jseg(seg):jseg(seg+1);     	pp1(1,j) = 0; pp2(1,j) = 0;
  end
  if (xbc(2,seg) == 1)|(xbc(2,seg) == 2)  % No-slip or inflow at upper boundary
    j = 1+jseg(seg):jseg(seg+1);	pp1(K+1,j) = 0; pp2(K+1,j) = 0;
  end
end
n = J*(K+1);   p1 = reshape(pp1',n,1);    p2 = reshape(pp2',n,1);
p1 = [p1(J+1:n); zeros(J,1)];		% Shift to accomodate spdiags
Pv = spdiags([p1  p2],[-J;0], n,n-J);
tijd = toc; disp(['  Pv time = ',num2str(tijd)])

%.................u divergence matrix Du ....................

%		  |   0     |
% Stencil: [Du] = |0  p1  p2|
%		  |   0     |

tic
Duu = spdiags([-ones(J,1) ones(J,1)], [0;1], J,J+1);
Du = kron(spdiags(dy',0,K,K),Duu);
tijd = toc; disp(['  Du time = ',num2str(tijd)])

%.................v divergence matrix Dv ....................

%		  |   p2   |
% Stencil: [Dv] = |0  p1  0|
%		  |   0    |

tic
Dvv = spdiags([-ones(K,1) ones(K,1)], [0;1], K,K+1);
Dv = kron(Dvv,spdiags(dx',0,J,J));
tijd = toc; disp(['  Dv time = ',num2str(tijd)])

clear  pp1 pp2 p1 p2 Puu Duu Dvv 			% Save storage


% predicting numerical values using Linear Regression
clear all;
format long
disp('===== Linear Regression ====');
disp('Reading featur vector');

Indices = crossvalind('Kfold', 1107, 10);
figure;

subplot(3,2,1);

for feat = 1:3
      MSEarray =[];
    elapsedarray =[];
    for crossvalidateIter = 1:10
        (fprintf('%d',crossvalidateIter));
    featurs = csvread('data\productsdesc.csv');
    num_data = size(featurs,1); %5000;
    %disp(sprintf('Number of datapoints %d',num_data))

    possiblefeaturizations =  {'bernouli', 'tfidf','multinomial'};
    %featurization = 'bernouli'%'tfidf'%'tfidf'%'multinomial'%'tfidf' %'multinomial'; % 'bernouli', 'tfidf'
    featurization  = possiblefeaturizations{feat};

    featurs = featurs(:,2:size(featurs,2));
    if strcmp(featurization,'multinomial')
        %just pass
    elseif strcmp(featurization,'bernouli')
        featurs = bernoulli(featurs);
    elseif strcmp(featurization,'tfidf')
        featurs = tfidf(featurs);
    end

     size_training = floor(.9*num_data);

        trainingset = featurs(Indices~=crossvalidateIter,:);
        testset = featurs(Indices==crossvalidateIter,:);

    %disp('Splitting up data into training/test sets');
   [num,txt,raw] = xlsread('data\productsratings.csv');

        responsevals = num;

        responsevals_training = responsevals(Indices~=crossvalidateIter,:);
        responsevals_test = responsevals(Indices==crossvalidateIter,:);

    %disp('Linear Regression');
    %

    tic;

    predictions = [];
    actual = [];
    for i =1:3
        a = responsevals_training(:,i);
        b = responsevals_test(:,i)';
        regresscoeff = regress(a, trainingset);
        C2 = (regresscoeff'*(testset'));
        predictions = [predictions, C2'];
        actual = [actual, b'];
    end

    MSE = mean(sum(((predictions-actual).^2)'))
     elapsed = toc;
        MSEarray = [MSEarray MSE];
        elapsedarray = [elapsedarray elapsed];

    end
    featurization  = possiblefeaturizations{feat}
    fprintf('Avergae MSE for Linear Regression is %0.10f\n', mean(MSEarray));
    subplot(3,2,feat*2-1)
    plot(MSEarray,'-o');
    title(strcat('MSE for Linear Regression (',featurization,') ', sprintf(' avergae MSE = %0.10f\n', mean(MSEarray))));
    xlabel('10 fold cross-validation (iteration no)')
    ylabel('MSE')
    subplot(3,2,feat*2)
    plot(elapsedarray,'-o');
    title(strcat('Elapsed time for Linear Regression (',featurization, ') ' , sprintf(' avergae elapsed time = %0.10f\n', mean(elapsedarray))));
    xlabel('10 fold cross-validation (iteration no)')
    ylabel('Elapsed Time')
    drawnow;
end


N = 15; % Set N as an odd number;
w0 = 0.4*pi;
n0 = floor(N/2);
hn = zeros(1,N);
for i = 1:length(hn)
    if i == n0
        hn(i) = w0/pi;
    else
        hn(i) = sin(w0*(i-n0))/(pi*(i-n0));
    end;
end;
subplot(3,1,1);stem(hn);title('h[n]');

Np = 1000;
hn = [hn,zeros(1, Np-length(hn))];%zero padding
[w,y] = calculateDiscreteFourierTransform(hn);

subplot(3,1,2);plot(w,abs(y)),title('幅度谱');
subplot(3,1,3);plot(w,angle(y)),title('相位谱');


function geometry_test2062 ( )

%*****************************************************************************80
%
%% TEST2062 tests TRIANGLE_AREA_HERON;
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  dim_num = 3;

  t = [ ...
    1.0, 0.0, 0.0; ...
    0.0, 1.0, 0.0; ...
    0.0, 0.0, 1.0 ]';

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST2062\n' );
  fprintf ( 1, '  For a triangle in any dimension,\n' );
  fprintf ( 1, '  TRIANGLE_AREA_HERON computes the area;\n' );

  r8mat_transpose_print ( dim_num, 3, t, '  Triangle vertices:' );

  for j = 1 : 3

    s(j) = 0.0;

    jp1 = mod ( j, 3 ) + 1;

    for i = 1 : dim_num
      s(j) = s(j) + ( t(i,j) - t(i,jp1) ).^2;
    end

    s(j) = sqrt ( s(j) );

  end

  r8vec_print ( 3, s, '  Side lengths:' );

  area = triangle_area_heron ( s );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The area is %f\n', area );

  return
end


function x = Adj_USFFT(n,y,omega,D,L,center)
% USFFT_simple -- 1d adjoint unequispaced Fourier transform
% Usage:
%   x = USFFT(n,y,omega,D,L,center)
% Inputs:
%   n      length of output x
%   y	   vector of length m,
%   omega  vector of sampled frequencies (length m)
%   center 0 for unbiased FT, 1 otherwise
% Outputs:
%   x      vector of length n
% Description:
%  Evaluates the adjoint of the FT
%
%  If center = 0,
%
%  x(t) = sum_{k} exp(i omega_k t) y(k), -n/2 <= t < n/2
%
%  If center = 1,
%
%  x(t) = sum_{k} exp(i omega_k t) y(k), 0 <= t < n
%
%  See Also
%   USFFT, USFT_simple, Evaluate_FT
%  Notes:
%   To work properly, D*n must be even
%
% By Emmanuel candes, 2003-2004

  if nargin < 6,
    center = 0;
  end

  if nargin < 5,
    L = 4;
  end

  if nargin < 4,
    D = 16;
  end

  n2 = n/2;
  N = D*n; N2 = N/2;
  m = length(omega);

  if rem(L,2) == 1,
    L = L + 1;
  end

  % Nearest point calculations

  near_index = round(N *omega/(2*pi));
  row = near_index + N/2 + 1;

  delta = omega - 2*pi*near_index/N;

  % Make matrix y(k) * delta(k)^l, l = 0, 1, ..., L-1

  FA = repmat(y(:),1,L);
  for l = 2:L;
    FA(:,l) = FA(:,l-1) .* delta/(l-1);
  end

  % Sum entries corresponding to the same index

  F = zeros(N,L);
  for k = 1:m,
    F(row(k),:) = F(row(k),:) + FA(k,:);
  end

  % Take IFFT along columns
  X = ifft_mid0(F) * N;
  X = X((N2-n2+1):(N2+n2),:);

  t = -n2:(n2-1);
  tp = ones(n,L);

  for k = 2:L;
    tp(:,k) = tp(:,k-1) .* (i*t.');
  end

  x = sum(X.*tp,2);


function imden=den2d_block(x,qmf,thd,L,sigma)

if (exist('x')~=1), 	error('Provide input image'); 		end;
if (exist('qmf')~=1), 	qmf = MakeONFilter('Symmlet',6); 	end;
if (exist('thd')~=1), 	thd = 4.5;				end;

[nx ny] = size(x);
if nx~=ny
	disp('Matrix must be square');
	return;
end

n 	= nx;
J	= nextpow2(n);
if (exist('L')~=1), 	L   = floor(sqrt(2*J)); 		end; % Size of block.
scale 	= floor(log2(L));   % Coarsest scale L#2^Jc matches the size of the block.

% wc=FWT2_PO(x,scale,qmf);
wc = x;

for j = 0:J-1-scale
 HH = wc(2^(J-j-1)+1 : 2^(J-j), 2^(J-j-1)+1 : 2^(J-j));
 HH = block_partition(HH,L);
 %HHmask = sqrt(sum(HH.^2)) >= thd*L*sigma;
 HHmask = max(1-thd*log(n)*sigma^2./sum(HH.^2),0);
 HH = HH .* repmat(HHmask,L*L,1);
 HH = invblock_partition(HH,2^(J-j-1),L);
 wc(2^(J-j-1)+1 : 2^(J-j), 2^(J-j-1)+1 : 2^(J-j)) = HH;

 HL = wc(2^(J-j-1)+1 : 2^(J-j), 1 : 2^(J-j-1));
 HL = block_partition(HL,L);
 %HLmask = sqrt(sum(HL.^2)) >= thd*L*sigma;
 HLmask = max(1-thd*log(n)*sigma^2./sum(HL.^2),0);
 HL = HL .* repmat(HLmask,L*L,1);
 HL = invblock_partition(HL,2^(J-j-1),L);
 wc(2^(J-j-1)+1 : 2^(J-j), 1 : 2^(J-j-1)) = HL;

 LH = wc(1 : 2^(J-j-1), 2^(J-j-1)+1 : 2^(J-j));
 LH = block_partition(LH,L);
 %LHmask = sqrt(sum(LH.^2)) >= thd*L*sigma;
 LHmask = max(1-thd*log(n)*sigma^2./sum(LH.^2),0);
 LH = LH .* repmat(LHmask,L*L,1);
 LH = invblock_partition(LH,2^(J-j-1),L);
 wc(1 : 2^(J-j-1), 2^(J-j-1)+1 : 2^(J-j)) = LH;
end

imden = IWT2_PO(wc,scale,qmf);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = block_partition(x,L)

n 	= length(x);
nblocks = floor(n/L);

out = [];
for i=0:nblocks-1
  out = [out x(i*L+1:(i+1)*L,:)];
end

out = reshape(out(:),L^2,nblocks^2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = invblock_partition(x,n,L)

nblocks = floor(n/L);

buf = reshape(x,L,L*nblocks^2);

out = [];
for i=0:nblocks-1
  out = [out;buf(:,i*L*nblocks+1:(i+1)*L*nblocks)];
end


function [Xt xfm] = perspective_xfm(X,Y)
% perspective_xfm - 2D perspective transformation
%
% Xt = perspective_xfm(X,Y)
%
% X,Y : input matrix (nx2) giving the each points (n) coordinates
% Xt  : X transformed to match Y
% xfm : tranformation matrices (3x3 and 1x3)
%
% 2008/03 SOD: after A Plane Measuring Device by A. Criminisi, I. Reid, and
% A. Zisserman

% input check
if ~exist('X','var') || isempty(X), error('Need X'); end
if ~exist('Y','var') || isempty(Y), error('Need Y'); end
if ~all(size(X) == size(Y)), error('X and Y need to be the same size'); end

% some variables needed
n   = size(Y,1);
my1 = ones(n,1);
my0 = zeros(n,3);

% compute xfm
B = [ X my1 my0  -X(:,1).*Y(:,1) -X(:,2).*Y(:,1) ...
      my0 X my1  -X(:,1).*Y(:,2) -X(:,2).*Y(:,2)];
B = reshape (B', 8 , n*2)';

D = reshape (Y', n*2 , 1 );
l = inv(B' * B) * B' * D;
A = reshape([l(1:6)' 0 0 1 ],3,3)';
C = [l(7:8)' 1];

% now transform
X  = [X my1]';
Xt = ((A*X) ./ (ones(3,1)*(C*X)))';
Xt = Xt(:,1:2);

% xfm matrices
xfm = [A;C];
return


% SQRT_COV  -  Factorizes a positive semi-definite symmetric matrix.
%
% V = SQRT_COV(A)
%
% The matrix A must be positive semi-definite and symmetric. The resulting
% matrix V is such that V*V'=A.

% Last modified 2011-10-20
% Copyright (c) Jaakko Luttinen (jaakko.luttinen@aalto.fi)

function V = sqrt_cov(A)

[L,D] = ldl(A);
D(D<0) = 0;
V = L * sqrt(D);


function [C_tri]=transitivity_bu(A)
%TRANSITIVITY_BU    Transitivity
%
%   T = transitivity_bu(A);
%
%   Transitivity is the ratio of 'triangles to triplets' in the network.
%   (A classical version of the clustering coefficient).
%
%   Input:      A       binary undirected connection matrix
%
%   Output:     T       transitivity scalar
%
%   Reference: e.g. Humphries et al. (2008) Plos ONE 3: e0002051
%
%
%   Alexandros Goulas, Maastricht University, 2010

    C_tri = trace(A^3) / (sum(sum(A^2)) - trace(A^2));

return;


function [ x, seed ] = pareto_sample ( a, b, seed )

%*****************************************************************************80
%
%% PARETO_SAMPLE samples the Pareto PDF.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A, B, the parameters of the PDF.
%    0.0 < A.
%    0.0 < B.
%
%    Input, integer SEED, a seed for the random number generator.
%
%    Output, real X, a sample of the PDF.
%
%    Output, integer SEED, an updated seed for the random number generator.
%
  [ cdf, seed ] = r8_uniform_01 ( seed );

  x = pareto_cdf_inv ( cdf, a, b );

  return
end


function [lambda_vec, error_train, error_val] = ...
    validationCurve(X, y, Xval, yval)
%VALIDATIONCURVE Generate the train and validation errors needed to
%plot a validation curve that we can use to select lambda
%   [lambda_vec, error_train, error_val] = ...
%       VALIDATIONCURVE(X, y, Xval, yval) returns the train
%       and validation errors (in error_train, error_val)
%       for different values of lambda. You are given the training set (X,
%       y) and validation set (Xval, yval).
%

% Selected values of lambda (you should not change this)
lambda_vec = [0 0.001 0.003 0.01 0.03 0.1 0.3 1 3 10]';

% You need to return these variables correctly.
error_train = zeros(length(lambda_vec), 1);
error_val = zeros(length(lambda_vec), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in
%               error_train and the validation errors in error_val. The
%               vector lambda_vec contains the different lambda parameters
%               to use for each calculation of the errors, i.e,
%               error_train(i), and error_val(i) should give
%               you the errors obtained after training with
%               lambda = lambda_vec(i)
%
% Note: You can loop over lambda_vec with the following:
%
%       for i = 1:length(lambda_vec)
%           lambda = lambda_vec(i);
%           % Compute train / val errors when training linear
%           % regression with regularization parameter lambda
%           % You should store the result in error_train(i)
%           % and error_val(i)
%           ....
%
%       end
%
%

for i=1:length(lambda_vec)
    [theta] = trainLinearReg(X,y,lambda_vec(i));
    [error_train(i),~] = linearRegCostFunction(X,y,theta,0);
    [error_val(i),~] = linearRegCostFunction(Xval,yval,theta,0);
end;

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

end


function [A, C, Z] = ldsPca(X, k, m)
% Subspace method for learning linear dynamic system.
% Input:
%   X: d x n data matrix
%   k: dimension of hidden variable
%   m: stacking order for the Hankel matrix
% Output:
%   A: k x k transition matrix
%   C: k x d emission matrix
%   Z: k x n latent variable
%   Y: d x n reconstructed data
% reference: Bayesian Reasoning and Machine Learning (BRML) chapter 24.5.3 p.507
% Written by Mo Chen (sth4nth@gmail.com).
[d,n] = size(X);
H = reshape(X(:,hankel(1:m,m:n)),d*m,[]);
[U,S,V] = svd(H,'econ');
C = U(1:d,1:k);
Z = S(1:k,1:k)*V(:,1:k)';
A = Z(:,2:end)/Z(:,1:end-1); % estimated transition
% Y = C*Z; % reconstructions


function [lList,rList]=genAllLRBinTreesAlt(n)
%%GENALLLRBINTREESALT Generate representations of all binary trees having n
%                     nodes. l(0+1) gives the root node index. l(i+1) and
%                     r(i) then give the left and right descendents of node
%                     i. If there is a zero, then there is no descendent.
%                     The structure of the (left-right) relationships is
%                     what is unique, not the specific nodes. That is, the
%                     identity of the root node does not really matter. For
%                     example, node 1 will never have a left neighbor and
%                     node 4 will never have a right neighbor. However,
%                     independent of node labels, all binary trees are
%                     formed. The format of the output differs from the
%                     function genAllLRBinTrees.
%
%INPUTS: n The number of nodes that will be in the trees. n>=1.
%
%OUTPUTS: lList The (n+1)XnumTrees lists of left links, where lList(i+1,k)
%               is the left link of node i in tree k with lList(0+1,k)
%               being the root.
%         rList The nXnumTrees lists of right links.
%
%This function implements Algorithm L of Section 7.2.1.6 of [1]. The number
%of binary trees is CatalanNumber(n).
%
%EXAMPLE:
%To recreate the list of links in Table 3 of Section 7.2.1.6 of [1], one
%can use
% [lList,rList]=genAllLRBinTreesAlt(4)
%
%REFERENCES:
%[1] D. E. Knuth, The Art of Computer Programming. Vol. 4A: Combinatorial
%    Algorithms, Part I, Boston: Addison-Wesley, 2011.
%
%October 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

numBinTrees=CatalanNumber(n);

%Allocate space
lList=zeros(n+1,numBinTrees);
rList=zeros(n,numBinTrees);
kList=zeros(n,numBinTrees);

k=zeros(n,1);
o=zeros(n+1,1);
l=zeros(n+1,1);
r=zeros(n,1);

%Step L1
l((1:n)+1)=0;
r(1:(n-1))=2:n;
k(1:n)=0:(n-1);
o((1:n)+1)=-1;
l(0+1)=1;
o(0+1)=1;
r(n)=0;

for curTree=1:numBinTrees
    %Step L2
    lList(:,curTree)=l;
    rList(:,curTree)=r;
    kList(:,curTree)=k;
    j=n;
    p=0;

    while(1)
        %Step L3
        if(o(j+1)>0)
            m=l(j+1);

            if(m~=0)
               %Step L5
                if(j==0)
                    return;
                end
                l(j+1)=r(m);
                r(m)=j;
                k(j)=m;
                x=k(m);
                if(x==0)
                    l(p+1)=m;
                else
                    r(x)=m;
                end
                break;
            else
                %p=j;
                o(j+1)=-o(j+1);
                j=j-1;
                continue;
            end
        else%o(j+1)<0
            m=k(j);

            if(m~=0)
                %Step L4
                r(m)=l(j+1);
                l(j+1)=m;
                x=k(m);
                k(j)=x;
                if(x==0)
                   l(p+1)=j;
                else
                   r(x)=j;
                end
                break;
            else
                p=j;
                o(j+1)=-o(j+1);
                j=j-1;
                continue;
            end
        end
    end
end
end

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


function varargout = slogfrac(varargin)
%logfrac
%
% y = SLOGFRAC(x)
%
% Computes/declares log(1 + x(1)/x(2))

switch class(varargin{1})
    case 'double'
        x = varargin{1};
        % Safe version with defined negative values (helps fmincon when
        % outside feasible region)
        if all(x==0)
            varargout{1} = log(2);% ?definition...
        elseif x(1)==0
            varargout{1} = log(1);
        else
            aux = 1 + (x(1)./(x(2)+sqrt(eps)));
            if aux <= 0
                varargout{1} = -15;
            else
                varargout{1} = log(1 + (x(1)./(x(2)+sqrt(eps))));
            end

        end

    case 'sdpvar'
        if min(size(varargin{1}))>1
            error('SLOGFRAC only defined for vector arguments');
        else
            varargout{1} = yalmip('define',mfilename,varargin{1});
        end

    case 'char'

        operator = CreateBasicOperator('callback');
        operator.range = [-inf inf];
        operator.domain = [-inf inf];
        operator.bounds = @bounds;
        operator.derivative = @(x) ([1./(x(1)+x(2)+sqrt(eps));1./(x(1)+x(2)+sqrt(eps))-(x(2)+sqrt(eps)).^-1]);

        varargout{1} = [];
        varargout{2} = operator;
        varargout{3} = varargin{3};

    otherwise
        error(['SDPVAR/' upper(mfilename) ' called with weird argument']);
end

function [L,U] = bounds(xL,xU)

% Derive bounds on x1/x2
z = [xL(1)./xL(2) xU(1)./xL(2) xL(1)./xU(2) xU(1)./xU(2)];
fL = min(z);
fU = max(z);

if fL <= -1
    L = -inf;
else
    L = log(1 + fL);
end
if fU <= -1
    U = -inf;
else
    U = log(1 + fU);
end


function [kf] = ext_kalman_filter(F, G, H, u, Q, R, time, last_measure, kf, P)

%EXT_KALMAN_FILTER - This is the extended Kalman filter.
%
% Synthax:
%
% Inputs:
%   uu          - state variables, delta, wind
%   kf.x_prev   - previous a posteriori state estimate
%   kf.P_prev   - previous a posteriori estimate covariance
%   last_measure- (time, sensor measure)
%   P           - parameters
%
% Outputs:
%   kf.x        - a priori/a posteriori state estimate
%   kf.P        - a priori/a posteriori estimate covariance
%   kf.z_res    - post-fit residual

% A -> F
% B -> G
% C -> H

% Q - covariance of the zero-mean Gaussian noise on x_dot
% R - covariance of the zero-mean Gaussian noise on y

time_m = last_measure(:,1);
z_m    = last_measure(:,2);

% prediction (a priori)
kf.x = F * kf.x_prev    + G * u;    % a priori state estimate
kf.P = F * kf.P_prev * (F') + Q;    % a priori estimate covariance

% update (a posteriori)
if time - time_m < P.dt
    kf.z_tilde = z_m - H*kf.x;      % innovation (a priori residual)
    S = R + H*kf.P*(H');            % innovation covariance
    K = kf.P*(H')/S;                 % optimal kalman gain
    kf.x = kf.x + K*kf.z_tilde;     % a posteriori state estimate
    kf.P = kf.P - K*S*(K');         % a posteriori estimate covariance
end

kf.z_res = z_m - H*kf.x;            % a posteriori residual

end


function ellipsoid_monte_carlo_test01 ( )

%*****************************************************************************80
%
%% ELLIPSOID_MONTE_CARLO_TEST01 uses ELLIPSOID_SAMPLE on a 2D ellipse centered at (0,0).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 August 2014
%
%  Author:
%
%    John Burkardt
%
  m = 2;

  a = [ ...
    9.0, 1.0;
    1.0, 4.0 ]';
  e_test = [ ...
    0, 0;
    1, 0;
    0, 1;
    2, 0;
    1, 1;
    0, 2;
    3, 0 ]';
  r = 2.0;
  v = [ 0.0, 0.0 ]';

  fprintf ( 1, '\n' );
  fprintf ( 1, 'ELLIPSOID_MONTE_CARLO_TEST01\n' );
  fprintf ( 1, '  Use ELLIPSOID_SAMPLE to estimate integrals\n' );
  fprintf ( 1, '  in a 2D ellipse x'' * A * x <= r^2.\n' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Ellipsoid radius R = %g\n', r );
  r8vec_print ( m, v, '  Ellipsoid center V:' );
  r8mat_print ( m, m, a, '  Ellipsoid matrix A:' );

  volume = ellipsoid_volume ( m, a, v, r );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Ellipsoid volume = %g\n', volume );

  seed = 123456789;

  fprintf ( 1, '\n' );
  fprintf ( 1, '         N        1              X               Y  ' );
  fprintf ( 1, '             X^2               XY             Y^2             X^3\n' );
  fprintf ( 1, '\n' );

  n = 1;

  while ( n <= 65536 )

    [ x, seed ] = ellipsoid_sample ( m, n, a, v, r, seed );

    fprintf ( 1, '  %8d', n );

    for j = 1 : 7

      e(1:m) = e_test(1:m,j);

      value = monomial_value ( m, n, e, x );

      result = volume * sum ( value(1:n) ) / n;

      fprintf ( 1, '  %14.6g', result );

    end

    fprintf ( 1, '\n' );

    n = 2 * n;

  end

  return
end


%% Author: epokh
%% Website: www.epokh.org/drupy
%% This software is under GPL

function [Mdh]=DHmatrix(theta,d,a,alpha)
%%output: DH matrix
%%input: DH parameters:
%%theta: rotation along x(n) axis
%%d: traslation along z(n) axis
%%a: translation along x(n+1) axis
%%alpha: rotation along x(n+1) axis
%%all angles expressed in degrees

Mdh=[cosd(theta) -sind(theta)*cosd(alpha) sind(theta)*sind(alpha) a*cosd(theta);
     sind(theta) cosd(theta)*cosd(alpha)  -cosd(theta)*sind(alpha) a*sind(theta);
     0,sind(alpha),cosd(alpha),d;
     0,0,0,1];

end


function disk_monte_carlo_test01 ( )

%*****************************************************************************80
%
%% DISK_MONTE_CARLO_TEST01 uses DISK01_SAMPLE with an increasing number of points.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 January 2014
%
%  Author:
%
%    John Burkardt
%
  e_test = [ ...
    0, 0; ...
    2, 0; ...
    0, 2; ...
    4, 0; ...
    2, 2; ...
    0, 4; ...
    6, 0 ]';

  fprintf ( 1, '\n' );
  fprintf ( 1, 'DISK_MONTE_CARLO_TEST01\n' );
  fprintf ( 1, '  Use DISK01_SAMPLE to estimate integrals in the unit disk.\n' );

  seed = 123456789;

  fprintf ( 1, '\n' );
  fprintf ( 1, '         N        1              X^2             Y^2        ' );
  fprintf ( 1, '     X^4             X^2Y^2           Y^4             X^6\n' );
  fprintf ( 1, '\n' );

  n = 1;

  while ( n <= 65536 )

    [ x, seed ] = disk01_sample ( n, seed );

    fprintf ( 1, '  %8d', n );
    for j = 1 : 7

      e(1:2) = e_test(1:2,j);

      value = monomial_value ( 2, n, e, x );

      result(j) = disk01_area ( ) * sum ( value(1:n) ) / n;
      fprintf ( 1, '  %14.6g', result(j) );
    end
    fprintf ( 1, '\n' );

    n = 2 * n;

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '     Exact' );
  for j = 1 : 7

    e(1:2) = e_test(1:2,j);

    result(j) = disk01_monomial_integral ( e );
    fprintf ( 1, '  %14.6g', result(j) );
  end

  fprintf ( 1, '\n' );

  return
end


function res = Fspikernel(seqS,seqT,N,lam,mu,p)

%spikernel kernel function
%
%   This code implements the spikernel function, defined in:
%   Spikernels: Embedding Spiking Neurons in Inner-Product Spaces
%   Lavi Shpigelman, Yoram Singer, Rony Paz and Eilon Vaadia
%   Advances in Neural Information Processing Systems (NIPS) 15
%   MIT Press, Cambridge, MA, 2003.
%
%   res= Fspikernel (seqS,seqT,N,lam,mu,p)
%
%   returns res = K(seqS,seqT) = sum_{i=1}^N p^i K_i(seqS,seqT) where the kernel parameters are:
%
%   Inputs
%       seqS and seqT are the two sequences to be compared.
%           Each row of these sequences is one 'letter' in the sequence (must be of same length)
%           Their column lengths are the sequence lengths (can be of different length).
%       N = max subsequence lengths (the kernel compares subsequences of lengths 1 to N and returns a sum of kernels)
%       lam=\lambda parameter from article
%       mu=\mu parameter from article
%       p= the q prarameter in the article - the parameter that is used in the sum of kernels to weigh them differently
%
%    This implementation assumes that the function d(x,y) is the squared \ell_2 norm
%

% ------------- parameter check -------------------------
lens=size(seqS,1);
lent=size(seqT,1);
q=size(seqS,2);

if (q~=size(seqT,2))
    error('the dimesions of the sequence letters in seqS and seqT must be the same')
end
if (length(N)~=1)
    error('N should be a scalar value');
end
if (length(lam)~=1)
    error('lam should be a scalar value');
end
if (length(mu)~=1)
    error('mu should be a scalar value');
end
if (length(p)~=1)
    error('p should be a scalar value');
end
% --------------------------------------------------------

dmu = mu.^(0.5*( repmat( sum( seqS.^2, 2), 1, lent) + ...
              ( repmat( sum(seqT.^2, 2), 1, lens))' - 2*seqS*seqT'))';

res = fspike( N, p, lam, dmu);


function [yi, ypi] = hermint(x, y, yp, xi, c)

% HERMINT 1-D piecewise hermite interpolation
%    HERMINT(X,Y,YP,XI,C) interpolates to find YI, the values of
%    the underlying function Y at the points in the array XI,
%    using piecewise hermite interpolation.  X and Y must be
%    vectors of length N.
%
%    C specifies the number of data points to use in the
%    interpolation.  The default is to use all points.
%
%    [YI,YPI] = HERMINT() also returns the interpolated derivative
%    of the underlying function Y at points XI.

% Joe Henning - Fall 2011

if (nargin < 5)
   c = 0;
end

n = length(x);

if (n < c)
   fprintf('??? Bad c input to hermint ==> c <= length(x)\n');
   yi = [];
   ypi = [];
   return
end

for i = 1:length(xi)
   % Find the right place in the table by means of a bisection.
   klo = 1;
   khi = n;
   while (khi-klo > 1)
      k = fix((khi+klo)/2.0);
      if (x(k) > xi(i))
         khi = k;
      else
         klo = k;
      end
   end

   h = x(khi) - x(klo);
   if (h == 0.0)
      fprintf('??? Bad x input to hermint ==> x values must be distinct\n');
      yi(i) = NaN;
      ypi(i) = NaN;
      continue;
   end

   isiny = 0;
   for k = 1:n
      if (xi(i) == x(k))
         yi(i) = y(k);
         ypi(i) = yp(k);
         isiny = 1;
         break
      end
   end

   if (isiny)
      continue
   end

   % Evaluate hermite polynomial
   yi(i) = 0;
   ypi(i) = 0;
   if (c == 0)
      for k = 1:n
         f = 0;
         g = 0;
         h = 1;

         for m = 1:n
            if (k ~= m)
               f = f + 1/(x(k)-x(m));
               h = h*(xi(i)-x(m))/(x(k)-x(m));
            end
         end

         for m = 1:n
            if (k ~= m)
               g = g + h/(xi(i)-x(m));
            end
         end

         a = h*h;
         b = 2*h*g;
         u = xi(i) - x(k);

         b1 = a*u;
         b2 = b*u + a;
         a1 = a - 2*f*b1;
         a2 = b - 2*f*b2;

         yi(i) = yi(i) + a1*y(k) + b1*yp(k);
         ypi(i) = ypi(i) + a2*y(k) + b2*yp(k);
      end
   else
      if (mod(c,2) == 0)   % even
         c2 = c/2;
         if (klo < c2)
            klo = c2;
         end
         if (klo > n-c2)
            klo = n-c2;
         end
         khi = klo + 1;
         for k = klo-(c2-1):klo+c2
            f = 0;
            g = 0;
            h = 1;

            for m = klo-(c2-1):klo+c2
               if (k ~= m)
                  f = f + 1/(x(k)-x(m));
                  h = h*(xi(i)-x(m))/(x(k)-x(m));
               end
            end

            for m = klo-(c2-1):klo+c2
               if (k ~= m)
                  g = g + h/(xi(i)-x(m));
               end
            end

            a = h*h;
            b = 2*h*g;
            u = xi(i) - x(k);

            b1 = a*u;
            b2 = b*u + a;
            a1 = a - 2*f*b1;
            a2 = b - 2*f*b2;

            yi(i) = yi(i) + a1*y(k) + b1*yp(k);
            ypi(i) = ypi(i) + a2*y(k) + b2*yp(k);
         end
      else   % odd
         c2 = floor(c/2);
         if (klo < c2+1)
            klo = c2+1;
         end
         if (klo > n-c2)
            klo = n-c2;
         end
         khi = klo + 1;
         for k = klo-c2:klo+c2
            f = 0;
            g = 0;
            h = 1;

            for m = klo-c2:klo+c2
               if (k ~= m)
                  f = f + 1/(x(k)-x(m));
                  h = h*(xi(i)-x(m))/(x(k)-x(m));
               end
            end

            for m = klo-c2:klo+c2
               if (k ~= m)
                  g = g + h/(xi(i)-x(m));
               end
            end

            a = h*h;
            b = 2*h*g;
            u = xi(i) - x(k);

            b1 = a*u;
            b2 = b*u + a;
            a1 = a - 2*f*b1;
            a2 = b - 2*f*b2;

            yi(i) = yi(i) + a1*y(k) + b1*yp(k);
            ypi(i) = ypi(i) + a2*y(k) + b2*yp(k);
         end
      end
   end
end


function [evects,evals] = pca(X)
% [evects,evals] = pca(X)
%
% finds principal components of
%
% input:
%  X  dxn matrix (each column is a dx1 input vector)
%
% output:
% evects  columns are principal components (leading from left->right)
% evals   corresponding eigenvalues
%
% See also applypca
%
% copyright by Kilian Q. Weinberger, 2006

[d,N]  = size(X);

mm = mean(X,2);
X = X - mm*ones(1,N); % remove mean from data

cc = cov(X',1); % compute covariance
[cvv,cdd] = eig(cc); % compute eignvectors
[zz,ii] = sort(diag(-cdd)); % sort according to eigenvalues
evects = cvv(:,ii); % pick leading eigenvectors
cdd = diag(cdd); % extract eigenvalues
evals = cdd(ii); % sort eigenvalues


function b = r8blt_vxm ( n, ml, a, x )

%*****************************************************************************80
%
%% R8BLT_VXM multiplies a vector by a R8BLT matrix.
%
%  Discussion:
%
%    The R8BLT storage format is appropriate for a banded lower triangular matrix.
%    The matrix is assumed to be zero below the ML-th subdiagonal.
%    The matrix is stored in an ML+1 by N array, in which the diagonal
%    appears in the first row, followed by successive subdiagonals.
%    Columns are preserved.
%
%  Example:
%
%    N = 5, ML = 2
%
%    A11   0   0   0   0
%    A21 A22   0   0   0
%    A31 A32 A33   0   0
%      0 A42 A43 A44   0
%      0   0 A53 A54 A55
%                --- ---
%                    ---
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 March 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%
%    Input, integer ML, the lower bandwidth.
%
%    Input, real A(ML+1,N), the R8BLT matrix.
%
%    Input, real X(N), the vector to be multiplied by A.
%
%    Output, real B(N), the product X*A.
%
  b(1:n) = 0.0;

  for i = 1 : n
    jlo = max ( 1, i - ml );
    jhi = i;
    for j = jlo : jhi
      b(j) = b(j) + x(i) * a(i-j+1,j);
    end
  end

  return
end


function [ o, x, w ] = en_r2_03_2 ( n )

%*****************************************************************************80
%
%% EN_R2_03_2 implements the Stroud rule 3.2 for region EN_R2.
%
%  Discussion:
%
%    The rule has order O = 2^N.
%
%    The rule has precision P = 3.
%
%    EN_R2 is the entire N-dimensional space with weight function
%
%      w(x) = exp ( - x1^2 - x2^2 ... - xn^2 )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 January 2010
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Arthur Stroud,
%    Approximate Calculation of Multiple Integrals,
%    Prentice Hall, 1971,
%    ISBN: 0130438936,
%    LC: QA311.S85.
%
%  Parameters:
%
%    Input, integer N, the spatial dimension.
%
%    Output, integer O, the order.
%
%    Output, real X(N,O), the abscissas.
%
%    Output, real W(O), the weights.
%
  o = 2^n;
  volume = sqrt ( pi^n );

  a = volume / o;
  r = sqrt ( 1 / 2 );

  x = zeros ( n, o );
  w = zeros ( o, 1 );

  k = 0;
%
%  2^N points.
%
  k = k + 1;
  x(1:n,k) = - r;
  w(k) = a;
  more = 1;

  while ( more )
    more = 0;
    for i = n : -1 : 1
      if ( x(i,k) < 0.0 )
        k = k + 1;
        x(1:n,k) = x(1:n,k-1);
        x(i,k)     = + r;
        x(i+1:n,k) = - r;
        w(k) = a;
        more = 1;
        break;
      end
    end
  end

  return
end


function [sMin,sMax,s0]=projEllipseOntoLine(z,A,gammaVal,x0,v)
%%PROJELLIPSEONTOLINE Determine the orthogonal projection of an ellipse or
%                     ellipsoid onto a line.
%
%INPUTS: z The numDimX1 center of the ellipsoid.
%        A A numDimXnumDim symmetric, positive definite matrix that
%          specifies the size and shape of the ellipse or ellipsoid, where
%          a point zp is on the ellipse/ellipsoid if
%          (zp-z)'*A*(zp-z)=gammaVal.
% gammaVal The threshold for declaring a point to be in the ellipsoid. If
%          an empty matrix is passed, the default value of 1 is used.
%     x0,v Two numDimX1 vectors that specify the line. A point y is on the
%          line if y=x0+s*v, where s is a scalar quantity.
%
%OUTPUTS: sMin,sMax The range of values of s on the line y=x0+s*v onto
%                   which the ellipsoid is orthogonally projected. These
%                   are scalar quantities.
%
%The mathematics for projecting an ellipsoid onto a line are described in
%Section 12 of [1].
%
%EXAMPLE:
% A=[1,0;
%    0,10];
% M=[0.413074198133900,  0.910697373904216;
%    -0.910697373904216,   0.413074198133900];%A rotation matrix
% A=M*A*M';
% z=[5;6];
% x0=[0;0];
% v=[1;2];
% [sMin,sMax,s0]=projEllipseOntoLine(z,A,[],x0,v);
% figure(1)
% clf
% hold on
% axis([-0.5, 7.5, 2, 10])%Same size in x and y
% axis square
% drawEllipse(z,A,1,'b','linewidth',2)
% %Draw a segment of the line.
% xStart=x0+v;
% xEnd=x0+5*v;
% plot([xStart(1);xEnd(1)],[xStart(2);xEnd(2)],'-k','linewidth',4)
% %Draw the segment of the orthogonal projection onto the line.
% xStart=x0+sMin*v;
% xEnd=x0+sMax*v;
% plot([xStart(1);xEnd(1)],[xStart(2);xEnd(2)],'-r','linewidth',2)
% %Draw the line from the center of the ellipse to the place on the line onto
% %which it is orthogonally projected.
% xS0=x0+s0*v;
% plot([z(1);xS0(1)],[z(2);xS0(2)],'--c')
%
%REFERENCES:
%[1] S. B. Pope, "Algorithms for ellipsoids," Cornell University, Tech.
%    Rep. FDA-08-01, Feb. 2008. [Online].
%    Available: https://tcg.mae.cornell.edu/pubs/Pope_FDA_08.pdf
%
%May 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(isempty(gammaVal))
   gammaVal=1;
end

A=A/gammaVal;

L=chol(A,'lower');

s0=v'*(z-x0)/(v'*v);
w=norm(L\v/(v'*v));

sMin=s0+w;
sMax=s0-w;
end

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


function [MBox] = MBoxtest(X,alpha)
% Multivariate Statistical Testing for the Homogeneity of Covariance Matrices by the Box's M.
%
%   Syntax: function [MBox] = MBoxtest(X,alpha)
%
%     Inputs:
%          X - data matrix (Size of matrix must be n-by-(1+p); sample=column 1, variables=column 2:p).
%      alpha - significance level (default = 0.05).
%     Output:
%          MBox - the Box's M statistic.
%          Chi-sqr. or F - the approximation statistic test.
%          df's - degrees' of freedom of the approximation statistic test.
%          P - observed significance level.
%
%    If the groups sample-size is at least 20 (sufficiently large), Box's M test
%    takes a Chi-square approximation; otherwise it takes an F approximation.
%
%    Example: For a two groups (g = 2) with three independent variables (p = 3), we
%             are interested to test the homogeneity of covariances matrices with a
%             significance level = 0.05. The two groups have the same sample-size
%             n1 = n2 = 5.
%                                       Group
%                      ---------------------------------------
%                            1                        2
%                      ---------------------------------------
%                       x1   x2   x3             x1   x2   x3
%                      ---------------------------------------
%                       23   45   15             277  230   63
%                       40   85   18             153   80   29
%                      215  307   60             306  440  105
%                      110  110   50             252  350  175
%                       65  105   24             143  205   42
%                      ---------------------------------------
%
%             Total data matrix must be:
%          X=[1 23 45 15;1 40 85 18;1 215 307 60;1 110 110 50;1 65 105 24;
%          2 277 230 63;2 153 80 29;2 306 440 105;2 252 350 175;2 143 205 42];
%
%             Calling on Matlab the function:
%                MBoxtest(X,0.05)
%
%             Answer is:
%
%  ------------------------------------------------------------
%       MBox         F           df1          df2          P
%  ------------------------------------------------------------
%     27.1622     2.6293          6           463       0.0162
%  ------------------------------------------------------------
%  Covariance matrices are significantly different.
%

%  Created by A. Trujillo-Ortiz and R. Hernandez-Walls
%             Facultad de Ciencias Marinas
%             Universidad Autonoma de Baja California
%             Apdo. Postal 453
%             Ensenada, Baja California
%             Mexico.
%             atrujo@uabc.mx
%             And the special collaboration of the post-graduate students of the 2002:2
%             Multivariate Statistics Course: Karel Castro-Morales, Alejandro Espinoza-Tenorio,
%             Andrea Guia-Ramirez, Raquel Muniz-Salazar, Jose Luis Sanchez-Osorio and
%             Roberto Carmona-Pina.
%  November 2002.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A., R. Hernandez-Walls, K. Castro-Morales, A. Espinoza-Tenorio, A. Guia-Ramirez
%    and R. Carmona-Pina. (2002). MBoxtest: Multivariate Statistical Testing for the Homogeneity of
%    Covariance Matrices by the Box's M. A MATLAB file. [WWW document]. URL http://www.mathworks.com/
%    matlabcentral/fileexchange/loadFile.do?objectId=2733&objectType=FILE
%
%  References:
%
%  Stevens, J. (1992), Applied Multivariate Statistics for Social Sciences. 2nd. ed.
%              New-Jersey:Lawrance Erlbaum Associates Publishers. pp. 260-269.

if nargin < 1,
    error('Requires at least one input arguments.');
end;

if nargin < 2,
    alpha = 0.05; %(default)
end;

if (alpha <= 0 | alpha >= 1)
   fprintf('Warning: significance level must be between 0 and 1\n');
   return;
end;

g = max(X(:,1)); %Number of groups.

n = []; %Vector of groups-size.
indice = X(:,1);
for i = 1:g
   Xe = find(indice==i);
   eval(['X' num2str(i) '= X(Xe,2);']);
   eval(['n' num2str(i) '= length(X' num2str(i) ') ;'])
   eval(['xn= n' num2str(i) ';'])
   n = [n,xn];
end;

[f,c] = size(X);
X = X(:,2:c);

[N,p]=size(X);
r=1;
r1=n(1);
bandera=2;
for k=1:g
   if n(k)>=20;
      bandera=1;
   end
end
%Partition of the group covariance matrices.
for k=1:g
   eval(['S' num2str(k) '=cov(X(r:r1,:));';]);
   if k<g
    r=r+n(k);
    r1=r1+n(k+1);
   end
end

deno=sum(n)-g;
suma=zeros(size(S1));

for k=1:g
   eval(['suma =suma + (n(k)-1)*S' num2str(k) ';']);
end

Sp=suma/deno;  %Pooled covariance matrix.
Falta=0;

for k=1:g
   eval(['Falta =Falta + ((n(k)-1)*log(det(S' num2str(k) ')));']);
end

MB=(sum(n)-g)*log(det(Sp))-Falta;  %Box's M statistic.
suma1=sum(1./(n(1:g)-1));
suma2=sum(1./((n(1:g)-1).^2));
C=(((2*p^2)+(3*p)-1)/(6*(p+1)*(g-1)))*(suma1-(1/deno));  %Computing of correction factor.
if bandera==1
X2=MB*(1-C);  %Chi-square approximation.
v=(p*(p+1)*(g-1))/2;  %Degrees of freedom.
P=1-chi2cdf(X2,v);  %Significance value associated to the observed Chi-square statistic.
disp(' ')
;
fprintf('------------------------------------------------\n');
disp('     MBox     Chi-sqr.         df          P')
fprintf('------------------------------------------------\n');
fprintf('%10.4f%11.4f%12.i%13.4f\n',MB,X2,v,P);
fprintf('------------------------------------------------\n');
   if P >= alpha
      disp('Covariance matrices are not significantly different.');
   else
      disp('Covariance matrices are significantly different.');
   end
else
%To obtain the F approximation we first define Co, which combined to the before C value
%are used to estimate the denominator degrees of freedom (v2); resulting two possible cases.
Co=(((p-1)*(p+2))/(6*(g-1)))*(suma2-(1/(deno^2)));
  if Co-(C^2)>= 0;
	v1=(p*(p+1)*(g-1))/2;  %Numerator degrees of freedom.
   v21=fix((v1+2)/(Co-(C^2)));  %Denominator degrees of freedom.
   F1=MB*((1-C-(v1/v21))/v1);  %F approximation.
   P1=1-fcdf(F1,v1,v21);  %Significance value associated to the observed F statistic.
disp(' ')
  ;
fprintf('------------------------------------------------------------\n');
disp('     MBox         F           df1          df2          P')
fprintf('------------------------------------------------------------\n');
fprintf('%10.4f%11.4f%11.i%14.i%13.4f\n',MB,F1,v1,v21,P1);
fprintf('------------------------------------------------------------\n');
   if P1 >= alpha
      disp('Covariance matrices are not significantly different.');
   else
      disp('Covariance matrices are significantly different.');
   end

 else
   v1=(p*(p+1)*(g-1))/2;  %Numerator degrees of freedom.
   v22=fix((v1+2)/((C^2)-Co));  %Denominator degrees of freedom.
   b=v22/(1-C-(2/v22));
   F2=(v22*MB)/(v1*(b-MB));  %F approximation.
   P2=1-fcdf(F2,v1,v22);  %Significance value associated to the observed F statistic.
disp(' ')
  ;
fprintf('------------------------------------------------------------\n');
disp('     MBox         F           df1          df2          P')
fprintf('------------------------------------------------------------\n');
fprintf('%10.4f%11.4f%11.i%14.i%13.4f\n',MB,F2,v1,v22,P2);
fprintf('------------------------------------------------------------\n');

   if P2 >= alpha
      disp('Covariance matrices are not significantly different.');
   else
      disp('Covariance matrices are significantly different.');
   end
 end
end


%................................................................

% MATLAB codes for Finite Element Analysis
% problem16vibrations.m
% Timoshenko beam in free vibrations
% AJM Ferreira 2008
% modified by Manuel Diaz, 2013.07.25

% clear memory
clear all

% E; modulus of elasticity
% G; shear modulus
% I: second moments of area
% L: length of beam
% thickness: thickness of beam
<<<<<<< HEAD
E=30e6; poisson = 0.30;L  = 1;thickness=0.01;
=======
E=10e7; poisson = 0.30;L = 3;thickness=0.001;
>>>>>>> 47f4967135b009f9128c106a6744c97a2ffd18ce
I=thickness^3/12;
EI=E*I;
kapa=5/6;
rho=1;
A=1*thickness;
%

P = 0; % uniform pressure
%P = -1; % uniform pressure
% constitutive matrix
G=E/2/(1+poisson);
C=[   EI   0; 0    kapa*A*G];

% mesh
<<<<<<< HEAD
numberElements     = 50;
=======
numberElements  = 40;
>>>>>>> 47f4967135b009f9128c106a6744c97a2ffd18ce
nodeCoordinates=linspace(0,L,numberElements+1);
xx=nodeCoordinates';x=xx';
for i=1:size(nodeCoordinates,2)-1
    elementNodes(i,1)=i;
    elementNodes(i,2)=i+1
end
% generation of coordinates and connectivities
numberNodes=size(xx,1);

% GDof: global number of degrees of freedom
GDof=2*numberNodes;

% computation of the system stiffness, force, mass
[stiffness,force,mass]=...
    formStiffnessMassTimoshenkoBeam(GDof,numberElements,...
    elementNodes,numberNodes,xx,C,P,rho,I,thickness);

% boundary conditions (simply-supported at both bords)
%fixedNodeW =[1 ; numberNodes];
%fixedNodeTX=[];
% boundary conditions (clamped at both bords)
%fixedNodeW =[1 ; numberNodes];
%fixedNodeTX=[1 ; numberNodes];
% boundary conditions (cantilever)
%fixedNodeW =[1];
%fixedNodeTX=[1];
<<<<<<< HEAD
% Free-free
%fixedNodeW =[];
%fixedNodeTX=[];
=======
% Free-Free conditions
fixedNodeW =[];
fixedNodeTX=[];
>>>>>>> 47f4967135b009f9128c106a6744c97a2ffd18ce
prescribedDof=[fixedNodeW; fixedNodeTX+numberNodes];

% solution (optional*)
displacements=solution(GDof,prescribedDof,stiffness,force);

% output displacements/reactions (optional*)
outputDisplacementsReactions(displacements,stiffness,...
    GDof,prescribedDof)

% free vibration problem
activeDof=setdiff([1:GDof]',[prescribedDof]);
modeNumber=4;

[V,D]=eig(stiffness(activeDof,activeDof),...
    mass(activeDof,activeDof));
D = diag(sqrt(D)*L*L*sqrt(rho*A/E/I));
[D,ii] = sort(D);

V1=zeros(GDof,1);
V1(activeDof,1:modeNumber)=V(:,1:modeNumber);
!
% drawing eigenmodes
drawEigenmodes1D(modeNumber,numberNodes,V1,xx,x)


%  Figure 4.16      Feedback Control of Dynamic Systems, 6e
%                   Franklin, Powell, Emami
%% script to generate Figure 4.16(a), 4.16 (b)
% case I, P control by reaction curve data.
% First, input the plant model and compute the reaction curve.
sysp=tf(1,[600 70 1])
set(sysp,'td', 5)

% From the curve, kp = 6.92 for proportional control
sysdP = tf(6.92,1)
% To get the closed loop response, we must use a pade approximation to the
% delay
syspade=pade(sysp,3);
sysforward=syspade*sysdP;
sysback=tf(1,1)
syscl=feedback(sysforward,sysback)
figure(1)
clf
step(syscl)
title('Figure 4.16a Closed loop response using Reaction Curve data')
gtext('P')
hold on
%case II, PI control
%kp = 6.22, TI=43.3
sysdPI=6.22*tf([43.3 1],[43.3 0])
sysforward=syspade*sysdPI;
syscl=feedback(sysforward,sysback)
step(syscl)
gtext('PI')
nicegrid
%
sysdP = tf(6.92/3,1)
% To get the closed loop response, we must use a pade approximation to the
% delay
syspade=pade(sysp,3);
sysforward=syspade*sysdP;
sysback=tf(1,1)
syscl=feedback(sysforward,sysback)
figure(2)
clf
step(syscl)
title('Figure 4.16b Closed loop response using Reaction Curve data')
gtext('P')
hold on
%case II, PI control
%kp = 6.22, TI=43.3
sysdPI=(6.22/3)*tf([43.3 1],[43.3 0])
sysforward=syspade*sysdPI;
syscl=feedback(sysforward,sysback)
step(syscl)
gtext('PI')
nicegrid;


function [logml log10ml ml]=mmlikdata(X,y,n,T,q,sigma,beta0,omega0,betabar)

% function [logml log10ml ml]=bear.mmlik(X,y,n,T,q,sigma,omega0,beta0,betabar)
% computes the marginal likelihood for a Minesota prior, by implementing algorithm XXX
% inputs:  - matrix 'X': matrix of regressors for the VAR model (defined in 1.1.8)
%          - vector 'y': vectorised regressands for the VAR model (defined in 1.1.12)
%          - integer 'n': number of endogenous variables in the BVAR model (defined p 7 of technical guide)
%          - integer 'T': number of sample time periods (defined p 7 of technical guide)
%          - integer 'q': total number of coefficients to estimate for the BVAR model (defined p 7 of technical guide)
%          - matrix 'sigma': 'true' variance-covariance matrix of VAR residuals, for the original Minnesota prior
%          - vector 'beta0': vector of prior values for beta (defined in 1.3.4)
%          - matrix 'omega0': prior covariance matrix for the VAR coefficients (defined in 1.3.8)
%          - vector 'betabar': posterior mean vector (defined in 1.3.18)
% outputs: - scalar 'logml': base e log of the marginal likelihood (defined in 1.2.9)
%          - scalar 'log10ml': base 10 log of the marginal likelihood (defined in 1.2.9)
%          - scalar 'ml': marginal likelihood (defined in 1.2.9)

% compute the constant part of the marginal likelihood
temp1=(-n*T/2)*log(2*pi)+(-T/2)*log(det(sigma));

% compute the log determinant part
% create the square root matrix of omega0
% because omega0 is diagonal, this is simply the square root of the diagonal terms of omega0
Fomega=spdiags(diag(omega0).^0.5,0,q,q);
% compute the inverse of sigma
C=chol(bear.nspd(sigma));
invC=C\speye(n);
invsigma=invC*invC';
% compute the product
product=Fomega'*kron(invsigma,X'*X)*Fomega;
% compute the eigenvalues of the product
if n == 1
    eigenvalues=eig(full(product));
else
    eigenvalues=eig(product);
end
% now compute the full determinant term
temp2=(-1/2)*log(prod(diag(eye(q)+diag(eigenvalues))));

% compute the final term
% first compute the inverse of omega0, which is a diagonal matrix (hence simply invert element wise the diagonal terms)
invomega0=spdiags(1./diag(omega0),0,q,q);
% compute the inverse of omegabar
invomegabar=invomega0+kron(invsigma,X'*X);
% now compute the whole matrix sum
summ=beta0'*invomega0*beta0-betabar'*invomegabar*betabar+y'*kron(invsigma,speye(T))*y;
% finally, compute the whole exponential term
temp3=-0.5*summ;

% compute the marginal likelihood
logml=real(temp1+temp2+temp3);
log10ml=logml/log(10);
ml=exp(logml);


function f = flops_solve_tri(n,m,k)
% FLOPS_SOLVE_TRI   Flops for triangular left division.
% FLOPS_SOLVE_TRI(T,b) returns the number of flops for solve_tri(T,b).
% FLOPS_SOLVE_TRI(n,m,k) returns the number of flops for
% solve_tril(tril(rand(n,m)),rand(n,k)).
%
% Example:  (n=2,m=2,k=1)
%  [g;h] = [a 0; b c]\[e; f]  has
%  g = e/a
%  h = (f - b*g)/c
%  which is 2 multiply+add and 2 divisions = 18 flops.

if nargin == 2
	T = n;
	b = m;
  f = flops_solve_tri(rows(T),cols(T),cols(b));
  return;
end
if n ~= m
	error('n ~= m case is not implemented');
end
% lower triangular case:
% number of multiplies+adds is
% sum(i=1..n) sum(k=1..i-1) 2 = sum(i=1..n) 2*(i-1) = n^2-n
% number of divides is n
f = (n*n + n*(flops_div-1))*k;


function [ Ix, Iy, Iz, It ] = imageDerivatives3D( image1, image2 )
%This fucntion computes 3D derivatives between two 3D images.
%
%   Description :
%
%   There are four derivatives here; three along X, Y, Z axes and one along
%   timeline axis.
%
%   -image1, image2 :   two subsequent images or frames
%   -dx, dy, dz : vectors along X, Y and Z axes respectively
%   -dt : vectors along timeline axis
%   -Ix, Iy, Iz : derivatives along X, Y and Z axes respectively
%   -It : derivatives along timeline axis
%
%   Author : Mohammad Mustafa
%   By courtesy of The University of Nottingham and
%   Mirada Medical Limited, Oxford, UK
%
%   Published under a Creative Commons Attribution-Non-Commercial-Share Alike
%   3.0 Unported Licence http://creativecommons.org/licenses/by-nc-sa/3.0/
%
%   June 2012

dx=zeros(2,2,2);
dx(:,:,1)=[-1 1; -1 1 ]; dx(:,:,2)=[-1 1; -1 1 ];
dx=0.25*dx;

dy=zeros(2,2,2);
dy(:,:,1)=[-1 -1; 1 1 ]; dy(:,:,2)=[-1 -1; 1 1 ];
dy=0.25*dy;

dz=zeros(2,2,2);
dz(:,:,1)=[-1 -1; -1 -1 ]; dz(:,:,2)=[1 1; 1 1 ];
dz=0.25*dz;

dt=ones(2,2,2);
dt=0.25*dt;

% Computing derivatives
Ix = 0.5 * (convn(image1,dx) + convn(image2,dx) );
Iy = 0.5 * (convn(image1,dy) + convn(image2,dy) );
Iz = 0.5 * (convn(image1,dz) + convn(image2,dz) );
It = 0.5 * (convn(image1,dt) - convn(image2,dt) );

% Adjusting sizes
Ix=Ix(1:size(Ix,1)-1, 1:size(Ix,2)-1, 1:size(Ix,3)-1);
Iy=Iy(1:size(Iy,1)-1, 1:size(Iy,2)-1, 1:size(Iy,3)-1);
Iz=Iz(1:size(Iz,1)-1, 1:size(Iz,2)-1, 1:size(Iz,3)-1);
It=It(1:size(It,1)-1, 1:size(It,2)-1, 1:size(It,3)-1);

end


function [] = mc_test_pdfs(MM,VV,desired_steps)
% Function tests and plots the Gaussian mixture in the desired simulation
% steps.
%
%% Syntax
% mc_test_pdfs(MM,VV,desired_steps)
%
%% Description
% Function tests and plots the Gaussian mixture described by the vector of Gaussian
% means and variances in the desired simulation steps.
%
% Input:
% * MM            ... the matrix of mean values of Gaussian components, the matrix of dimensions
%                       ksteps x Nsamples
% * VV            ... the matrix of associated variances of Gaussian components
% * desired_steps ... the vector with indices of steps, where we want to test
%                       the distributions
%
% See Also:
% demo_example_gp_simulation
%
% Examples:
% demo_example_gp_simulation
%

%%
% * Written by K. Azman, 2007.
%

% variables
MM = MM';
VV = VV';

Nsamples = size(MM,1);
mu = mean(MM);
s2 = mean(VV) + mean((MM-repmat(mu,Nsamples,1)).^2);
mu = mu';
s2 = s2';

% "calculation"
STD = sqrt(VV);
Xwide = 200; % resolution = no. of samples between xmin and xmax
Xmin = min(MM-4*STD);
Xmax = max(MM+4*STD);
dX = (Xmax-Xmin)/(Xwide-1);

% XX,YY ... matrices for storing the GP model's pdfs
% XX(:,k) ... yvalues at time step k
% YY(:,k) ... density values at time step k and coresponding y values in XX(:,k)
% sum(YY(:,k)) = 1 for all k-s
XX = zeros(Xwide,size(VV,2));
YY = zeros(size(XX));

% for desired steps
for jj = 1:length(desired_steps)
    kk = desired_steps(jj);
    XX(:,kk) = Xmin(kk):dX(kk):Xmax(kk);
    % ... and for every realisation
    ytemp = zeros(size(YY,1),1);

    for ii=1:Nsamples
        ytemp = normpdf(XX(:,kk), MM(ii,kk), STD(ii,kk));
        YY(:,kk) = YY(:,kk) + ytemp;
    end
    YY(:,kk) = YY(:,kk)/Nsamples;

    yapprox = normpdf(XX(:,kk),mu(kk),sqrt(s2(kk)));

    % plot pdfs
    figure(10000+kk);

    plot(XX(:,kk),YY(:,kk),'Color',[0.6 0.6 0.6],'LineWidth',4);
    hold on;
    plot(XX(:,kk),yapprox,'LineStyle','--','Color',[0 0 1],'LineWidth',4);
     for ii=1:Nsamples
        ytemp = normpdf(XX(:,kk), MM(ii,kk), STD(ii,kk));
        plot(XX(:,kk),ytemp,'Color',[0.9 0.9 0.9]);
    end
    plot(XX(:,kk),YY(:,kk),'Color',[0.6  0.6 0.6],'LineWidth',4);
    plot(XX(:,kk),yapprox,'LineStyle','--','Color',[0 0 1],'LineWidth',4);
    hold off;
    grid;
    legend('true','GP approx','samples')

    title(strcat(['test MC pdfs, simul step=', num2str(kk)]));

    disp('press sth to continue');
    pause;

end


function gray = rgb_to_gray ( rgb, equal )

%*****************************************************************************80
%
%% RGB_TO_GRAY returns a grayscale version of an RGB image.
%
%  Discussion:
%
%    An RGB image is an (M,N,3) array.
%
%    A grayscale image is an (M,N) array.
%
%    A grayscale version of an RGB image can be made by averaging
%    the RGB components of each pixel.
%
%    A more sophisticated approach uses the luminance function:
%
%      GRAY = 0.2126 * R + 0.7152 * G + 0.0722 * B
%
%    which attempts to better model the contributions to brightness
%    of the different colors.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 February 2011
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, uint8 RGB(M,N,3), the original data.
%
%    Input, logical EQUAL, is TRUE (1) if the R, G and B channels
%    are to be assigned equal weight.  EQUAL is FALSE by default.
%
%    Output, uint8 GRAY(M,N), the grayscale version of the data.
%
  if ( nargin < 1 )
    error ( 'RGB_TO_GRAY - Fatal error! Missing RGB input argument.' )
  end

  if ( nargin < 2 )
    equal = 0;
  end

  if ( equal )
    v = [ 1, 1, 1 ]' / 3;
  else
    v = [ 0.2126, 0.7152, 0.0722 ]';
  end
%
%  This is really a matrix multiply, but the obvious equation
%    gray = rgb * v
%  is illegal according to MATLAB.
%
  gray = uint8 ( double ( rgb(:,:,1) ) * v(1) ...
               + double ( rgb(:,:,2) ) * v(2) ...
               + double ( rgb(:,:,3) ) * v(3) );

  return
end


function yp = p15_fun ( neqn, t, y )

%*****************************************************************************80
%
%% P15_FUN evaluates the function for problem P15.
%
%  Discussion:
%
%    30 equations.
%
%    This system models the motion of the five outer planets of the
%    solar system.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    18 February 2013
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Wayne Enright, John Pryce,
%    Algorithm 648,
%    ACM Transactions on Mathematical Software,
%    Volume 13, Number 1, pages 28-34.
%
%  Parameters:
%
%    Input, integer NEQN, the number of equations.
%
%    Input, real T, Y(NEQN), the arguments of the derivative
%    function.
%
%    Output, real YP(NEQN), the value of the derivative function.
%
  yp = zeros ( neqn, 1 );

  m = zeros ( 5, 1 );
  k2 = p15_param ( 'GET', 'K2', [] );
  m0 = p15_param ( 'GET', 'M0', [] );
  m(1) = p15_param ( 'GET', 'M1', [] );
  m(2) = p15_param ( 'GET', 'M2', [] );
  m(3) = p15_param ( 'GET', 'M3', [] );
  m(4) = p15_param ( 'GET', 'M4', [] );
  m(5) = p15_param ( 'GET', 'M5', [] );
  i = 0;
  for l = 3 : 3 : 15
    i = i + 1;
    p = y(l-2).^2 + y(l-1).^2 + y(l).^2;
    r(i) = 1.0 / ( p * sqrt ( p ) );
    j = 0;
    for ll = 3 : 3 : 15
      j = j + 1;
      if ( ll ~= l )
        p = ( y(l-2) - y(ll-2) ).^2 + ( y(l-1) - y(ll-1) ).^2 ...
          + ( y(l) - y(ll) ).^2;
        q(i,j) = 1.0 / ( p * sqrt ( p ) );
        q(j,i) = q(i,j);
      end
    end
  end
  i3 = 0;
  for i = 1 : 5
    i3 = i3 + 3;
    for ll = i3-2 : i3
      mm = ll - i3;
      yp(ll) = y(ll+15);
      p = 0.0;
      for j = 1 : 5
        mm = mm + 3;
        if ( j ~= i )
          p = p + m(j) ...
             * ( y(mm) * ( q(i,j) - r(j) ) - y(ll) * q(i,j) );
        end
      end
      yp(ll+15) = k2 * ( - ( m0 + m(i) ) * y(ll) * r(i) + p );
    end
  end

  return
end


function [x, w, v] = radaupts(n, alp, bet)
%RADAUPTS   Gauss-Jacobi-Radau quadrature nodes and weights.
%  RADAUPTS(N) returns N Legendre-Radau points X in [-1,1).
%
%  [X, W] = RADAUPTS(N) returns also a row vector W of weights for
%  Gauss-Legendre-Lobatto quadrature.
%
%  [X, W, V] = RADAUPTS(N) returns additionally a column vector V of weights in
%  the barycentric formula corresponding to the points X. The weights are scaled
%  so that max(abs(V)) = 1.
%
%  [...] = RADUAPTS(N, ALP, BET) is similar, but for the Gauss-Jacobi-Radau
%  nodes and weights. Here ALP and BET should be scalars > -1.
%
%  In each case, N should be a positive integer.
%
% See also CHEBPTS, LEGPTS, JACPTS, LEGPOLY, JACPOLY, LOBPTS.

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Developer note:
%   The approach used here is to observe that the Gauss-Radau points are
%   precisely the roots of (1+x)P^(0,1)_{n-2}(x), which can be obtained from
%   JACPTS. A similar identity [NIST, (18.9.5) and (18.9.17)] is used for the
%   computation of the quadrature weights from those of JACPTS, and the missing
%   barycentric weights are determined by enforcing the interpolation of f(x) =
%   x at x = 0.
%
%    x_j = roots of (1+x)P^(0,1)_{n-2}(x)
%    w_j = { 2/n^2                                 : x_j = -1
%          { 1/(1-x_j) * 1/[d/dx P_{n-1}(x_j)]^2   : otherwise
%
%   (Note that the weights for n-1 point Gauss-Jacobi with a = 0, b = 1 satisfy
%    u_j = C/(1-x_j^2)/[d/dx P^(0,1)_{n-1}(x_j)]^2)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% TODO: Scaled domains?

if ( nargin < 3 )
    % Default to Gauss-Legendre-Lobatto:
    alp = 0; bet = 0;
end

%% Trivial cases:
if ( n == 1 )
    x = -1;
    w = 2^(1+alp+bet)*beta(1+alp,1+bet);
    v = 1;
    return
end

%% Call JACPTS():
[xi, w, v] = jacpts(n-1, alp, bet+1);

%% Nodes:
x = [-1 ; xi];

%% Quadrature weights:
wi = w./(1 + xi.');
if ( alp == 0 && bet == 0 )
    w = [2/n^2, wi];
else
    % See Walter Gautschi, "Gauss–Radau formulae for Jacobi and Laguerre
    % weight functions", Mathematics and Computers in Simulation, (2000).
    w = [2^(alp+bet+1)*beta(bet+1,n)*beta(alp+n,bet+1)*(bet+1), wi];
end

%% Barycentric weights:
v = v./(1 + xi);
v = v/max(abs(v));
v1 = -sum(v);
v = [v1 ; v];

end


function [a1, b1, c1, a2, b2, c2] = aaad(A, B, C)
%AAAD  gives both solutions to the angle-angle-angle problem, in degrees.
%
%   AAAD(A, B, C) will result in NaNs if the existence condition
%   |pi - |A|-|B|| <= |C| <= pi - ||A| - |B||
%   is not met.
%
%   See also AAAD.

% Rody P.S. Oldenhuis
% Delft University of Technology
% oldenhuis@gmail.com
%
% Crated     : 23/Feb/2009
% Last edited: 30/Nov/2012

    % find both solutions by calling aaa directly
    r2d = 180/pi;
    d2r = 1/r2d;
    [a1, b1, c1, a2, b2, c2] = aaa(A*d2r, B*d2r, C*d2r);
    [a1, b1, c1, a2, b2, c2] = deal(a1*r2d, b1*r2d, c1*r2d, a2*r2d, b2*r2d, c2*r2d);

end


function [xr,Yr]=noisergeo(x,dim,tau,r,q,p,theta)
%Syntax: [xr,Yr]=noisergeo(x,dim,tau,r,q,p,theta)
%________________________________________________
%
% Noise reduction by Local Geometric Projection.
%
% xr is the vector/matrix with the cleaned time series.
% Yr is the phase space of the last cleaned xr.
% x is the time series.
% dim is the embedding dimension.
% tau is the time delay.
% r can be either
%   real defining the neighborhood range.
%   integer defining the number of nearest neighbors.
% q can take one of the following values
%  'wAV' for the weighted average.
%  [an integer from 0 to dim-1] for the local geometric projection.
%  'mod' for the adaptive selection of the local neighborhood dimensions.
% p defines the norm.
% theta is the correction paprameter [0,1]
%   1: full correction.
%   0: no correction.
%
%
% References:
%
% Kantz H, Schreiber T, Hoffmann I, Buzug T, Pfister G, Flepp L G, Simonet
% J, Badii R, Brun E (1993): Nonlinear noise reduction: A case study on
% experimental data. Physical Review E 48: 1529-1538
%
% Leontitsis A., Bountis T., Pange J. (2004): An adaptive way for improving
% noise reduction using local geometric projection. CHAOS 14(6): 106-110
%
%
% Alexandros Leontitsis
% Department of Education
% University of Ioannina
% Ioannina
% Greece
%
% University e-mail: me00743@cc.uoi.gr
% Lifetime e-mail: leoaleq@yahoo.com
% Homepage: http://www.geocities.com/CapeCanaveral/Lab/1421
%
% 14 Jul 2001

if nargin<1 | isempty(x)==1
   error('You should provide a time series.');
else
   % x must be a vector
   if min(size(x))>1
      error('Invalid time series.');
   end
   x=x(:);
   % n is the time series length
   n=length(x);
end

if nargin<2 | isempty(dim)==1
   dim=2;
else
   % dim must be either a scalar or a vector
   if min(size(dim))>1
      error('dim must be a scalar or a vector.');
   end
   % dim must be an integer
   if round(dim)-dim~=0
      error('dim must be an integer.');
   end
   % dim values must be above 1
   if any(dim<1)==1
      error('dim values must be above 1');
   end
end

if nargin<3 | isempty(tau)==1
   tau=1;
else
   % tau must be either a scalar or a vector
   if min(size(tau))>1
      error('tau must be a scalar or a vector.');
   end
   % tau must be an integer
   if round(tau)-tau~=0
      error('tau must be an integer.');
   end
   % tau values must be above 1
   if any(tau<1)==1
      error('tau values must be above 1');
   end
end

if nargin<4 | isempty(r)==1
   r=dim+1;
else
   % r must be either a scalar or a vector
   if min(size(r))>1
      error('r must be a scalar or a vector.');
   end
   % r values must be above 0
   if any(r<=0)==1
      error('r values must be greater than 0');
   end
end

if nargin<5 | isempty(q)==1
   q=1;
end

if nargin<6 | isempty(p)==1
   p=2;
else
   % p must be either a scalar or a vector
   if min(size(p))>1
      error('p must be a scalar or a vector.');
   end
end

if nargin<7 | isempty(theta)==1
   theta=1;
else
   % theta must be either a scalar or a vector
   if min(size(theta))>1
      error('theta must be a scalar or a vector.');
   end
   % theta must be above 0 and bellow 1
   if theta<0 | theta>1
      error('theta must be above 0 and bellow 1.')
   end
end

% Only one of dim, tau, r, p or theta should be vector
l=[length(dim),length(tau),length(r),length(p),length(theta)];
if length(find(l>1))>1
   error('Only one of dim, tau, r, p, or theta should be vector.');
end

% Make the phase-space
[Y,T]=phasespace(x,dim,tau);

m=max(l);
dim=ones(1,m).*dim;
tau=ones(1,m).*tau;
r=ones(1,m).*r;
p=ones(1,m).*p;
theta=ones(1,m).*theta;

for i=1:m

    % Initialize Yr
    Yr=zeros(T,dim(i));

    % For every phase-space point
    for j=1:T

        % Locate the j-th point
        y=Y(j,:);

        % Check neighborhood or neighbors
        if mod(r(i),floor(r(i)))==0
            lock=Knearest(y,Y,r(end)+1,p(i));
        else
            lock=radnearest(y,Y,T,r(i),p(i));
            lock(find(j==lock))=[];
        end

        if isempty(lock)==1
            Yr(j,:)=y;
        elseif q=='wAV'
            % The calculations for the weighted average
            Ynearest=Y(lock,:);
            w=[];
            for k=1:length(lock)
                w(k)=norm(y-Ynearest(k,:))/2/r(i);
                w(k)=exp(-w(k)^2);
                Ynearest(k,:)=Ynearest(k,:)*w(k);
            end
            Yr(j,:)=sum(Ynearest)/sum(w);
        else
            % All the neighboring points have equal weight
            if length(lock)<dim(i)
                Yr(j,:)=mean(Y(lock,:));
            else
                % Make the matrix of the local neighborhood
                Ynearest=Y(lock,:);
                %Ynearest=quality3(Ynearest,2);
                %Ynearest=quality2(Ynearest,2,.99,r,y);
                Ynearest=quality(Ynearest,2,.7);
                % Neigborhood average
                ybar=mean(Ynearest);
                % Subtract the average from the neighborhood poionts
                Ynearest=Ynearest-ones(size(Ynearest,1),1)*ybar;
                % SVD on the local neighborhood
                [u,s,v]=svd(Ynearest,0);
                % Either use the Adaptive selection ...
                if q=='mod'
                    s=diag(s);
                    d=s(1:dim(i)-1)./(s(2:dim(i))+eps);
                    q=find(d==max(d));
                    Yr(j,:)=y-(y-ybar)*(v(:,q+1:dim(i))*v(:,q+1:dim(i))');
                % ... or use a predifined q
                else
                    Yr(j,:)=y-(y-ybar)*(v(:,dim(i)-q:dim(i))*v(:,dim(i)-q:dim(i))');
                end
            end
        end
        Yr(j,:)=theta(i)*Yr(j,:)+(1-theta(i))*Y(j,:);
    end

    % Initialize xr
    xr(:,i)=zeros(n,1);
    times(:,i)=zeros(n,1);

    % Calculate the reconstructed time series
    for j=1:dim
        xr(1+(j-1)*tau:T+(j-1)*tau,i)=xr(1+(j-1)*tau:T+(j-1)*tau,i)+Yr(:,j);
        times(1+(j-1)*tau:T+(j-1)*tau,i)=times(1+(j-1)*tau:T+(j-1)*tau,i)+1;
    end
    xr(:,i)=xr(:,i)./times(:,i);
    xr(:,i)=xr(:,i)-mean(xr(:,i)-x);

end


function [xc,yc,radius,h] = fitcircle_manual(hfig,color,ti)

xc = [];
yc = [];
radius = [];
h = [];

if nargin < 1,
  hfig = gcf;
end
hold on;

if nargin < 2,
  color = 'm';
end

if nargin < 3,
  ti = 'Click points on circle';
end
title(ti);
try
  [x,y] = getline(hfig,'closed');
catch %#ok<CTCH>
  return;
end

if isempty(x),
  return;
end

% solve for parameters a, b, and c in the least-squares sense by
% using the backslash operator
abc = [x y ones(length(x),1)] \ -(x.^2+y.^2);
a = abc(1); b = abc(2); c = abc(3);

% calculate the location of the center and the radius
xc = -a/2;
yc = -b/2;
radius  =  sqrt((xc^2+yc^2)-c);

% display the calculated center
h(1) = plot(xc,yc,[color 'x'],'LineWidth',2);
h(2) = text(xc,yc,sprintf('  (%.1f, %.1f)',xc,yc),'Color',color,'FontWeight','bold');

% plot the entire circle
theta = 0:0.01:2*pi;

% use parametric representation of the circle to obtain coordinates
% of points on the circle
Xfit = radius*cos(theta) + xc;
Yfit = radius*sin(theta) + yc;

h(3) = plot(Xfit, Yfit,[color '-'],'LineWidth',2);

message = sprintf('The estimated radius is %2.3f pixels', radius);
h(4) = text(15,15,message,'Color',color,'FontWeight','bold','BackgroundColor','k');


% Nonnegativity Constrained Least Squares with Multiple Righthand Sides
%      using Block Principal Pivoting method
%
% This software solves the following problem: given A and B, find X such that
%              minimize || AX-B ||_F^2 where X>=0 elementwise.
%
% Reference:
%      Jingu Kim and Haesun Park. Fast Nonnegative Matrix Factorization: An Activeset-like Method and Comparisons,
%      SIAM Journal on Scientific Computing, 33(6), pp. 3261-3281, 2011.
%
% Written by Jingu Kim (jingu.kim@gmail.com)
%            School of Computational Science and Engineering,
%            Georgia Institute of Technology
%
% Note that this algorithm assumes that the input matrix A has full column rank.
% This code comes with no guarantee or warranty of any kind.
% Please send bug reports, comments, or questions to Jingu Kim.
%
% Modified Feb-20-2009
% Modified Mar-13-2011: numChol and numEq
%
% <Inputs>
%        A : input matrix (m x n) (by default), or A'*A (n x n) if isInputProd==1
%        B : input matrix (m x k) (by default), or A'*B (n x k) if isInputProd==1
%        isInputProd : (optional, default:0) if turned on, use (A'*A,A'*B) as input instead of (A,B)
%        init : (optional) initial value for X
% <Outputs>
%        X : the solution (n x k)
%        Y : A'*A*X - A'*B where X is the solution (n x k)
%        success : 0 for success, 1 for failure.
%                  Failure could only happen on a numericall very ill-conditioned problem.
%        numChol : number of unique cholesky decompositions done
%        numEqs : number of systems of linear equations solved

function [ X,Y,success,numChol,numEq ] = nnlsm_blockpivot( A, B, isInputProd, init )
    if nargin<3, isInputProd=0;, end
    if isInputProd
        AtA = A;, AtB = B;
    else
        AtA = A'*A;, AtB = A'*B;
    end

    if size(AtA,1)==1
        X = AtB/AtA; X(X<0) = 0;
        Y = AtA*X - AtB;
        numChol = 1; numEq = size(AtB,2); success = 1;
        return
    end

    [n,k]=size(AtB);
    MAX_BIG_ITER = n*5;
    % set initial feasible solution
    X = zeros(n,k);
    if nargin<4
        Y = - AtB;
        PassiveSet = false(n,k);
        numChol = 0;
        numEq = 0;
    else
        PassiveSet = (init > 0);
        [ X,numChol,numEq] = normalEqComb(AtA,AtB,PassiveSet);
        Y = AtA * X - AtB;
    end
    % parameters
    pbar = 3;
    P = zeros(1,k);, P(:) = pbar;
    Ninf = zeros(1,k);, Ninf(:) = n+1;

    NonOptSet = (Y < 0) & ~PassiveSet;
    InfeaSet = (X < 0) & PassiveSet;
    NotGood = sum(NonOptSet)+sum(InfeaSet);
    NotOptCols = NotGood > 0;

    bigIter = 0;, success=0;
    while(~isempty(find(NotOptCols)))
        bigIter = bigIter+1;
        if ((MAX_BIG_ITER >0) && (bigIter > MAX_BIG_ITER))   % set max_iter for ill-conditioned (numerically unstable) case
            success = 1;, break
        end

        Cols1 = NotOptCols & (NotGood < Ninf);
        Cols2 = NotOptCols & (NotGood >= Ninf) & (P >= 1);
        Cols3Ix = find(NotOptCols & ~Cols1 & ~Cols2);
        if ~isempty(find(Cols1))
            P(Cols1) = pbar;,Ninf(Cols1) = NotGood(Cols1);
            PassiveSet(NonOptSet & repmat(Cols1,n,1)) = true;
            PassiveSet(InfeaSet & repmat(Cols1,n,1)) = false;
        end
        if ~isempty(find(Cols2))
            P(Cols2) = P(Cols2)-1;
            PassiveSet(NonOptSet & repmat(Cols2,n,1)) = true;
            PassiveSet(InfeaSet & repmat(Cols2,n,1)) = false;
        end
        if ~isempty(Cols3Ix)
            for i=1:length(Cols3Ix)
                Ix = Cols3Ix(i);
                toChange = max(find( NonOptSet(:,Ix)|InfeaSet(:,Ix) ));
                if PassiveSet(toChange,Ix)
                    PassiveSet(toChange,Ix)=false;
                else
                    PassiveSet(toChange,Ix)=true;
                end
            end
        end
        [ X(:,NotOptCols),tempChol,tempEq ] = normalEqComb(AtA,AtB(:,NotOptCols),PassiveSet(:,NotOptCols));
        numChol = numChol + tempChol;
        numEq = numEq + tempEq;
        X(abs(X)<1e-12) = 0;            % One can uncomment this line for numerical stability.
        Y(:,NotOptCols) = AtA * X(:,NotOptCols) - AtB(:,NotOptCols);
        Y(abs(Y)<1e-12) = 0;            % One can uncomment this line for numerical stability.

        % check optimality
        NotOptMask = repmat(NotOptCols,n,1);
        NonOptSet = NotOptMask & (Y < 0) & ~PassiveSet;
        InfeaSet = NotOptMask & (X < 0) & PassiveSet;
        NotGood = sum(NonOptSet)+sum(InfeaSet);
        NotOptCols = NotGood > 0;
    end
end


function cvt_test14 ( )

%*****************************************************************************80
%
%% CVT_TEST14 generates a 10 point CVT on [0,1].
%
%  Discussion:
%
%    Generate 10 CVT points on the interval [0,1].
%    We expect them to be at 1/20, 3/20, 5/20, ..., 19/20.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 September 2010
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST14\n' );
  fprintf ( 1, '  Generate a CVT in the interval [0,1] using 10 points.\n' );
  fprintf ( 1, '  Exact answer: { 0.05, 0.15, 0.25, ..., 0.85, 0.95 }\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  It turns out that, for a fixed number of points N,\n' );
  fprintf ( 1, '  a 1D problem will converge much more slowly than for\n' );
  fprintf ( 1, '  cases where the dimension is higher.\n' );

  dim_num = 1;
  n = 10;
  batch = 10000;
  init = 1;
  init_string = 'uniform';
  it_max = 50;
  it_fixed = 1;
  sample = 1;
  sample_num = 100000;
  sample_string = 'uniform';
  seed = 123456789;
  r = [];

  seed_init = seed;

  [ r, seed, it_num, it_diff, energy ] = cvt ( dim_num, n, batch, init, ...
    sample, sample_num, it_max, it_fixed, seed, r );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Dimension DIM_NUM =        %12d\n', dim_num );
  fprintf ( 1, '  Number of points N =       %12d\n', n );
  fprintf ( 1, '  Initial SEED =             %12d\n', seed_init );
  fprintf ( 1, '  Current SEED =             %12d\n', seed );
  fprintf ( 1, '  INIT =                    "%s".\n', init_string );
  fprintf ( 1, '  Max iterations IT_MAX =    %12d\n', it_max );
  fprintf ( 1, '  IT_FIXED (fixed samples) = %12d\n', it_fixed );
  fprintf ( 1, '  Iterations IT_NUM =        %12d\n', it_num );
  fprintf ( 1, '  Difference IT_DIFF =       %14f\n', it_diff );
  fprintf ( 1, '  CVT ENERGY =               %14f\n', energy );
  fprintf ( 1, '  SAMPLE =                  "%s".\n', sample_string );
  fprintf ( 1, '  Samples SAMPLE_NUM    =    %12d\n', sample_num );
  fprintf ( 1, '  Sampling BATCH size =      %12d\n', batch );
  fprintf ( 1, '  EPSILON (unit roundoff) =  %12e\n', eps );

  r = sort ( r );

  r8mat_transpose_print ( dim_num, n, r, '  Generators (rows):' );

  return
end


%% Spherical Functions
%
%%
% By a variable of type @S2Fun it is possible to represent an entire
% function on the two dimensional sphere. A typical example of such a
% function is the pole density function of a given ODF with respect to a
% fixed crystal direction.

% the famouse Santa Fe orientation distribution function
odf = SantaFe;

% the (100) pole density function
pdf = odf.calcPDF(Miller(1,0,0,odf.CS))

%%
% Since, the variable |pdf| stores all information about this function we
% may evaluate it for any direction |r|

% take a random direction
r = vector3d.rand;

% and evaluate the pdf at this direction
pdf.eval(r)

%%
% We may also plot the function in any spherical projection

plot(pdf)

%%
% or find its local maxima

[~,localMax] = max(pdf,'numLocal',12)

annotate(localMax)

%%
% A complete list of operations that can be performed with spherical
% functions can be found in section <S2FunOperations.html Operations>.
%

%% Representation of Spherical Functions
%
% In MTEX there exist different ways for representing spherical functions
% internally.
%
% || harmonic expansion || @S2FunHarmonic ||
% || finite elements || @S2FunTri ||
% || function handle || @S2FunHandle ||
% || Bingham distribution || @BinghamS2 ||
%
% All representations allow for the same operations which are specified for
% the abstact class @S2Fun. In particular it is possible
% to calculate with spherical functions as with ordinary numbers, i.e., you
% can add, multiply arbitrary functions, take the mean, integrate them or
% compute gradients.
%
%% Generalizations of Spherical Functions
%
% || spherical vector fields || @S2VectorField ||
% || spherical axis fields || @S2AxisField ||
% || radial spherical functions || @S2Kernel ||
% || symmetric spherical functions || @S2FunHarmonicSym ||
%


function [cc,c0]=v_lpcar2cc(ar,np)
%V_LPCAR2CC LPC: Convert AR filter to complex cepstrum [CC,C0]=(AR,NP)
%
%  Inputs: ar(nf,n+1)   AR coefficients, one frame per row
%          np           Number of cepstral coefficients to calculate [n]
%
% Outputs: cc(nf,np)    Complex cepstral coefficients, excluding c(0)
%          c0(nf,1)     Coefficient c(0)
%
% The "complex cepstral coefficients", cc(n), are the inverse discrete-time Fourier transform
% of the log of the complex-valued spectrum. The cc(n) are real-valued and, for n<0, cc(n)=0.
% The "real cepstral coeffcients", rc(n), are the inverse discrete-time Fourier transform
% of the log of the magnitude spectrum; rc(0)=cc(0) and rc(n)=0.5*cc(n) for n~=0.

%      Copyright (C) Mike Brookes 1998-2014
%      Version: $Id: v_lpcar2cc.m 10865 2018-09-21 17:22:45Z dmb $
%
%   VOICEBOX is a MATLAB toolbox for speech processing.
%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   This program 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 2 of the License, or
%   (at your option) any later version.
%
%   This program 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 can obtain a copy of the GNU General Public License from
%   http://www.gnu.org/copyleft/gpl.html or by writing to
%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[nf,p1]=size(ar);
p=p1-1;
if (nargin<2) np=p; end
cc=zeros(nf,np);
if any(ar(:,1)~=1)
    c0=-log(ar(:,1));
    ar=ar./ar(:,ones(1,p1));
else
    c0=zeros(nf,1);
end
cm=(1:np).^(-1);
if np>p
    xm=-(1:p);
    nz=np-p;
    for k=1:nf
        cc(k,:)=filter(1,ar(k,:),[ar(k,2:p1).*xm zeros(1,nz)]).*cm;
    end
else
    p1=np+1;
    xm=-(1:np);
    for k=1:nf
        cc(k,:)=filter(1,ar(k,:),ar(k,2:p1).*xm).*cm;
    end
end
if ~nargout
    v_lpccc2pf(cc,[],[],c0);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function (ldiv) : calculate inverse Z-transform by long division
% Author : Tamer mohamed samy abdelazim Mellik
% Contact information :
%Department of Electrical & Computer Engineering,
%University of Calgary,
%2500 University Drive N.W. ,
%Calgary, AB T2N 1N4 ,
%Canada .
% email :abdelasi@enel.ucalgary.ca
% email : tabdelaz@ucalgary.ca
% Webpage : http://www.enel.ucalgary.ca/~abdelasi/
% Date    : 2-5-2002
% Version : 1.0.0
%Example
% This function like deconv but it help if the numerator less or equal degree of denominator
% if you have this function (It must arranged in terms of minus power of Z):
%              1
% G(z)= -----------------
%             -1     -2
%      ( 5 - Z  - 3 Z  )
% and you want to calculate long division or inverse Z transform :
% The numerator  is a=[1] and  the denominator is b= [5 -1 -3 ]
% call the function ldiv(a,b) to get the funresult 20 items (default)
% another example :
%                -2     -3
%       ( 5 - 3 Z  + 4 Z  )
%  G(z)-----------------
%              -1     -2
%        (5 - Z  - 3 Z )
% a=[5 0 -3 4] , b= [5 -1 -3 ] and you want the funresult 100 terms !
% ldiv(a,b,100)
% Note : The author doesn't have any responsibility for any harm caused by the use of this file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function funresult=ldiv(a,b,N)
%a numerator
%b denominator
%default order of the filter == 20
funresult=[];
if nargin < 3
    if nargin > 1
            N=20;
    else
            disp('Usage: M = ldiv(a,b,N)')
            disp('a:numerator , b denominator and N is the order of the resultant filter')
            return
    end
end

if size(a) < 1
    disp('Error: numerator must at least have one element not empty')
    return
end
if size(b) < 1
    disp('Error: denominator must at least have one element not empty')
    return
end

if b(1)==0
    disp('Error: The first element of denominator must have nonzero value')
    return
end
if size(b) < 2
    funresult=a./b;
    for i =length(funresult)+1:N
       funresult(i)=0;
    end
    return
end

for i = length(a)+1:N
    a(i)=0;
end
for i = 1 : N
    funresult(i)=a(1)/b(1);
    if length(a)>1
            for k= 2:length(b)
                if k > length(a)
                    a(k)=0;
                end
                a(k)=a(k)-funresult(length(funresult))*b(k);
            end

        for i = 1:length(a)-1
                a(i)=a(i+1);
        end
        a=a(1:length(a)-1);
    end
end


function a = frank_inverse ( n )

%*****************************************************************************80
%
%% FRANK_INVERSE returns the inverse of the FRANK matrix.
%
%  Formula:
%
%    if ( I = J-1 )
%      A(I,J) = -1
%    elseif ( I = J )
%      if ( I = 1 )
%        A(I,J) = 1
%      else
%        A(I,J) = N + 2 - I
%    elseif ( J < I )
%      A(I,J) = - (N+1-I) * A(I-1,J)
%    else
%      A(I,J) = 0
%
%  Example:
%
%    N = 5
%
%     1  -1  0  0  0
%    -4   5 -1  0  0
%    12 -15  4 -1  0
%   -24  30 -8  3 -1
%    24 -30  8 -3  2
%
%  Properties:
%
%    A is generally not symmetric: A' /= A.
%
%    A is lower Hessenberg.
%
%    det ( A ) = 1.
%
%    A is unimodular.
%
%    A is integral: int ( A ) = A.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 October 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of A.
%
%    Output, real A(N,N), the matrix.
%
  a = zeros ( n, n );

  for i = 1 : n
    for j = 1 : n

      if ( i == j-1 )
        a(i,j) = - 1.0;
      elseif ( i == j )
        if ( i == 1 )
          a(i,j) = 1.0;
        else
          a(i,j) = ( n + 2 - i );
        end
      elseif ( j < i )
        a(i,j) = - ( n + 1 - i ) * a(i-1,j);
      else
        a(i,j) = 0.0;
      end

    end
  end

  return
end


function [detJ,J] = dtiJacobian(inData)
%
% [detJ,J] = dtiJacobian(inData)
%
% Computes the Jacobian (J) and the determinant of the Jacobian (detJ)
% given an XxYxZx3 or Nx3 input array. The array is assumed to represent a
% vector field. The Jacobian is a useful metric for interpreting
% deformations. E.g., abs(detJ) represents the local volume change for a
% deformation field.
%
% HISTORY:
% 2008.06.25 RFD wrote it.

sz = size(inData);

if((numel(sz)~=4 && numel(sz)~=2) || sz(end)~=3)
    error('inData must be XxYxZx3 or Nx3.');
end

% Approximate the Jacobian of the input array using grad.

if(numel(sz)==2)
    inData = reshape(inData,[1 1 1 sz(1) sz(2)]);
end

dim = size(inData);
J = zeros(dim(1),dim(2),dim(3),3,3);

for(ii=1:3)
    [gradX,gradY,gradZ] = gradient(inData(:,:,:,ii),1);
    J(:,:,:,ii,1) = gradX;
    J(:,:,:,ii,2) = gradY; % Or Y,X,Z?
    J(:,:,:,ii,3) = gradZ;
end
% Compute the determinant of J
% det(J) for a 3x3 [a b c; d e f; g h i] is: (aei+bfg+cdh)-(gec+hfa+idb)
% a=1,1; b=1,2; c=1,3; d=2,1; e=2,2; f=2,3; g=3,1; h=3,2; i=3,3;
detJ =  J(:,:,:,1,1).*J(:,:,:,2,2).*J(:,:,:,3,3) ...
      + J(:,:,:,1,2).*J(:,:,:,2,3).*J(:,:,:,3,1) ...
      + J(:,:,:,1,3).*J(:,:,:,2,1).*J(:,:,:,3,2) ...
      - J(:,:,:,3,1).*J(:,:,:,2,2).*J(:,:,:,1,3) ...
      - J(:,:,:,3,2).*J(:,:,:,2,3).*J(:,:,:,1,1) ...
      - J(:,:,:,3,3).*J(:,:,:,2,1).*J(:,:,:,1,2);

if(numel(sz)==2)
    J = squeeze(J);
    detJ = squeeze(detJ);
end
return;