function [ grid_weight, grid_point ] = sparse_grid ( dim_num, level_max, ...
  rule, point_num )

%*****************************************************************************80
%
%% SPARSE_GRID computes a sparse grid.
%
%  Discussion:
%
%    A Smolyak construction is used to create a multidimensional sparse grid.
%
%    The user specifies:
%    * the spatial dimension of the quadrature region,
%    * the level that defines the Smolyak grid.
%    * the 1D quadrature rule.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    30 March 2008
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Fabio Nobile, Raul Tempone, Clayton Webster,
%    A Sparse Grid Stochastic Collocation Method for Partial Differential
%    Equations with Random Input Data,
%    SIAM Journal on Numerical Analysis,
%    Volume 46, Number 5, 2008, pages 2309-2345.
%
%  Parameters:
%
%    Input, integer DIM_NUM, the spatial dimension.
%
%    Input, integer LEVEL_MAX, controls the size of the final
%    sparse grid.
%
%    Input, integer RULE, the index of the rule.
%    1, "CC", Clenshaw Curtis Closed Fully Nested rule.
%    2, "F1", Fejer 1 Open Fully Nested rule.
%    3, "F2", Fejer 2 Open Fully Nested rule.
%    4, "GP", Gauss Patterson Open Fully Nested rule.
%    5, "GL", Gauss Legendre Open Weakly Nested rule.
%    6, "GH", Gauss Hermite Open Weakly Nested rule.
%    7, "LG", Gauss Laguerre Open Non Nested rule.
%
%    Input, integer POINT_NUM, the number of points in the grid,
%    as determined by LEVELS_INDEX_SIZE.
%
%    Output, real GRID_WEIGHT(POINT_NUM), the weights.
%
%    Output, real  GRID_POINT(DIM_NUM,POINT_NUM), the points.
%
  if ( rule == 1 )
    [ grid_weight, grid_point ] = sparse_grid_cfn ( dim_num, level_max, ...
      rule, point_num );
  elseif ( 2 <= rule & rule <= 4 )
    [ grid_weight, grid_point ] = sparse_grid_ofn ( dim_num, level_max, ...
      rule, point_num );
  elseif ( 5 <= rule & rule <= 6 )
    [ grid_weight, grid_point ] = sparse_grid_own ( dim_num, level_max, ...
      rule, point_num );
  elseif ( 7 == rule )
    [ grid_weight, grid_point ] = sparse_grid_onn ( dim_num, level_max, ...
      rule, point_num );
  else
    grid_weight = [];
    grid_point = [];
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SPARSE_GRID - Fatal error!\n' );
    fprintf ( 1, '  Illegal input rule index = %d\n', rule );
    error ( 'SPARSE_GRID - Fatal error!' );
  end

  return
end


function [p,f] = oct3bank(x);
% OCT3BANK Simple one-third-octave filter bank.
%    OCT3BANK(X) plots one-third-octave power spectra of signal vector X.
%    Implementation based on ANSI S1.11-1986 Order-3 filters.
%    Sampling frequency Fs = 44100 Hz. Restricted one-third-octave-band
%    range (from 100 Hz to 5000 Hz). RMS power is computed in each band
%    and expressed in dB with 1 as reference level.
%
%    [P,F] = OCT3BANK(X) returns two length-18 row-vectors with
%    the RMS power (in dB) in P and the corresponding preferred labeling
%    frequencies (ANSI S1.6-1984) in F.
%
%    See also OCT3DSGN, OCT3SPEC, OCTDSGN, OCTSPEC.

% Author: Christophe Couvreur, Faculte Polytechnique de Mons (Belgium)
%         couvreur@thor.fpms.ac.be
% Last modification: Aug. 23, 1997, 10:30pm.

% References:
%    [1] ANSI S1.1-1986 (ASA 65-1986): Specifications for
%        Octave-Band and Fractional-Octave-Band Analog and
%        Digital Filters, 1993.
%    [2] S. J. Orfanidis, Introduction to Signal Processing,
%        Prentice Hall, Englewood Cliffs, 1996.

pi = 3.14159265358979;
Fs = 44100; 				% Sampling Frequency
N = 3; 					% Order of analysis filters.
F = [ 100 125 160, 200 250 315, 400 500 630, 800 1000 1250, ...
	1600 2000 2500, 3150 4000 5000 ]; % Preferred labeling freq.
ff = (1000).*((2^(1/3)).^[-10:7]); 	% Exact center freq.
P = zeros(1,18);
m = length(x);

% Design filters and compute RMS powers in 1/3-oct. bands
% 5000 Hz band to 1600 Hz band, direct implementation of filters.
for i = 18:-1:13
   [B,A] = oct3dsgn(ff(i),Fs,N);
   y = filter(B,A,x);
   P(i) = sum(y.^2)/m;
end
% 1250 Hz to 100 Hz, multirate filter implementation (see [2]).
[Bu,Au] = oct3dsgn(ff(15),Fs,N); 	% Upper 1/3-oct. band in last octave.
[Bc,Ac] = oct3dsgn(ff(14),Fs,N); 	% Center 1/3-oct. band in last octave.
[Bl,Al] = oct3dsgn(ff(13),Fs,N); 	% Lower 1/3-oct. band in last octave.
for j = 3:-1:0
   x = decimate(x,2);
   m = length(x);
   y = filter(Bu,Au,x);
   P(j*3+3) = sum(y.^2)/m;
   y = filter(Bc,Ac,x);
   P(j*3+2) = sum(y.^2)/m;
   y = filter(Bl,Al,x);
   P(j*3+1) = sum(y.^2)/m;
end

% Convert to decibels.
Pref = 1; 				% Reference level for dB scale.
idx = (P>0);
P(idx) = 10*log10(P(idx)/Pref);
P(~idx) = NaN*ones(sum(~idx),1);

% Generate the plot
if (nargout == 0)
  bar(P);
  ax = axis;
  axis([0 19 ax(3) ax(4)])
  set(gca,'XTick',[2:3:18]); 		% Label frequency axis on octaves.
  set(gca,'XTickLabels',F(2:3:length(F)));  % MATLAB 4.1c
%  set(gca,'XTickLabel',F(2:3:length(F)));  % MATLAB 5.1
  xlabel('Frequency band [Hz]'); ylabel('Power [dB]');
  title('One-third-octave spectrum')
% Set up output parameters
elseif (nargout == 1)
  p = P;
elseif (nargout == 2)
  p = P;
  f = F;
end


%[2016]-"The whale optimization algorithm"

% (9/12/2020)

function WOA = jWhaleOptimizationAlgorithm(feat,label,opts)
% Parameters
lb    = 0;
ub    = 1;
thres = 0.5;
b     = 1;     % constant

if isfield(opts,'T'), max_Iter = opts.T; end
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'b'), b = opts.b; end
if isfield(opts,'thres'), thres = opts.thres; end

% Objective function
fun = @jFitnessFunction;
% Number of dimensions
dim = size(feat,2);
% Initial
X   = zeros(N,dim);
for i = 1:N
	for d = 1:dim
    X(i,d) = lb + (ub - lb) * rand();
	end
end
% Fitness
fit  = zeros(1,N);
fitG = inf;
for i = 1:N
  fit(i) = fun(feat,label,(X(i,:) > thres),opts);
  % Global best
  if fit(i) < fitG
    fitG = fit(i);
    Xgb  = X(i,:);
  end
end
% Pre
curve = zeros(1,max_Iter);
curve(1) = fitG;
t = 2;
while t <= max_Iter
	% Define a, linearly decreases from 2 to 0
  a = 2 - t * (2 / max_Iter);
  for i = 1:N
    % Parameter A (2.3)
    A = 2 * a * rand() - a;
    % Paramater C (2.4)
    C = 2 * rand();
    % Parameter p, random number in [0,1]
    p = rand();
    % Parameter l, random number in [-1,1]
    l = -1 + 2 * rand();
    % Whale position update (2.6)
    if p  < 0.5
      % {1} Encircling prey
      if abs(A) < 1
        for d = 1:dim
          % Compute D (2.1)
          Dx     = abs(C * Xgb(d) - X(i,d));
          % Position update (2.2)
          X(i,d) = Xgb(d) - A * Dx;
        end
      % {2} Search for prey
      elseif abs(A) >= 1
        for d = 1:dim
          % Select a random whale
          k      = randi([1,N]);
          % Compute D (2.7)
          Dx     = abs(C * X(k,d) - X(i,d));
          % Position update (2.8)
          X(i,d) = X(k,d) - A * Dx;
        end
      end
    % {3} Bubble-net attacking
    elseif p >= 0.5
      for d = 1:dim
        % Distance of whale to prey
        dist   = abs(Xgb(d) - X(i,d));
        % Position update (2.5)
        X(i,d) = dist * exp(b * l) * cos(2 * pi * l) + Xgb(d);
      end
    end
    % Boundary
    XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
    X(i,:) = XB;
  end
  % Fitness
  for i = 1:N
    % Fitness
    fit(i) = fun(feat,label,(X(i,:) > thres),opts);
    % Global best
    if fit(i) < fitG
      fitG = fit(i);
      Xgb  = X(i,:);
    end
  end
  curve(t) = fitG;
  fprintf('\nIteration %d Best (WOA)= %f',t,curve(t))
  t = t + 1;
end
% Select features
Pos   = 1:dim;
Sf    = Pos((Xgb > thres) == 1);
sFeat = feat(:,Sf);
% Store results
WOA.sf = Sf;
WOA.ff = sFeat;
WOA.nf = length(Sf);
WOA.c  = curve;
WOA.f  = feat;
WOA.l  = label;
end


function [Pro,Res] = interpolationAMGa(A,isC)
%% INTERPOLATIONAMGA construct prolongation and restriction matrices
%
% [Pro,Res] = INTERPOLATIONAMGA(A,isC) construct prolongation and
% restriction matrices use matrix-dependent interpolation. Each fine nodes
% use only one coarse node.
%
% In the input, A is a SPD matrix and isC is a logical array to indicate
% nodes in coarse matrix. In the output Pro and Res are prolongation and
% restriction matrices satisfying Res = Pro'.
%
% The submatrix A_{cf} is used to construct the interpolation of values on
% fine nodes from that of coarse nodes. The weight is normalized to
% preserve the constant.
%
% Example
%   load lakemesh
%   A = assemblematrix(node,elem);
%   [isC,As] = coarsenAMGc(A);
%   [Pro,Res] = interpolation(As,isC);
%
% See also: coarsenAMGc, amg
%
% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

N = size(A,1);
%% Index map between coarse grid and fine grid
allNode = (1:N)';
fineNode = allNode(~isC);
% Nf = length(fineNode);
Nc = N-length(fineNode);
coarseNode = (1:Nc)';    % coarse node index
coarseNodeFineIdx = find(isC); % coarse node index in the fine grid

%% Construct prolongation and restriction operator
Afc = A(fineNode,coarseNodeFineIdx);     % matrix-dependent interpolation
[Dsum,j] = max(abs(Afc),[],2);
idx = (Dsum ~= 0);
ip = [coarseNodeFineIdx; fineNode(idx)];   % fine node index
jp = [coarseNode; j(idx)];                 % coarse node index
sp = [ones(Nc,1); ones(length(j(idx)),1)]; % weight = 1;
Pro = sparse(ip,jp,sp,N,Nc);
Res = Pro';


function w = flattopwin (L, sym)
% Author: Paul Kienzle <pkienzle@users.sf.net> (2004)
% This program is granted to the public domain.
%
% flattopwin(L, [periodic|symmetric])
%
% Return the window f(w):
%
%   f(w) = 1 - 1.93 cos(2 pi w) + 1.29 cos(4 pi w)
%            - 0.388 cos(6 pi w) + 0.0322cos(8 pi w)
%
% where w = i/(L-1) for i=0:L-1 for a symmetric window, or
% w = i/L for i=0:L-1 for a periodic window.  The default
% is symmetric.  The returned window is normalized to a peak
% of 1 at w = 0.5.
%
% This window has low pass-band ripple, but high bandwidth.
%
% According to [1]:
%
%    The main use for the Flat Top window is for calibration, due
%    to its negligible amplitude errors.
%
% [1] Gade, S; Herlufsen, H; (1987) 'Use of weighting functions in DFT/FFT
% analysis (Part I)', Bruel & Kjaer Technical Review No.3.

  if nargin == 0 || nargin > 2
    help(mfilename);
  end % if

  divisor = L-1;
  if nargin > 1
    if strcmp(sym, 'periodic')
      divisor = L;
    elseif strcmp(sym, 'symmetric')
      divisor = L-1;
    else
      error('second argument must be ''periodic'' or ''symmetric''');
    end
  end % if

  x = 2*pi*(0:(L-1))'/divisor;
  if L==1
    w = 1;
  else
    % these coefficients come from wikipedia, and match better with what
    % matlab outputs (JM)
    a0 = 0.21557895;
    a1 = 0.41663158;
    a2 = 0.277263158;
    a3 = 0.083578947;
    a4 = 0.006947368;
    w = a0 - a1*cos(x) + a2*cos(2*x) - a3*cos(3*x) + ...
            a4*cos(4*x);
    %w = (1-1.93*cos(x)+1.29*cos(2*x)-0.388*cos(3*x)+0.0322*cos(4*x))/4.6402;
  end
end


function value = r8_normal_01_sample ( )

%*****************************************************************************80
%
%% R8_NORMAL_01_SAMPLE returns a unit pseudonormal R8.
%
%  Discussion:
%
%    The standard normal probability distribution function (PDF) has
%    mean 0 and standard deviation 1.
%
%    The Box-Muller method is used, which is efficient, but
%    generates two values at a time.
%
%    Typically, we would use one value and save the other for the next call.
%    However, the fact that this function has saved memory makes it difficult
%    to correctly handle cases where we want to re-initialize the code,
%    or to run in parallel.  Therefore, we will instead use the first value
%    and DISCARD the second.
%
%    EFFICIENCY must defer to SIMPLICITY.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    05 August 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Output, real VALUE, a sample of the standard normal PDF.
%
  r1 = r8_uniform_01_sample ( );
  r2 = r8_uniform_01_sample ( );

  x = sqrt ( - 2.0 * log ( r1 ) ) * cos ( 2.0 * pi * r2 );

  value = x;

  return
end


function g=BaylissLinearTapering(sidelobedB,N,xPoints,a)
%%BAYLISSLINEARTAPERING The Bayliss tapering is a set of complex amplitude
%          weights for a continuous linear (narrowband) aperture antenna
%          that will form a difference beam (odd symmetry about an axis)
%          and hold the four closest sidelobes to a desired level. Such a
%          tapering can be discretized and applied to the elements in a
%          circular phased array (An array of antenna elements can be
%          viewed as a discrete approximation to a continuous aperture).
%          This function will provide the tapering values at a set of
%          discrete points given by xPoints (the origin is taken to be the
%          center of the aperture). The radius of the aperture can either
%          be provided or is taken as value of the farthest point provided.
%
%INPUTS: sidelobedB The number of decibels of the ratio of the close-in
%             sidelobe voltages to the main lobe voltage. This must be a
%             negative number. A typical value is -30.
%           N The Bayliss tapering is computed using a certain number of
%             terms. Using too many terms can be undesirable as edge
%             illumination increases, as noted in [1]. If this parameter is
%             omitted or an empty matrix is passed, then the default of 17
%             is used. In [1], it is suggested that N be chosen to be
%             <2*a/lambda, where a is the radius of the aperture and
%             lambda the wavelength.
%    xyPoints An NX1 or 1XN set of N points at which the tapering values
%             should be evaluated. The center of the aperture is taken to
%             be the origin.
%           a The radius of the aperture. Tapering weights for points in
%             xPoints outside of the aperture are taken to be zero. If
%             this parameter is omitted or an empty matrix is passed, then
%             the radius is taken to be the distance of the farthest point
%             from the origin in xPoints.
%
%OUTPUTS: g The NX1 set of discretized Bayliss tapering values evaluated at
%           the points given in xPoints. All Bayliss tapering values are
%           imaginary. The coefficients are not normalized.
%
%This function implements the algorithm given in the appendix of in [1]
%using the polynomial interpolation values in the table below Figure 4.
%This approximation means that low sidelobe patterns (-45 dB and below)
%will not have good fidelity sidelobes.
%
%EXAMPLE 1:
%Here, we evaluate the tapering values for 30dB down on a fine grid of
%points to plot what the amplitude and phase of the tapering weights looks
%like.
% numPoints=300;
% xPoints=linspace(-1,1,numPoints);
% %The Bayliss tapering weights, evaluated across the aperture.
% a=1;%Aperture radius=1.
% %gTest=BaylissLinear();
% gBayliss=BaylissLinearTapering(-30,17,xPoints,a);
%
% figure(1)
% clf
% plot(xPoints,abs(gBayliss),'-b','linewidth',2)
% h1=xlabel('x');
% h2=ylabel('Imaginary Weight');
% title('Bayliss Tapering Amplitude')
% set(gca,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h1,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h2,'FontSize',14,'FontWeight','bold','FontName','Times')
%
% figure(2)
% clf
% plot(xPoints,angle(gBayliss),'-b','linewidth',2)
% h1=xlabel('x');
% h2=ylabel('Imaginary Weight');
% title('Bayliss Tapering Phase')
% set(gca,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h1,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h2,'FontSize',14,'FontWeight','bold','FontName','Times')
%
%EXAMPLE 2:
%Here, we consider the array response when using tapering values for 30dB
%sidelobes on a linear array with lambda/2 spacing between elements.
% N=17;
% sidelobedB=-30;
% Nx=41;%There are 2*Nx+1 points total.
% %Generate points symmetric about the origin.
% xPoints=(-(Nx-1)/2):1/2:((Nx-1)/2);
% g=BaylissLinearTapering(sidelobedB,N,xPoints);
%
% %Now, display the response with the tapering
% T=diag(g);
% [Rsp,U]=standardUVBeamPattern(T,xPoints,'NormRealVal');
%
% figure(2)
% clf
% plot(U,Rsp,'-b','LineWidth',2);
% h1=xlabel('u');
% h2=ylabel('Array Response');
% title('Bayliss Weighted Array Response')
% set(gca,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h1,'FontSize',14,'FontWeight','bold','FontName','Times')
% set(h2,'FontSize',14,'FontWeight','bold','FontName','Times')
%
%REFERENCES:
%[1] E. T. Bayliss, "Design of monopulse antenna difference patterns with
%    low sidelobes," The Bell System Technical Journal, vol. 47, no. 5, pp.
%    623-650, May-Jun. 1968.
%
%August 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<2||isempty(N))
   N=17;
end

%Defined below Equation 41 in [1].
mu=((0:(N-1))+1/2)';

%This holds the coefficients for the interpolating polynomials given below
%Figure 4 in [1]. The polynomials all take the desired sidelobe level in
%decibels as an input parameter. The first row is for the term A, which
%is a translation of the SNR parameter to a parameter in the paper. The
%next four rows are xi_1 to xi_4, which are the locations of the first four
%zeroes in the modified pattern. The final row is for p_0, which is related
%to the point at which the peak of the asymptotic difference pattern=1.
polyCoeffTable=[0.30387530,-0.05042922,-0.00027989,-0.00000343,-0.00000002;
                0.98583020,-0.03338850, 0.00014064, 0.00000190, 0.00000001;
                2.00337487,-0.01141548, 0.00041590, 0.00000373, 0.00000001;
                3.00636321,-0.00683394, 0.00029281, 0.00000161, 0;
                4.00518423,-0.00501795, 0.00021735, 0.00000088, 0;
                0.47972120,-0.01456692,-0.00018739,-0.00000218,-0.00000001];

A  =polyCoeffTable(1,1)+sidelobedB*(polyCoeffTable(1,2)+sidelobedB*(polyCoeffTable(1,3)+sidelobedB*(polyCoeffTable(1,4)+sidelobedB*polyCoeffTable(1,5))));
xi1=polyCoeffTable(2,1)+sidelobedB*(polyCoeffTable(2,2)+sidelobedB*(polyCoeffTable(2,3)+sidelobedB*(polyCoeffTable(2,4)+sidelobedB*polyCoeffTable(2,5))));
xi2=polyCoeffTable(3,1)+sidelobedB*(polyCoeffTable(3,2)+sidelobedB*(polyCoeffTable(3,3)+sidelobedB*(polyCoeffTable(3,4)+sidelobedB*polyCoeffTable(3,5))));
xi3=polyCoeffTable(4,1)+sidelobedB*(polyCoeffTable(4,2)+sidelobedB*(polyCoeffTable(4,3)+sidelobedB*(polyCoeffTable(4,4)+sidelobedB*polyCoeffTable(4,5))));
xi4=polyCoeffTable(5,1)+sidelobedB*(polyCoeffTable(5,2)+sidelobedB*(polyCoeffTable(5,3)+sidelobedB*(polyCoeffTable(5,4)+sidelobedB*polyCoeffTable(5,5))));
%p0 =polyCoeffTable(6,1)+sldelobedB*(polyCoeffTable(6,2)+sldelobedB*(polyCoeffTable(6,3)+sldelobedB*(polyCoeffTable(6,4)+sldelobedB*polyCoeffTable(6,5))));

Z=zeros(N+1,1);
%Equation 15
Z(1)=0;%The Z(0) term
%Now, the moved zeros
Z(2)=xi1;
Z(3)=xi2;
Z(4)=xi3;
Z(5)=xi4;
for k=5:N
    %The location of the non-moved zeros as given by Equation 13.
    Z(k+1)=sqrt(A^2+k^2);
end

sigma=(N+1/2)/Z(N+1);

B=zeros(N,1);
for m=0:(N-1)
    %This loop implements Equation 47.
    n=1:(N-1);
    num=prod(1-((m+1/2)./(sigma*Z(1+n))).^2);
    l=[0:(m-1),(m+1):(N-1)];
    denom=prod(1-((m+1/2)./(l+1/2)).^2);
    %We just use C=1.
    B(m+1)=1/(2*1j)*(-1)^m*(m-1/2)^2*num/denom;
end

numPoints=length(xPoints);

if(nargin<4||isempty(a))
    %The maximum distance from the origin to a point is taken to be the
    %radius of the aperture.
    a=sqrt(max(sum(xPoints.^2,1)));
end

%The loop below implements Equation 41 in [1].
g=zeros(numPoints,1);
for curPoint=1:numPoints
    if(abs(xPoints(curPoint))<=a)
        %The normalized radius at this point.
        p=pi*xPoints(curPoint)/a;
        g(curPoint)=g(curPoint)+sum(B.*sin(mu*p));
    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 circle_integrals_test01 ( )

%*****************************************************************************80
%
%% CIRCLE_INTEGRALS_TEST01 tests CIRCLE01_SAMPLE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    12 January 2014
%
%  Author:
%
%    John Burkardt
%
  m = 2;
  n = 4192;
  test_num = 20;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST01\n' );
  fprintf ( 1, '  Use CIRCLE01_SAMPLE to compare exact and\n' );
  fprintf ( 1, '  estimated integrals along the circumference \n' );
  fprintf ( 1, '  of the unit circle in 2D.\n' );
%
%  Get sample points.
%
  seed = 123456789;
  [ x, seed ] = circle01_sample ( n, seed );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of sample points used is %d\n', n );
%
%  Randomly choose X, Y exponents.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  If any exponent is odd, the integral is zero.\n' );
  fprintf ( 1, '  We restrict this test to randomly chosen even exponents.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Ex  Ey     MC-Estimate           Exact      Error\n' );
  fprintf ( 1, '\n' );

  for test = 1 : test_num

    [ e, seed ] = i4vec_uniform_ab ( m, 0, 5, seed );

    e(1:m) = e(1:m) * 2;

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

    result = circle01_length ( ) * sum ( value(1:n) ) / n;
    exact = circle01_monomial_integral ( e );
    error = abs ( result - exact );

    fprintf ( 1, '  %2d  %2d  %14.6g  %14.6g  %10.2e\n', ...
      e(1:m), result, exact, error );

  end

  return
end


function a = kahan_inverse ( alpha, n )

%*****************************************************************************80
%
%% KAHAN_INVERSE returns the inverse of the Kahan matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 October 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real ALPHA, the scalar that defines A.  A typical
%    value is 1.2.  The "interesting" range of ALPHA is 0 < ALPHA < PI.
%
%    Input, integer N, the order of A.
%
%    Output, real A(N,N), the matrix.
%
  ci = cos ( alpha );

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

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

    end
  end
%
%  Scale the columns.
%
  for j = 1 : n
    si = sin ( alpha )^j;
    a(1:n,j) = a(1:n,j) / si;
  end

  return
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  In this script, we perform phase transition analysis
%  of Orthogonal matching pursuit.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;
clc;
rng('default');
% Create the directory for storing results
[status_code,message,message_id] = mkdir('bin');
target_file_path = 'bin/ra_mmv_phase_transition_snr_30db_s_4.mat';
N = 256;
S = 4;
pta = spx.pursuit.PhaseTransitionAnalysis(N);
pta.SNR = 30;

% options for CoSaMP MMV solver
solver_options.RankAwareResidual = true;
P = 2;

% pta.NumTrials = 100;
dict_model = @(M, N) spx.dict.simple.gaussian_dict(M, N);
data_model = @(N, K) spx.data.synthetic.SparseSignalGenerator(N, K, S).gaussian;
recovery_solver = @(Phi, K, y) spx.pursuit.joint.CoSaMP(Phi, K, P, solver_options).solve(y).Z;
pta.run(dict_model, data_model, recovery_solver);
pta.save_results(target_file_path);


function varargout = leon(X)
% Leon funcion
%
%   LEON([x1, x2]) returns the value of the value of the Leon
%   function at the specified points. [x1] and [x2] may be vectors.
%   The search domain is
%
%               -1.2 < x_i < 1.2
%
%   The global minimum is
%
%               f(x1, x2) = f(1, 1) = 0.

% Author: Rody P.S. Oldenhuis
% Delft University of Technology
% E-mail: oldenhuis@dds.nl
% Last edited 20/Jul/2009

    % if no input is given, return dimensions, bounds and minimum
    if (nargin == 0)
        varargout{1} = 2;  % # dims
        varargout{2} = [-1.2, -1.2]; % LB
        varargout{3} = [+1.2, +1.2]; % UB
        varargout{4} = [1,1]; % solution
        varargout{5} = 0; % function value at solution

    % otherwise, output function value
    else

        % keep values inside the search domain
        X(X < -1.2) = inf;      X(X > 1.2) = inf;

        % split input vector X into x1, x2
        if size(X, 1) == 2
            x1 = X(1, :);        x2 = X(2, :);
        else
            x1 = X(:, 1);        x2 = X(:, 2);
        end

        % output function value
        varargout{1} = 100*(x2 - x1.^2).^2 + (1 - x1).^2;
    end

end


function G = get_payoff_G_matrix_from_ygrid_3d( y_1, y_2, y_3, S_0s, sigmas, R, contractParams)
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here

payoff_type = contractParams.payoff_type;

rho12 = R(1,2);
rho23 = R(2,3);
rho13 = R(1,3);

gamma = (rho12*rho13 - rho23)/(1 - rho12^2);

if payoff_type == 1 || payoff_type == 2   % G = S_1, or G=S_2, or G=S_3
    dim = contractParams.dim;
    if dim == 1
        payoff = @(y1,y2,y3)S_0s(1)*exp(sigmas(1)*y1);
    elseif dim == 2
        payoff = @(y1,y2,y3)S_0s(2)*exp(sigmas(2)*(y2 + rho12*y1));
    else
        payoff = @(y1,y2,y3)S_0s(3)*exp(sigmas(3)*(rho13*y1 -gamma*y2 + y3));
    end

elseif payoff_type == 5  % Geometric Basket Call / Put:  G = (sqrt(S_1) * sqrt(S_2) - K)^+ and
    K = contractParams.K;
    if contractParams.call == 1
        payoff = @(y1,y2,y3) max(0, (S_0s(1)*exp(sigmas(1)*y1) * S_0s(2)*exp(sigmas(2)*(y2 + rho12*y1))* S_0s(3)*exp(sigmas(3)*(rho13*y1 -gamma*y2 + y3)))^(1/3) - K);
    else
        payoff = @(y1,y2,y3) max(0, K - (S_0s(1)*exp(sigmas(1)*y1) * S_0s(2)*exp(sigmas(2)*(y2 + rho12*y1))* S_0s(3)*exp(sigmas(3)*(rho13*y1 -gamma*y2 + y3)))^(1/3));
    end

elseif payoff_type == 6  % Arithmetic Basket Call / Put:  G = (sqrt(S_1) * sqrt(S_2) - K)^+
    K = contractParams.K;
    if contractParams.call == 1
        payoff = @(y1,y2,y3) max(0, (1/3)*(S_0s(1)*exp(sigmas(1)*y1) + S_0s(2)*exp(sigmas(2)*(y2 + rho12*y1)) + S_0s(3)*exp(sigmas(3)*(rho13*y1 -gamma*y2 + y3))) - K);
    else
        payoff = @(y1,y2,y3) max(0, K - (1/3)*(S_0s(1)*exp(sigmas(1)*y1) + S_0s(2)*exp(sigmas(2)*(y2 + rho12*y1)) + S_0s(3)*exp(sigmas(3)*(rho13*y1 -gamma*y2 + y3))));
    end

end

m_0 = length(y_1);
G = zeros(m_0, m_0, m_0);

for i=1:m_0
    for j=1:m_0
        for k=1:m_0
            G(i,j,k) = payoff(y_1(i), y_2(j), y_3(k));
        end
    end
end

end


function v=dlyapsq(a,b)
% Solves the discrete Lyapunov equation AV'VA' - V'V +BB' =0
% V is upper triangular with real non-negative diagonal entries
% this is equivalent to v=chol(dlyap(a,b*b')) but better conditioned numerically

%      Copyright (C) Mike Brookes 2002
%      Version: $Id: dlyapsq.m,v 1.4 2007/05/04 07:01:38 dmb Exp $
%
%   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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[q,s]=schur(a');
[q,s]=rsf2csf(q,s);
[qd,r]=qr(b'*q,0);
% save r for testing
r0=r;
[m,n]=size(r);
u=zeros(n,n);
if m==1
   for i=1:n-1
      in=i+1:n;
      si=s(i,i);
      aa=sqrt(1-si*si');
      u(i,i)=r(1)/aa;
      u(i,in)=(u(i,i)*si'*s(i,in)+aa*r(2:end))/(eye(n-i)-si'*s(in,in));
      r=aa*(u(i,i)*s(i,in)+u(i,in)*s(in,in))-si*r(2:end);
   end
   u(n,n)=r/sqrt(1-s(n,n)*s(n,n)');

else
   w=zeros(m,1); w(m)=1;
   em=eye(m);
   for i=1:n-m
      in=i+1:n;
      si=s(i,i);
      aa=sqrt(1-si*si');
      u(i,i)=r(1,1)/aa;
      u(i,in)=(u(i,i)*si'*s(i,in)+aa*r(1,2:end))/(eye(n-i)-si'*s(in,in));
      vv=aa*(u(i,i)*s(i,in)+u(i,in)*s(in,in))-si*r(1,2:end);
      rr=zeros(m,n-i);
      rr(1:m-1,:)=r(2:end,2:end);
      [qq,r]=qrupdate(em,rr,w,vv');
   end
   for i=n-m+1:n-1
      in=i+1:n;
      si=s(i,i);
      aa=sqrt(1-si*si');
      u(i,i)=r(1,1)/aa;
      u(i,in)=(u(i,i)*si'*s(i,in)+aa*r(1,2:end))/(eye(n-i)-si'*s(in,in));
      vv=aa*(u(i,i)*s(i,in)+u(i,in)*s(in,in))-si*r(1,2:end);
      rr=zeros(n-i+1,n-i);
      rr(1:n-i,:)=r(2:end,2:end);
      [qq,rr]=qrupdate(eye(n-i+1),rr,w(m-n+i:end),vv');
      r=rr(1:n-i,:);
   end

   u(n,n)=r/sqrt(1-s(n,n)*s(n,n)');

end

v=triu(qr(u*q'));
dv=diag(v);
ix=dv~=0;
v(ix,:)=diag(abs(dv(ix))./dv(ix))*v(ix,:);
if isreal(a) & isreal(b)
   v=real(v);
end


function L = log_prob_node(CPD, self_ev, pev)
% LOG_PROB_NODE Compute prod_m log P(x(i,m)| x(pi_i,m), theta_i) for node i (gaussian)
% L = log_prob_node(CPD, self_ev, pev)
%
% self_ev(m) is the evidence on this node in case m.
% pev(i,m) is the evidence on the i'th parent in case m (if there are any parents).
% (These may also be cell arrays.)

if iscell(self_ev), usecell = 1; else usecell = 0; end

use_log = 1;
ncases = length(self_ev);
nparents = length(CPD.sizes)-1;
assert(ncases == size(pev, 2));

if ncases == 0
  L = 0;
  return;
end

L = 0;
for m=1:ncases
  if isempty(CPD.dps)
    i = 1;
  else
    if usecell
      dpvals = cat(1, pev{CPD.dps, m});
    else
      dpvals = pev(CPD.dps, m);
    end
    i = subv2ind(CPD.sizes(CPD.dps), dpvals(:)');
  end
  if usecell
    y = self_ev{m};
  else
    y = self_ev(m);
  end
  if length(CPD.cps) == 0
    L = L + gaussian_prob(y, CPD.mean(:,i), CPD.cov(:,:,i), use_log);
  else
    if usecell
      x = cat(1, pev{CPD.cps, m});
    else
      x = pev(CPD.cps, m);
    end
    L = L + gaussian_prob(y, CPD.mean(:,i) + CPD.weights(:,:,i)*x, CPD.cov(:,:,i), use_log);
  end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Q = T2quaternion2(T)
%   Returns the quaternion corresponding to an homogeneous transformation
%   matrix T. Only the 3x3 rotation matrix in T is used.
%
%   See also QPROD, QUATERNION2T.
% The method implemented here was extracted from:
% Accurate Computation of Quaternions from Rotation Matrices.
% Soheil Sarabandi and Federico Thomas
%  http://www.iri.upc.edu/files/scidoc/2068-Accurate-Computation-of-Quaternions-from-Rotation-Matrices.pdf
%   Author: Arturo Gil. Universidad Miguel Hern�ndez de Elche. email:
%   arturo.gil@umh.es date:   21/04/2012
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Copyright (C) 2012, by Arturo Gil Aparicio
%
% This file is part of ARTE (A Robotics Toolbox for Education).
%
% ARTE is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% ARTE is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with ARTE.  If not, see <http://www.gnu.org/licenses/>.
function Q = T2quaternion2(T)
% use only the orientation from T
R = T(1:3, 1:3);

if R(1,1) + R(2,2) + R(3,3) > 0
    Q(1) = 0.5*sqrt(1+R(1,1) + R(2,2) + R(3,3));
else
    num = (R(3,2)-R(2,3))^2 + (R(1,3)-R(3,1))^2 + (R(2,1)-R(1,2))^2;
    den = 3 - R(1,1) - R(2,2) - R(3,3);
    Q(1) = 0.5*sqrt(num/den);
end

if R(1,1) - R(2,2) - R(3,3) > 0
    Q(2) = 0.5*sqrt(1 + R(1,1) - R(2,2) - R(3,3));
else
    num = (R(3,2)-R(2,3))^2 + (R(1,3)+R(3,1))^2 + (R(2,1)+R(1,2))^2;
    den = 3 - R(1,1) + R(2,2) + R(3,3);
    Q(2) = 0.5*sqrt(num/den);
end

if -R(1,1) + R(2,2) - R(3,3) > 0
    Q(3) = 0.5*sqrt(1 - R(1,1) + R(2,2) - R(3,3));
else
    num = (R(3,2)+R(2,3))^2 + (R(1,3)-R(3,1))^2 + (R(2,1)+R(1,2))^2;
    den = 3 + R(1,1) - R(2,2) + R(3,3);
    Q(3) = 0.5*sqrt(num/den);
end

if -R(1,1) - R(2,2) + R(3,3) > 0
    Q(4) = 0.5*sqrt(1 - R(1,1) - R(2,2) + R(3,3));
else
    num = (R(3,2)+R(2,3))^2 + (R(1,3)+R(3,1))^2 + (R(2,1)-R(1,2))^2;
    den = 3 + R(1,1) + R(2,2) + R(3,3);
    Q(4) = 0.5*sqrt(num/den);
end


%computes the spectral spread from the magnitude spectrum
%> called by ::ComputeFeature
%>
%> @param X: spectrogram (dimension FFTLength X Observations)
%> @param f_s: sample rate of audio data
%>
%> @retval vss spectral spread (in Hz)
% ======================================================================
function [vss] = FeatureSpectralSpread (X, f_s)

    % get spectral centroid as index
    vsc = FeatureSpectralCentroid(X, f_s) * 2 / f_s * (size(X, 1)-1);

    % allocate memory
    vss = zeros(size(vsc));

    % compute spread
    for n = 1:size(X,2)
        vss(n) = (((0:size(X, 1)-1)-vsc(n)).^2 * X(:, n)) ./ sum(X(:, n));
    end
    vss = sqrt(vss);

    % convert from index to Hz
    vss = vss / (size(X, 1)-1) * f_s / 2;

    % avoid NaN for silence frames
    vss (sum(X, 1) == 0) = 0;
end


function overlap = calcRectInt(A,B)
%
%each row is a rectangle.
% A(i,:) = [x y w h]
% B(j,:) = [x y w h]
% overlap(i,j) = area of intersection
% normoverlap(i,j) = overlap(i,j) / (area(i)+area(j)-overlap)
%
% Same as built-in rectint, but faster and uses less memory (since avoids repmat).

lena = size(A, 1);
lenb = size(B, 1);
lena = min(lena, lenb);
A = A(1:lena, :);
B = B(1:lena, :);
leftA = A(:,1);
bottomA = A(:,2);
rightA = leftA + A(:,3) - 1;
topA = bottomA + A(:,4) - 1;

leftB = B(:,1);
bottomB = B(:,2);
rightB = leftB + B(:,3) - 1;
topB = bottomB + B(:,4) - 1;

tmp = (max(0, min(rightA, rightB) - max(leftA, leftB)+1 )) .* (max(0, min(topA, topB) - max(bottomA, bottomB)+1 ));
    areaA = A(:,3) .* A(:,4);
    areaB = B(:,3) .* B(:,4);
    overlap = tmp./(areaA+areaB-tmp);
% if tmp > 0
%
%     overlap = tmp;
%
%     areaA = A(3) .* A(4);
%     areaB = B(3) .* B(4);
%     overlap = tmp./(areaA+areaB-tmp);
% else
%     overlap = 0;
% end


function [FaceArea, VertArea] = tess_area(Vertices, Faces)
% TESS_AREA: Compute the surface area associated with each face and each vertex.

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2012-2016

% Compute the area of all the faces
r12 = Vertices(Faces(:,1),:);        % temporary holding
r13 = Vertices(Faces(:,3),:) - r12;  % negative of r31
r12 = Vertices(Faces(:,2),:) - r12;  % from 1 to 2
FaceArea = sqrt(sum(bst_cross(r12,r13,2).^2, 2)) / 2;

% Compute the triangle area only if needed
if (nargout >= 2)
    % Build vertex-face connectivity matrix, with the area information
    nFaces = size(Faces,1);
    rowno = double([Faces(:,1); Faces(:,2); Faces(:,3)]);
    colno = [1:nFaces, 1:nFaces, 1:nFaces]';
    data  = [FaceArea; FaceArea; FaceArea];
    VertFacesArea = sparse(rowno,colno,data);

    % Compute the vertex area: 1/3 of each triangle involving this vertex
    VertArea = 1/3 * full(sum(VertFacesArea,2));
end


% This test example is taken from demo_gabmulappr

% Setup parameters for the Gabor system and length of the signal
L=576; % Length of the signal
a=32;   % Time shift
M=72;  % Number of modulations
N=L/a;
fs=44100; % assumed sampling rate
SNRtv=63; % signal to noise ratio of change rate of time-variant system

% construction of slowly time variant system
% take an initial vector and multiply by random vector close to one
A = [];
c1=(1:L/2); c2=(L/2:-1:1); c=[c1 c2].^(-1); % weight of decay x^(-1)
A(1,:)=(tester_rand(1,L)-0.5).*c;  % convolution kernel
Nlvl = exp(-SNRtv/10);
Slvl = 1-Nlvl;
for ii=2:L;
     A(ii,:)=(Slvl*circshift(A(ii-1,:),[0 1]))+(Nlvl*(tester_rand(1,L)-0.5));
end;
A = A/norm(A)*0.99; % normalize matrix

% perform best approximation by gabor multiplier
g=gabtight(a,M,L);
sym1=gabmulappr(A,g,a,M);

% Now do the same using the general frame algorithm.

F=frame('dgt',g,a,M);

sym2=framemulappr(F,F,A);

norm(sym1-reshape(sym2,M,N))

% Test for exactness

testsym=tester_crand(M,N);
FT=frsynmatrix(F,L);

T=FT*diag(testsym(:))*FT';

sym1b=gabmulappr(T,g,a,M);
sym2b=framemulappr(F,F,T);

norm(testsym-sym1b)
norm(testsym-reshape(sym2b,M,N))


% LOWPASSFILTER - Constructs a low-pass butterworth filter.
%
% usage: f = lowpassfilter(sze, cutoff, n)
%
% where: sze    is a two element vector specifying the size of filter
%               to construct [rows cols].
%        cutoff is the cutoff frequency of the filter 0 - 0.5
%        n      is the order of the filter, the higher n is the sharper
%               the transition is. (n must be an integer >= 1).
%               Note that n is doubled so that it is always an even integer.
%
%                      1
%      f =    --------------------
%                              2n
%              1.0 + (w/cutoff)
%
% The frequency origin of the returned filter is at the corners.
%
% See also: HIGHPASSFILTER, HIGHBOOSTFILTER, BANDPASSFILTER
%

% Copyright (c) 1999 Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% http://www.csse.uwa.edu.au/
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.

% October 1999
% August  2005 - Fixed up frequency ranges for odd and even sized filters
%                (previous code was a bit approximate)

function f = lowpassfilter(sze, cutoff, n)

    if cutoff < 0 | cutoff > 0.5
	error('cutoff frequency must be between 0 and 0.5');
    end

    if rem(n,1) ~= 0 | n < 1
	error('n must be an integer >= 1');
    end

    if length(sze) == 1
	rows = sze; cols = sze;
    else
	rows = sze(1); cols = sze(2);
    end

    % Set up X and Y matrices with ranges normalised to +/- 0.5
    % The following code adjusts things appropriately for odd and even values
    % of rows and columns.
    if mod(cols,2)
	xrange = [-(cols-1)/2:(cols-1)/2]/(cols-1);
    else
	xrange = [-cols/2:(cols/2-1)]/cols;
    end

    if mod(rows,2)
	yrange = [-(rows-1)/2:(rows-1)/2]/(rows-1);
    else
	yrange = [-rows/2:(rows/2-1)]/rows;
    end

    [x,y] = meshgrid(xrange, yrange);
    radius = sqrt(x.^2 + y.^2);        % A matrix with every pixel = radius relative to centre.
    f = ifftshift( 1.0 ./ (1.0 + (radius ./ cutoff).^(2*n)) );   % The filter


function term  = sdlfmjMeanCompute(sdlfmKern, t , option)

% SDLFMJMEANCOMPUTE Jolt mean for the switching dynamical LFM model.
% Computes the terms $r_d$ and $q_d$ that appear in the mean function
% associated with the switching dynamical LFM model. If the mean function
% is mu(t), then
%
%     mu(t) = r_d(t)y_d(t_0) + q_d(t)\dot{y}_d(t_0),
%
% where $y_d(t_0)$ is the initial condition associated to the position and
% $\dot{y}_d(t_0)$ is the initial condition associated to the velocity.
%
% FORMAT
% DESC
% ARG sdlfmKern : switching dynamical LFM kernel structure with the
% parameters.
% ARG t : input times for which the mean is to be computed.
% RETURN term : the value of $r_d$.
%
% FORMAT
% DESC
% Computes the terms that appear in the mean function associated with the
% switching dynamical LFM model.
% ARG sdlfmKern : switching dynamical LFM kernel structure with the
% parameters.
% ARG t : input times for which the mean is to be computed.
% ARG option : indicates which term of the mean should be computed. Option
% 'Pos' computes the term $r_d$ and option 'Vel' computes $q_d$ that
% accompanies the initial condition of the velocity.
% RETURN term : the value of $r_d$ or $q_d$ depending of option.
%
% COPYRIGHT : Mauricio A. Alvarez, 2010

% KERN

if nargin < 3
    option = 'Pos';
end

alpha = sdlfmKern.damper/(2*sdlfmKern.mass);
omega = sqrt(sdlfmKern.spring/sdlfmKern.mass-alpha^2);
freq = omega*t;

switch option
    case 'Pos'
        term = (alpha^2/omega + omega)*exp(-alpha*t).*...
            ((omega^2 - alpha^2)*sin(freq) + 2*alpha*omega*cos(freq));
    case 'Vel'
        term = exp(-alpha*t).*((3*alpha*omega - alpha^3/omega)*sin(freq)...
            +(3*alpha^2- omega^2)*cos(freq));
    otherwise
     error('No recognized option')
end


% LOGNORMAL_LOGPDF
%
% Y = LOGNORMAL_LOGPDF(X)
% Y = LOGNORMAL_LOGPDF(X, MU, SIGMA)
%
% MU is the location parameter in (-INF, INF), default 0
% SIGMA is the scale parameter in (0, INF)

% Last modified 2010-11-12
% Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi)

function y = lognormal_logpdf(x, varargin)

if nargin <= 3
  if nargin == 1
    mu = 0;
    sigma = 1;
  else
    mu = varargin{1};
    sigma = varargin{2};
  end
  logx = log(x);
  y = -0.5*log(2*pi) ...
      -logx ...
      -log(sigma) ...
      -0.5*((logx-mu)./sigma).^2;
end


function [W] = amuse(X)
% BSS using eigenvalue value decomposition
% Program written by A. Cichocki and R. Szupiluk
%
% X [m x N] matrix of observed (measured) signals,
% W separating matrix,
% y estimated separated sources
% p time delay used in computation of covariance matrices
% optimal time-delay default p= 1
%
% First stage: Standard prewhitening

[m,N]=size(X);
if nargin==1,
 n=m; %
 end;

Rxx=(X*X')/N;

[Ux,Dx,Vx]=svd(Rxx);
 Dx=diag(Dx);
% n=xxx;
 if n<m, % under assumption of additive white noise and
        %when the number of sources are known or can a priori estimated
  Dx=Dx-real((mean(Dx(n+1:m))));
  Q= diag(real(sqrt(1./Dx(1:n))))*Ux(:,1:n)';
%
else    % under assumption of no additive noise and when the
        % number of sources is unknown
   n=max(find(Dx>1e-199)); %Detection the number of sources
   Q= diag(real(sqrt(1./Dx(1:n))))*Ux(:,1:n)';
end;
%
% else    %assumes no noise
%    Q=inv(sqrtm(Rxx));
% end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Second stage: Fast separation using sorting EVD
% notation the same as used in the Chapter 4
Xb=Q*X;
p=1;
% paramter p can take here value different than 1
% for example -1 or 2.
N=max(size(Xb));
Xb=Xb-kron(mean(Xb')',ones(1,N));

Rxbxbp=(Xb(:,1:N-1)*Xb(:,2:N)')/(N-1);
Rxbxbp= Rxbxbp+Rxbxbp';
[Vxb Dxb]=eig(Rxbxbp);
[D1 perm]=sort(diag(Dxb));
D1=flipud(D1);
Vxb=Vxb(:,flipud(perm));
W = Vxb'*Q;
%y = Vxb' * x1;


%NMC Nearest Mean Classifier
%
%   W = NMC(A)
%   W = A*NMC
%
% INPUT
%   A    Dataset
%
% OUTPUT
%   W    Nearest Mean Classifier
%
% DESCRIPTION
% Computation of the nearest mean classifier between the classes in the
% dataset A.  The use of soft labels is supported. Prior probabilities are
% not used.
%
% The difference with NMSC is that NMSC is based on an assumption of normal
% distributions and thereby automatically scales the features and is
% sensitive to class priors. NMC is a plain nearest mean classifier for
% which the assigned classes are are sensitive to feature scaling and
% unsensitive to class priors.
%
% The estimated class confidences by B*NMC(A), however, are based on
% assumed spherical gaussian distributions of the same size.
%
% SEE ALSO (PRTools Guide)
% DATASETS, MAPPINGS, NMSC, LDC, FISHERC, QDC, UDC

% Copyright: R.P.W. Duin, r.p.w.duin@37steps.com
% Faculty EWI, Delft University of Technology
% P.O. Box 5031, 2600 GA Delft, The Netherlands

% $Id: nmc.m,v 1.14 2009/12/09 15:49:54 duin Exp $

function W = nmc(varargin)

	mapname = 'NearestMean';
  argin = setdefaults(varargin,[],false);

  if mapping_task(argin,'definition')
    W = define_mapping(argin,'untrained',mapname);

  elseif mapping_task(argin,'training')			% Train a mapping.

    [a,flag] = deal(argin{:});
    % flag = true forces execution by knn_map instead of normal_map
    % flag = false (defaults) finds posteriors by spherical normal
    % distributions

    if nargin < 2, flag = false; end
    % flag=true forces to skip computation of posteriors based on normal
    % densities

    if nargin < 1 | isempty(a)
      W = prmapping(mfilename);
      W = setname(W,'NearestMean');
      return
    end

    islabtype(a,'crisp','soft');
    isvaldfile(a,1,2); % at least 1 object per class, 2 classes

    [m,k,c] = getsize(a);
    if isdatafile(a), a = setfeatsize(a,k); end

    if ~flag & c == 2      % 2-class case: store linear classifier
      u = meancov(a);
      u1 = +u(1,:);
      u2 = +u(2,:);
      R = [u1-u2]';
      offset =(u2*u2' - u1*u1')/2;
      W = affine([R -R],[offset -offset],a,getlablist(a));
      W = cnormc(W,a);
      W = setname(W,'Nearest Mean');
    else
      if ~flag & all(classsizes(a) > 1)
        a = setprior(a,0);  % NMC should be independent of priors: make them equal
        p = getprior(a);
        U = zeros(c,k);
        V = zeros(c,k);
        s = sprintf('NMC: Processing %i classes: ',c);
        prwaitbar(c,s);
        for j=1:c
          prwaitbar(c,j,[s int2str(j)]);
          b = seldat(a,j);
          [v,u] = var(b);
          U(j,:) = +u;
          V(j,:) = +v;
        end
        prwaitbar(0);
        %G = mean(V'*p') * eye(k);
        G = mean(V(:));
        w.mean = +U;
        w.cov = G;
        w.prior =p;
        W = normal_map(w,getlablist(a),k,c);
        W = setname(W,'Nearest Mean');
        W = setcost(W,a);
      else
        u = meancov(a);
        W = knnc(u,1);
        W = setname(W,mapname);
      end

    end

    W = setcost(W,a);

  end

	return


function pass = test_integral(pref)
% Test path integral for 3D functions.

if ( nargin == 0)
    pref = chebfunpref;
end
tol = 1e3*pref.cheb3Prefs.chebfun3eps;

f = chebfun3(@(x,y,z) 2*x.*y+3*z);
curve = chebfun(@(t) [2*t 5*t 4*t], [0, 1]);
exact = 38*sqrt(5);
pass(1) = abs(integral(f, curve) - exact)/exact < tol;

f = chebfun3(@(x,y,z) x.^2.*z);
curve = chebfun(@(t) [-t 6+2*t 2+5*t], [0 1]);
exact = 23*sqrt(30)/12;
pass(2) = abs(integral(f, curve) - exact )/exact < tol;

% http://tutorial.math.lamar.edu/Classes/CalcIII/LineIntegralsPtI.aspx
dom = [-1 1 -1 1 0 12*pi];
f = chebfun3(@(x,y,z) x.*y.*z, dom);
curve = chebfun(@(t) [cos(t) sin(t) 3*t], [0 4*pi]);
exact = -3*sqrt(10)*pi;
pass(3) = abs(integral(f, curve) - exact )/exact < tol;

end


function point = cart2geod(src, curve)
%CART2GEOD Convert cartesian coordinates to geodesic coord.
%
%   PT2 = cart2geod(PT1, CURVE)
%   PT1 is the point to transform, in Cartesian coordinates (same system
%   used for the curve).
%   CURVE is a N-by-2 array which represents coordinates of curve vertices.
%
%   The function first compute the projection of PT1 on the curve. Then,
%   the first geodesic coordinate is the length of the curve to the
%   projected point, and the second geodesic coordinate is the
%   distance between PT1 and it projection.
%
%
%   TODO : add processing of points not projected on the curve.
%   -> use the closest end
%
%   See also
%   polylines2d, geod2cart, curveLength
%

% ---------
% Author: David Legland
% e-mail: david.legland@grignon.inra.fr
% created the 08/04/2004.
% Copyright 2010 INRA - Cepia Software Platform.

%   HISTORY
%   15/02/2007 replace minDistance by minDistancePoints

% parametrization approximation
t = parametrize(curve);

% compute distance between each src point and the curve
[dist, ind] = minDistancePoints(src, curve);

% convert to 'geodesic' coordinate
point = [t(ind) dist];

% Old version:
% for i=1:size(pt1, 1)
%     [dist, ind] = minDistance(src(i,:), curve);
%     point(i,:) = [t(ind) dist];
% end


function [U,c] = MgnCalibration(X)
% performs magnetometer calibration from a set of data
% using Merayo technique with a non iterative algoritm
% J.Merayo et al. "Scalar calibration of vector magnemoters"
% Meas. Sci. Technol. 11 (2000) 120-132.
%
%   X      : a Nx3 (or 3xN) data matrix
%              each row (columns) contains x, y, z measurements
%              N must be such that the data set describes
%              as completely as possible the 3D space
%              In any case N > 10
%
%    The calibration tries to find the best 3D ellipsoid that fits the data set
%    and returns the parameters of this ellipsoid
%
%    U     :  shape ellipsoid parameter, (3x3) upper triangular matrix
%    c      : ellipsoid center, (3x1) vector
%
%    Ellipsoid equation : (v-c)'*(U'*U)(v-c) = 1
%    with v a rough triaxes magnetometer  measurement
%
%    calibrated measurement w = U*(v-c)
%
%   author : Alain Barraud, Suzanne Lesecq 2008
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[N,m] = size(X);
if m>3&&N==3,X = X';N = m;m = 3;end;%check that X is not transposed
if N<=10,U = [];c = [];return;end;%not enough data no calibration !!
% write  the ellipsoid equation as D*p=0
% the best parameter is the solution of min||D*p|| with ||p||=1;
% form D matrix from X measurements
x = X(:,1); y = X(:,2); z = X(:,3);
D = [x.^2, y.^2, z.^2, x.*y, x.*z, y.*z, x, y, z, ones(N,1)];
D=triu(qr(D));%avoids to compute the svd of a large matrix
[U,S,V] = svd(D);%because usually N may be very large
p = V(:,end);if p(1)<0,p =-p;end;
% the following matrix A(p) must be positive definite
% The optimization done by svd does not include such a constraint
% With "good" data the constraint is allways satisfied
% With too poor data A may fail to be positive definite
% In this case the calibration fails
%
A = [p(1) p(4)/2 p(5)/2;
       p(4)/2 p(2) p(6)/2;
       p(5)/2 p(6)/2 p(3)];
[U,ok] = fchol(m,A);
if ~ok,U = [];c = [];return;end%calibration fails too poor data!!
b = [p(7);p(8);p(9)];
v = Utsolve(U,b/2,m);
d = p(10);
s = 1/sqrt(v*v'-d);
c =-Usolve(U,v,m)';%ellipsoid center
U = s*U;%shape ellipsoid parameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [A,ok] = fchol(n,A)
% performs Cholesky factoristation
A(1,1:n) = A(1,1:n)/sqrt(A(1,1));
A(2:n,1) = 0;
for j=2:n
  A(j,j:n) = A(j,j:n) - A(1:j-1,j)'*A(1:j-1,j:n);
  if A(j,j)<=0,ok=0;break;end%A is not positive definite
  A(j,j:n) = A(j,j:n)/sqrt(A(j,j));
  A(j+1:n,j) = 0;
end
ok=1;
function x=Utsolve(U,b,n)
% solves U'*x=b
x(1) = b(1)/U(1,1);
for k=2:n
    x(k) = (b(k)-x(1:k-1)*U(1:k-1,k))/U(k,k);
end
function x=Usolve(U,b,n)
% solves U*x=b
x(n) = b(n)/U(n,n);
for k=n-1:-1:1
    x(k) = (b(k)-U(k,k+1:n)*x(k+1:n)')/U(k,k);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function area = polyArea(v,varargin)
% area of a flat polygon given by vertices v1, v2, ..., v_n
%
% Input
%  v - @vector3d
%
% Output
%  area - area the polygon v1, v2, ..., v_n
%

nv = length(v);
if nv<3, area = 0; return; end

% formula from http://geomalgorithms.com/a01-_area.html
v1 = v.subSet(1);
N = normalize(mean(cross(v.subSet(2)-v1,v.subSet(3:nv-1)-v1)));

area = dot(N, sum(cross(v.subSet(1:nv-1),v.subSet(2:nv))))./2;


function[f,name]=tidefreq(str)
%TIDEFREQ  Frequencies of the eight major tidal components.
%
%   [F,NAME]=TIDEFREQ returns the frequencies of the eight major tidal
%   components together with a string matrix containing their names.  In
%   order of increasing frequency, these are
%
%        Mf O1 P1 K1 N2 M2 S2 K2.
%
%   The units are given in *radians* per hour. See Gill (1982) page 335.
%
%   F=TIDEFREQ(STR) returns the frequency for the component named STR,
%   if STR is a string.  F=TIDEFREQ(N) where N is a number also works.
%
%   Usage:   [f,name]=tidefreq;
%            f=tidefreq(str);
%   __________________________________________________________________
%   This is part of JLAB --- type 'help jlab' for more information
%   (C) 2005--2015 J.M. Lilly --- type 'help jlab_license' for details

name=('MFO1P1K1N2M2S2K2');

%Gill only gives first two decimal points
%I got the more exact numbers from
%http://oceanworld.tamu.edu/resources/ocng_textbook/chapter17/chapter17_04.htm

p(1)=327.85;
p(2)=25.8194;
p(3)=24.0659;
p(4)=23.9344;
p(5)=12.6584;
p(6)=12.4206;
p(7)=12.0000;
p(8)=11.9673;

f=1./p;
if nargin==1
  if ~ischar(str)
    f=f(str);
  else
    str=upper(str);
    ii=strfind(name,str);
    f=f((ii-1)/2+1);
  end
  clear name
else
  name=reshape(name,2,8)';
end

f=f*2*pi;


function [pTS,fmin]=grTravSale(C)
% Function [pTS,fmin]=grTravSale(C) solve the nonsymmetrical
% traveling salesman problem.
% Input parameter:
%   C(n,n) - matrix of distances between cities,
%     maybe, nonsymmetrical;
%     n - number of cities.
% Output parameters:
%   pTS(n) - the order of cities;
%   fmin - length of way.
% Uses the reduction to integer LP-problem:
% Look: Miller C.E., Tucker A. W., Zemlin R. A.
% Integer Programming Formulation of Traveling Salesman Problems.
% J.ACM, 1960, Vol.7, p. 326-329.
% Needed other products: MIQP.M.
% This software may be free downloaded from site:
% http://control.ee.ethz.ch/~hybrid/miqp/
% Author: Sergiy Iglin
% e-mail: siglin@yandex.ru
% personal page: http://iglin.exponenta.ru

% ============= Input data validation ==================
if nargin<1,
  error('There are no input data!')
end
if ~isnumeric(C),
  error('The array C must be numeric!')
end
if ~isreal(C),
  error('The array C must be real!')
end
s=size(C); % size of array C
if length(s)~=2,
  error('The array C must be 2D!')
end
if s(1)~=s(2),
  error('Matrix C must be square!')
end
if s(1)<3,
  error('Must be not less than 3 cities!')
end

% ============ Size of problem ====================
n=s(1); % number of vertexes
m=n*(n-1); % number of arrows

% ============ Parameters of integer LP problem ========
Aeq=[]; % for the matrix of the boundary equations
for k1=1:n,
  z1=zeros(n);
  z1(k1,:)=1;
  z2=[z1;eye(n)];
  Aeq=[Aeq z2([1:2*n-1],setdiff([1:n],k1))];
end
Aeq=[Aeq zeros(2*n-1,n-1)];
A=[]; % for the matrix of the boundary inequations
for k1=2:n,
  z1=[];
  for k2=1:n,
    z2=eye(n)*(n-1)*(k2==k1);
    z1=[z1 z2(setdiff([2:n],k1),setdiff([1:n],k2))];
  end
  z2=-eye(n);
  z2(:,k1)=z2(:,k1)+1;
  A=[A;[z1 z2(setdiff([2:n],k1),2:n)]];
end
beq=ones(2*n-1,1); % the right parts of the boundary equations
b=ones((n-1)*(n-2),1)*(n-2); % the right parts of the boundary inequations
C1=C'+diag(ones(1,n)*NaN);
C2=C1(:);
c=[C2(~isnan(C2));zeros(n-1,1)]; % the factors for objective function
vlb=[zeros(m,1);-inf*ones(n-1,1)]; % the lower bounds
vub=[ones(m,1);inf*ones(n-1,1)]; % the upper bounds
H=zeros(n^2-1); % Hessian

% ============= We solve the MIQP problem ==========
[xmin,fmin]=MIQP(H,c,A,b,Aeq,beq,[1:m],vlb,vub);

% ============= We return the results ==============
eik=round(xmin(1:m)); % the arrows of the way
e1=[zeros(1,n-1);reshape(eik,n,n-1)];
e2=[e1(:);0]; % we add zero to a diagonal
e3=(reshape(e2,n,n))'; % the matrix of the way
pTS=[1 find(e3(1,:))]; % we build the way
while pTS(end)>1, % we add the city to the way
  pTS=[pTS find(e3(pTS(end),:))];
end
return


% Test how accurately we can orthogonalize ill-conditioned bases of tangent
% vectors. We test three metrics:
%  1) Is Q actually orthonormal (the figure displays log(|Q'Q|); since Q'Q
%     should be identity, we hope to see 0 on diagonal and -inf everywhere
%     else.)
%  2) Is A = QR? This is in the title of the plots.
%  3) How much time does it take to compute?
%
% Nicolas Boumal, Oct. 5, 2017

clear all;
clc;

M = spherefactory(100);

% M = stiefelcomplexfactory(200, 56);
% M = productmanifold(struct('S', spherefactory(5), 'R', rotationsfactory(3, 10)));

x = M.rand();

% Create a poorly conditioned basis
A = cell(M.dim()-5, 1);
A{1} = M.randvec(x);
for k = 2 : numel(A)
    A{k} = M.lincomb(x, 1, A{k-1}, 1e-6, M.randvec(x));
end

t1 = tic();
[Q1, R1] = orthogonalize(M, x, A);
t1 = toc(t1);

t2 = tic();
[Q2, R2] = orthogonalizetwice(M, x, A);
t2 = toc(t2);

t3 = tic();
[Q3, R3] = orthogonalize_legacy(M, x, A);
t3 = toc(t3);

% To check if Q is really orthonormal
G1 = grammatrix(M, x, Q1);
G2 = grammatrix(M, x, Q2);
G3 = grammatrix(M, x, Q3);

% To check of QR = A
distsq = 0;
for k = 1 : numel(A)
    distsq = distsq + M.norm(x, M.lincomb(x, 1, lincomb(M, x, Q1(1:k), R1(1:k, k)), -1, A{k}))^2;
end
dist1 = sqrt(distsq);

distsq = 0;
for k = 1 : numel(A)
    distsq = distsq + M.norm(x, M.lincomb(x, 1, lincomb(M, x, Q2(1:k), R2(1:k, k)), -1, A{k}))^2;
end
dist2 = sqrt(distsq);

distsq = 0;
for k = 1 : numel(A)
    distsq = distsq + M.norm(x, M.lincomb(x, 1, lincomb(M, x, Q3(1:k), R3(1:k, k)), -1, A{k}))^2;
end
dist3 = sqrt(distsq);

subplot(1, 3, 1);
imagesc(log10(abs(G1))); axis equal, axis tight; colorbar; set(gca, 'CLim', [-20, 0]);
title(sprintf('MGS: CPU time: %g\n||A - QR|| = %g', t1, dist1));
subplot(1, 3, 2);
imagesc(log10(abs(G2))); axis equal, axis tight; colorbar; set(gca, 'CLim', [-20, 0]);
title(sprintf('MGS-twice: CPU time: %g\n||A - QR|| = %g', t2, dist2));
subplot(1, 3, 3);
imagesc(log10(abs(G3))); axis equal, axis tight; colorbar; set(gca, 'CLim', [-20, 0]);
title(sprintf('Legacy: CPU time: %g\n||A - QR|| = %g', t3, dist3));


function [c, ceq] = unitdisk(x)
% Unit disk. The vector x should be a row vector. This is to test the
% non-linear constraint functionality of pso.
%
% Non-linear constraints such that
% c(x) <= 0
% ceq(x) = 0
% Within a tolerance of options.TolCon

c = x*x' - 1 ;
ceq = [] ;


function box_display_test06 ( )

%*****************************************************************************80
%
%% BOX_DISPLAY_TEST06 compares several methods that reach degree 4 in X, 8 in Y.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    10 January 2014
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'BOX_DISPLAY_TEST06:\n' );
  fprintf ( 1, '  Plot index sets for various anisotropic methods\n' );
  fprintf ( 1, '  that reach degree 4 in X, 8 in Y.\n' );
  fprintf ( 1, '\n' );

  m = 10;
  n = 10;
  title_string = '';
%
%  Total degree <= 8.
%
  box_display ( m, n, @(x,y)x+y<=-1, @(x,y)2*x+y<=8, title_string );
  filename = 'degree48_total.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Created "%s".\n', filename );
%
%  Maximum degree <= 8.
%
  box_display ( m, n, @(x,y)x+y<=-1, @(x,y)max(2*x,y)<=8, title_string );
  filename = 'degree48_max.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Created "%s".\n', filename );
%
%  Hyperbolic cross.
%
  box_display ( m, n, @(x,y)x+y<=-1, @(x,y)(2*x+1)*(y+1)<=9, title_string );
  filename = 'degree48_hyper.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Created "%s".\n', filename );
%
%  Clenshaw-Curtis sparse grid.
%
% box_display ( m, n, @(x,y)x+y<=-1, @cc_aniso, title_string );
% filename = 'degree48_cc.png';
% print ( '-dpng', filename );
% fprintf ( 1, '  Created "%s".\n', filename );
%
%  Smolyak: Log2(2*x) + Log2(y) <= Log2(L)
%
  box_display ( m, n, @(x,y)x+y<=-1, @(x,y)log2(max(2*x,1))+log2(max(y,1))<=log2(8.1), title_string );
  filename = 'degree48_smolyak.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Created "%s".\n', filename );

  return
end


function b = isPointOnLine3d(point, line, varargin)
%ISPOINTONLINE3D Test if a 3D point belongs to a 3D line.
%
%   B = isPointOnLine3d(POINT, LINE)
%   with POINT being [xp yp zp], and LINE being [x0 y0 z0 dx dy dz].
%   Returns 1 if point lies on the line, 0 otherwise.
%
%   If POINT is an N-by-3 array of points, B is a N-by-1 array of booleans.
%
%   If LINE is a N-by-6 array of lines, B is a N-by-1 array of booleans.
%
%   B = isPointOnLine3d(POINT, LINE, TOL)
%   Specifies the tolerance used for testing location on 3D line.
%
%   See also
%   lines3d, distancePointLine3d, linePosition3d, isPointOnLine
%

% ------
% Author: David Legland
% E-mail: david.legland@inra.fr
% Created: 2003-10-31
% Copyright 2003-2022 INRA - TPV URPOI - BIA IMASTE

% extract computation tolerance
tol = 1e-14;
if ~isempty(varargin)
    tol = varargin{1};
end

% size of inputs
np = size(point,1);
nl = size(line, 1);

if np == 1 || nl == 1 || np == nl
    % test if lines are colinear, using norm of the cross product
    b = bsxfun(@rdivide, vectorNorm3d( ...
        crossProduct3d(bsxfun(@minus, line(:,1:3), point), line(:,4:6))), ...
        vectorNorm3d(line(:,4:6))) < tol;
else
    % same test, but after reshaping arrays to manage difference of
    % dimensionality
    point = reshape(point, [np 1 3]);
    line = reshape(line, [1 nl 6]);
    b = bsxfun(@rdivide, vectorNorm3d( ...
        cross(bsxfun(@minus, line(:,:,1:3), point), line(ones(1,np),:,4:6), 3)), ...
        vectorNorm3d(line(:,:,4:6))) < tol;
end


function geometry_test011 ( )

%*****************************************************************************80
%
%% TEST011 tests CIRCLE_DIA2IMP_2D.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 December 2010
%
%  Author:
%
%    John Burkardt
%
  dim_num = 2;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST011\n' );
  fprintf ( 1, '  CIRCLE_DIA2IMP_2D converts a diameter to an\n' );
  fprintf ( 1, '  implicit circle in 2D.\n' );

  theta = 2.0;

  p1(1,1) = 2.0 + 5.0 * cos ( theta );
  p1(2,1) = 3.0 + 5.0 * sin ( theta );

  p2(1,1) = 2.0 - 5.0 * cos ( theta );
  p2(2,1) = 3.0 - 5.0 * sin ( theta );

  r8vec_print ( dim_num, p1, '  P1:' )
  r8vec_print ( dim_num, p2, '  P2:' )

  [ r, center ] = circle_dia2imp_2d ( p1, p2 );

  circle_imp_print_2d ( r, center, '  The implicit circle:' );

  return
end


function [cc] = chaincode(b,unwrap)
%   Freeman Chain Code
%
%   Description: Give Freeman chain code 8-connected representation of a
%                boundary
%   Author.....: Alessandro Mannini <mannini@esod.it>
%   Date.......: 2010, september
%
%   usage:
%   --------------------------------------------------------
%   [cc] = chaincode(b,u)
%
%   INPUT:
%   --------------------------------------------------------
%   b       - boundary as np-by-2 array;
%             np is the number of pixels and each element is a pair (y,x) of
%             pixel coordinates
%   unwrap  - (optional, default=false) unwrap code;
%             if enable phase inversions are eliminated
%
%
%   OUTPUT:
%   --------------------------------------------------------
%   cc is structure with the following fields:
%
%   cc.code     - 8-connected Freeman chain code as 1-by-np array (or
%                 1-by-(np-1) if the boundary isn't close)
%   cc.x0,cc.y0 - respectively the abscissa and ordinate of start point
%   cc.ucode    - unwrapped 8-connected Freeman chain code (if required)
%

%
%
%   used direction-to-code convention is:       3  2  1
%                                                \ | /
%                                             4 -- P -- 0
%                                                / | \
%                                               5  6  7
%
%   and in terms of deltax,deltay if next pixel compared to the current:
%   --------------------------
%   | deltax | deltay | code |
%   |------------------------|
%   |    0   |   +1   |   2  |
%   |    0   |   -1   |   6  |
%   |   -1   |   +1   |   3  |
%   |   -1   |   -1   |   5  |
%   |   +1   |   +1   |   1  |
%   |   +1   |   -1   |   7  |
%   |   -1   |    0   |   4  |
%   |   +1   |    0   |   0  |
%   --------------------------
%

% check input arguments
if nargin>2
    error('Too many arguments');
elseif nargin==0
    error('Too few arguments');
elseif nargin==1
    unwrap=false;
end
% compute dx,dy  by a circular shift on coords arrays by 1 element
sb=circshift(b,[-1 0]);
delta=sb-b;
% check if boundary is close, if not cut last element
if abs(delta(end,1))>1 || abs(delta(end,2))>1
    delta=delta(1:(end-1),:);
end
% check if boundary is 8-connected
n8c=find(abs(delta(:,1))>1 | abs(delta(:,2))>1);
if size(n8c,1)>0
    s='';
    for i=1:size(n8c,1)
        s=[s sprintf(' idx -> %d \n',n8c(i))];
    end
    error('Curve isn''t 8-connected in elements: \n%s',s);
end

% convert dy,dx pairs to scalar indexes thinking to them (+1) as base-3 numbers
% according to: idx=3*(dy+1)+(dx+1)=3dy+dx+4 (adding 1 to have idx starting
% from 1)
% Then use a mapping array cm
%   --------------------------------------
%   | deltax | deltay | code | (base-3)+1 |
%   |-------------------------------------|
%   |    0   |   +1   |   2  |      8     |
%   |    0   |   -1   |   6  |      2     |
%   |   -1   |   +1   |   3  |      7     |
%   |   -1   |   -1   |   5  |      1     |
%   |   +1   |   +1   |   1  |      9     |
%   |   +1   |   -1   |   7  |      3     |
%   |   -1   |    0   |   4  |      4     |
%   |   +1   |    0   |   0  |      6     |
%   ---------------------------------------

idx=3*delta(:,1)+delta(:,2)+5;
cm([1 2 3 4 6 7 8 9])=[5 6 7 4 0 3 2 1];

% finally the chain code array and the starting point
cc.x0=b(1,2);
cc.y0=b(1,1);
cc.code=(cm(idx))';

% If unwrapping is required, use the following algorithm
%
% if a(k), k=1..n is the original code and u(k) the unwrapped:
%
% - u(1)=a(1)
% - u(k)=g(k),
%        g(k) in Z | (g(k)-a(k)) mod 8=0 and |g(k)-u(k-1)| is minimized
%
if (unwrap)
    a=cc.code;
    u(1)=a(1);
    la=size(a,1);
    for i=2:la
        n=round((u(i-1)-a(i))/8);
        u(i)=a(i)+n*8;
    end
    cc.ucode=u';
end

end


% this script represents the evolution of the covariance of an OU process in terms of the dispersion ellipsoid
% see A. Meucci (2009)
% "Review of Statistical Arbitrage, Cointegration, and Multivariate Ornstein-Uhlenbeck"
% available at ssrn.com

% Code by A. Meucci, April 2009
% Most recent version available at www.symmys.com > Teaching > MATLAB

clear; clc; close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input parameters of multivariate OU process
K=1;
J=1;

x0=rand(K+2*J,1);

Mu=rand(K+2*J,1);

A=rand(K+2*J,K+2*J)-.5;
ls=rand(K,1)-.5;
gs=rand(J,1)-.5;
os=rand(J,1)-.5;

S=rand(K+2*J,K+2*J)-.5;

ts=.01*[0:10:100];
NumSimul=10000;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% process inputs
Gamma=diag(ls);
for j=1:J
    G=[gs(j)  os(j)
           -os(j) gs(j)];
    Gamma=blkdiag(Gamma,G);
end
Theta=A*Gamma*inv(A);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% one-step exact simulation
Sigma=S*S';
X_0=repmat(x0',NumSimul,1);
[X_t1, MuHat_t1, SigmaHat_t1]=OUstep(X_0,ts(end),Mu,Theta,Sigma);

% multi-step simulation: exact and Euler approximation
X_t=repmat(x0',NumSimul,1);
X_tE=X_t;
for s=1:length(ts)
    Dt=ts(1);
    if s>1
        Dt=ts(s)-ts(s-1);
    end
    [X_t,MuHat_t,SigmaHat_t]=OUstep(X_t,Dt,Mu,Theta,Sigma);
    %[X_tE,MuHat_tE,SigmaHat_tE]=OUstepEuler(X_tE,Dt,Mu,Theta,Sigma);

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots
Pick=[K+2*J-1 K+2*J];

% horizon simulations
hold on
h5=plot(X_t1(:,Pick(1)),X_t1(:,Pick(2)),'.');
set(h5,'color','r','markersize',4)

% horizon location
hold on
h4=plot(MuHat_t1(Pick(1)),MuHat_t1(Pick(2)),'.');
set(h4,'color','k','markersize',5)

% horizon dispersion ellipsoid
hold on
h3=TwoDimEllipsoid(MuHat_t1(Pick),SigmaHat_t1(Pick,Pick),2,0,0);
set(h3,'color','k','linewidth',2);

% starting point
hold on
h2=plot(x0(Pick(1)),x0(Pick(2)),'.');
set(h2,'color','b','markersize',5)

% starting generating dispersion ellipsoid
hold on
h1=TwoDimEllipsoid(x0(Pick),Sigma(Pick,Pick),2,0,0);
set(h1,'color','b','linewidth',2);

legend([h1 h3],'generator','horizon');


function fx = p15_fun ( option, nvar, x )

%*****************************************************************************80
%
%% P15_FUN evaluates the function for problem 15.
%
%  Title:
%
%    The Trigger Circuit.
%
%  Description:
%
%    The current flow of a trigger circuit with an operational amplifier
%    is modeled.  The variables are voltages, with X(6) the output
%    voltage and X(7) the input voltage.
%
%    The function has the form
%
%      F(X) = A * X + PHI ( X )
%
%    where A is a 6 by 7 matrix, and PHI is a nonlinear term, that is,
%
%      F(I) = SUM ( 1 <= J <= 7 ) A(I,J) * X(J) + PHI ( X )
%
%  Options:
%
%    Melhem lists the following limit points in X(7):
%
%    ( 0.04936  0.54735  0.04944  0.04944  0.12920  1.16602  0.60185 )
%    ( 0.23577  0.66296  0.23759  0.23760  0.62083  9.60913  0.32286 )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    29 August 20008
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Rami Melhem, Werner Rheinboldt,
%    A Comparison of Methods for Determining Turning Points of Nonlinear Equations,
%    Computing,
%    Volume 29, Number 3, September 1982, pages 201-226.
%
%    Gerd Poenisch, Hubert Schwetlick,
%    Computing Turning Points of Curves Implicitly Defined by Nonlinear
%    Equations Depending on a Parameter,
%    Computing,
%    Volume 26, Number 2, June 1981, pages 107-121.
%
%  Parameters:
%
%    Input, integer OPTION, the option index.
%
%    Input, integer NVAR, the number of variables.
%
%    Input, real X(NVAR), the argument of the function.
%
%    Output, real FX(NVAR-1), the value of the function at X.
%

%
%  Get the linear coefficients.
%
  array = p15_gx ( );
%
%  Compute the linear portion of the function.
%
  fx(1:nvar-1) = 0.0;

  for i = 1 : nvar - 1
    for j = 1 : nvar
      fx(i) = fx(i) + array(i,j) * x(j);
    end
  end
%
%  Add the nonlinear terms.
%
  fx(2) = fx(2) + 5.6D-08 * ( exp ( 25.0 * x(2) ) - 1.0 );
  fx(5) = fx(5) + 5.6D-08 * ( exp ( 25.0 * x(5) ) - 1.0 );
  fx(6) = fx(6) - 7.65 * atan ( 1962.0 * ( x(3) - x(1) ) ) / 0.201;

  return
end


function out=prox_l1_squared(x,alpha)
%PROX_L1_SQUARED computes the proximal operator of the function alpha*(norm(x(:),1)^2)
%
%  Usage:
%  out = PROX_L1_SQUARED(x,alpha)
%  ===========================================
%  INPUT:
%  x - point to be projected (vector/matrix)
%  alpha - positive scalar
%  ===========================================
%  Output:
%  out - proximal operator at x

% 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/>.

if (nargin < 2)
    error ('usage: prox_l1_squared(x,alpha)') ;
end

if (alpha < 0)
    error('usage: prox_l1_squared(x,alpha) - alpha should be positive')
end

%setting eps to defalut value : 1e-10
eps = 1e-10 ;

if (norm(x) < eps)
    out = x;
    return ;
end

%defining f on mu - to be used by the bisetion
f=@(mu)   sum(sum( max(sqrt(alpha) *abs(x)/sqrt(mu) - 2*alpha,0))) - 1 ;

mu_min = 0 ;
mu_max = 1 ;
while(f(mu_max)> 0)
    mu_max = mu_max * 2 ;
end

final_mu = bisection(f,mu_min,mu_max,eps) ;
lam = max(sqrt(alpha) * abs(x)/sqrt(final_mu) - 2 *alpha,0) ;

out = (lam .* x) ./ (lam + 2 * alpha) ;

end


% MAIN - chain integrator
%
% Problem statement:
%
% Find the minimum-snap trajectory that moves a system between two boundary
% points. Note that snap is the 4th derivative of position. Since the
% dynamics are in first-order form, we need to include position, velocity,
% acceleration, jerk in our state vector. We then set the control to be the
% snap of the trajectory.
%

clc; clear;
addpath ../../..

%%%% Boundary-value problem:

t0 = 0;        %initial time
x0 = [1;0];    %initial position
dx0 = [0;0];   %initial velocity
ddx0 = [0;0];  %initial acceleration
dddx0 = [0;0]; %initial jerk (derivative of acceleration)
z0 = [x0;dx0;ddx0;dddx0];   %Full initial state

tF = 1;        %final time
xF = [0;1];    %final position
dxF = [0;0];   %final velocity
ddxF = [0;0];  %final acceleration
dddxF = [0;0]; %final jerk (derivative of acceleration)
zF = [xF;dxF;ddxF;dddxF];  %full final state

%%%% Construct bounds struct, given problem specifications

problem.bounds.initialTime.low = t0;
problem.bounds.initialTime.upp = t0;
problem.bounds.finalTime.low = tF;
problem.bounds.finalTime.upp = tF;

problem.bounds.initialState.low = z0;
problem.bounds.initialState.upp = z0;
problem.bounds.finalState.low = zF;
problem.bounds.finalState.upp = zF;

%%%% Construct a simple initial guess (linear between boundary)
problem.guess.time = [t0, tF];
problem.guess.state = [z0, zF];
problem.guess.control = [zeros(size(x0)), zeros(size(xF))];

%%%% Define dynamics and objective functions:

% Enforce the chain integrator dynamics:
problem.func.dynamics = @(t,z,u)(  dynamics(z,u)  );

% Minimize the integral of the snap-squared along the trajectory.
% Sum along each dimension of the state space.
problem.func.pathObj = @(t,z,u)( sum(u.^2,1) );

%%%% Select the method of choice:

% problem.options.method = 'trapezoid';
% problem.options.method = 'hermiteSimpson';
problem.options.method = 'chebyshev';
% problem.options.method = 'rungeKutta';
% problem.options.method = 'gpops';    % requires license for GPOPS-II

%%%% Solve!
soln = optimTraj(problem);

%%%% Unpack the solution

tGrid = soln.grid.time;
xGrid = soln.grid.state(1:2, :);
dxGrid = soln.grid.state(3:4, :);
ddxGrid = soln.grid.state(5:6, :);
dddxGrid = soln.grid.state(7:8, :);
ddddxGrid = soln.grid.control;

t = linspace(tGrid(1), tGrid(end), 100);
z = soln.interp.state(t);
x = z(1:2,:);
dx = z(3:4,:);
ddx = z(5:6,:);
dddx = z(7:8,:);
ddddx = soln.interp.control(t);

%%%% Plot the trajectory against time
figure(1); clf;

subplot(5,1,1); hold on;
plot(t,x)
plot(tGrid,xGrid,'ko','MarkerSize',8,'LineWidth',2);

subplot(5,1,2); hold on;
plot(t,dx)
plot(tGrid,dxGrid,'ko','MarkerSize',8,'LineWidth',2);

subplot(5,1,3); hold on;
plot(t,ddx)
plot(tGrid,ddxGrid,'ko','MarkerSize',8,'LineWidth',2);

subplot(5,1,4); hold on;
plot(t,dddx)
plot(tGrid,dddxGrid,'ko','MarkerSize',8,'LineWidth',2);

subplot(5,1,5); hold on;
plot(t,ddddx)
plot(tGrid,ddddxGrid,'ko','MarkerSize',8,'LineWidth',2);


% Example of use for function 'power_flow_solver'.
% Written by Dr. Yoash Levron, October 2012.
%
% All documentation may be obtained by typing:
% 'HELP power_flow_solver' at the matlab command prompt.
%
% This script defines a simple power network
% and uses the 'power_flow_solver' function to
% compute the power flow in it.
%
% Associated file: 'power_flow_solver.m'

%%%%%%% Define the network  %%%%%%%
% The network composes three buses.
% bus 1 - (slack bus) a voltage source, E=1.0
% bus 2 - a generator and a load.
% bus 3 - a load.
%
% Define the line impedances:
z12 = 0.05 + j*0.1;  % impedance (inductive) connecting bus 1 to 2.
z13 = 0.02 + j*0.05; % impedance (inductive) connecting bus 1 to 3.
z23 = j*0.05; % impedance (inductive) connecting bus 2 to 3.
z11 = -j*100;  % shunt impedance (capacitive) at bus 1
z22 = inf;  % no shunt impedance at bus 2
z33 = -j*40;  % shunt impedance (capacitive) at bus 3

% A matrix of the network impedances
Zmat = [z11 z12 z13;
        z12 z22 z23;
        z13 z23 z33];

% compute the admittance matrix (admittamce = 1/impedance):
Ymat = 1./Zmat;

% Define voltage at bus 1
E1_phase = 0; % degree (reference phase angle).
E1_mag = 1.0; % voltage magnitude
E1 = E1_mag*exp(j*E1_phase*(pi/180));

% Define generators and loads.
% Pg&Qg - generators.   Pl&Ql - loads.
Pg1=0;  Qg1=0; Pl1=0; Ql1=0; % no power source on the slack bus.
Pg2=2;  Qg2=0;  Pl2=1; Ql2=1;  % bus 2. generator and load.
Pg3=0;  Qg3=0;  Pl3=3; Ql3=0.5; % bus 3. a load.

% sum generators and load to form united power sources.
% (generators taken positive, loads taken negative).
P1 = Pg1 - Pl1;
P2 = Pg2 - Pl2;
P3 = Pg3 - Pl3;
Q1 = Qg1 - Ql1;
Q2 = Qg2 - Ql2;
Q3 = Qg3 - Ql3;

% Define power vectors:
Pbus = [P1 P2 P3];
Qbus = [Q1 Q2 Q3];

% run function to solve the power flow
[Ebus, Ibus, Imat, iter] = ...
    power_flow_solver(Ymat, Pbus, Qbus, E1);

% display results
clc;
disp('---  SUMMARY OF RESULTS  ---');
disp('number of iterations until convergence:');
disp(iter);
disp('voltage magnitudes');
disp(abs(Ebus.'));
disp('voltage phase angles (degree)');
disp(phase(Ebus.')*180/pi);
disp('current supplied by each source (amplitude)');
disp(abs(Ibus.'));
disp('Active power supplied by each source');
disp(real(Ebus.*conj(Ibus)));
disp('Reactive power supplied by each source');
disp(imag(Ebus.*conj(Ibus)));
disp('current in bus 1 --> bus 2');
disp(Imat(1,2));
disp('current in bus 1 --> bus 3');
disp(Imat(1,3));
disp('current in bus 2 --> bus 3');
disp(Imat(2,3));
disp('shunt currents in each bus (magnitude)');
disp(abs(diag(Imat).'));


clear all; close all; clc

n=100; L=4;
x=linspace(0,L,n);
f=(x.^2).';  % parabola with 100 data points

M=21;  % polynomial degree
for j=1:M
    phi(:,j)=(x.').^(j-1);  % build matrix A
end

trials=[2 10 100];
for j=1:3
    for jj=1:trials(j)
       f=(x.^2+0.2*randn(1,n)).';
       a1=pinv(phi)*f; f1=phi*a1; E1(jj)=norm(f-f1)/norm(f);
       a2=phi\f; f2=phi*a2; E2(jj)=norm(f-f2)/norm(f);
       [a3,stats]=lasso(phi,f,'Lambda',0.1); f3=phi*a3; E3(jj)=norm(f-f3)/norm(f);
       A1(:,jj)=a1; A2(:,jj)=a2; A3(:,jj)=a3;
    end
    A1m=mean(A1.'); A2m=mean(A2.'); A3m=mean(A3.');
    Err=[E1; E2; E3];

    subplot(3,3,j), bar(A1m), axis([0 21 -1 1.2])
    subplot(3,3,3+j), bar(A2m), axis([0 21 -1 1.2])
    subplot(3,3,6+j), bar(A3m), axis([0 21 -1 1.2])
end

%subplot(3,3,j), bar(A1m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2])
%subplot(3,3,3+j), bar(A2m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2])
%subplot(3,3,6+j), bar(A3m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2])

%%
figure(2)

Atot=[A1m; A2m; A3m];  % average loadings of three methods
Atot2=(Atot>0.2).*Atot; % threshold
Atot3=[Atot; Atot2];    % combine both thresholded and not

figure(3), bar3(Atot.'), axis([0 4 0 20 0 1]), view(-70,38), set(gca,'Xtick',[],'Fontsize',[18])
figure(4), bar3(Atot2.'), axis([0 4 0 20 0 1]), view(-70,38), set(gca,'Xtick',[],'Fontsize',[18])

n=200; L=8;
x=linspace(0,L,n);
x1=x(1:100);   % train (interpolation)
x2=x(101:200); % test (extrapolation)

ftrain=(x1.^2).';  % interpolated parabola x=[0,4]
ftest=(x2.^2).';   % extrapolated parbola x=[4,5]

for j=1:M
    phi_i(:,j)=(x1.').^(j-1); % interpolation key
    phi_e(:,j)=(x2.').^(j-1); % extrapolation key
end

for jj=1:6 % compute inter/extra-polation scores
    ani=Atot3(jj,:).';
    fnai=phi_i*ani;
    Eni(jj)=norm(ftrain-fnai)/norm(ftrain);
    fnae=phi_e*ani;
    Ene(jj)=norm(ftest-fnae)/norm(ftest);
end

figure(5)
subplot(3,2,1), bar(Eni)
subplot(3,2,2), bar(Ene)

subplot(3,2,3), bar(Eni), axis([0 7 0 0.01])
subplot(3,2,4), bar(Ene), axis([0 7 0 0.1])

figure(3)
legend('','','','Location','NorthEast')
legend boxoff

%% Dendrograms


% Peak of polynomial Wigner-Ville frequency estimation
%
% Estimates the instantaneous frequency of the input signal by
% extracting the peaks of the sixth order kernel polynomial
% Wigner-Ville distribution.
%
%
% Usage:
%
%     ife = pwvpe( signal, lag_window_length, time_res, interpol [, fft_length] ))
%
%
% Parameters:
%
%    signal
%
%         Input one dimensional signal to be analysed. An analytic signal
%	  is required for this function, howver, if signal is real, a
%	  default analytic transformer routine will be called from this
%	  function before computing tfd.
%
%    lag_window_length
%
%	  The size of the kernel used for analysis. window_length must be
%	  odd. The kernel used will be defined from -(lag_window_length+1)/2 to
%	  +(lag_window_length+1)/2 in both time and lag dimensions.
%
%    time_res
%
%	  The number of time samples to skip between successive slices of
%	  the analysis.
%
%    interpol
%
%	  The number of interpolating the input signal in time domain to get
%	  the proper time fractional time lags required to compute the sixth
%         order kernel.
%
%    fft_length
%
%         Zero-padding at the fft stage of the analysis may be specified by
%         giving an fft_length larger than normal. If fft_length is not
%         specified, or is smaller than the lag_window_length, then the
%         next highest power of two above lag_window_length is used. If
%         fft_length is not a power of two, the next highest power of two is
%	  used.
%
%
%
%  See Also: pwvd4, pwvd6
%
% TFSAP 7.0
% Copyright Prof. B. Boashash
% Qatar University, Doha
% email: tfsap.research@gmail.com


function pass = test_undampedNewton(pref)
%TEST_UNDAMPEDNEWTON    Test what happens when we turn off damping in Newton

% This test tries to solve steady state Allen-Cahn using undamped Newton
% iteration.

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

% Turn off damping
pref.damping = 0;

%% Setup and solve
dom = [0, 10];
x = chebfun(@(x) x, dom);
f = sin(x);
N = chebop(@(u) diff(u,2) + u - u.^3, dom, 1, -1);
u = solvebvp(N, f, pref);

% Happy?
err = norm(N(u) - f);
pass = ( err < 1e-8 );

% end


function t = dec2twos(x, nbits)

% DEC2TWOS Convert decimal integer to binary string two's complement.
%
% Usage: T = DEC2TWOS(X, NBITS)
%
% Converts the signed decimal integer given by X (either a scalar, vector,
% or matrix) to the two's complement representation as a string. If X is a
% vector or matrix, T(i, :) is the representation for X(i) (the shape of X
% is not preserved).
%
% Example:
%     >> dec2twos([23 3 -23 -3])
%
%     ans =
%
%     010111
%     000011
%     101001
%     111101
%
% Inputs:
%   -X: decimal integers to convert to two's complement.
%   -NBITS: number of bits in the representation (optional, default is the
%   fewest number of bits necessary).
%
% Outputs:
%   -T: two's complement representation of X as a string.
%
% See also: TWOS2DEC, DEC2FIX, FIX2DEC, DEC2BIN, DEC2HEX, DEC2BASE.

error(nargchk(1, 2, nargin));
x = x(:);
maxx = max(abs(x));
nbits_min = nextpow2(maxx + (any(x == maxx))) + 1;

% Default number of bits.
if nargin == 1 || isempty(nbits)
    nbits = nbits_min;
elseif nbits < nbits_min
    warning('dec2twos:nbitsTooSmall', ['Minimum number of bits to ' ...
        'represent maximum input x is %i, which is greater than ' ...
        'input nbits = %i. Setting nbits = %i.'], ...
        nbits_min, nbits, nbits_min)
    nbits = nbits_min;
end

t = repmat('0', numel(x), nbits); % Initialize output:  Case for x = 0
if any(x > 0)
    t(x > 0, :) = dec2bin(x(x > 0), nbits);           % Case for x > 0
end
if any(x < 0)
    t(x < 0, :) = dec2bin(2^nbits + x(x < 0), nbits); % Case for x < 0
end


function r8_walsh_1d_test ( )

%*****************************************************************************80
%
%% R8_WALSH_1D_TEST tests R8_WALSH_1D;
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 April 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'R8_WALSH_1D_TEST\n' );
  fprintf ( 1, '  R8_WALSH_1D evaluates 1D Walsh functions:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  X  W(+2) W(+1) W(0) W(-1) W(-2) W(-3)\n' );
  fprintf ( 1, '\n' );

  for i = 0 : 32

    x = i / 4.0;

    wp2 = r8_walsh_1d ( x,  2 );
    wp1 = r8_walsh_1d ( x,  1 );
    w0  = r8_walsh_1d ( x,  0 );
    wm1 = r8_walsh_1d ( x, -1 );
    wm2 = r8_walsh_1d ( x, -2 );
    wm3 = r8_walsh_1d ( x, -3 );

    fprintf ( 1, '  %10f  %4f  %4f  %4f  %4f  %4f  %4f\n', ...
      x, wp2, wp1, w0, wm1, wm2, wm3 );

  end

  return
end


function p = predictOneVsAll(all_theta, X)
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
%are in the range 1..K, where K = size(all_theta, 1).
%  p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
%  for each example in the matrix X. Note that X contains the examples in
%  rows. all_theta is a matrix where the i-th row is a trained logistic
%  regression theta vector for the i-th class. You should set p to a vector
%  of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
%  for 4 examples)

m = size(X, 1);
num_labels = size(all_theta, 1);

% You need to return the following variables correctly
p = zeros(size(X, 1), 1);

% Add ones to the X data matrix
X = [ones(m, 1) X];

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters (one-vs-all).
%               You should set p to a vector of predictions (from 1 to
%               num_labels).
%
% Hint: This code can be done all vectorized using the max function.
%       In particular, the max function can also return the index of the
%       max element, for more information see 'help max'. If your examples
%       are in rows, then, you can use max(A, [], 2) to obtain the max
%       for each row.
%

% x * all_theta' : 5000 * 10，每一行的每一个列值代表图像是1,2...10的可能性
% use max(),M = max(A,[],dim) 沿着维度 dim 返回最大元素。例如，如果 A 为矩阵，则 max(A,[],2) 是包含每一行的最大值的列向量。
% probaitilty代表每一行最大的值，p代表索引
[probability, p] = max(sigmoid(X * all_theta'), [], 2);

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

end


function [J,J_od,J_id,J_bl] = jdegree(CIJ)
%JDEGREE    Joint degree distribution
%
%   [J,J_od,J_id,J_bl] = jdegree(CIJ);
%
%   This function returns a matrix in which the value of each element (u,v)
%   corresponds to the number of nodes that have u outgoing connections
%   and v incoming connections.
%
%   Input:      CIJ,    directed (weighted/binary) connection matrix
%
%   Outputs:    J,      joint degree distribution matrix (shifted by one)
%               J_od,   number of vertices with od>id.
%               J_id,   number of vertices with id>od.
%               J_bl,   number of vertices with id=od.
%
%   Note: Weights are discarded.
%
%
%   Olaf Sporns, Indiana University, 2002/2006/2008

% ensure CIJ is binary...
CIJ = double(CIJ~=0);

N = size(CIJ,1);

id = sum(CIJ,1);    % indegree = column sum of CIJ
od = sum(CIJ,2)';   % outdegree = row sum of CIJ

% Create the joint degree distribution matrix
% Note:  the matrix is shifted by one, to accomodate zero id and od in the first row/column.
% Upper triangular part of the matrix has vertices with an excess of
%    outgoing edges (od>id)
% Lower triangular part of the matrix has vertices with an excess of
%    outgoing edges (id>od)
% Main diagonal has units with id=od

szJ = max(max(id,od))+1;
J = zeros(szJ);

for i=1:N
   J(id(i)+1,od(i)+1) = J(id(i)+1,od(i)+1) + 1;
end;

J_od = sum(sum(triu(J,1)));
J_id = sum(sum(tril(J,-1)));
J_bl = sum(diag(J));


function zList=genAllNestedZParenth(n,algorithm)
%%GENALLNESTEDZPARENTH0 Generate all nested sets of n parenthesis pairs,
%           recording only the indices of the left parantheses.
%
%INPUTS: n The number of nested paranthesis pairs; n>=0. Passing zero will
%          result in an empty matrix being returned.
% algorithm An optional parameter specifying the algorithm that should be
%          used to generate the parenthesis pairs. Possible values are:
%          0 (The default if omitted or an empty matrix is passed) Use
%            the algorithm of Problem 2 of Section 7.2.1.6 of [1].
%          1 Use algorithm N of Section 7.2.1.6 of [1].
%
%OUTPUTS: zList The nXnumPairs list of indices where the left parenthesis
%               goes.
%
%There is a total of CatalanNumber(n) possible arrangements of nested
%parentheses.
%
%EXAMPLE:
%To recreate the values in Table I of Section 7.2.1.6 of [1],
%one can run
% zList=genAllNestedZParenth(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.

if(nargin<2||isempty(algorithm))
    algorithm=0;
end

if(n==0)
    zList=[];
    return;
end

switch(algorithm)
    case 0
        zList=genAllNestedZParenth0(n);
    case 1
        zList=genAllNestedZParenth1(n);
    otherwise
        error('Unknown algorithm specified.')
end

end

function zList=genAllNestedZParenth0(n)
%%GENALLNESTEDZPARENTH0 Generate all nested sets of n parenthesis pairs,
%           recording only the indices of the left parantheses using the
%           algorithm in Problem 2 of Section 7.2.1.6 of [1].
%
%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.

if(n==1)
    zList=1;
    return;
end

numBinTrees=CatalanNumber(n);
%Allocate space.
zList=zeros(n,numBinTrees);

%Step T1 Initialize
z=2*(0:n).'-1;

for curTree=1:numBinTrees
    %Step T2
    zList(:,curTree)=z(2:(n+1));

    %Step T3
    if(z(n-1+1)<z(n+1)-1)
        z(n+1)=z(n+1)-1;
        continue;
    end

    %Step T4, find j.
    j=n-1;
    z(n+1)=2*n-1;
    while(z(j-1+1)==z(j+1)-1)
        z(j+1)=2*j-1;
        j=j-1;
    end

    if(j==1)
        break;
    end
    z(j+1)=z(j+1)-1;
end

end

function zList=genAllNestedZParenth1(n)
%%GENALLNESTEDZPARENTH0 Generate all nested sets of n parenthesis pairs,
%           recording only the indices of the left parantheses using
%           Algorithm N of Section 7.2.1.6 of [1].
%
%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.

numBinTrees=CatalanNumber(n);
%Allocate space.
zList=zeros(n,numBinTrees);

%Step N1
z=2*(1:n).'-1;
g=2*(1:n).'-2;

for curTree=1:numBinTrees
    %Step N2
    zList(:,curTree)=z;
    j=n;

    %Step N3, Find j.
    while(z(j)==g(j))
        g(j)=bitxor(g(j),1);
        j=j-1;
    end

    %Step N4
    if(mod(g(j)-z(j),2)==0)
        z(j)=z(j)+2;
        continue;
    end

    %Step N5
    t=z(j)-2;
    if(t<0)
        return
    end

    if(t<=z(j-1))
        t=t+2*(t<z(j-1))+1;
    end
    z(j)=t;
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 idx = findClosestCentroids(X, centroids)
%FINDCLOSESTCENTROIDS computes the centroid memberships for every example
%   idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids
%   in idx for a dataset X where each row is a single example. idx = m x 1
%   vector of centroid assignments (i.e. each entry in range [1..K])
%

% Set K
K = size(centroids, 1);

% You need to return the following variables correctly.
idx = zeros(size(X,1), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Go over every example, find its closest centroid, and store
%               the index inside idx at the appropriate location.
%               Concretely, idx(i) should contain the index of the centroid
%               closest to example i. Hence, it should be a value in the
%               range 1..K
%
% Note: You can use a for-loop over the examples to compute this.
%

for i = 1 : size(X, 1)
    minDist = 10000000;
    minDistIdx = 0;
    for j = 1 : K
        dist = sqrt(sum(power((X(i,:) - centroids(j,:)),2)));
        if dist < minDist
            minDist = dist;
            minDistIdx = j;
        end
    end
    idx(i,1) = minDistIdx;
end

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

end


function P=ar1spectrum(ar1,period)
% AR1 power spectrum...
%
% power=ar1spectrum(ar1,period)
%
%
% (c) Aslak Grinsted 2002-2004
%

% -------------------------------------------------------------------------
%   Copyright (C) 2002-2004, Aslak Grinsted
%   This software may be used, copied, or redistributed as long as it is not
%   sold and this copyright notice is reproduced on each copy made.  This
%   routine is provided as is without any express or implied warranties
%   whatsoever.

freq=1./period;
P=(1-ar1.^2)./(abs(1-ar1.*exp(-2*pi*i*freq))).^2; %http://www.madsci.org/posts/archives/may97/864012045.Eg.r.html
%fixed typo in numerical recipes


function y = t_lpdf(x,v,mu,sigma)
% T_LPDF  Log probability density function (lpdf) for Student's T distribution
%
%   Y = T_LPDF(X,V,MU,SIGMA) returns the lpdf of Student's T distribution with
%   degrees of freedom, V, mean, MU and standard deviation, SIGMA, at the
%   values in X.
%
%   The size of Y is the common size of the input arguments. A scalar input
%   functions as a constant matrix of the same size as the other inputs.
%
%   Default values for MU and SIGMA are 0 and 1 respectively.

% Copyright (c) 1998-2004 Aki Vehtari

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

if nargin < 4,
  sigma = 1;
end

if nargin < 3;
  mu = 0;
end

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

term = gammaln((v + 1) / 2) - gammaln(v/2) -log(v.*pi)/2;
y = term - log(sigma) + log(1 + ((((x-mu)./sigma).^2)./v)) .* (-(v+1)/2);


function [A,B,Ypredict] = opls(X,Y,nhidden,algorithm)
% BLS  Bottleneck least squares aka sparse orthogonalized least squares
%
% X: ninput x nsamples input data matrix
% Y: noutput x nsamples output data matrix
% nhidden: dimension of hidden
%
% A: noutput x nhidden weight matrix
% B: ninput x nhidden weight matrix

if nargin < 3,
  nhidden = min(size(Y,1),2);
end

if nargin < 4,
  algorithm = 'direct';
end

[ninput,nsamples] = size(X);

Sxx = symmetric(X*X')/nsamples;

switch algorithm

  case 'direct'
    Syx = Y*X'/nsamples;
    Sxy = Syx';
    Sxxinv = symmetric(pinv(Sxx));
    S = symmetric(Syx * Sxxinv * Sxy);
    opts.disp = 0;
    opts.issym = 'true';
    [A,D] = eigs(S,nhidden,'LM',opts);   % first nhidden eigenvectors of S
    B = Sxxinv*Sxy*A;

  case 'indirect'          % same result and subspace as 'direct', but then different linear combination

    Syx = Y*X'/nsamples;
    Sxy = Syx';
    Sxxinv = symmetric(pinv(Sxx));

    % initialize randomly

    B = randn(ninput,nhidden);

    iter = 0;
    maxiter = 1000;
    tol = nhidden*ninput*(1e-10);
    Bold = B;
    while iter < maxiter,
      Z = B'*X;
      %		Szz = symmetric(Z*Z')/nsamples;
      Szz = symmetric(B'*Sxx*B);
      %		Syz = symmetric(Y*Z')/nsamples;
      Syz = Syx*B;
      A = Syz*pinv(Szz);

      B = Sxxinv*Sxy*A*pinv(A'*A);
      if sumsqr(B - Bold) < tol,
        fprintf('done after %d iterations!!\n',iter+1);
        iter = maxiter;
      else
        iter = iter + 1;
        Bold = B;
      end
    end

  case 'sequential'      % same result as 'direct', up to minus sign per hidden unit

    R = Y;
    noutput = size(Y,1);
    A = zeros(noutput,nhidden);
    B = zeros(ninput,nhidden);
    for i=1:nhidden,
      [A(:,i),B(:,i)] = bls(X,R,1,'direct');
      if i < nhidden,
        R = R - A(:,i)*B(:,i)'*X;
      end
    end

end

% transform to make latent variable orthonormal

Szz = symmetric(B'*Sxx*B);
Szzinv = symmetric(pinv(Szz));
sqrtSzzinv = chol(Szzinv(end:-1:1,end:-1:1));    % such that the rescaled B(i,:) is a linear combi of the unscaled B(1:i,:); this only makes sense for the direct method
sqrtSzzinv = sqrtSzzinv(end:-1:1,end:-1:1);
B = B*sqrtSzzinv;
A = A*pinv(sqrtSzzinv);

if nargout > 2,
  Ypredict = A*B'*X;
end

function A = symmetric(A)

A = (A+A')/2;


function [W,distances] = compute_relation(data,scale_sig,order)
%
%      [W,distances] = compute_relation(data,scale_sig)
%       Input: data= Feature_dimension x Num_data
%       ouput: W = pair-wise data similarity matrix
%              Dist = pair-wise Euclidean distance
%
%
% Jianbo Shi, 1997

% distances = zeros(size(data, 2), size(data, 2));
% for j = 1:length(data),
%   distances(j,:) = (sqrt((data(1,:)-data(1,j)).^2 +...
%                 (data(2,:)-data(2,j)).^2));
% end

distances = X2distances(data');

if (~exist('scale_sig')),
    scale_sig = 0.05*max(distances(:));
end

if (~exist('order')),
  order = 2;
end

tmp = (distances/scale_sig).^order;

W = exp(-tmp);


function dist = pntdist(p1, p2)

% PNTDIST returns the euclidian distance between two points
%
%  [dist] = pntdist(pnt1, pnt2)
%
% where pnt1 and pnt2 must be Npnt x 3
% or either one can be Npnt x 1

% Copyright (C) 2002, Robert Oostenveld
%
% $Log: pntdist.m,v $
% Revision 1.2  2003/03/04 21:46:19  roberto
% added CVS log entry and synchronized all copyright labels
%

if size(p1,1)==1
  p1 = repmat(p1, size(p2,1), 1);
elseif size(p2,1)==1
  p2 = repmat(p2, size(p1,1), 1);
end

dist = sqrt(sum((p1-p2).^2, 2));