function hammersley_dataset ( )

%*****************************************************************************80
%
%% HAMMERSLEY_DATASET generates a Hammersley dataset and writes it to a file.
%
%  Discussion:
%
%    This program is meant to be used interactively.  It's also
%    possible to prepare a simple input file beforehand and use it
%    in batch mode.
%
%    The program requests input values from the user:
%
%    * NDIM, the spatial dimension,
%    * N, the number of points to generate,
%    * STEP, the index of the first subsequence element to be computed.
%    * SEED(1:NDIM), the Hammersley sequence index corresponding to STEP = 0.
%    * LEAP(1:NDIM), the successive jumps in the Hammersley sequence.
%    * BASE(1:NDIM), the bases (usually distinct primes or -N).
%
%    The program generates the data, writes it to the file
%
%      hammersley_NDIM_N.txt
%
%    where "NDIM" and "N" are the numeric values specified by the user,
%    and then asks the user for more input.   To indicate that no further
%    computations are desired, it is enough to input a nonsensical
%    value, such as -1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 September 2004
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '  Generate a Hammersley dataset.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  This program is meant to be used interactively.\n' );
  fprintf ( 1, '  It is also possible to prepare a simple input \n' );
  fprintf ( 1, '  file beforehand and use it in batch mode.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The program requests input values from the user:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  * NDIM, the spatial dimension,\n' );
  fprintf ( 1, '  * N, the number of points to generate,\n' );
  fprintf ( 1, '  * STEP, the index of the first subsequence element.\n' );
  fprintf ( 1, '  * SEED(1:NDIM), the sequence element\n' );
  fprintf ( 1, '    corresponding to STEP = 0\n' );
  fprintf ( 1, '  * LEAP(1:NDIM), the successive jumps in the sequence.\n' );
  fprintf ( 1, '  * BASE(1:M), the bases, usually distinct primes\n' );
  fprintf ( 1, '    or -N (to generate values like J/N).\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The program generates the data, writes it to the file\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '    hammersley_NDIM_N.txt\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  where "NDIM" and "N" are the numeric values specified\n' );
  fprintf ( 1, '  by the user, and then asks the user for more input.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  To indicate that no further computations are \n' );
  fprintf ( 1, '  desired, it is enough to input a nonsensical value, \n' );
  fprintf ( 1, '  such as -1.\n' );

  while ( 1 )

    fprintf ( 1, '  *\n' );
    fprintf ( 1, ' *\n' );
    fprintf ( 1, '*  Ready to generate a new dataset:\n' );
    fprintf ( 1, ' *\n' );
    fprintf ( 1, '  *\n' );
    fprintf ( 1, '\n' );
    fprintf ( 1, '  NDIM is the spatial dimension.\n' );
    fprintf ( 1, '  (Try "2" if you have no preference.\n' );
    fprintf ( 1, '  Any value less than 1 terminates execution.\n' );
    ndim = [];
    ndim = input ( '  Enter NDIM:  ' );

    fprintf ( 1, '  User input NDIM = %d\n', ndim );

    if ( ~halham_ndim_check ( ndim ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The input value of NDIM = %d\n', ndim );
      fprintf ( 1, '  is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
      break
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  N is the number of points.\n' );
    fprintf ( 1, '  (Try "25" if you have no preference.\n' );
    fprintf ( 1, '  (Any value less than 1 terminates execution.)\n' );
    n = [];
    n = input ( '  Enter N:  ' );

    fprintf ( 1, '  User input N = %d\n', n );

    if ( ~halham_n_check ( n ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The input value of N = %d\n', n );
      fprintf ( 1, '  is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
      break
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  STEP is the index of the first subsequence element.\n' );
    fprintf ( 1, '  (Try "0" or "1" if you have no preference.\n' );
    fprintf ( 1, '  (Any value less than 0 terminates execution.)\n' );
    step = [];
    step = input ( '  Enter STEP:  ' );

    fprintf ( 1, '  User input STEP = %d\n', step );

    if ( ~halham_step_check ( step ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The input value of STEP = %d\n', step );
      fprintf ( 1, '  is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
      break
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  SEED(1:NDIM) is the starting element index\n' );
    fprintf ( 1, '  for each coordinate.\n' );
    fprintf ( 1, '  (Try "[0,0,...,0]" if you have no preference.\n' );
    fprintf ( 1, '  (Any value less than 0 terminates execution.)\n' );
    seed = [];
    seed(1:ndim) = input ( '  Enter SEED(1:NDIM):  ' );

    fprintf ( 1, '\n' );
    i4vec_transpose_print ( ndim, seed, 'SEED:' );

    if ( ~halham_seed_check ( ndim, seed ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The negative input value of at least one entry of\n' );
      fprintf ( 1, '  SEED is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  LEAP(1:NDIM) is the leaping multiplier\n' );
    fprintf ( 1, '  for each coordinate.\n' );
    fprintf ( 1, '  (Try "[1,1,...,1]" if you have no preference.\n' );
    fprintf ( 1, '  (Another choice is a prime bigger than all the bases.)\n' );
    fprintf ( 1, '  (Any value less than 1 terminates execution.)\n' );
    leap = [];
    leap(1:ndim) = input ( '  Enter LEAP(1:NDIM):  ' );

    fprintf ( 1, '\n' );
    i4vec_transpose_print ( ndim, leap, 'LEAP:' );

    if ( ~halham_leap_check ( ndim, leap ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The input value of at least one entry of\n' );
      fprintf ( 1, '  LEAP is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  BASE(1:NDIM) is the base for each coordinate,\n' );
    fprintf ( 1, '  usually distinct primes or -N for values like J/N.\n' );
    fprintf ( 1, '  (Try "[-N,2,3,5,7,11,13,...]" if you have no preference.\n' );
    fprintf ( 1, '  (Any value of 0 or 1 terminates execution.)\n' );
    base = [];
    base(1:ndim) = input ( '  Enter BASE:  ' );

    fprintf ( 1, '\n' );
    i4vec_transpose_print ( ndim, base, 'BASE:' );

    if ( ~hammersley_base_check ( ndim, base ) )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'HAMMERSLEY_DATASET\n' );
      fprintf ( 1, '  The input value of at least one entry of\n' );
      fprintf ( 1, '  BASE is interpreted as a request for termination.\n' );
      fprintf ( 1, '  Normal end of execution.\n' );
    end

    r = i4_to_hammersley_sequence ( ndim, n, step, seed, leap, base );

    file_out_name = ...
      strcat ( 'hammersley_', num2str ( ndim ), '_', num2str ( n ), '.txt' );

    halham_write ( ndim, n, step, seed, leap, base, r, file_out_name );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  The data was written to the file "%s".\n', ...
      file_out_name );

  end

  fprintf ( 1, '\n' );
  timestamp ( );

  return
end


disp(' ');
disp('fdct_wrapping_demo_basic.m -- Displays a curvelet')
disp ('both in the spatial and frequency domains.');
disp(' ');
disp(['This is achieved by setting all the coefficients in the curvelet'])
disp(['domain to zero except that at the required location (which'])
disp(['is set to one). The curvelet is obtained by taking the'])
disp(['adjoint curvelet transform. Notice how the curvelet is sharply '])
disp(['localized in both space and frequency.']);
disp(' ');

% fdct_wrapping_demo_basic.m -- Displays a curvelet both in the spatial and frequency domains.

% m = 512;
% n = 512;
%
% X = zeros(m,n);

%forward curvelet transform
disp('Take curvelet transform: fdct_wrapping');
tic; C = fdct_wrapping(X,0); toc;

%specify one curvelet
s = 5;
w = 1;
[A,B] = size(C{s}{w});6
a = ceil((A+1)/2);
b = ceil((B+1)/2);
C{s}{w}(a,b) = 1;

%adjoint curvelet transform
disp('Take adjoint curvelet transform: ifdct_wrapping');
tic; Y = ifdct_wrapping(C,0); toc;

%display the curvelet
F = ifftshift(fft2(fftshift(Y)));
subplot(1,2,1); colormap gray; imagesc(real(Y)); axis('image'); ...
    title('a curvelet: spatial viewpoint');
subplot(1,2,2); colormap gray; imagesc(abs(F)); axis('image'); ...
    title('a curvelet: frequency viewpoint');

%get parameters
[SX,SY,FX,FY,NX,NY] = fdct_wrapping_param(C);


function [sorted_vertices, ...
          h_fes, h_bnd, h_fill, h_vert, h_int, h_max, g_labels] = ...
    plot_feasible(A, b, c, lower_b, upper_b, varargin)
% Plot the feasible region of a linear program
%
% file:      	plot_feasible.m, (c) Matthew Roughan, 2009
% created: 	Mon Jun 22 2009
% author:  	Matthew Roughan
% email:   	matthew.roughan@adelaide.edu.au
%
% Plot the feasible region of the 2D linear program
%           maximize            f = c'*x
%           subject to       A x <= b
% on the region bounded by lower_b and upper_b. Note that the inputs
%     A  is a mx2 matrix, where there are m constraints
%     b  is a mx1 column vector, where there are m constraints
%     c  is a 2x1 column vector
%     lower_b, upper_b are 2x1 column vectors
%
% If the problem includes non-negativity bounds, i.e.,
%               x >= 0
% which is quite common, then these need to be explicitly included into
% the constraints:  A x <= b, not implicitly through the bounds, which are mainly there to
% allow us to make a nice plot.
%
% The feasible region is filled with lines at a setable angle and separation, much as fill
% would do with a solid colour, though options allow you to fill with a color, or both.
%
% This code is primarily (for me) useful for teaching about linear programming, and only
% works for 2D problems (for the obvious reason that problems with more than two variables
% are hard to plot).
%
% Note that if you want to cross-hatch just call this routine twice, with a change in angle
% of 90 degrees.
%
% There are a bunch of optional inputs in pairs, as for the plot command
%    'linecolor'         color of feasible region boundary lines (default black)
%    'backgroundcolor'   background color of feasible region (default no background)
%    'linewidth'         width of lines (default 1)
%    'linestyle'         style of the boundary lines
%    'filllinestyle'     style of the fill lines
%    'linesep'           separation of cross-hatch lines (default 1)
%                        if this is set to -1, don't put any fill lines in
%    'lineangle'         angle (in degrees) of cross-hatch lines
%                           the default is to put them along lines of equal values of the
%                           objective function
%    'hold'              hold_n=1 means keep previous plot
%                        hold_n=0 (default) means clear previous figure
%                        use this to plot multiple feasible regions on same plot, or to do
%                        crosshatching.
%
%    'plot_vertices'     if defined indicates that we should plot the vertices of the feasible region
%                        the value will determine the symbol to use to plot the vertices,
%                        e.g., 'rx', or 'o+'
%    'label_vertices'    if not 0, then label the vertices with their position (label_vertices=1)
%                        or value (label_vertices=1) or both (label_vertices=3)
%    'label_vertices_size'  size of vertex labels, default = 12
%    'label_vertices_color' size of vertex labels, default = 'k'
%    'label_vertices_prec'  precision of vertex labels (default = 2 decimal places)
%
%    'plot_intersections' if defined indicates that we should plot the intersection points of boundaries
%                        the value will determine the symbol to use to plot the vertices,
%                        e.g., 'rx', or 'o+'
%                        Note that vertices are also intersection points, and will be double
%                        plotted if used with the plot_vertices option
%
%    'plot_max'          if not 0, then plot the location of the maximum, using the defined symbol
%
%    'extend_boundaries' if defined, plot each of the constraint lines past the feasible region
%                        using the linewidth and color defined above. The style of the line can
%                        be given as the value, e.g., ':'
%
% OUTPUTS:
%     sorted_vertices = a 2x(n+1) vector of the vertices of the feasible region
%                        note the last one is a repeat of the first to aid in plotting the region
%
%
%     h_fes    = handles to boundary lines of the linear program
%     h_bnd    = handles to the boundary lines
%     h_fill   = handles to the fill lines
%     h_vert   = handles to the vertices (if plotted)
%     h_int    = handles to the intersection points (if plotted)
%     h_max    = a handle to the plotted maximum point
%     g_labels = handles to vertex labels if used
%
% version 0.1, July 22nd 2009
%
% TODO
%     speckled fill
%     testing for boundedness and autosizing the figure
%     plotting bounds with cross-hatch on feasible side
%     at present the code uses the optimization toolkit (through the linprog) function
%       which we use to solve the linear program, but as this is a 2D problem we should be able to
%       remove this dependence with a little work.
%     currently the intersections with upper and lower bounds for plotting are also included
%       in intersection set, and plotted
%     automate ability to put text description of the problem in the window
%

% check sizes of inputs
sA = size(A);
sb = size(b);
if (sA(1) ~= sb(1))
  error('incorrect input sizes\n');
end
if (sA(2) ~= 2)
  error('can only plot for 2 variables\n');
end
n = sA(1); % number of constraints
if (size(upper_b,1) ~=2 | size(upper_b,2) ~= 1)
  error('upper_b is the wrong size\n');
end
if (size(lower_b,1) ~=2 | size(lower_b,2) ~= 1)
  error('lower_b is the wrong size\n');
end

% set default output values
h_fes    = [];
h_bnd    = [];
h_fill   = [];
h_vert   = [];
h_int    = [];
h_max    = [];

% parse the variable input arguments
linecolor = 'k';
backgroundcolor = 0;
linewidth = 1;
linestyle = '-';
filllinestyle = '-';
linesep = 1;
% default line angle is so that lines will be iso-objective
if (abs(c(1)) < 1.0e-12)
  lineangle = 90; % degrees
else
  lineangle = atand(c(2)/c(1));
end
hold_n = 0;
plot_vertices = 0;
label_vertices = 0;
label_vertices_size = 12;
label_vertices_color = 'k';
label_vertices_prec = 2;
plot_intersections = 0;
extend_boundaries = 0;
plot_max = 0;
if (length(varargin) > 0)
  % process variable arguments
  for k = 1:2:length(varargin)
    if (ischar(varargin{k}))
      argument = char(varargin{k});
      value = varargin{k+1};
      switch argument
       case {'linecolor','lc'}
	linecolor = value;
       case {'backgroundcolor','bgc'}
	backgroundcolor = value;
       case {'linewidth','lw'}
	linewidth = value;
       case {'linestyle','ls'}
	 linestyle = value;
       case {'filllinestyle','fls'}
	 filllinestyle = value;
       case {'linesep','ls'}
	linesep = value;
       case {'lineangle','la'}
	lineangle = value;
       case {'hold'}
	hold_n = value;
       case {'plot_vertices', 'pv'}
	plot_vertices = value;
       case {'label_vertices', 'lv'}
	label_vertices = value;
       case {'label_vertices_size', 'lvs'}
	label_vertices_size = value;
       case {'label_vertices_color', 'lvc'}
	label_vertices_color = value;
       case {'label_vertices_prec', 'lvp'}
	label_vertices_prec = value;
       case {'plot_intersections', 'pi'}
	plot_intersections = value;
       case {'extend_boundaries', 'eb'}
	extend_boundaries = value;
       case {'plot_max', 'pm'}
	 plot_max = value;
       otherwise
	error('incorrect input parameters');
      end
    end
  end
end

% set up the plot region
if (hold_n==0)
  hold off
  plot(lower_b(1), lower_b(2));
end
diff_x = (upper_b(1)-lower_b(1))/20;
diff_y = (upper_b(2)-lower_b(2))/20;
set(gca, 'xlim', [lower_b(1)-diff_x upper_b(1)+diff_x]);
set(gca, 'ylim', [lower_b(2)-diff_y upper_b(2)+diff_y]);
hold on
epsilon = 1.0e-14;

% plot the boundary line lines of the linear program across the plot region
if ~(extend_boundaries == 0)
  for i=1:n
    % find end-points of the lines along the boundaries
    constraint = A(i,:);
    k = b(i);
    if (abs(constraint(1)) < epsilon)
      % fprintf('(abs(constraint(1) < epsilon): i = %d\n', i);
      if (lower_b(1)<=k/constraint(2) & k/constraint(2)<=upper_b(1))
	x = [lower_b(1) upper_b(1)];
	y = [1 1] * k/constraint(2);
      else
	x = [];
	y = [];
      end
    elseif (abs(constraint(2)) < epsilon)
      % fprintf('(abs(constraint(2) < epsilon): i = %d\n', i);
      if (lower_b(2)<=k/constraint(1) & k/constraint(1)<=upper_b(2))
	x = [1 1] * k/constraint(1);
	y = [lower_b(2) upper_b(2)];
      else
	x = [];
	y = [];
      end
    else
      % compute all four intersections, and decide which is feasible
      % horizontal intercept points
      y_1 = [lower_b(2) upper_b(2)];
      x_1 = (k - constraint(2)*y_1) / constraint(1);
      i_1 = find(lower_b(1)<x_1 & x_1<upper_b(1));
      % vertical intercept points
      x_2 = [lower_b(1) upper_b(1)];
      y_2 = (k - constraint(1)*x_2) / constraint(2);
      i_2 = find(lower_b(2)<y_2 & y_2<upper_b(2));

      x = [x_1(i_1) x_2(i_2)];
      y = [y_1(i_1) y_2(i_2)];
    end
    % plot the line
    h_fes(i) = plot(x, y, 'color', linecolor, 'linestyle', extend_boundaries);
  end
end

% find the vertices of the lines, plus the borders
Aex = [A; -eye(2); eye(2)];
bex = [b; -lower_b; upper_b];
m = n + 4;
intersections = [];
vertices = [];
for i=1:m-1
  for j=i+1:m
    M = [Aex(i,:); Aex(j,:)];
    bm = bex([i,j]);
    x = M \ bm;
    intersections = [intersections, x];
    % test for feasibility
    if (A*x <= b)
      vertices = [vertices, x];
    end
  end
end

% find and fill the polygon defined by the vertices
k = convhull(vertices(1,:), vertices(2,:));
sorted_vertices(1,:) = vertices(1,k)';
sorted_vertices(2,:) = vertices(2,k)';
if (backgroundcolor == 0)
  h_bnd = plot(sorted_vertices(1,:), sorted_vertices(2,:),  ...
	       'color', linecolor, 'linewidth', linewidth, 'linestyle', linestyle);
else
  h_bnd = fill(sorted_vertices(1,:), sorted_vertices(2,:), backgroundcolor, ...
	       'edgecolor', linecolor, 'linewidth', linewidth, 'linestyle', linestyle);
end

% plot the intersection points and vertices
if ~(plot_intersections == 0)
  h_int = plot(intersections(1,:), intersections(2, :), plot_intersections);
end

if ~(plot_vertices == 0)
  h_vert = plot(sorted_vertices(1,:), sorted_vertices(2, :), plot_vertices);

  if (label_vertices == 1)
    % write the position of the vertex
    format_str = sprintf('(%%.%.0ff,%%.%.0ff)', label_vertices_prec, label_vertices_prec);
    for i=1:size(sorted_vertices, 2)
      temp = 10^label_vertices_prec;
      x = round(temp*sorted_vertices(1,i))/temp;
      y = round(temp*sorted_vertices(2,i))/temp;
      g_labels(i) = text(x+diff_x/2, y, sprintf(format_str, x, y), ...
			 'fontsize', label_vertices_size, 'color', label_vertices_color);
    end
  elseif (label_vertices == 2)
    % write the value of the vertex
    format_str = sprintf('f = %%.%.0ff', label_vertices_prec);
    for i=1:size(sorted_vertices, 2)
      temp = 10^label_vertices_prec;
      x = sorted_vertices(1,i);
      y = sorted_vertices(2,i);
      f = round(temp* (c' * [x; y]))/temp;
      g_labels(i) = text(x+diff_x/2, y, sprintf(format_str, f), ...
			 'fontsize', label_vertices_size, 'color', label_vertices_color);
    end
  elseif (label_vertices == 3)
    % write the position and value
    % write the position of the vertex
    format_str = sprintf('(%%.%.0ff,%%.%.0ff), f=%%.%.0ff', ...
			 label_vertices_prec, label_vertices_prec, label_vertices_prec);
    for i=1:size(sorted_vertices, 2)
      temp = 10^label_vertices_prec;
      x = sorted_vertices(1,i);
      y = sorted_vertices(2,i);
      f = round(temp* (c' * [x; y]))/temp;
      x = round(temp*x)/temp;
      y = round(temp*y)/temp;
      g_labels(i) = text(x+diff_x/2, y, sprintf(format_str, x, y, f), ...
			 'fontsize', label_vertices_size, 'color', label_vertices_color);
    end
  end

end

% plot the max if needed
if ~(plot_max == 0)
  x_max = linprog(-c, A, b, [], [], lower_b, upper_b);
  h_max = plot(x_max(1), x_max(2), plot_max);
end

% do the fill lines
if (linesep > 0)
  %   find min and max via linprog to see start and end points of lines
  %      add in implicit boundary constraints when do it
  c_line = [cosd(lineangle), sind(lineangle)];
  x_min = linprog(c_line, A, b, [], [], lower_b, upper_b);
  % plot(x_min(1), x_min(2), 'b*');

  x_max = linprog(-c_line, A, b, [], [], lower_b, upper_b);
  % plot(x_max(1), x_max(2), 'b*');

  distance = sqrt( sum((x_max-x_min).^2)  );

  %   draw lines clipping them at the boundaries
  for i=0:linesep:distance
    counter = ceil(i/linesep) + 1;

    % sigma = x_min + i*(x_max-x_min)/distance;
    sigma = x_min + i*c_line';
    % plot(sigma(1), sigma(2), '+', 'color', linecolor);

    % for each line, consider its intersepts with each possible
    % boundary, and check whether they are feasible
    edge_vertices = [];
    for j=1:m
      M = [Aex(j,:); c_line];
      d = [bex(j); sum(c_line.*sigma')];
      x = M \ d;
      % test for feasibility
      if (A*x <= b)
	edge_vertices = [edge_vertices, x];
      end
    end
    edge_vertices = round(100*edge_vertices)'/100;
    edge_vertices = unique(edge_vertices, 'rows')';
    if (size(edge_vertices,2) >= 2)
      h_fill(counter) = plot(edge_vertices(1,:), edge_vertices(2,:), ...
			     'color', linecolor, 'linestyle', filllinestyle, 'linewidth', linewidth);
    end
  end
end


function value = rodman_determinant ( n, alpha )

%*****************************************************************************80
%
%% RODMAN_DETERMINANT returns the determinant of the RODMAN matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 October 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of A.
%
%    Input, real ALPHA, the parameter.
%
%    Output, real VALUE, the determinant.
%
  value = ( 1.0 - alpha )^( n - 1 ) * ( 1.0 + alpha * ( n - 1 ) );

  return
end


function prob_test1188 ( )

%*****************************************************************************80
%
%% PROB_TEST1188 tests NORMAL_TRUNCATED_B_CDF, NORMAL_TRUNCATED_B_CDF_INV, NORMAL_TRUNCATED_B_PDF;
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 August 2013
%
%  Author:
%
%    John Burkardt
%
  seed = 123456789;
  b = 150.0;
  mu = 100.0;
  s = 25.0;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'PROB_TEST1188\n' );
  fprintf ( 1, '  For the Upper Truncated Normal PDF:\n' );
  fprintf ( 1, '  NORMAL_TRUNCATED_B_CDF evaluates the CDF.\n' );
  fprintf ( 1, '  NORMAL_TRUNCATED_B_CDF_INV inverts the CDF.\n' );
  fprintf ( 1, '  NORMAL_TRUNCATED_B_PDF evaluates the PDF.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The "parent" normal distribution has\n' );
  fprintf ( 1, '    mean =               %g\n', mu );
  fprintf ( 1, '    standard deviation = %g\n', s );
  fprintf ( 1, '  The parent distribution is truncated to\n' );
  fprintf ( 1, '  the interval [-oo,%g]\n', b );

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

  for i = 1 : 10

    [ x, seed ] = normal_truncated_b_sample ( mu, s, b, seed );

    pdf = normal_truncated_b_pdf ( x, mu, s, b );

    cdf = normal_truncated_b_cdf ( x, mu, s, b );

    x2 = normal_truncated_b_cdf_inv ( cdf, mu, s, b );

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

  end

  return
end


function [ num_int, pint ] = halfspace_normal_triangle_int_3d ( p, normal, t )

%*****************************************************************************80
%
%% HALFSPACE_NORMAL_TRIANGLE_INT_3D: intersection ( normal halfspace, triangle ) in 3D.
%
%  Discussion:
%
%    The normal form of a halfspace in 3D may be described as the set
%    of points (X,Y,Z) on or "above" a plane described in normal form:
%
%      P is a point on the plane,
%      NORMAL is the unit normal vector, pointing "out" of the
%      halfspace.
%
%    The triangle is specified by listing its three vertices.
%
%    The intersection may be described by the number of vertices of the
%    triangle that are included in the halfspace, and by the location of
%    points between vertices that separate a side of the triangle into
%    an included part and an unincluded part.
%
%    0 vertices, 0 separators    (no intersection)
%    1 vertex, 0 separators      (point intersection)
%    2 vertices, 0 separators    (line intersection)
%    3 vertices, 0 separators    (triangle intersection)
%
%    1 vertex, 2 separators,     (intersection is a triangle)
%    2 vertices, 2 separators,   (intersection is a quadrilateral).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    08 May 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real T(3,3), the vertices of the triangle.
%
%    Input, real P(3,1), a point on the bounding plane
%    that defines the halfspace.
%
%    Input, real NORMAL(3,1), the components of the normal vector
%    to the bounding plane that defines the halfspace.  By convention, the
%    normal vector points "outwards" from the halfspace.
%
%    Output, integer NUM_INT, the number of intersection points returned,
%    which will always be between 0 and 4.
%
%    Output, real PINT(3,4), the coordinates of the NUM_INT
%    intersection points.  The points will lie in sequence on the triangle.
%    Some points will be vertices, and some may be separators.
%
  dim_num = 3;
%
%  Compute the signed distances between the vertices and the plane.
%
  d = - normal(1:dim_num,1)' * p(1:dim_num,1);
%
%  Compute the signed distances between the vertices and the plane.
%
  dist1 = d + normal(1:dim_num,1)' * t(1:dim_num,1);
  dist2 = d + normal(1:dim_num,1)' * t(1:dim_num,2);
  dist3 = d + normal(1:dim_num,1)' * t(1:dim_num,3);
%
%  Now we can find the intersections.
%
  [ num_int, pint ] = halfspace_triangle_int_3d ( t, dist1, dist2, dist3 );

  return
end


function divdif_test18 ( )

%*****************************************************************************80
%
%% DIVDIF_TEST18 tests ROOTS_TO_DIF and DIF_TO_R8POLY.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    07 October 2006
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'DIVDIF_TEST18\n' );
  fprintf ( 1, '  ROOTS_TO_DIF computes the divided difference\n' );
  fprintf ( 1, '  polynomial with given roots;\n' );
  fprintf ( 1, '  DIF_TO_R8POLY converts it to a standard form\n' );
  fprintf ( 1, '  polynomial.\n' );
  fprintf ( 1, '\n' );

  nroots = 1;
  roots(1) = 3.0;
  r8vec_print ( nroots, roots, '  The roots:' );

  [ ntab, xtab, diftab ] = roots_to_dif ( nroots, roots );

  c = dif_to_r8poly ( ntab, xtab, diftab );

  r8poly_print ( ntab, c, '  The polynomial:' );

  nroots = 2;
  roots(1:2) = [ 3.0, 1.0 ];
  r8vec_print ( nroots, roots, '  The roots:' );

  [ ntab, xtab, diftab ] = roots_to_dif ( nroots, roots );

  c = dif_to_r8poly ( ntab, xtab, diftab );

  r8poly_print ( ntab, c, '  The polynomial:' );

  nroots = 3;
  roots(1:3) = [ 3.0, 1.0, 2.0 ];
  r8vec_print ( nroots, roots, '  The roots:' );

  [ ntab, xtab, diftab ] = roots_to_dif ( nroots, roots );

  c = dif_to_r8poly ( ntab, xtab, diftab );

  r8poly_print ( ntab, c, '  The polynomial:' );

  nroots = 4;
  roots(1:4) = [ 3.0, 1.0, 2.0, 4.0 ];
  r8vec_print ( nroots, roots, '  The roots:' );

  [ ntab, xtab, diftab ] = roots_to_dif ( nroots, roots );

  c = dif_to_r8poly ( ntab, xtab, diftab );

  r8poly_print ( ntab, c, '  The polynomial:' );

  return
end


function determ = vand2_determinant ( n, x )

%*****************************************************************************80
%
%% VAND2_DETERMINANT returns the determinant of the VAND2 matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    21 October 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix desired.
%
%    Input, real X(N), the values that define A.
%
%    Output, real DETERM, the determinant.
%
  determ = 1.0;

  for i = 1 : n
    for j = 1 : i - 1
      determ = determ * ( x(i) - x(j) );
    end
  end

  return
end


function value = r4_cosh ( x )

%*****************************************************************************80
%
%% R4_COSH evaluates the hyperbolic cosine of an R4 argument.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 September 2011
%
%  Author:
%
%    Original FORTRAN77 version by Wayne Fullerton.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Wayne Fullerton,
%    Portable Special Function Routines,
%    in Portability of Numerical Software,
%    edited by Wayne Cowell,
%    Lecture Notes in Computer Science, Volume 57,
%    Springer 1977,
%    ISBN: 978-3-540-08446-4,
%    LC: QA297.W65.
%
%  Parameters:
%
%    Input, real X, the argument.
%
%    Output, real VALUE, the hyperbolic cosine of X.
%
  persistent ymax

  if ( isempty ( ymax ) )
    ymax = 1.0 / sqrt ( r4_mach ( 3 ) );
  end

  y = exp ( abs ( x ) );

  value = 0.5 * y;

  if ( y < ymax )
    value = 0.5 * ( y + 1.0 / y );
  end

  return
end


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

%*****************************************************************************80
%
%% EPN_LAG_02_XIU implements the Xiu rule for region EPN_LAG.
%
%  Discussion:
%
%    The rule has order
%
%      O = N + 1.
%
%    The rule has precision P = 2.
%
%    EPN is the N-dimensional positive space [0,+oo)^N with exponential
%    or Laguerre weight function:
%
%      w(x(1:n)) = exp ( - sum ( x(1:n) ) )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    07 March 2010
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Dongbin Xiu,
%    Numerical integration formulas of degree two,
%    Applied Numerical Mathematics,
%    Volume 58, 2008, pages 1515-1520.
%
%  Parameters:
%
%    Input, integer N, the spatial dimension.
%
%    Output, integer O, the order.
%
%    Output, real X(N,O), the abscissas.
%
%    Output, real W(O), the weights.
%
  o = n + 1;

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

  for j = 1 : o

    i = 0;
    for r = 1 : floor ( n / 2 )
      arg = 2 * r * ( j - 1 ) * pi / ( n + 1 );
      i = i + 1;
      x(i,j) = sqrt ( 2.0 ) * cos ( arg );
      i = i + 1;
      x(i,j) = sqrt ( 2.0 ) * sin ( arg );
    end

    if ( i < n )
      i = i + 1;
      x(i,j) = r8_mop ( j - 1 );
    end

  end

  gamma0 = - 1.0;
  delta0 = 1.0;
  c1 = - 1.0;

  x(1:n,1:o) = ( sqrt ( gamma0 * c1 ) * x(1:n,1:o) - delta0 ) / gamma0;

  expon = 0;
  volume_1d = ep1_lag_monomial_integral ( expon );
  volume = volume_1d ^ n;

  w(1:o) = volume / o;

  return
end


%
% Calculation of the fractal dimension which is the slope on the
% log-log graph.
%
deriv = diff(log10(corint))./diff(log10(r));			% deriv= Vector of the appr. derivatives
r2 = r(1:(end-1));											% Forward difference approximation: deriv has one element less, and r must have the # of elements so r2

switch(dof)

    case 'newf'

        %
        % Calculation of the fractal dimension, by first calculating the
        % distances of depopulation "rd" and of saturation "rs".
        %
        rad = 0.8;				% 2rmax= linear size of the hypercube encompassing a given dataset
        ras = 7;
        v = find(r2 >= rad & r2 <= ras);					% v= Vector of the all the interevent distances that fall in the interval [rn,rs]
        lr = log10(r2(v));
        lc = log10(corint(v));

        [fd, Err] = polyfit(lr,lc,1);
        [ev, delta] = polyval(fd, log10(r), Err);

        %[fdlo,Err] = polyfit(log10(r2(v)), log10(corint(v)), 1);
        %[fd,Err] = polyfit(log10(r2(v)), log10(corint(v)), 1);
        %[deriv3, delta] = polyval(fd, log10(r2(v)), Err);

        Ha_Cax = gca;
        Hf_Cfig;
        hold on;
        Hl_gr2a = loglog(r2(v), corint(v), 'k.','Markersize',7);
        Hl_gr2b = plot(r,10.^ev,'k','Linewidth',1);
        xlabel('Interevent Distance R [km]');
        ylabel('Correlation Integral C(R)');
        set(Ha_Cax,'pos',[0.21 0.1 0.75 0.75]);

        if d == 2
            title('Correlation Integral versus the 2D Interevent Distance R');
        else
            title('Correlation Integral versus the 3D Interevent Distance R');
        end

        g = [];
        uicontrol('Units','normal','Position',[.0 .92 .15 .07],...
            'String','Scaling range', 'Callback','g = ginput(2);dof = ''newc''; dofdim')

        str1 = ['Range = ' num2str(rd,3) ' - ' num2str(rs,3) ' [km]'];
        str2 = ['D =  ' num2str(fd(1,1),3)];
        axes('pos',[0 0 1 1]); axis off; hold on;
        te1 = text(0.25, 0.8, str1 ,'Fontweight','bold');
        te2 = text(0.25, 0.75, str2, 'Fontweight', 'bold');

        %str3 = [' The scaling range is calculated as proposed by Nerenberg & Essex (1990): ' num2str(rd,3) ' - ' num2str(rs,4) ' [km] . If you wish to change the scaling range please click on the "scaling range" button'];
        %msg1 = msgbox( str3,'Fractal Dimension');

    case 'newc'

        rd = min(g(:,1)); rs = max(g(:,1));
        v = find(r2 >= rd & r2 <= rs);

        lr = log10(r2(v));
        lc = log10(corint(v));

        [fd, Err] = polyfit(lr,lc,1);
        [ev, delta] = polyval(fd, log10(r), Err);

        delete(Hl_gr2a);
        delete(Hl_gr2b);
        Hl_gr2a = loglog(r2(v), corint(v), 'k.','Markersize',7);
        Hl_gr2b = plot(r,10.^ev,'k','Linewidth',1);

        delete(te1);
        delete(te2);
        str1 = ['Range = ' num2str(rd,3) ' - ' num2str(rs,3) ' [km]'];
        str2 = ['D =  ' num2str(fd(1,1),3)];
        axes('pos',[0 0 1 1]); axis off; hold on;
        te1 = text(0.25, 0.8, str1 ,'Fontweight','bold');
        te2 = text(0.25, 0.75, str2, 'Fontweight', 'bold');

end


function [H,S,D]=sobi(X,n,p),
% program  by  A. Belouchrani and A. Cichocki
%
% Second
% Order
% Blind
% Identification
% SOBI
%**************************************************
% blind identification by joint diagonalization   *
% of correlation  matrices.                       *
%                       			  *
% ------------------------------------------------*
% THIS CODE ASSUMES TEMPORALLY CORRELATED SIGNALS *
% in estimating the cumulants                     *
% ------------------------------------------------*
%
% [H,S]=SOBI(X,m,p) produces a matrix H of dimension [m by n] and a matrix S
% of dimension [n by N] wich are respectively an estimate of the mixture matrix and
% an estimate of the source signals of the linear model from where the
% observation matrix X of dimension [m by N] derive.
% Note: > m: sensor number.
%       > n: source number by default m=n.
%       > N: sample number.
%	> p: number of correlation matrices to be diagonalized by default p=4.
%
% REFERENCES:
% A. Belouchrani, K. Abed-Meraim, J.-F. Cardoso, and E. Moulines, ``Second-order
%  blind separation of temporally correlated sources,'' in Proc. Int. Conf. on
%  Digital Sig. Proc., (Cyprus), pp. 346--351, 1993.
%
% A. Belouchrani and K. Abed-Meraim, ``Separation aveugle au second ordre de
%  sources correlees,'' in  Proc. Gretsi, (Juan-les-pins),
%  pp. 309--312, 1993.
%
%  A. Belouchrani, and A. Cichocki,
%  Robust whitening procedure in blind source separation context,
%  Electronics Letters, Vol. 36, No. 24, 2000, pp. 2050-2053.
%
%  A. Cichocki and S. Amari,
%  Adaptive Blind Signal and Image Processing, Wiley,  2002.

[m,N]=size(X);
if nargin==1,
 n=m; % source detection (hum...)

 p=4; % number of correlation matrices to be diagonalized
 end;
if nargin==2,
 p=4 ; % number of correlation matrices to be diagonalized
end;
pm=p*m; % for convenience
X=X-kron(mean(X')',ones(1,N)); % zero mean
%%%% whitening
% Rx=(X*X')/T;
% if m<n, %assumes white noise
%   [U,D]=eig(Rx); [puiss,k]=sort(diag(D));
%   ibl= sqrt(puiss(n-m+1:n)-mean(puiss(1:n-m)));
%    bl = ones(m,1) ./ ibl ;
%   BL=diag(bl)*U(1:n,k(n-m+1:n))';
%   IBL=U(1:n,k(n-m+1:n))*diag(ibl);
% else    %assumes no noise
%    IBL=sqrtm(Rx);
%    Q=inv(IBL);
% end;
% X=Q*X;
%prewitening based directly on SVD
[UU,S,VV]=svd(X',0);
Q= pinv(S)*VV';
X=Q*X;
%
%%%correlation matrices estimation
k=1;for u=1:m:pm, k=k+1; Rxp=X(:,k:N)*X(:,1:N-k+1)'/(N-k+1);
    M(:,u:u+m-1)=norm(Rxp,'fro')*Rxp; end;
%%%joint diagonalization
epsil=1/sqrt(N)/100; encore=1; V=eye(m);
while encore, encore=0;
 for p=1:m-1,
  for q=p+1:m,
   %%% Givens rotations
   g=[   M(p,p:m:pm)-M(q,q:m:pm)  ;
         M(p,q:m:pm)+M(q,p:m:pm)  ;
      i*(M(q,p:m:pm)-M(p,q:m:pm)) ];
	  [vcp,D] = eig(real(g*g')); [la,K]=sort(diag(D));
   angles=vcp(:,K(3));angles=sign(angles(1))*angles;
   c=sqrt(0.5+angles(1)/2);
   sr=0.5*(angles(2)-j*angles(3))/c; sc=conj(sr);
   oui = abs(sr)>epsil ;
   encore=encore | oui ;
   if oui , %%%update of the M and V matrices
    colp=M(:,p:m:pm);colq=M(:,q:m:pm);
    M(:,p:m:pm)=c*colp+sr*colq;M(:,q:m:pm)=c*colq-sc*colp;
    rowp=M(p,:);rowq=M(q,:);
    M(p,:)=c*rowp+sc*rowq;M(q,:)=c*rowq-sr*rowp;
    temp=V(:,p);
    V(:,p)=c*V(:,p)+sr*V(:,q);V(:,q)=c*V(:,q)-sc*temp;
   end%% if
  end%% q loop
 end%% p loop
end%% while
%%%estimation of the mixing matrix and source signals
Hf=pinv(Q)*V; %estimated mixing matrix
Sf=V'*X; %estimated sources

H = Hf(:,1:n);
S = Sf(1:n,:);


function [X,nuclearnorm] = prox_nuclear(B,lambda)

% The proximal operator of the nuclear norm of a matrix
%
% min_X lambda*||X||_*+0.5*||X-B||_F^2
%
% version 1.0 - 18/06/2016
%
% Written by Canyi Lu (canyilu@gmail.com)
%

[U,S,V] = svd(B,'econ');
S = diag(S);
svp = length(find(S>lambda));
if svp>=1
    S = S(1:svp)-lambda;
    X = U(:,1:svp)*diag(S)*V(:,1:svp)';
    nuclearnorm = sum(S);
else
    X = zeros(size(B));
    nuclearnorm = 0;
end


clear all; close all; clear classes; clc;

%% Ag in Table I on p.4374 of Johnson and Christy, 1972 Phys. Rev. B
eV = [0.64 0.77 0.89 1.02 1.14 1.26 1.39 1.51 1.64 1.76 1.88 2.01 2.13 2.26 2.38 2.50 2.63 2.75 2.88 3.00 3.12 3.25 3.37 3.50 3.62 3.74 3.87 3.99 4.12 4.24 4.36 4.49 4.61 4.74 4.86 4.98 5.11 5.23 5.36 5.48 5.60 5.73 5.85 5.98 6.10 6.22 6.35 6.47 6.60];
n = [0.24 0.15 0.13 0.09 0.04 0.04 0.04 0.04 0.03 0.04 0.05 0.06 0.05 0.06 0.05 0.05 0.05 0.04 0.04 0.05 0.05 0.05 0.07 0.10 0.14 0.17 0.81 1.13 1.34 1.39 1.41 1.41 1.38 1.35 1.33 1.31 1.30 1.28 1.28 1.26 1.25 1.22 1.20 1.18 1.15 1.14 1.12 1.10 1.07];
k = [14.08 11.85 10.10 8.828 7.795 6.992 6.312 5.727 5.242 4.838 4.483 4.152 3.858 3.586 3.324 3.093 2.869 2.657 2.462 2.275 2.070 1.864 1.657 1.419 1.142 0.829 0.392 0.616 0.964 1.161 1.264 1.331 1.372 1.387 1.393 1.389 1.378 1.367 1.357 1.344 1.342 1.336 1.325 1.312 1.296 1.277 1.255 1.232 1.212];

%% Convert the photon energies to the wavelengths.
wvlen = PhysC.h * PhysC.c0 * 1e9 ./ eV;

%% Make the row vectors into column vectors to be used in interp1q().
eV = eV.';
n = n.';
k = k.';
wvlen = wvlen.';

%% Calculate the permittivity from n and k following the exp(+i w t) time dependence.
eps = (n - 1i*k).^2;

%% Plot real(eps) and imag(eps).  Compare with Fig.3 on p.4375 of Johnson.

nk_wvlen = 1;
eps_eV = 2;
eps_wvlen = 3;
eps_omega = 4;
q_omega = 5;
plotstyle = eps_eV;
switch plotstyle
    case nk_wvlen  % plot n and k
        loglog(wvlen, n, 'o-', wvlen, k, 'o-')
        %plot(wvlen, n, wvlen, k)
        legend('n', 'k', 'Location', 'SouthEast');
        xlabel 'wavelength (nm)'
        %axis([1e2 1e4 1e-2 1e2])
    case eps_eV  % plot real(eps) and -imag(eps)
        plot(eV, real(eps), 'o-', eV, -imag(eps), 'o-')
        legend('\epsilon_1', '\epsilon_2', 'Location', 'SouthEast');
        xlabel 'Photon Energy (eV)'
%         axis([0.5 6.5 -7 7]);
    case eps_wvlen
        plot(wvlen, real(eps), 'o-', wvlen, -imag(eps), 'o-')
        legend('\epsilon_1', '\epsilon_2', 'Location', 'SouthEast');
        xlabel 'wavelength (nm)'
        %axis([1e2 1e4 1e-2 1e2])
    case eps_omega
        plot(2*pi./wvlen, real(eps), 'o-', 2*pi./wvlen, -imag(eps), 'o-')
        legend('\epsilon_1', '\epsilon_2', 'Location', 'SouthEast');
        xlabel '\omega (c/nm)'
    case q_omega
        omega = 2*pi./wvlen;
        deps1 = real(eps(2:end)) - real(eps(1:end-1));
        domega = omega(2:end) - omega(1:end-1);
        eps1_inter = (real(eps(2:end)) + real(eps(1:end-1)))/2;
        omega_inter = (omega(2:end) + omega(1:end-1))/2;
        numer = eps1_inter + omega_inter .* (deps1./domega);
        denom = -(imag(eps(2:end)) + imag(eps(1:end-1)));  % extra factor 2
        plot(2*pi./omega_inter, numer./denom, 'o-');
        xlabel 'wavelength (nm)'
        ylabel 'electric Q';
end
title(mfilename)

%% Save data.
save(mfilename, 'eV', 'n', 'k');


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

all_predictions = all_theta * X';
[M, I] = max(all_predictions);
p = I(:);

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

end


function order = gl_level_to_order ( dim_num, level )

%*****************************************************************************80
%
%% GL_LEVEL_TO_ORDER converts a GL level to a GL order.
%
%  Discussion:
%
%    Gauss-Legendre rules cannot be nested, but in order to facilitate
%    comparison with Clenshaw Curtis grids, we need to be able to
%    convert a "level" into an order.  Except for the
%    first case of LEVEL = 0, the relationship is
%
%      ORDER = 2**LEVEL + 1
%
%    Level      Order
%
%    0          1
%    1          3 =  2 + 1
%    2          5 =  4 + 1
%    3          9 =  8 + 1
%    4         17 = 16 + 1
%    5         33 = 32 + 1
%
%    In this routine, we assume that a vector of levels is given,!
%    and the corresponding orders are desired.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    06 November 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer DIM_NUM, the spatial dimension.
%
%    Input, integer LEVEL(DIM_NUM), the level.
%
%    Output, integer ORDER(DIM_NUM), the order (number of points) of the
%    corresponding rule.
%
  for dim = 1 : dim_num

    if ( level(dim) < 0 )
      order(dim) = -1;
    elseif ( level(dim) == 0 )
      order(dim) = 1;
    else
      order(dim) = ( 2^level(dim) ) + 1;
    end

  end

  return
end


function r = taylorinit(v,k)
%TAYLORINIT   Initialization of taylor variable
%
%  x = taylorinit(v,k)
%
%The dependent variable x is identified and initialized with v, and Taylor
%coefficients up to k>0 are calculated. For example,
%
%  u = taylorinit(-3,5)
%
%initializes "u" to be a Taylor variable with value -3 and Taylor coefficients
%up to order k=5, i.e. [-3,1,0,0,0,0]. The default for k is 4. After initialization,
%the order of Taylor coefficients can be obtained by
%
%  k = taylororder
%
%In our example, the result will be k=5. The evaluation
%
%  f = inline('x*sin(x)-exp(x^2)')
%  y = f(u)
%
%computes the Taylor coefficients of f up to order 5. Note the first component of y
%is f(u), followed by f'(u), f''(u)/2!, f'''(u)/3!, etc. For example,
%
%  t0 = y{0}
%
%is the 0-th Taylor coefficients, namely f(u), and
%
%  sum( y{0:k} .* e.^(0:k) )
%
%is approximately equal to f(u+e) up to O(e^(k+1)). The subsequent statement
%
%  v = taylor( intval('3.14159_') )
%
%initializes v to be the constant with interval value infsup(3.14158,3.14160).
%
%Taylor expansions are implemented for univariate functions. However, it is
%sometimes useful to evaluate a function at an array of points. The statement
%
%  w = taylorinit((-3:1)',5)
%
%initializes w to be a vector of dependent variables with values -3,-2,-1,0,1. All
%operations with u are executed pointwise, so y=f(w) is executed as w.*sin(w)-exp(w.^2).
%In this case
%
%  y(3)
%
%is the Taylor expansion of f at -1 up to order 5.
%
%For other examples, see demotaylor.
%

% written  05/21/09     S.M. Rump
% modified 09/15/10     S.M. Rump
% modified 08/26/12     S.M. Rump  global variables removed
%

  setappdata(0,'INTLAB_TAYLOR_END',0);

  if nargin==0
    setappdata(0,'INTLAB_TAYLOR_ORDER',0);
    return
  end

  if ~isa(v,'double') & ~isa(v,'intval')
    error('invalid initialization of dependent Taylor variables')
  end
  if nargin==1
    setappdata(0,'INTLAB_TAYLOR_ORDER',4);
  else
    setappdata(0,'INTLAB_TAYLOR_ORDER',k);
  end
  dummy.init = v;
  r = taylor( dummy , 'taylorinit' );


function [C, dC] = gplvm_grad(x, X, sigma)
%GPLVM_GRAD Gradient of the Gaussian Process Latent Variable model
%
%   [C, dC] = gplvm_grad(x, no_dims, sigma)
%
% Computes the gradient of the Gaussian Process Latent Variable model.

% This file is part of the Matlab Toolbox for Dimensionality Reduction.
% The toolbox can be obtained from http://homepage.tudelft.nl/19j49
% You are free to use, change, or redistribute this code in any way you
% want for non-commercial purposes. However, it is appreciated if you
% maintain the name of the original author.
%
% (C) Laurens van der Maaten, Delft University of Technology

    % Decode solution
    [n, d] = size(X);
    no_dims = numel(x) ./ n;
    Y = reshape(x, [n no_dims]);

    % Compute kernel matrix (in latent space)
    sum_Y = sum(Y .^ 2, 2);
    K = exp(bsxfun(@minus, bsxfun(@minus, 2 * (Y * Y'), sum_Y'), sum_Y) / (2 * sigma ^ 2));

    % Compute gradient with respect to kernel
%     invK = inv(K);
%     dLdK = invK * X * X' * invK - d * invK;
    tmp = (K \ X) * X';
    dLdK = (tmp - d * eye(n)) / K;

    % Compute gradient with respect to coordinates
    dC = zeros(n, no_dims);
    dLdK = K .* dLdK;
    for i=1:n
        dC(i,:) = sum(bsxfun(@times, dLdK(:,i), bsxfun(@minus, Y(i,:), Y)), 1);
    end
    dC = (-1 / sigma ^ 2) * dC(:);

    % Compute log-likelihood
    C = -((d * n) / 2) * log(2 * pi) - (d / 2) * log(det(K) + realmin) - .5 * trace(tmp);


function [OP,MM] = CurvedPoissonIPDG2D()

% function [OP,MM] = CurvedPoissonIPDG2D()
% Purpose: Set up a discrete Poisson matrix and mass matrix
%          using Interior Penalty Discontinuous Galerkin (IPDG).

Globals2D;

NGauss = gauss.NGauss;

% build DG derivative matrices
MM  = zeros(K*Np*Np, 3);  OP = zeros(K*Np*Np*(1+Nfaces), 3);

% global node numbering
entries = (1:Np*Np)'; entriesMM = (1:Np*Np)';
for k1=1:K
  if(~mod(k1,200)) k1, end;

  % Location of k1'th diagonal block entries in OP matrix
  rows1 = ((k1-1)*Np+1:k1*Np)'*ones(1,Np); cols1 = rows1';

  % Extract local mass matrix and cubature weights
  locmm = cub.mm(:,:,k1);
  cw  = spdiags(cub.W (:,k1), 0, cub.Ncub, cub.Ncub);

  % Evaluate derivatives of Lagrange basis functions at cubature nodes
  [cDx, cDy]  = PhysDmatrices2D(x(:,k1), y(:,k1), cub.V);

  % Evaluate local stiffness matrix
  OP11 = cDx'*cw*cDx + cDy'*cw*cDy;

  % Build element-to-element parts of stiffness matrix for element k1
  for f1=1:Nfaces

    % Find neighbor
    k2 = EToE(k1,f1); f2 = EToF(k1,f1);

    idsM = (f1-1)*NGauss+1:f1*NGauss;

    % Extract Lagrange basis function -> Gauss node interpolation matrix
    gVM = gauss.finterp(:,:,f1);
    gVP = gauss.finterp(:,:,f2);    gVP = gVP(NGauss:-1:1,:);

    % Evaluate spatial derivatives of  Lagrange basis function at Gauss nodes
    [gDxM, gDyM] = PhysDmatrices2D(x(:,k1), y(:,k1),gVM);
    [gDxP, gDyP] = PhysDmatrices2D(x(:,k2), y(:,k2),gVP);

    % Evaluate normals at Gauss nodes on face
    gnx = spdiags(gauss.nx(idsM, k1), 0, NGauss, NGauss);
    gny = spdiags(gauss.ny(idsM, k1), 0, NGauss, NGauss);
    gw  = spdiags(gauss.W(idsM, k1),  0, NGauss, NGauss);

    % Compute normal derivatives of Lagrange basis functions at Gauss nodes
    gDnM = gnx*gDxM + gny*gDyM;
    gDnP = gnx*gDxP + gny*gDyP;

    % Locate global numbers of Lagrange nodes in neighboring element
    cols2 = ones(Np,1)*((k2-1)*Np+1:k2*Np);

    % Find minimum height of two elements sharing this face
    hinv = max(Fscale( 1 + (f1-1)*Nfp, k1), Fscale( 1 + (f2-1)*Nfp, k2));

    % Set penalty scaling
    gtau = 20*(N+1)*(N+1)*hinv;

    % Determine type of face
    switch(BCType(k1,f1))
      case {Dirichlet}
        % Dirichlet boundary face variational terms
	OP11 = OP11 + ( gVM'*gw*gtau*gVM - gVM'*gw*gDnM - gDnM'*gw*gVM);

      case {Neuman}
        % Do nothing
      otherwise
	% Interior face variational terms for stiffness matrix
	OP11 = OP11 + 0.5*( gVM'*gw*gtau*gVM - gVM'*gw*gDnM - gDnM'*gw*gVM );

	OP12 =      - 0.5*( gVM'*gw*gtau*gVP + gVM'*gw*gDnP - gDnM'*gw*gVP );

        % Store self-neighbor interaction term in global stiffness matrix
        OP(entries(:), :) = [rows1(:), cols2(:), OP12(:)];
	entries = entries + Np*Np;
    end
  end

  % Store k1'th self-self interaction term in global stiffness matrix
  OP(entries(:), :)   = [rows1(:), cols1(:), OP11(:)];
  MM(entriesMM(:), :) = [rows1(:), cols1(:), locmm(:)];
  entries = entries + Np*Np; entriesMM = entriesMM + Np*Np;
end

% Convert OP and MM from coordinate storage format to Matlab's intrinsic sparse matrix format
OP = OP(1:max(entries)-Np*Np,:);
OP = myspconvert(OP, Np*K, Np*K, 1e-15); MM = myspconvert(MM, Np*K, Np*K, 1e-15);
return


function b_n = beamWeightsHypercardioid2Spherical(N)
%BEAMWEIGHTSHYPERCARDIOID2SPHERICAL Generate spherical beamweights for hypercardioids
%
%   The hypercardioid is the pattern that maximizes the directivity-factor
%   for a certain SH order N. The hypercardioid is also the plane-wave
%   decomposition beamformer in the SHD, also called 'regular' because the
%   beamweights are just the SH values on the beam-direction.
%   Since the pattern is axisymmetric only the N+1 coefficients of m=0 are
%   returned.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% BEAMWEIGHTSHYPERCARDIOID2SPHERICAL.M - 11/7/2013
% Archontis Politis, archontis.politis@aalto.fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

c_n = 4*pi/(N+1)^2 * getSH(N,[0 0],'real');
b_n = zeros(N+1,1);
for n=0:N
    b_n(n+1) = c_n((n+1)^2 -n);
end


function lambda = bab_eigenvalues ( n, alpha, beta )

%*****************************************************************************80
%
%% BAB_EIGENVALUES returns the eigenvalues of the BAB matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 November 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%
%    Input, real ALPHA, BETA, the parameters.
%
%    Output, real LAMBDA(N,1), the eigenvalues.
%
  lambda = zeros ( n, 1 );

  for i = 1 : n
    angle = i * pi / ( n + 1 );
    lambda(i,1) = alpha + 2.0 * beta * cos ( angle );
  end

  return
end


function [x, infos] = rank2nmf(V, in_options)
% Rank-two NMF computes a rank-two NMF of X
%
%       If X >= 0 and rank(X) = 1, take W as any non-zero column of X and compute
%       H accordingly
%
%       If X >= 0 and rank(X) = 2, this is Algorithm 4.1 in Chapter 4,
%       with alpha1 = alpha2 = 0. This is an exact algorithm.
%
%       Otherwise, the input matrix has is replaced by max(0,X2) where X2 is its
%       best rank-two approximation; cf. Chapter 6.
%
% Inputs:
%       matrix      V
%       options     options
% Output:
%       w           solution of w
%       infos       information
%
% References:
%       Nicolas Gillis,
%       "Nonnegative Matrix Factorization,"
%       SIAM, 2020.
%
%
% This file is part of NMFLibrary.
%
% This file has been ported from
%       Rank2NMF.m at https://gitlab.com/ngillis/nmfbook/-/tree/master/algorithms
%       by Nicolas Gillis (nicolas.gillis@umons.ac.be)
%
% Change log:
%
%       June 13, 2022 (Mitsuhiko Horie and Hiroyuki Kasai): Ported initial version
%

    % set dimensions and samples
    [m, n] = size(V);

    % set local options
    local_options = [];
    local_options.delta = 1e-6;

    % check input options
    if ~exist('in_options', 'var') || isempty(in_options)
        in_options = struct();
    end
    % merge options
    options = mergeOptions(get_nmf_default_options(), local_options);
    options = mergeOptions(options, in_options);

    if options.verbose > 0
        fprintf('# Rank2-NMF: started ...\n');
    end

    if min(V(:)) < 0
        if options.verbose > 0
            fprintf('  * Rank2-NMF: The input matrix is not nonnegative: V <-- max(V,0).\n');
        end
        V = max(0,V);
    end
    [u, s, v] = svds(V, 3);

    % Case 0. rank(V) = 0
    if s(1, 1) <= eps
        if options.verbose > 0
            fprintf('  * Rank2-NMF: The input matrix is (close to) zero.');
        end
        [m,n] = size(V);
        W = zeros(m,1);
        H = zeros(1,n);
        return;
    end

    % Case 1. rank(V) = 1
    if s(2, 2) < 1e-9 * s(1, 1)
        if options.verbose > 0
            fprintf('  * Rank2-NMF: The input matrix has rank one, an optimal solution is returned.\n');
        end
        sumV = sum(V);
        [~,j] = max(sumV);
        W = V(:,j);
        H = sumV/sum(W);
        return;
    end

    % Case 2. rank(V) >= 2
    if s(3, 3) > 1e-9 * s(2, 2)
        if options.verbose > 0
	        fprintf('  * Rank2-NMF: The input matrix does not have rank two.\n');
            fprintf('  * Rank2-NMF: V <-- rank(V2,0) where V2 is the rank-2 truncated SVD of V.\n');
            fprintf('  * Rank2-NMF: The solution computed might not be optimal.\n');
        end
        [u, s, v] = svds(V, 2);
        Vj = max(u*s*v', 0);
    else
        if options.verbose > 0
            fprintf('  * Rank2-NMF: The input matrix has rank two, an optimal solution is returned.\n');
        end
        Vj = V;
    end

    % remove zero columns
    sumVj = sum(Vj);
    J = find(sumVj > 0);
    Vj = Vj(:, J);

    % normalize columns
    [m, n] = size(Vj);
    for i = 1 : n
        Vj(:, i) = Vj(:, i) / sum(Vj(:, i));
    end

    % compute extreme points of conv(Vj) hence a feasible solution W
    % given that rank(V) = 2.
    W = zeros(m, 2);
    [~, j1] = max(sum(Vj.^2));
    W(:, 1) = Vj(:, j1);
    [~, j2] = max(sum( (Vj-repmat(Vj(:, j1), 1, n)).^2));
    W(:, 2) = Vj(:, j2);

    W = full(W);
    WtW = W' * W;
    WtV = W' * V;

    % If no initial matrices are provided, H is initialized as follows:
    if ~isfield(options,'init') || isempty(options.init)
        H = nnls_init_nmflibrary(V, W, WtW, WtV);
    else
        H = options.init;
    end

    %% perform main routine
    options.alg_name = 'Rank2-NMF';
    options.V = V;
    options.W = W;
    options.main_routine_mode = true;
    options.eps0 = 0;
    options.eps = 1;

    [H, WtW, WtV, infos] = nnls_solver(V, W, options);

    x.W = W;
    x.H = H;

end


function sqi = ksqi(signal)
%kSQI Kurtosis SQI
%
% Returns the kurtosis of a signal
%
% Reference:
% Li, Q., Mark, R. G., Clifford, G. D., & Li. (2008). Robust heart rate
% estimation from multiple asynchronous noisy sources using signal quality
% indices and a Kalman filter. Physiol. Meas., 29(1), 15–32.
% http://doi.org/10.1088/0967-3334/29/1/002
%
% Input:
%   signal:         single channel (F)ECG [1xN double]
%
% Output:
%   sqi:            resulting kSQI for segment
%
% Fetal Extraction Toolbox, version 1.0, February 2014
% Released under the GNU General Public License
%
% Copyright (C) 2014 Fernando Andreotti
% Dresden University of Technology, Institute of Biomedical Engineering
% fernando.andreotti@mailbox.tu-dresden.de
%
% Last updated : 09-03-2014
%
%
% 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.

sqi=kurtosis(signal);

end


function res = qdist(a,b)
%QDIST        Implements  q(a,b)  metrical distance
%  name  qdist  is used to avoid ambiguities with variable  q
%
%     res = qdist(a,b)          max( abs(inf(a)-inf(b)) , abs(sup(a)-sup(b)) )
%
% for real intervals      max( abs(inf(a)-inf(b)) , abs(sup(a)-sup(b)) )
% for complex intervals   qdist(real(a),real(b)) + qdist(imag(a),imag(b))
%

% written  10/16/98     S.M. Rump
% modified 04/06/05     S.M. Rump  rounding unchanged
% modified 09/28/08     S.M. Rump  check for rounding to nearest improved
%

  e = 1e-30;
  if 1+e==1-e                           % fast check for rounding to nearest
    rndold = 0;
  else
    rndold = getround;
    setround(0)
  end

  if ~isa(a,'intval')
    a = intval(a);
  end
  if ~isa(b,'intval')
    b = intval(b);
  end

  if ~a.complex & ~b.complex
    res = max( abs(a.inf-b.inf) , abs(a.sup-b.sup) ) ;
  else
    res = qdist(real(a),real(b)) + qdist(imag(a),imag(b)) ;
  end

  if rndold
    setround(rndold)
  end


function [nll,g,H,T] = LogisticLoss(w,X,y)
% w(feature,1)
% X(instance,feature)
% y(instance,1)

[n,p] = size(X);

Xw = X*w;
yXw = y.*Xw;

nll = sum(mylogsumexp([zeros(n,1) -yXw]));

if nargout > 1
    if nargout > 2
        sig = 1./(1+exp(-yXw));
        g = -X.'*(y.*(1-sig));
    else
        g = -X.'*(y./(1+exp(yXw)));
    end
end

if nargout > 2
    H = X.'*diag(sparse(sig.*(1-sig)))*X;
end

if nargout > 3
    T = zeros(p,p,p);
    for j1 = 1:p
        for j2 = 1:p
            for j3 = 1:p
                T(j1,j2,j3) = sum(y(:).^3.*X(:,j1).*X(:,j2).*X(:,j3).*sig.*(1-sig).*(1-2*sig));
            end
        end
    end
end


function[C] = localCorrections(x,y,a,G,rq,tol,gradOpt)
% C = localCorrections(x,y,a,G,rq,rMax,tol)
% Local correction matrix for the EBD.
% inputs :
% - x and y : arrays of size N1x2 and N2x2 (clouds of points) inside the
% unit disk.
% - a : the cutoff parameter of the EBD method
% - G : the kernel
% - rq : the radial quadrature that has been computed for G
% - rMax : such that initially, the data was X = rMax*x, Y = rMax*y (before
% rescaling).
% - tol : required tolerance.
% output : the sparse correction matrix C.

N1 = size(x,1);
N2 = size(y,1);
[I,rxyTemp] = rangesearch(y,x,a*1.05);
jdx = cell2mat(I')';
rxy = cell2mat(rxyTemp')';
idx = zeros(size(jdx));
sp_ind = 1;
for x_ind=1:length(I)
    idx(sp_ind:(sp_ind+length(I{x_ind})-1)) = x_ind;
    sp_ind = sp_ind + length(I{x_ind});
end
% Save memory
clear I;
NCI = length(rxy);

if NCI ~= 0

    if gradOpt
        exact_Interactions = G.evalDer(rxy); % Exact local interactions
        C1 = sum(abs(rq.alpha0.*Cp(rq.rho)).*rq.rho.^3); % Bound for the second derivative.
        Ninterp = fix(sqrt(C1)*a/sqrt(8*tol))+10; % Guarantees interpolation error < tol.
        xinterp = linspace(0,a*1.1,Ninterp);
        yinterp = rq.evalDer(xinterp);
        radial_quadratureNear0 = interp1(xinterp,yinterp,rxy); % we remove the radial
    else
        exact_Interactions = G.eval(rxy); % Exact local interactions
        C1 = sum(abs(rq.alpha0.*Cp(rq.rho)).*rq.rho.^2); % Bound for the second derivative.
        Ninterp = fix(sqrt(C1)*a/sqrt(8*tol))+10; % Guarantees interpolation error < tol.
        xinterp = linspace(0,a*1.1,Ninterp);
        yinterp = rq.eval(xinterp);
        radial_quadratureNear0 = interp1(xinterp,yinterp,rxy); % we remove the radial
    end

    % quadrature contribution (using interpolation to avoid evaluating at
    % all points).

    C_val = exact_Interactions - radial_quadratureNear0;
    C = sparse(idx,jdx,C_val,N1,N2);

    if gradOpt
        Tx = sparse(idx,jdx,y(jdx,1) - x(idx,1),N1,N2);
        Ty = sparse(idx,jdx,y(jdx,2) - x(idx,2),N1,N2);
        idx = idx(rxy> 1e-12);
        jdx = jdx(rxy> 1e-12);
        rxy = rxy(rxy> 1e-12);
        R =  sparse(idx,jdx,1./rxy,N1,N2);
        Cx = C.*R.*Tx;
        Cy = C.*R.*Ty;
        C = {Cx,Cy};
    end
else
    % No close interactions
    if gradOpt
        C = {sparse(N1,N2),sparse(N1,N2)};
    else
        C = sparse(N1,N2); % all zeros
    end
end


function [ p2, dp2, p1 ] = jacobi_ss_recur ( x, n, alpha, beta, b, c )

%*****************************************************************************80
%
%% JACOBI_SS_RECUR finds the value and derivative of a Jacobi polynomial.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    12 October 2005
%
%  Author:
%
%    Original FORTRAN77 version by Arthur Stroud, Don Secrest.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Arthur Stroud, Don Secrest,
%    Gaussian Quadrature Formulas,
%    Prentice Hall, 1966,
%    LC: QA299.4G3S7.
%
%  Parameters:
%
%    Input, real X, the point at which polynomials are evaluated.
%
%    Input, integer N, the order of the polynomial to be computed.
%
%    Input, real ALPHA, BETA, the exponents of (1-X) and
%    (1+X) in the quadrature rule.
%
%    Input, real B(N), C(N), the recursion coefficients.
%
%    Output, real P2, the value of J(N)(X).
%
%    Output, real DP2, the value of J'(N)(X).
%
%    Output, real P1, the value of J(N-1)(X).
%
  p1 = 1.0;
  dp1 = 0.0;

  p2 = x + ( alpha - beta ) / ( alpha + beta + 2.0 );
  dp2 = 1.0;

  for i = 2 : n

    p0 = p1;
    dp0 = dp1;

    p1 = p2;
    dp1 = dp2;

    p2 = ( x - b(i) ) * p1 - c(i) * p0;
    dp2 = ( x - b(i) ) * dp1 + p1 - c(i) * dp0;

  end

  return
end


% SIMPDEMO
% This program solves the simple two dimensional problem in Chapter 2,
% and makes figure 2.1.
%
tol=[1.d-6,1.d-6];
%
% Create the mesh for the contour plot of \| f \|.
%
vl=.1:.5:2; vr=2:4:40; v=[vl,vr];
v=.5:4:40;
v=[.25,.5:2:40];
xr=-5:.2:5; n=length(xr); z=zeros(n,n);
for i=1:n
for j=1:n
    w=[xr(i),xr(j)]';
    z(i,j)=norm(simple(w));
end
end
%
% Newton's method
%
params=[40, 1, 0,0];
%
% x0 is a good initial iterate.
%
x0=[2,.5]';
[sn, errsn, ierrn, x_hist]=nsold(x0, 'simple', tol, params);
%
% x1 is a poor initial iterate. The iteration from x1 will stagnate
%    at a point where $F'$ is singular.
%
x1=[3,5]';
[sn2, errsn2, ierrn2, x_hist2]=nsold(x1, 'simple', tol, params);
%
% Draw a contour plot of $\| f \|$.
%
figure(1)
contour(xr,xr,z,v)
hold
%
% Use the x_hist array to plot the iterations on the coutour plot.
%
plot(x_hist(1,:),x_hist(2,:),'-*', x_hist2(1,:),x_hist2(2,:),'-o');
legend('Convergence','Stagnation');
xlabel('x_1');
ylabel('x_2');
axis([0 5 -5 5])
hold
%
% Default
%
params=[40, 1000, .5, 0];
[sc, errsd, ierrd]=nsold(x0, 'simple', tol, params);
inewt=length(errsn(:,1)); cnewt=0:inewt-1;
idefault=length(errsd(:,1)); cdefault=0:idefault-1;
figure(2)
semilogy(cnewt,errsn(:,1),'-',cdefault,errsd(:,1),'--')
legend('Newton','default')


function area = voronoi_areas_direct ( d_num, d_xyz, face_num, face_d, v_xyz )

%*****************************************************************************80
%
%% VORONOI_AREAS_DIRECT computes the area of each polygon in a Voronoi diagram.
%
%  Discussion:
%
%    This algorithm works directly from the Delaunay triangulation and the
%    Voronoi vertex locations, without having to determine the explicit
%    description of each Voronoi polygon as a list of Voronoi vertices.
%    This means this routine should be significantly faster than
%    VORONOI_AREAS, at least for larger problems.
%
%
%    The Delaunay triangle has vertices D1, D2, D3, and a Voronoi vertex
%    V which we imagine is inside the triangle, although in fact it need
%    not be.
%
%    Include now the Delaunay vertex averages, D12, D23, and D31, and
%    the Delaunay triangle can be divided into 3 pieces, contributing to
%    the Voronoi polygons associated with D1, D2 and D3 as follows:
%
%      D1 gets D1:V:D31 + D1:D12:V
%      D2 gets D2:V:D12 + D2:D23:V
%      D3 gets D3:V:D23 + D3:D31:V
%
%    The advantage of computing things this way is that it can be done
%    directly from the Delaunay triangle information.  If we must first
%    determine the explicit list of Voronoi vertices that form a particular
%    Voronoi polygon, then compute the area, it is cumbersome and expensive
%    to determine this connectivity information.
%
%    Presumably, if things have been done correctly, the cases where
%    the Voronoi vertex is outside of the Delaunay triangle will correspond
%    to situations where some of the areas come out negative, and
%    sum to the correct final result.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    11 May 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer D_NUM, the number of data points and Voronoi polygons.
%
%    Input, real D_XYZ(3,D_NUM), the coodinates of the data points.
%
%    Input, integer FACE_NUM, the number of faces, and Voronoi vertices.
%
%    Input, integer FACE_D(3,FACE_NUM), the data points that form
%    each triangle.
%
%    Input, real V_XYZ(3,FACE_NUM), the coordinates of the Voronoi vertices.
%
%    Output, real AREA(D_NUM), the area of each Voronoi polygon.
%
  area = zeros ( d_num, 1 );

  r = 1.0;
%
%  Consider each triangle.
%
  for face = 1 : face_num
%
%  Define some things.
%
    p1 = face_d(1,face);
    p2 = face_d(2,face);
    p3 = face_d(3,face);

    d1 = d_xyz ( 1:3, p1 );
    d2 = d_xyz ( 1:3, p2 );
    d3 = d_xyz ( 1:3, p3 );

    d12 = 0.5 * ( d1 + d2 );
    d23 = 0.5 * ( d2 + d3 );
    d31 = 0.5 * ( d3 + d1 );
    v = v_xyz(1:3,face);
%
%  Force all points to lie on the sphere.
%
    d12 = d12 / norm ( d12 );
    d23 = d23 / norm ( d23 );
    d31 = d31 / norm ( d31 );
    v = v / norm ( v );
%
%  Compute orientation and area of the 6 subtriangles.
%
    o1a = stri_vertices_to_orientation ( d1, v, d31 );
    a1a = stri_vertices_to_area ( r, d1, v, d31 );
    o1b = stri_vertices_to_orientation ( d1, d12, v );
    a1b = stri_vertices_to_area ( r, d1, d12, v );

    o2a = stri_vertices_to_orientation ( d2, v, d12 );
    a2a = stri_vertices_to_area ( r, d2, v, d12 );
    o2b = stri_vertices_to_orientation ( d2, d23, v );
    a2b = stri_vertices_to_area ( r, d2, d23, v );

    o3a = stri_vertices_to_orientation ( d3, v, d23 );
    a3a = stri_vertices_to_area ( r, d3, v, d23 );
    o3b = stri_vertices_to_orientation ( d3, d31, v );
    a3b = stri_vertices_to_area ( r, d3, d31, v );
%
%  Contribute to the Voronoi areas.
%
    area(p1) = area(p1) + o1a * a1a + o1b * a1b;
    area(p2) = area(p2) + o2a * a2a + o2b * a2b;
    area(p3) = area(p3) + o3a * a3a + o3b * a3b;

  end

  return
end


function out = ggrnd(n,p,g)
% GG - random numbers from generalized Gaussian distribution (requires
% Statistics toolbox)
%   R = GGRND(N,P,G) returns a vector of N random numbers having a
%   generalized gaussian distribution with shape parameter P, and scale
%   parameter G, i.e. p(x) \propto exp(-G*|x|^P).
%
%   If P=2 it GGRND returns normally distributed random numbers
%
%   Reference: Johnson, M. E., Computer generation of the exponential power
%   distributions", Journal of Statistical Computation and Simulation, 9,
%   pp. 239--240, 1979
%
%   See also: RANDN, GAMRND

%   Author: G. Gomez-Herrero, german.gomezherrero@ieee.org
%   $Revision: 1.0 $Date: 21/06/2007

if nargin < 3, g=1; end
if abs(p-2)<1e-3,
    x = randn(1,n);
else
    y = gamrnd(1/p,1,1,n);
    x = abs(g.*y).^(1/p);
    pos = find(rand(1,n)<0.5);
    x(pos) = -x(pos);
end
% normalize variance and return
out = x'./sqrt(var(x));
return;


function r = toep_cholesky_upper ( n, g )

%*****************************************************************************80
%
%% TOEP_CHOLESKY_UPPER: upper Cholesky factor of a compressed Toeplitz matrix.
%
%  Discussion:
%
%    The Toeplitz matrix A is supplied in a compressed form G.
%
%    The Toeplitz matrix must be positive semi-definite.
%
%    After factorization, A = R' * R.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    05 November 2012
%
%  Author:
%
%    Michael Stewart
%
%  Reference:
%
%    Michael Stewart,
%    Cholesky factorization of semi-definite Toeplitz matrices.
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%
%    Input, real G(2,N), the compressed Toeplitz matrix.
%    G(1,1:N) contains the first row.
%    G(2,2:N) contains the first column.
%
%    Output, real R(N,N), the upper Cholesky factor.
%
  r = zeros ( n, n );
  r(1,1:n) = g(1,1:n);
  g(1,2:n) = g(1,1:n-1);
  g(1,1) = 0.0;
  for i = 2 : n
    rho = - g(2,i) / g(1,i);
    g(:,i:n) = ( [ 1 rho; rho 1 ] * g(:,i:n) ) / sqrt ( ( 1 - rho ) * ( 1 + rho ) );
    r(i,i:n) = g(1,i:n);
    g(1,i+1:n) = g(1,i:n-1);
    g(1,i) = 0.0;
  end

  return
end


% ASTROTIK by Francesco Santilli
% Compute spherical coordinates of a given position.
%
% Usage: [r,alpha,delta] = spherical(X)
%
% where: X = position [L]
%        r = radius [L]
%        alpha = right ascension [rad]
%        delta = declination [rad]

function [r,alpha,delta] = spherical(X)

    if ~(nargin == 1)
        error('Wrong number of input arguments.')
    end

    D = check(X,1);

    if ~(D==3)
        error('Wrong size of an input argument.')
    end

    xx1 = X(1)^2;
    xx2 = X(2)^2;
    xx3 = X(3)^2;
    r = sqrt(xx1 + xx2 + xx3);
    xy = sqrt(xx1 + xx2);
    alpha = atan2(X(2),X(1));
    delta = atan(X(3)/xy);

end


function out_m = updownsample( in_m,out_x_sz,out_y_sz,is_fourier_flag,is_real_flag )
%
% updownsample - up-sample or down-sample an input series using fourier domain
%                input series needs to be continuous of a high degree
%
% format:   out_m = updownsample( in_m,out_x_sz,out_y_sz,is_fourier_flag,is_real_flag )
%
% input:    in_m                - input matrix for up/down sampling. can be in
%                                 space domain OR in fourier domain, in such
%                                 case, needs to be in matlab format !!!
%                                 (matlab-format = the save as given from fft/fft2)
%           out_x_sz,out_y_sz   - desired number of pixels in the output image
%           is_fourier_flag     - 1: the input is given in the fourier domain
%                                 0: the input is given in the space domain
%                                    (we need to use fft2 to convert to fourier domain)
%           is_real_flag        - 0: the input is a complex matrix -> don't use
%                                    abs() at the output, perform complex
%                                    up/down sampling
%                                 1: the input is real BUT has negative values ->
%                                    use real() at the output
%                                 2: the input is real and positive -> using
%                                    abs() at the output
%
% output:   out_m               - up/down sampled image
%
% NOTE: it is important to specify if the image is REAL or COMPLEX, since
%       if the image is REAL -> we have to use ABS() on the inverse fourier
%       transform (because of roundoff errors of the transform).
%
% NOTE: since a desired amount of pixels is needed at the output, there is
%       no attempt to use matrices which are in size of power of 2. this
%       optimization can not be used in this case
%
% NOTE: input series needs to be CONTINUOUS OF A HIGH DEGREE, since the
%       upsampling is done in the frequency domain, which samples the output
%       grid with SINE-like (harmonic) functions
%
%
% Example:  type "updownsample" for an example. it will load the matlab's
%           child image to a temporary variable, as upsample it
%
%               out = updownsample( A,300,300,0,1 );
%               figure;
%               colormap gray;
%               subplot( 1,2,1 );
%               imagesc( A );
%               subplot( 1,2,2 );
%               imagesc( out );
%

%
% Theory:   the upsampling is done by zero-padding in the input domain BETWEEN the samples,
%           then at the fourier domain, taking the single spectrum (out of the repetition of spectrums)
%           i.e. low pass with Fcutoff=PI/upsample_factor, zeroing the rest of the spectrum
%           and then doing ifft to the distribution.
%           since we have a zero padding operation in time, we need to multiply by the fourier gain.
%
%              +-----------+     +-------+     +---------+     +--------+     +--------+
%   y[n,m] --> | up-sample | --> |  FFT  | --> |   LPF   | --> | * Gain | --> |  IFFT  | --> interpolated
%              | factor M  |     +-------+     | Fc=PI/M |     +--------+     +--------+
%              +-----------+                   +---------+
%
%           this operation is the same as the following one (which has less operations):
%
%              +-------+     +--------+     +--------------+     +--------+
%   y[n,m] --> |  FFT  | --> | * Gain | --> | Zero Padding | --> |  IFFT  | --> interpolated
%              +-------+     +--------+     +--------------+     +--------+
%
%           NOTE THAT, the zero-padding must be such that the D.C. ferquency remains the D.C. frequency
%           and that the zero padding is applied to both positive and negative frequencies.
%           The zero padding actually condences the frequency -> which yields a longer series in the
%           image domain, but without any additional information, thus the operation must be an interpolation.

% Copyright (c) 2003, Ohad Gal
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.

% ==============================================
%   check input parameters
% ==============================================
if (nargin==0)
    hFig    = figure( 'visible','off' );
    hImage  = image;
    Child   = get( hImage,'cdata' );
    close( hFig );
	out     = updownsample( Child,300,300,0,1 );
	figure;
	colormap gray;
	subplot( 1,2,1 );
	imagesc( Child );
	subplot( 1,2,2 );
	imagesc( out );
    return
elseif (nargin<5)
    error( 'UpDownSample - insufficient input parameters' );
end

% ==============================================
% get input image size, and calculate the gain
% ==============================================
[in_y_sz,in_x_sz] = size( in_m );
gain_x = out_x_sz/in_x_sz;
gain_y = out_y_sz/in_y_sz;

% ==============================================
% check if up/down sampling is needed at all
% ==============================================
if (gain_x == 1) & (gain_y==1)

    % same gain -> do not change sampling rate
    % ==========================================
    if is_fourier_flag
        switch is_real_flag
            case 0, out_m = ifft2( in_m );
            case 1, out_m = real( ifft2( in_m ) );
            case 2, out_m = abs( ifft2( in_m ) );
        end
    else
        out_m = in_m;
    end

else

    % upsample or downsample as needed
    % ==================================

    % convert to fourier domain, if input is given in the space domain
    if ~is_fourier_flag
        in_m = fft2(in_m);
    end

    % build grid vectors for the up/down sampling
    % ============================================
    % if the input is even & output is odd-> use floor for all
    % if the output is even & input is odd -> use ceil for all
    % other cases - don't care
    % for downsampling -> the opposite
    if (~mod( in_x_sz,2 ) & (out_x_sz>in_x_sz)) | (mod( in_x_sz,2 ) & (out_x_sz<in_x_sz))
        x_output_space  = max(floor((out_x_sz-in_x_sz)/2),0) + [1:min(in_x_sz,out_x_sz)];
        x_input_space   = max(floor((in_x_sz-out_x_sz)/2),0) + [1:min(in_x_sz,out_x_sz)];
    else
        x_output_space  = max(ceil((out_x_sz-in_x_sz)/2),0) + [1:min(in_x_sz,out_x_sz)];
        x_input_space   = max(ceil((in_x_sz-out_x_sz)/2),0) + [1:min(in_x_sz,out_x_sz)];
    end
    if (~mod( in_y_sz,2 ) & (out_y_sz>in_y_sz)) | (mod( in_y_sz,2 ) & (out_y_sz<in_y_sz))
       y_output_space  = max(floor((out_y_sz-in_y_sz)/2),0) + [1:min(in_y_sz,out_y_sz)];
       y_input_space   = max(floor((in_y_sz-out_y_sz)/2),0) + [1:min(in_y_sz,out_y_sz)];
   else
       y_output_space  = max(ceil((out_y_sz-in_y_sz)/2),0) + [1:min(in_y_sz,out_y_sz)];
       y_input_space   = max(ceil((in_y_sz-out_y_sz)/2),0) + [1:min(in_y_sz,out_y_sz)];
   end

    % perform the up/down sampling
    padded_out_m    = zeros( out_y_sz,out_x_sz );
    in_m            = fftshift(in_m);
    padded_out_m( y_output_space,x_output_space ) = in_m(y_input_space,x_input_space);
    out_m           = (gain_x*gain_y)*ifft2(ifftshift(padded_out_m));

    % check the output format real or complex
    switch is_real_flag
        case 0, % do nothing
        case 1, out_m   = real( out_m );
        case 2, out_m   = abs( out_m );
    end

end


function value = f2 ( x )

%*****************************************************************************80
%
%% F2 evaluates a function of a scalar used in defining P2(Q).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    18 November 2008
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Ian Sloan, Stephen Joe,
%    Lattice Methods for Multiple Integration,
%    Oxford, 1994,
%    ISBN: 0198534728,
%    LC: QA311.S56
%
%  Parameters:
%
%    Input, real X, the value of the argument.
%
%    Output, real VALUE, the value of F2(X).
%
  value = 1.0 + 2.0 * pi * pi * ( x * x - x + 1.0 / 6.0 );

  return
end


function c = imcmass(im)
% IMCMASS  Centre of mass of an image.
%
% IMCMASS computes the centre of mass of a grayscale image weighting the
% pixel coordinates by the pixel intensities.
%
% C = IMCMASS(IM)
%
%   IM is a grayscale 2D image.
%
%   C is the center of mass of the image in [row, col] format.

% Author: Ramon Casero <rcasero@gmail.com>
% Copyright © 2015 University of Oxford
% Version: 0.1.0
%
% University of Oxford means the Chancellor, Masters and Scholars of
% the University of Oxford, having an administrative office at
% Wellington Square, Oxford OX1 2JD, UK.
%
% This file is part of Gerardus.
%
% 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. The offer of this
% program under the terms of the License is subject to the License
% being interpreted in accordance with English Law and subject to any
% action against the University of Oxford being under the jurisdiction
% of the English Courts.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

% check arguments
narginchk(1, 1);
nargoutchk(0, 1);

if (size(im, 3) > 1)
    error('IM must be a grayscale image')
end

% grid for image coordinates
[grow, gcol] = ndgrid(1:size(im, 1), 1:size(im, 2));

% centre of mass
c = single(im(:));
c = sum([c .* grow(:), c .* gcol(:)]) / sum(c);


function b = r8blt_to_r8ge ( n, ml, a )

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

  return
end


function t = transmission_exponential(d, beta, varargin)
%TRANSMISSION_EXPONENTIAL  Compute transmission map using given depth map based
%on the Lambert-Beer law
%   Inputs:
%       -|d|: H-by-W matrix with values of depth for processed image in meters.
%       -|beta|: positive constant that parameterizes the density of haze.
%       Larger values indicate thicker haze.
%
%   Outputs:
%       -|t|: H-by-W matrix with medium transmission values ranging in [0, 1].

t = exp(-beta * d);

end


function bessel_j1_values_test ( )

%*****************************************************************************80
%
%% BESSEL_J1_VALUES_TEST demonstrates the use of BESSEL_J1_VALUES.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 February 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'BESSEL_J1_VALUES_TEST:\n' );
  fprintf ( 1, '  BESSEL_J1_VALUES stores values of \n' );
  fprintf ( 1, '  the Bessel J1 function.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      X            J1(X)\n' );
  fprintf ( 1, '\n' );

  n_data = 0;

  while ( 1 )

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

    if ( n_data == 0 )
      break
    end

    fprintf ( 1, '  %12f  %24.16f\n', x, fx );

  end

  return
end


% KERNEL = binomialFilter(size)
%
% Returns a vector of binomial coefficients of order (size-1) .

% Eero Simoncelli, 2/97.

function [kernel] = binomialFilter(sz)

if (sz < 2)
  error('size argument must be larger than 1');
end

kernel = [0.5 0.5]';

for n=1:sz-2
  kernel = conv([0.5 0.5]', kernel);
end


function [rho, stationary]=inverse_ar_roots(phi)
% Computes the inverted roots of the characteristic equation of an AR(P)
%
% USAGE:
%  [RHO, STATIONARY] = inverse_ar_roots(PARAMETERS)
%
% INPUTS:
%   PARAMETERS - A column vector containing the AR parameters in the order
%                  y(t) = mu + phi(1) y(t-1) + phi(2)y(t-2) + ... + phi(P) y(t-P) + e(t)
%
% OUTPUTS:
%   RHO        - A max(P) by 1 vector containing the inverted roots of the characteristic equation
%                  corresponding to the AR model input
%   STATIONARY - Logical indicating whether the AR process is stationary
%
% COMMENTS:
% Process is stationary if min(abs(rho)) > 1
%
%  See also ARMAROOTS, ACF

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

%Make sure phi is a row vector;

p=length(phi);
%Make sure phi is a row vector
if size(phi,1)>=size(phi,2) && min(size(phi))==1
    phi=phi';
else
    error('Phi should be a column vector.')
end
rho=roots([-fliplr(phi) 1]);
stationary=min(abs(rho))>1;


function tet_mesh_test005 ( )

%*****************************************************************************80
%
%% TET_MESH_TEST005 tests TETRAHEDRON_BARYCENTRIC.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 August 2009
%
%  Author:
%
%    John Burkardt
%
  test1_num = 3;
  test2_num = 5;
  seed = 123456789;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST005\n' );
  fprintf ( 1, '  TETRAHEDRON_BARYCENTRIC computes the barycentric\n' );
  fprintf ( 1, '  coordinates of a point.\n' );
%
%  Choose a random tetrahedron.
%
  for test1 = 1 : test1_num

    [ tet_xyz, seed ] = r8mat_uniform_01 ( 3, 4, seed );

    r8mat_transpose_print ( 3, 4, tet_xyz, '  Random tetrahedron:' );
%
%  Choose barycentric coordinates C1 at random.
%
%  Define a point P.
%
%  Have TETRAHEDRON_BARYCENTRIC compute C2, the barycentric coordinates of P.
%
    for test2 = 1 : test2_num

      [ c1, seed ] = r8vec_uniform_01 ( 4, seed );
      c1_sum = sum ( c1(1:4) );
      c1(1:4) = c1(1:4) / c1_sum;

      p(1:3) = tet_xyz(1:3,1:4) * c1(1:4)';

      c2 = tetrahedron_barycentric ( tet_xyz, p );

      fprintf ( 1, '\n' );
      fprintf ( 1, '  C1 =  %14.6f  %14.6f  %14.6f  %14.6f\n', c1 );
      fprintf ( 1, '  C2 =  %14.6f  %14.6f  %14.6f  %14.6f\n', c2 );

    end
  end

  return
end


% RES = RCONV2(MTX1, MTX2, CTR)
%
% Convolution of two matrices, with boundaries handled via reflection
% about the edge pixels.  Result will be of size of LARGER matrix.
%
% The origin of the smaller matrix is assumed to be its center.
% For even dimensions, the origin is determined by the CTR (optional)
% argument:
%      CTR   origin
%       0     DIM/2      (default)
%       1     (DIM/2)+1

% Eero Simoncelli, 6/96.

function c = rconv2(a,b,ctr)

if (exist('ctr') ~= 1)
  ctr = 0;
end

if (( size(a,1) >= size(b,1) ) & ( size(a,2) >= size(b,2) ))
    large = a; small = b;
elseif  (( size(a,1) <= size(b,1) ) & ( size(a,2) <= size(b,2) ))
    large = b; small = a;
else
  error('one arg must be larger than the other in both dimensions!');
end

ly = size(large,1);
lx = size(large,2);
sy = size(small,1);
sx = size(small,2);

%% These values are one less than the index of the small mtx that falls on
%% the border pixel of the large matrix when computing the first
%% convolution response sample:
sy2 = floor((sy+ctr-1)/2);
sx2 = floor((sx+ctr-1)/2);

% pad with reflected copies
clarge = [
    large(sy-sy2:-1:2,sx-sx2:-1:2), large(sy-sy2:-1:2,:), ...
	large(sy-sy2:-1:2,lx-1:-1:lx-sx2); ...
    large(:,sx-sx2:-1:2),    large,   large(:,lx-1:-1:lx-sx2); ...
    large(ly-1:-1:ly-sy2,sx-sx2:-1:2), ...
      large(ly-1:-1:ly-sy2,:), ...
      large(ly-1:-1:ly-sy2,lx-1:-1:lx-sx2) ];

c = conv2(clarge,small,'valid');


function [u,v,un,vn]=CalculateVelocityFromVortex2D(s,xc,yc,x,y)

%--------------------------------------------------------------------------
%CalculateVelocityFromVortex2D
%Version 1.00
%Created by Stepen
%Created 8 January 2011
%--------------------------------------------------------------------------
%CalculateVelocityFromVortex2D calculates the flow velocity influenced by
%several given vortex flow at several given locations in 2D domain.
%CalculateVelocityFromVortex2D assumes that the vortexs that influence the
%flow are point vortexes.
%--------------------------------------------------------------------------
%Syntax:
%[u,v,un,vn]=CalculateVelocityFromvortex2D(s,xc,yc,x,y)
%Input argument:
%- s (m x 1 num) specifies the strength of all point vortexes.
%- xc (m x 1 num) specifies the x axis location of all point vortexes.
%- yc (m x 1 num) specifies the y axis location of all point vortexes.
%- x (p x q num) specifies the x axis location of the corresponding
%  velocity vector.
%- y (p x q num) specifies the y axis location of the corresponding
%  velocity vector.
%Output argument:
%- u (m x n) specifies the horizontal velocity component at the
%  corresponding location.
%- u (m x n) specifies the vertical velocity component at the corresponding
%  location.
%- un (m x n num) specifies the normalized horizontal velocity component of
%  the corresponding location. un output was generated for plotting
%  purpose. Use this output instead of u for velocity quiver plotting.
%- vn (m x n num) specifies the normalized vertical velocity component of
%  the corresponding location. vn output was generated for plotting
%  purpose. Use this output instead of v for velocity quiver plotting.
%--------------------------------------------------------------------------

%CodeStart-----------------------------------------------------------------
%Checking input point vortexes
    s_size=size(s);
    xc_size=size(xc);
    yc_size=size(yc);
    if (s_size(2)~=1)||(xc_size(2)~=1)||(yc_size(2)~=1)
        error('Input point vortexs must be a i x 1 array!')
    end
    if (numel(s)~=numel(xc))||(numel(s)~=numel(yc))
        error('Input s, xc, and yc do not have the same size!')
    end
    vortexcount=numel(s);
%Checking input location
    x_size=size(x);
    y_size=size(y);
    if (x_size(1)~=y_size(1))||(x_size(2)~=y_size(2))
        error('Input x and y do not have the same size!')
    end
    rowcount=x_size(1);
    colcount=x_size(2);
%Preallocating array for speed
    u=zeros(rowcount,colcount);
    v=zeros(rowcount,colcount);
    un=zeros(rowcount,colcount);
    vn=zeros(rowcount,colcount);
%Calculating influenced velocity
    for i=1:1:rowcount
        for j=1:1:colcount
            for k=1:1:vortexcount
                if (x(i,j)==xc(k))&&(y(i,j)==yc(k))
                    u(i,j)=u(i,j);
                    v(i,j)=v(i,j);
                else
                    r=norm([(x(i,j)-xc(k)),(y(i,j)-yc(k))]);
                    vtheta=-s(k)/(2*pi()*r);
                    u(i,j)=u(i,j)+(vtheta*((y(i,j)-yc(k))/r));
                    v(i,j)=v(i,j)+(vtheta*((xc(k)-x(i,j))/r));
                end
            end
        end
    end
%Calculating normalized vector
    for i=1:1:rowcount
        for j=1:1:colcount
            V=norm([u(i,j),v(i,j)]);
            un(i,j)=u(i,j)/V;
            vn(i,j)=v(i,j)/V;
        end
    end
%CodeEnd-------------------------------------------------------------------

end


function neg_log_p = neglogsigmoid(log_odds)
% neg_log_p = NEGLOGSIGMOID(log_odds)
%   This is mathematically equivalent to -log(sigmoid(log_odds)),
%   but numerically better, when log_odds is large negative

assert(nargin==1)

neg_log_p = -log_odds;
e = exp(-log_odds);
f=find(e<e+1);
neg_log_p(f) = log(1+e(f));


%  Figure 5.45 Feedback Control of Dynamic Systems, 5e
%                   Franklin, Powell, Emami
%
clf
numG=[1 0];
denG=[1 1 4];
K1=0:.1:8;   % K = 2 at breakin point
K2=100;
Kt=[K1 K2];
axis('square')
r=rlocus(numG,denG,Kt);    % roots vs. Kt
plot(r,'--'),grid on;
axis([-6 2 -4 4])
hold on
p=roots(denG);             % find and plot OL poles
z=roots(numG);             % find and plot OL zeros
plot(z(1),0,'o')
plot(real(p(1)),imag(p(1)),'x',real(p(2)),imag(p(2)),'x')
title('Fig. 5.45  Root Locus vs. K with K_{T} = 1')
xlabel('Re(s)')
ylabel('Im(s)')

% now do RL vs. K

Kt1 = 1;
r1=rlocus(numG,denG,Kt1);    % root at Kt = 1
plot(r1,'*')
num = 1;
den = poly(r1);
K = [0 30]
r2=rlocus(num,den,K);    % root locus vs. K
plot(r2,'-')

hold off
axis('normal')


function [A,S]=jade(X,m,Wprev)
% ICA implementation using JADE (used by the ICA function).
%
%   Inputs:
%       X               = Each column of X is a sample from the n sensors.
%       m               = Number of source signals (optional).
%       Wprev           = Timepoint at which to start process (default = 0 seconds).
%
%   Outputs:
%       A               = An n x m estimate of an unknown matrix.
%       S               = An m x T naive estimate of the source signals.
%
% Implementation by: JF Cardoso. (Version 1.5)
%
% Please Cite:
% Reference:
%  @article{CS_iee_94,
%   author = "Jean-Fran\c{c}ois Cardoso and Antoine Souloumiac",
%   journal = "IEE Proceedings-F",
%   title = "Blind beamforming for non {G}aussian signals",
%   number = "6",
%   volume = "140",
%   month = dec,
%   pages = {362-370},
%   year = "1993"}

% Determine the no of rows and colums of the observation matrix.
[n,T]	= size(X);
if nargin==1, m=n ; end;

nem     = m;                    % The nuumber of eigen-matrices to be diagonalized.
seuil	= 1/sqrt(T)/100;    % A statistical threshold for stopping the joint diag.

%% Whiten the Matrix:
% Assuming white noise:
if m<n,
 	[U,D]       = eig((X*X')/T);
	[pu,k]   = sort(diag(D));
 	ibl         = sqrt(pu(n-m+1:n)-mean(pu(1:n-m)));
 	bl          = ones(m,1) ./ ibl ;
 	W           = diag(bl)*U(1:n,k(n-m+1:n))';
 	IW          = U(1:n,k(n-m+1:n))*diag(ibl);
% Assuming no noise:
else
 	IW          = sqrtm((X*X')/T);
 	W           = inv(IW);
end;
Y	= W*X;

%% Cumulant Estimation:
R       = (Y*Y' )/T ;
C       = (Y*Y.')/T ;
Q       = zeros(m*m*m*m,1) ;
index	= 1;

for lx = 1:m ; Yl 	= Y(lx,:);
    for kx = 1:m ; Ykl 	= Yl.*conj(Y(kx,:));
        for jx = 1:m ; Yjkl	= Ykl.*conj(Y(jx,:));
            for ix = 1:m ;
                Q(index) = ...
                (Yjkl * Y(ix,:).')/T -  R(ix,jx)*R(lx,kx) -  R(ix,kx)*R(lx,jx) -  C(ix,lx)*conj(C(jx,kx))  ;
                index	= index + 1;
            end
        end
    end
end

%% Compute and Reshape the significant Eigen Matrices:
[U,D]	= eig(reshape(Q,m*m,m*m));
[la,K]	= sort(abs(diag(D)));

M	= zeros(m,nem*m);
Z	= zeros(m)	;
h	= m*m;
for u=1:m:nem*m,
	Z(:)            = U(:,K(h));
	M(:,u:u+m-1)	= la(h)*Z;
	h               = h-1;
end;

%% Approximate the Diagonalization of the Eigen Matrices:
B       = [ 1 0 0 ; 0 1 1 ; 0 -i i ] ;
Bt      = B' ;

encore	= 1;
if exist('Wprev','var'), V=inv(Wprev); else, V	= eye(m); end

%% Main Loop:
while encore, encore=0;
    for p = 1:m-1,
        for q = p+1:m,

            Ip = p:m:nem*m ;
            Iq = q:m:nem*m ;

            % Computing the Givens Angles:
            g	= [ M(p,Ip)-M(q,Iq)  ; M(p,Iq) ; M(q,Ip) ] ;
            [vcp,D] = eig(real(B*(g*g')*Bt));
            [la, K]	= sort(diag(D));
            angles	= vcp(:,K(3));
            if angles(1)<0 , angles= -angles ; end ;
            c	= sqrt(0.5+angles(1)/2);
            s	= 0.5*(angles(2)-j*angles(3))/c;

            % Update the matrices M and V by a Givens Rotation:
            if abs(s)>seuil,
                encore          = 1 ;
                pair            = [p;q] ;
                G               = [ c -conj(s) ; s c ] ;
                V(:,pair)       = V(:,pair)*G ;
                M(pair,:)       = G' * M(pair,:) ;
                M(:,[Ip Iq]) 	= [ c*M(:,Ip)+s*M(:,Iq) -conj(s)*M(:,Ip)+c*M(:,Iq) ] ;
            end
        end
    end
end

%% Estimation of the mixing matrix and signal separation
A	= IW*V;
S	= V'*Y ;

return ;


function [nll,g,H,T] = LogisticLoss(w,X,y)
% w(feature,1)
% X(instance,feature)
% y(instance,1)

[n,p] = size(X);

Xw = X*w;
yXw = y.*Xw;

nll = sum(mylogsumexp([zeros(n,1) -yXw]));

if nargout > 1
    if nargout > 2
        sig = 1./(1+exp(-yXw));
        g = -X.'*(y.*(1-sig));
    else
        %g = -X.'*(y./(1+exp(yXw)));
        g = -(X.'*(y./(1+exp(yXw))));
    end
end

if nargout > 2
    H = X.'*diag(sparse(sig.*(1-sig)))*X;
end

if nargout > 3
    T = zeros(p,p,p);
    for j1 = 1:p
        for j2 = 1:p
            for j3 = 1:p
                T(j1,j2,j3) = sum(y(:).^3.*X(:,j1).*X(:,j2).*X(:,j3).*sig.*(1-sig).*(1-2*sig));
            end
        end
    end
end


function Qd = calculateQd(tau,alpha,h)
%calculates Qd for noise simulation algorithm from paper DISCRETE SIMULATION OF POWER LAW NOISE
%equation: Qd = h / (2*(2pi)^alpha * tau^(alpha-1))

Qd = h / (2*(2*pi)^alpha * tau^(alpha-1)); %calculate Qd


function [data,data2,Isymbols,Qsymbols]=serial_to_parallel(N)
%Bob Gess, June 2008 for EE 473
%Extracted and modified from original code (QPSK_TX_IQ_RX) written by JC and posted on
%Mathworks 12-23-2005.

%This module generates the data and splits it into the I channel and Q
%channel
%N=1e3;		        % Number of data bits(bit rate)
fs=40*2e3;		    % Sampling frequency
Fn=fs/2;            % Nyquist frequency
Ts=1/fs;	        % Sampling time = 1/fs
T=1/N;		        % Bit time
randn('state',0);   % Keeps PRBS from changing on reruns
td=[0:Ts:(N*T)-Ts]';% Time vector(data)(transpose)
%===================================================================
% Input data
%===================================================================
data=sign(randn(N,1))';%transpose
data1=ones(T/Ts,1)*data;
data2=data1(:);
%length(data2)

%display input data bits in command window
data_2=data2';
data_2=data_2 >0;
x=0;
transmitted_data_bits=data_2(1:(fs+x)/N:end);

%Serial to parallel (alternating)
tiq = [0:Ts*2:(N*T)-Ts]';% Time vector for I and Q symbols(transpose)

bs1=data(1:2:length(data));%odd
symbols=ones(T/Ts,1)*bs1;
Isymbols=symbols(:);%I_waveform
%LI=length(Isymbols)

bs2=data(2:2:length(data));%even
symbols1=ones(T/Ts,1)*bs2;
Qsymbols=symbols1(:);%Q_waveform
%LQ=length(Qsymbols)

%=====================================================================
%Plots
%======================================================================
figure(1)
subplot(3,1,1)
plot(td,data2)
axis([0 20/N -2 2]);
grid on
xlabel('                                                          Time')
ylabel('Amplitude')
title('Input Data')

subplot(3,1,2)
plot(tiq,Isymbols)
axis([0 20/N -2 2]);
grid on
xlabel('                                                          Time')
ylabel('Amplitude')
title('I Channel(one bit/symbol(phase)) Data')

subplot(3,1,3)
plot(tiq,Qsymbols)
axis([0 20/N -2 2]);
grid on
xlabel('                                                          Time')
ylabel('Amplitude')
title('Q Channel(one bit/symbol(phase)) Data')