function h = r8mat_house_pre ( n, a, row, col )

%*****************************************************************************80
%
%% R8MAT_HOUSE_PRE computes a Householder pre-multiplier matrix.
%
%  Discussion:
%
%    H(ROW,COL) has the property that the COL-th column of
%    H(ROW,COL)*A is zero from entry ROW+1 to the end.
%
%    In the most common case, where a QR factorization is being computed,
%    ROW = COL.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    17 April 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrices.
%
%    Input, real A(N,N), the matrix whose Householder matrix
%    is to be computed.
%
%    Input, integer ROW, COL, specify the location of the
%    entry of the matrix A which is to be preserved.  The entries in
%    the same column, but higher rows, will be zeroed out if A is
%    premultiplied by H.
%
%    Output, real H(N,N), the Householder matrix.
%

%
%  Set up the vector V.
%
  a_col(1:row-1,1) = 0.0;
  a_col(row:n,1) = a(row:n,col);

  v = r8vec_house_column ( n, a_col, row );
%
%  Form the matrix H(V).
%
  h = r8mat_house_form ( n, v );

  return
end


function [result] = mc(x)

%MC calculates the medcouple, a robust measure of skewness.
%
% The medcouple is described in:
%    Brys, G., Hubert, M. and Struyf, A. (2004),
%    "A Robust Measure of Skewness",
%    Journal of Computational and Graphical Statistics,
%    13, 996-1017.
%
% Required input arguments:
%    x : Data matrix.
%        Rows of x represent observations, and columns represent variables.
%        Missing values (NaN's) and infinite values (Inf's) are allowed, since observations (rows)
%        with missing or infinite values will automatically be excluded from the computations.
%
% The output of MC is a vector containing the medcouple for each column
%     of the data matrix x.
%
% Example:
%    result = mc([chi2rnd(5,1000,1) trnd(3,1000,1)]);
%
% This function is part of LIBRA: the Matlab Library for Robust Analysis,
% available at:
%              http://wis.kuleuven.be/stat/robust.html
%
% Written by Guy Brys
% Last Update: 31/07/2007

if (nargin<1)
    error('No input arguments')
end
[n,p] = size(x);
if n==1
    x = x';
    p = 1;
end
x(sum(~isfinite(x),2)>0,:)=[];
n = size(x,1);
if (n>50000)
    error('When there are more than 50000 observations, the MC may be uncomputable due to memory limitations.')
elseif (n>100)
    result = mcc(x);
elseif n==0
    error('Due to missing values, the data matrix is empty and the MC can not be computed.')
else
    result = 0.5*(-mcc(repmat(max(x),size(x,1),1)-x)+mcc(x));
end


function fx = p18_fx ( x )

%*****************************************************************************80
%
%% P18_FX evaluates the function for problem 18.
%
%  Discussion:
%
%    F(X) = 10^14 * (x-1)^7, but is written in term by term form.
%
%    This polynomial becomes difficult to evaluate accurately when
%    written this way.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 October 2011
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Cleve Moler,
%    Numerical Computing with MATLAB,
%    SIAM, 2004,
%    ISBN13: 978-0-898716-60-3,
%    LC: QA297.M625.
%
%  Parameters:
%
%    Input, real X(*), the point at which F is to be evaluated.
%
%    Output, real FX(*), the value of the function at X.
%
  fx = 10.0^14 * ( ...
            x.^7 ...
     -  7 * x.^6 ...
     + 21 * x.^5 ...
     - 35 * x.^4 ...
     + 35 * x.^3 ...
     - 21 * x.^2 ...
     +  7 * x ...
     -  1 );

  return
end


function M=movwv2at(N,Np);
%MOVWV2AT Oscillating structure of the interferences of the WVD.
%	M=MOVWV2AT(N,Np) generates the movie frames illustrating the
%	influence of the distance between two components on the oscillating
%	structure of the interferences of the WVD.
%
%	N : number of points for the signal;
%	Np : number of snapshots (default : 9)
%	M : matrix of movie frames.
%
%	Example :
%	 M=movwv2at(128,15); movie(M,10);

%	O. Lemoine - May 1996.
%	Copyright (c) 1996 by CNRS (France).
%
%  This program is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2 of the License, or
%  (at your option) any later version.
%
%  This program is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software
%  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

if nargin<1,
 error('At least one argument required');
elseif nargin==1,
 Np=9;
end
Np=odd(Np);

M  = moviein(Np);

c  = Np/0.2;

ampl=amgauss(N/2,N/2,N/2);

for k=1:(Np+1)/2,
 sig=[ampl.*fmconst(N/2,0.25-(k-1)/c);ampl.*fmconst(N/2,0.25+(k-1)/c)];
 [tfr,t,f]=tfrwv(sig);
 Max=max(max(tfr));V=[0.1 0.3 0.5 0.7 0.9]*Max;
 contour(t,f,tfr,V);xlabel('Time'); ylabel('Frequency');
 title('Wigner-Ville distribution'); axis('xy')
 M(:,k) = getframe;
end

for k=(Np+3)/2:Np,
 M(:,k) = M(:,Np+1-k);
end


function rank = gray_code_rank ( n, t )

%*****************************************************************************80
%
%% GRAY_CODE_RANK computes the rank of a Gray code element.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 January 2011
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Kreher, Douglas Simpson,
%    Combinatorial Algorithms,
%    CRC Press, 1998,
%    ISBN: 0-8493-3988-X,
%    LC: QA164.K73.
%
%  Parameters:
%
%    Input, integer N, the number of digits in each element.
%    N must be positive.
%
%    Input, integer T(N), an element of the Gray code.
%    Each entry T(I) is either 0 or 1.
%
%    Output, integer RANK, the rank of the element.
%

%
%  Check.
%
  ierror = gray_code_check ( n, t );

  if ( ierror ~= 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'GRAY_CODE_RANK - Fatal error!\n' );
    fprintf ( 1, '  The input array is illegal.\n' );
    fprintf ( 1, '  IERROR = %d\n', ierror );
    error ( 'GRAY_CODE_RANK - Fatal error!' );
  end

  rank = 0;
  b = 0;

  for i = n - 1 : -1 : 0

    if ( t(n-i) ~= 0 )
      b = 1 - b;
    end

    if ( b == 1 )
      rank = rank + 2^i;
    end

  end

  return
end


function [centroid, dimension_weight, class] = Subspace_Kmeans(data, iteration, K, beta, verbose)

% Pre-allocate weights
centroid = zeros(K,size(data,2)); % centroid for each class
class = zeros(size(data,1),1); % classification result for each observation
dimension_weight = ones(K,size(data,2)); % weights for each dimension in each class

% Initialization
n = size(data,1);
J = 0; % used to record objective function value

% First round
centroid = data(unidrnd(n,K,1),:); % randomly choose K initial centroids
dimension_weight = 1/size(data,2) * dimension_weight; % initialize weights for each dimension using uniform distribution

% Partially optimization
for i = 1:iteration

    J0 = J;

    % body part
    class = classify(centroid,dimension_weight,data);
    centroid = centroid_update(centroid,data,class);
    [dimension_weight,J] = dimension_weight_update(K,centroid,class,dimension_weight,data,beta);

    % whether display intermediate information
    if(verbose == 1)
        disp(['Objective Function(J) Value: ',num2str(J)]);
    end

    % early stop condition
    if(abs(J-J0)<1e-9)
        fprintf('*** Clustering terminates after %i iterations ***\n',i);
        break;
    end
end
end

% Details on the scheme of updating dimension weights
function [alpha,J] = dimension_weight_update(K,m,c,alpha,data,beta)
all_matrix = zeros(size(data,1),size(data,2));
for i = 1:size(data,1)
    all_matrix(i,:) = (m(c(i,1),:)-data(i,:)) .* (m(c(i,1),:)-data(i,:));
end
J = 0;

for i = 1:K
    class_matrix = all_matrix(c==i,:); % pick out data in all_matrix that corresponds to class i
    t_sum = sum(class_matrix,1) + 1e-9; % adding a small positive constant to make weights computatable
    J = J + (alpha(i,:).^beta) * t_sum';
    if(size(class_matrix,1)>0)
        for j = 1:size(alpha,2)
            % variables used to avoid redundant calculation
            tt_numerator = t_sum(1,j);
            t_denominator = sum(tt_numerator./t_sum,2);
            alpha(i,j) = 1/((t_denominator+1e-9)^(1/(beta-1)));
        end
    else
        continue;
    end
end

% Normalize
for i = 1:size(alpha,1)
    alpha(i,:) = alpha(i,:)./(sum(alpha(i,:),2));
end

end

function [result] = classify(m,alpha,data)

result = zeros(size(data,1),1);

% Construct temporary matrix for efficiently computing dimention-weighted distance
matrix = zeros(size(m,1),size(data,2));

for i = 1:size(result,1)
    for j = 1:size(m,1)
        matrix(j,:) = (m(j,:)-data(i,:)) .* (m(j,:)-data(i,:));
    end
    temp = sum(alpha .* matrix,2);

    % To avoid more than one class having the minimum distance.
    t_index = find(temp == min(temp));
    result(i,1) = t_index(1,1);
end
end

function [m] = centroid_update(m,data,c)
for i = 1:size(m,1)
    if(~isempty(data(c==i,:)))
        m(i,:) = mean(data(c==i,:),1);
    else
        continue;
    end
end
end


% The Hurst exponent
%--------------------------------------------------------------------------
% The first 20 lines of code are a small test driver.
% You can delete or comment out this part when you are done validating the
% function to your satisfaction.
%
% Bill Davidson, quellen@yahoo.com
% 13 Nov 2005

function []=hurst_exponent()
disp('testing Hurst calculation');

n=100;
data=rand(1,n);
plot(data);

hurst=estimate_hurst_exponent(data);

[s,err]=sprintf('Hurst exponent = %.2f',hurst);disp(s);

%--------------------------------------------------------------------------
% This function does dispersional analysis on a data series, then does a
% Matlab polyfit to a log-log plot to estimate the Hurst exponent of the
% series.
%
% This algorithm is far faster than a full-blown implementation of Hurst's
% algorithm.  I got the idea from a 2000 PhD dissertation by Hendrik J
% Blok, and I make no guarantees whatsoever about the rigor of this approach
% or the accuracy of results.  Use it at your own risk.
%
% Bill Davidson
% 21 Oct 2003

function [hurst] = estimate_hurst_exponent(data0)   % data set

data=data0;         % make a local copy

[M,npoints]=size(data0);

yvals=zeros(1,npoints);
xvals=zeros(1,npoints);
data2=zeros(1,npoints);

index=0;
binsize=1;

while npoints>4

    y=std(data);
    index=index+1;
    xvals(index)=binsize;
    yvals(index)=binsize*y;

    npoints=fix(npoints/2);
    binsize=binsize*2;
    for ipoints=1:npoints % average adjacent points in pairs
        data2(ipoints)=(data(2*ipoints)+data((2*ipoints)-1))*0.5;
    end
    data=data2(1:npoints);

end % while

xvals=xvals(1:index);
yvals=yvals(1:index);

logx=log(xvals);
logy=log(yvals);

p2=polyfit(logx,logy,1);
hurst=p2(1); % Hurst exponent is the slope of the linear fit of log-log plot

return;


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

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

[probability indices] = max(sigmoid(all_theta * X'));
p = indices';

end


function value = r4_modp ( x, y )

%*****************************************************************************80
%
%% R4_MODP returns the nonnegative remainder of R4 division.
%
%  Discussion:
%
%    If
%      REM = R4_MODP ( X, Y )
%      RMULT = ( X - REM ) / Y
%    then
%      X = Y * RMULT + REM
%    where REM is always nonnegative.
%
%    The MOD function computes a result with the same sign as the
%    quantity being divided.  Thus, suppose you had an angle A,
%    and you wanted to ensure that it was between 0 and 360.
%    Then mod(A,360.0) would do, if A was positive, but if A
%    was negative, your result would be between -360 and 0.
%
%    On the other hand, R4_MODP(A,360.0) is between 0 and 360, always.
%
%  Example:
%
%        I         J     MOD R4_MODP  R4_MODP Factorization
%
%      107        50       7       7    107 =  2 *  50 + 7
%      107       -50       7       7    107 = -2 * -50 + 7
%     -107        50      -7      43   -107 = -3 *  50 + 43
%     -107       -50      -7      43   -107 =  3 * -50 + 43
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    09 January 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real X, the number to be divided.
%
%    Input, real Y, the number that divides X.
%
%    Output, real VALUE, the nonnegative remainder when X is divided by Y.
%
  if ( y == 0.0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R4_MODP - Fatal error!\n' );
    fprintf ( 1, '  R4_MODP ( X, Y ) called with Y = %f\n', y );
    error ( 'R4_MODP - Fatal error!' );
  end

  value = mod ( x, y );

  if ( value < 0.0 )
    value = value + abs ( y );
  end

  return
end


function [cCombo,dataRecur]=getNextRucksackFill(param1,N)
%%GETNEXTRUCKSACKFILL Get the next combination of items with given positive
%               weights that will fit into a rucksack (knapsack, backpack)
%               with a capacity of N. This goes through all feasible
%               combinations (starting with putting nothing in the bag).
%               Going through all feasible combinations is not an efficient
%               approach to determining the set of items that can be placed
%               into the bag to maximize its weight.
%
%INPUTS: param1 To generate the first combination (which will be the empty
%               set - put nothing in the bag) this is w, an nX1 vector
%               holding the weights of each of the items that can be placed
%               in the bag. All weights>0, but need not be integer.
%               Otherwise, this is the dadaRecur output from the previous
%               call to this function.
%             N The capacity of the rucksack. N>0.
%
%OUTPUTS: cCombo The tX1 vector of indices of the items in w that form the
%                current combination of elements that fit into the
%                rucksack. An empty matrix is the first set (out nothing
%                in) and is also returned when the final value has been
%                passed.
%      dataRecur A data structure that can be passed to this function on
%                subsequent calls to get the next value in the sequence. If
%                an empty matrix is reurned, then the final value in the
%                sequence has been passed.
%
%This function implements Algorithm F of Section 7.2.1.3 of [1].
%
%EXAMPLE:
%This displays all of the combination indices for a simple problem.
% w=[1;1.5;5;3;1.5];
% N=6;
% [cCombo,dataRecur]=getNextRucksackFill(w,N);
% while(~isempty(dataRecur))
%     cCombo
%     [cCombo,dataRecur]=getNextRucksackFill(dataRecur);
% end
%
%REFERENCES:
%[1] D. E. Knuth, The Art of Computer Programming. Vol. 4A: Combinatorial
%    Algorithms, Part I, Boston: Addison-Wesley, 2011.
%
%October 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

%If we are getting the first filling possibility (the empty sack).
if(nargin==2)
    w=param1;%The set of all-positive weights.
    [w,idx]=sort(w,'ascend');

    n=length(w);
    deltaJ=w(2:n)-w(1:(n-1));

    %The extra one at the beginning is for unassigned weights.
    c=zeros(n+1,1);

    %Step F1, Initialize
    t=0;
    c(0+1)=n;
    r=N;
    %The initial combination is the empty set.
    cCombo=[];
    dataRecur.w=w;
    dataRecur.idx=idx;
    dataRecur.deltaJ=deltaJ;
    dataRecur.c=c;
    dataRecur.r=r;
    dataRecur.t=t;
    return;
else
    dataRecur=param1;
    w=dataRecur.w;
    idx=dataRecur.idx;
    deltaJ=dataRecur.deltaJ;
    c=dataRecur.c;
    r=dataRecur.r;
    t=dataRecur.t;
end

%Step F3, try to add w(0).
if(c(t+1)>0&&r>=w(0+1))
    t=t+1;
    c(t+1)=0;
    r=r-w(0+1);

    %Step F2, visit the current combination.
    cCombo=idx(c(2:(t+1))+1);
    dataRecur.t=t;
    dataRecur.c=c;
    dataRecur.r=r;
    return;
end

while(1)
    if(t==0)%We have passed the last combination.
        cCombo=[];
        dataRecur=[];
        return;
    end

    if(c(t-1+1)>c(t+1)+1&&r>=deltaJ(c(t+1)+1))
        %Step F4
        c(t+1)=c(t+1)+1;
        r=r-deltaJ(c(t+1));
        %Step F2, visit the current combination.
        cCombo=idx(c(2:(t+1))+1);
        dataRecur.t=t;
        dataRecur.c=c;
        dataRecur.r=r;
        return
    else
        %Step F5
        r=r+w(c(t+1)+1);
        t=t-1;
    end
end

end

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


function [ cluster_center, seed ] = cluster_initialize_3 ( dim_num, ...
  point_num, cluster_num, point, seed )

%*****************************************************************************80
%
%% CLUSTER_INITIALIZE_3 initializes the cluster centers to random values.
%
%  Discussion:
%
%    In this case, each point is randomly assigned to a cluster, and
%    the cluster centers are then computed as the centroids of the points
%    in the cluster.
%
%    Random integers for this function can be generated by the MATLAB
%    function randi(), or by the source code i4_uniform();
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    11 February 2014
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer DIM_NUM, the number of spatial dimensions.
%
%    Input, integer POINT_NUM, the number of points.
%
%    Input, integer CLUSTER_NUM, the number of clusters.
%
%    Input, real POINT(DIM_NUM,POINT_NUM), the coordinates
%    of the points.
%
%    Input, integer SEED, a seed for the random
%    number generator.
%
%    Output, real CLUSTER_CENTER(DIM_NUM,CLUSTER_NUM),
%    the coordinates of the cluster centers.
%
%    Output, integer SEED, a seed for the random
%    number generator.
%

%
%  Assign one point to each cluster center.
%
  for i = 1 : cluster_num
    cluster_center(1:dim_num,i) = point(1:dim_num,i);
  end

  cluster_population(1:cluster_num) = 1;
%
%  The rest of the points get assigned randomly.
%
  for i = cluster_num + 1 : point_num
    if ( 0 )
      [ j, seed ] = i4_uniform ( 1, cluster_num, seed );
    else
      j = randi ( [ 1, cluster_num ], 1, 1 );
    end
    cluster_center(1:dim_num,j) = cluster_center(1:dim_num,j) + point(1:dim_num,i);
    cluster_population(j) = cluster_population(j) + 1;
  end
%
%  Now average the points to get the centroid.
%
  for i = 1 : cluster_num
    cluster_center(1:dim_num,i) = cluster_center(1:dim_num,i) ...
      / cluster_population(i);
  end

  return
end


function q = simpqual ( p, t, type )

%*****************************************************************************80
%
%% SIMPQUAL computes the simplex quality.
%
%  Discussion:
%
%    If TYPE = 1, the quality measure used computes the ratio of
%    the radii of the inscribed and circumscribed circles or spheres.
%
%    Only dimensions 1, 2 and 3 can be handled.  In 1D, the quality is always 1.
%
%  Licensing:
%
%    (C) 2004 Per-Olof Persson.
%    See COPYRIGHT.TXT for details.
%
%  Reference:
%
%    Per-Olof Persson and Gilbert Strang,
%    A Simple Mesh Generator in MATLAB,
%    SIAM Review,
%    Volume 46, Number 2, June 2004, pages 329-345.
%
%  Parameters:
%
%    Input, real P(NP,ND), the coordinates of a set of nodes.
%
%    Input, integer T(NT,1:ND+1), a list of the nodes which make up each
%    simplex in the mesh.
%
%    Input, integer TYPE, specifies the quality measure:
%    1: Compute the radius ratio (default)
%    2: Use an approximate formula.
%
%    Output, real Q(NT), the simplex quality measure.
%
  if ( nargin < 3 )
    type = 1;
  end

  switch type
%
%  RADIUS RATIO
%
    case 1

      switch size ( p, 2 )

        case 1

          q = ones ( 1, size(t,2) );

        case 2

          a=sqrt(sum((p(t(:,2),:)-p(t(:,1),:)).^2,2));
          b=sqrt(sum((p(t(:,3),:)-p(t(:,1),:)).^2,2));
          c=sqrt(sum((p(t(:,3),:)-p(t(:,2),:)).^2,2));
          r=1/2*sqrt((b+c-a).*(c+a-b).*(a+b-c)./(a+b+c));
          R=a.*b.*c./sqrt((a+b+c).*(b+c-a).*(c+a-b).*(a+b-c));
          q = 2 * r ./ R;

        case 3

          d12=p(t(:,2),:)-p(t(:,1),:);
          d13=p(t(:,3),:)-p(t(:,1),:);
          d14=p(t(:,4),:)-p(t(:,1),:);
          d23=p(t(:,3),:)-p(t(:,2),:);
          d24=p(t(:,4),:)-p(t(:,2),:);
          d34=p(t(:,4),:)-p(t(:,3),:);
          v=abs(dot(cross(d12,d13,2),d14,2))/6;
          s1=sqrt(sum(cross(d12,d13,2).^2,2))/2;
          s2=sqrt(sum(cross(d12,d14,2).^2,2))/2;
          s3=sqrt(sum(cross(d13,d14,2).^2,2))/2;
          s4=sqrt(sum(cross(d23,d24,2).^2,2))/2;
          p1=sqrt(sum(d12.^2,2)).*sqrt(sum(d34.^2,2));
          p2=sqrt(sum(d23.^2,2)).*sqrt(sum(d14.^2,2));
          p3=sqrt(sum(d13.^2,2)).*sqrt(sum(d24.^2,2));
          q=216*v.^2./(s1+s2+s3+s4)./sqrt((p1+p2+p3).*(p1+p2-p3).* ...
                                    (p1+p3-p2).*(p2+p3-p1));

        otherwise

          error ( 'SIMPQUAL - Dimension not implemented.' );
    end
%
%  APPROXIMATE FORMULA
%
    case 2

     switch size(p,2)

       case 1

         q = ones ( 1, size(t,2) );

       case 2

         d12 = sum((p(t(:,2),:)-p(t(:,1),:)).^2,2);
         d13 = sum((p(t(:,3),:)-p(t(:,1),:)).^2,2);
         d23 = sum((p(t(:,3),:)-p(t(:,2),:)).^2,2);
         q = 4*sqrt(3)*abs(simpvol(p,t))./(d12+d13+d23);

       case 3

         d12 = sum((p(t(:,2),:)-p(t(:,1),:)).^2,2);
         d13 = sum((p(t(:,3),:)-p(t(:,1),:)).^2,2);
         d14 = sum((p(t(:,4),:)-p(t(:,1),:)).^2,2);
         d23 = sum((p(t(:,3),:)-p(t(:,2),:)).^2,2);
         d24 = sum((p(t(:,4),:)-p(t(:,2),:)).^2,2);
         d34 = sum((p(t(:,4),:)-p(t(:,3),:)).^2,2);
         q = 216*abs(simpvol(p,t))/sqrt(3)./(d12+d13+d14+d23+d24+d34).^(3/2);

       otherwise

         error ( 'SIMPQUAL - Dimension not implemented.' );
    end

  otherwise

    error ( 'SIMPQUAL - Incorrect type.' );

  end

  return
end


function A = multiheadAttention(Q, K, V,nvp)
% multiheadAttention   Multi-head Attention
%
%   A = multiheadAttention(Q, K, V) computes scaled dot product attention
%   for multiple attention heads as outlined in [1] (see Section 3.2.1 and
%   Figure 2). Note that this function computes the attention for multiple
%   attention heads at once for efficiency. Q is a collection of query
%   matrices, K is a collection of key matrices and V is a collection of
%   value matrices. The output A is a collection of attention matrices. See
%   below for details.
%
%   Inputs:
%       Q   - numFeatures-by-numInputSubWords-by-numHeads-by-numObs array of queries.
%       K   - numFeatures-by-numAllSubWords-by-numHeads-by-numObs array of keys.
%       V   - numFeatures-by-numAllSubWords-by-numHeads-by-numObs array of values.
%
%   Outputs:
%       A   - numFeatures-by-numInputSubWords-by-numHeads-by-numObs array of attention matrices.
%
%   A = multiheadAttention(Q, K, V, 'PARAM1', VAL1, 'PARAM2', VAL2, ...)
%   specifies the optional parameter name/value pairs:
%
%     'CausalMask'  - A scalar logical to turn causal masking on or off. Causal
%                     masking prevents tokens at time T attending to tokens
%                     at time S<T. The default is true.
%
%     'Dropout'     - The dropout probability for the attention
%                     probabilities. The default is 0.
%
%     'InputMask'   - A logical mask to mask attending to particular
%                     tokens, for example padding tokens. The default is
%                     [], interpreted as not applying any masking.
%
%   References:
%
%   [1] Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion
%       Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin, "Attention
%       Is All You Need", https://arxiv.org/abs/1706.03762
arguments
    Q
    K
    V
    nvp.CausalMask (1,1) logical = true
    nvp.Dropout (1,1) double {mustBeNonnegative,mustBeLessThanOrEqual(nvp.Dropout,1)} = 0
    nvp.InputMask = []
end

% We compute attention weights by taking the product between Q and K
% matrices. W is numAllSubWords-by-numInputSubWords-by-numHeads. Each
% element of W is the dot product of a query vector from Q and a key vector
% from K.
W = dlmtimes(permute(K, [2 1 3 4]), Q);

% Divide by square root of d
W = W./sqrt(size(Q,1));

% Apply masking
W = transformer.layer.maskAttentionWeights(W,'CausalMask',nvp.CausalMask,'InputMask',nvp.InputMask);

% Apply softmax
W = softmax(W, 'DataFormat', 'CTUB');

% Apply dropout
W = transformer.layer.dropout(W,nvp.Dropout);

% We compute the attention by taking products between the attention weights
% W and V. A is numFeatures-by-numInputSubWords-by-numHeads. One
% interpretation of A is that it is the expected value of V according to
% the probability distribution given by W.
A = dlmtimes(V, W);
end


function jd = julday(y,m,d,h)
% JULDAY  Conversion of date as given by
%         y ... year (four digits)
%         m ... month
%         d ... day
%         h ... hour and fraction hereof
%         The conversion is only valid in the time span
%         from March 1, 1900 to February 28, 2100

%  For further information see
%  Meeus, Jean (1991) Astronomical Algorithms,
%         Willmann-Bell, Richmond, Virginia, p. 59--62

%  Written by Kai Borre
%  February 14, 2001

      if m <= 2, y = y-1; m = m+12; end
      jd = floor(365.25*(y+4716))+floor(30.6001*(m+1))+d+h/24-1537.5;
%      mjd = jd-2400000.5;
%%%%%%% end julday.m  %%%%%%%%%%%%


function geometry_test0126 ( )

%*****************************************************************************80
%
%% TEST0126 tests SPHERE_CAP_VOLUME_3D, SPHERE_CAP_VOLUME_ND.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  dim_num = 3;
  ntest = 12;

  center(1:3) = [ 0.0, 0.0, 0.0 ];
  r = 1.0;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST0126\n' );
  fprintf ( 1, '  SPHERE_CAP_VOLUME_3D computes the volume of a\n' );
  fprintf ( 1, '    spherical cap, defined by a plane that cuts the\n' );
  fprintf ( 1, '    sphere to a thickness of H units.\n' );
  fprintf ( 1, '  SPHERE_CAP_VOLUME_ND does the same operation,\n' );
  fprintf ( 1, '    but in N dimensions.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Using a radius R = %f\n', r );

  fprintf ( 1, '\n' );
  fprintf ( 1, '        H           Cap        Cap\n' );
  fprintf ( 1, '                    volume_3d  volume_nd\n' );
  fprintf ( 1, '\n' );

  for i = 0 : ntest

    h = 2.0 * r * i / ntest;

    volume1 = sphere_cap_volume_3d ( r, h );

    volume2 = sphere_cap_volume_nd ( dim_num, r, h );

    fprintf ( 1, '  %10f  %10f  %10f\n', h, volume1, volume2 );

  end

  return
end


function x = ncc_compute_points ( n )

%*****************************************************************************80
%
%% NCC_COMPUTE_POINTS: points of a Newton-Cotes Closed quadrature rule.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 November 2009
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order.
%
%    Output, real X(N), the abscissas.
%
  x_min = -1.0;
  x_max =  1.0;

  if ( n == 1 )

    x(1) = ( x_max + x_min ) / 2.0;

  else

    for i = 1 : n
      x(i) = ( ( n - i     ) * x_min   ...
             + (     i - 1 ) * x_max ) ...
             / ( n     - 1 );
    end

  end

  return
end


function divdif_test08 ( )

%*****************************************************************************80
%
%% DIVDIF_TEST17 tests NCO_RULE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 June 2011
%
%  Author:
%
%    John Burkardt
%
  norder = 8;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'DIVDIF_TEST17\n' );
  fprintf ( 1, '  NCO_RULE computes open Newton Cotes formulas.\n' );
  fprintf ( 1, '\n' );

  [ xtab, weight ] = nco_rule ( norder );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Newton-Cotes Open Quadrature Rule:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      Abscissa       Weight\n' );
  fprintf ( 1, '\n' );

  for i = 1 : norder
    fprintf ( 1, '  %3d  %14f  %14f\n', i, xtab(i), weight(i) );
  end

  return
end


function exact = p40_exact ( )

%*****************************************************************************80
%
%% P40_EXACT returns the exact integral for problem 40.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 November 2009
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Output, real EXACT, the value of the integral.
%
  alpha = p40_param_get ( );

  exact = ( ( 1.0 - 0.25 * pi )^( alpha + 1.0 ) ...
          + (     + 0.25 * pi )^( alpha + 1.0 ) ) ...
          / ( alpha + 1.0 );

  return
end


function quad = fibonacci_lattice_q ( k, f )

%*****************************************************************************80
%
%% FIBONACCI_LATTICE_Q applies a Fibonacci lattice integration rule in 2D.
%
%  Discussion:
%
%    Because this is a standard lattice rule, it is really only suited
%    for functions which are periodic, of period 1, in both X and Y.
%
%    The related routines FIBONACCI_LATTICE_S and FIBONACCI_LATTICE_B
%    may be helpful in cases where the integrand does not satisfy this
%    requirement.
%
%  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, integer K, the index of the Fibonacci number to be used.
%    K must be at least 3.
%
%    Input, external real F, the name of the user-supplied routine
%    which evaluates the function, of the form:
%    function f ( dim_num, x )
%    integer dim_num
%    real f
%    real x(dim_num)
%    f = ...
%
%    Output, real QUAD, the estimated integral.
%
  dim_num = 2;

  quad = 0.0;

  m = fibonacci ( k );

  z(1) = 1;
  z(2) = fibonacci ( k - 1 );

  for j = 0 : m - 1
    x(1:dim_num) = mod ( j * z(1:dim_num) / m, 1.0 );
    quad = quad + f ( dim_num, x );
  end

  quad = quad / m;

  return
end


function n = vectorNorm3d(v)
%VECTORNORM3D Norm of a 3D vector or of set of 3D vectors.
%
%   N = vectorNorm3d(V);
%   Returns the norm of vector V.
%
%   When V is a N-by-3 array, compute norm for each vector of the array.
%   Vectors are given as rows. Result is then a N-by-1 array.
%
%   NOTE: Computes only the Euclidean norm.
%
%   See also
%     vectors3d, normalizeVector3d, vectorAngle3d, hypot3
%

% ------
% Author: David Legland
% E-mail: david.legland@inrae.fr
% Created: 2005-02-21
% Copyright 2005-2022 INRA - TPV URPOI - BIA IMASTE

n = sqrt(sum(v.*v, ndims(v)));


function [Q] = VBA_spm_Q(a,n,q)
% returns an (n x n) (inverse) autocorrelation matrix for an AR(p) process
% FORMAT [Q] = spm_Q(a,n,q)
%
% a  - vector of (p) AR coefficients
% n  - size of Q
% q  - switch to return inverse autocorrelation or precision [default q = 0]
%__________________________________________________________________________
% spm_Q uses a Yule-Walker device to compute K where:
%
% y = K*z
%
% such that y is an AR(p) process generated from an i.i.d innovation
% z.  This means
%
% cov(y) = <K*z*z'*K> = K*K'
%
% If called with q ~= 0, a first order process is assumed when evaluating
% the precision (inverse covariance) matrix; i.e., a = a(1)
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

% Karl Friston
% $Id: spm_Q.m 5838 2014-01-18 18:40:37Z karl $

% default
%--------------------------------------------------------------------------
try, q; catch, q = 0; end

if q

    % compute P (precision)
    %----------------------------------------------------------------------
    A    = [-a(1) (1 + a(1)^2) -a(1)];
    Q    = spdiags(ones(n,1)*A,(-1:1),n,n);

else

    % compute Q (covariance)
    %----------------------------------------------------------------------
    p    = length(a);
    A    = [1 -a(:)'];
    P    = spdiags(ones(n,1)*A,-(0:p),n,n);
    K    = inv(P);
    K    = K.*(abs(K) > 1e-4);
    Q    = K*K';
    Q    = toeplitz(Q(:,1));

end


function c=ref_dstiv(f)
%REF_DSTIV  Reference Discrete Sine Transform type IV
%   Usage:  c=ref_dstiv(f);
%
%

L=size(f,1);
W=size(f,2);

% Create weights.
w=sqrt(2/L);

% Create transform matrix.
F=zeros(L);

for m=0:L-1
  for n=0:L-1
    F(m+1,n+1)=w*sin(pi*(n+.5)*(m+.5)/L);
  end;
end;

% Compute coefficients.
c=F*f;


function [p,L] = tspsearch(X,m)
%TSPSEARCH Heuristic method for Traveling Salesman Problem (TSP).
%   [P,L] = TSPSEARCH(X,M) gives a tour P of length L. X is either a
%   coordinate matrix of size Nx2 or Nx3 or a symmetric distance matrix.
%   Euclidian distances are used in the coordinate case. M is an integer
%   in the range 1 to N. Default is M = 1.
%
%   METHOD
%   M nearest neighbour tours are generated from randomly selected starting
%   points. Each tour is improved by 2-opt heuristics (pairwise exchange of
%   edges) and the best result is selected.
%
%   EXAMPLES
%
%   X = rand(100,2);
%   [p,L] = tspsearch(X,100);
%   tspplot(p,X)
%
%   % Optimal tour length 1620
%   X = load('hex162.dat');
%   [p,L] = tspsearch(X,10);
%   tspplot(p,X)
%
%   % Optimal tour length 4860
%   X = load('hex486.dat');
%   [p,L] = tspsearch(X);
%   tspplot(p,X)

%   Author: Jonas Lundgren <splinefit@gmail.com> 2012

% Check first argument
[n,dim] = size(X);
if dim == 2 || dim == 3
    % X is a coordinate matrix, compute euclidian distances
    D = distmat(X);
elseif n == dim && min(X(:)) >= 0 && isequal(X,X')
    % X is a distance matrix
    D = X;
else
    mess = 'First argument must be Nx2, Nx3 or symmetric and nonnegative.';
    error('tspsearch:first',mess)
end

% Check second argument
if nargin < 2 || isempty(m)
    m = 1;
elseif ~isscalar(m) || m < 1 || m > n || fix(m) < m
    mess = 'M must be an integer in the range 1 to %d.';
    error('tspsearch:second',mess,n)
end

% Starting points for nearest neighbour tours
s = randperm(n);

Lmin = inf;
for k = 1:m
    % Nearest neighbour tour
	p = greedy(s(k),D);
    % Improve tour by 2-opt heuristics
	[p,L] = exchange2(p,D);
    % Keep best tour
	if L < Lmin
        Lmin = L;
        pmin = p;
	end
end

% Output
p = double(pmin);
L = Lmin;

%--------------------------------------------------------------------------
function D = distmat(X)
%DISTMAT Compute euclidian distance matrix from coordinates

[n,dim] = size(X);
D = zeros(n);
for j = 1:n
    for k = 1:dim
        v = X(:,k) - X(j,k);
        D(:,j) = D(:,j) + v.*v;
    end
end
D = sqrt(D);

%--------------------------------------------------------------------------
function p = greedy(s,D)
%GREEDY Travel to nearest neighbour, starting with node s.

n = size(D,1);
p = zeros(1,n,'uint16');
p(1) = s;

for k = 2:n
    D(s,:) = inf;
    [junk,s] = min(D(:,s)); %#ok
    p(k) = s;
end

%--------------------------------------------------------------------------
function [p,L] = exchange2(p,D)
%EXCHANGE2 Improve tour p by 2-opt heuristics (pairwise exchange of edges).
%   The basic operation is to exchange the edge pair (ab,cd) with the pair
%   (ac,bd). The algoritm examines all possible edge pairs in the tour and
%   applies the best exchange. This procedure continues as long as the
%   tour length decreases. The resulting tour is called 2-optimal.

n = numel(p);
zmin = -1;

% Iterate until the tour is 2-optimal
while zmin < 0

    zmin = 0;
    i = 0;
    b = p(n);

    % Loop over all edge pairs (ab,cd)
    while i < n-2
        a = b;
        i = i+1;
        b = p(i);
        Dab = D(a,b);
        j = i+1;
        d = p(j);
        while j < n
            c = d;
            j = j+1;
            d = p(j);
            % Tour length diff z
            % Note: a == d will occur and give z = 0
            z = (D(a,c) - D(c,d)) + D(b,d) - Dab;
            % Keep best exchange
            if z < zmin
                zmin = z;
                imin = i;
                jmin = j;
            end
        end
    end

    % Apply exchange
    if zmin < 0
        p(imin:jmin-1) = p(jmin-1:-1:imin);
    end

end

% Tour length
q = double(p);
ind = sub2ind([n,n],q,[q(2:n),q(1)]);
L = sum(D(ind));


function [embedding_layer_state, hidden_layer_state, output_layer_state] = ...
  fprop(input_batch, word_embedding_weights, embed_to_hid_weights,...
  hid_to_output_weights, hid_bias, output_bias)
% This method forward propagates through a neural network.
% Inputs:
%   input_batch: The input data as a matrix of size numwords X batchsize where,
%     numwords is the number of words, batchsize is the number of data points.
%     So, if input_batch(i, j) = k then the ith word in data point j is word
%     index k of the vocabulary.
%
%   word_embedding_weights: Word embedding as a matrix of size
%     vocab_size X numhid1, where vocab_size is the size of the vocabulary
%     numhid1 is the dimensionality of the embedding space.
%
%   embed_to_hid_weights: Weights between the word embedding layer and hidden
%     layer as a matrix of soze numhid1*numwords X numhid2, numhid2 is the
%     number of hidden units.
%
%   hid_to_output_weights: Weights between the hidden layer and output softmax
%               unit as a matrix of size numhid2 X vocab_size
%
%   hid_bias: Bias of the hidden layer as a matrix of size numhid2 X 1.
%
%   output_bias: Bias of the output layer as a matrix of size vocab_size X 1.
%
% Outputs:
%   embedding_layer_state: State of units in the embedding layer as a matrix of
%     size numhid1*numwords X batchsize
%
%   hidden_layer_state: State of units in the hidden layer as a matrix of size
%     numhid2 X batchsize
%
%   output_layer_state: State of units in the output layer as a matrix of size
%     vocab_size X batchsize
%

[numwords, batchsize] = size(input_batch);
[vocab_size, numhid1] = size(word_embedding_weights);
numhid2 = size(embed_to_hid_weights, 2);

%% COMPUTE STATE OF WORD EMBEDDING LAYER.
% Look up the inputs word indices in the word_embedding_weights matrix.
embedding_layer_state = reshape(...
  word_embedding_weights(reshape(input_batch, 1, []),:)',...
  numhid1 * numwords, []);

%% COMPUTE STATE OF HIDDEN LAYER.
% Compute inputs to hidden units.
inputs_to_hidden_units = embed_to_hid_weights' * embedding_layer_state + ...
  repmat(hid_bias, 1, batchsize);

% Apply logistic activation function.
% FILL IN CODE. Replace the line below by one of the options.
% hidden_layer_state = zeros(numhid2, batchsize);
% Options
% (a) hidden_layer_state = 1 ./ (1 + exp(inputs_to_hidden_units));
% (b) hidden_layer_state = 1 ./ (1 - exp(-inputs_to_hidden_units));
hidden_layer_state = 1 ./ (1 + exp(-inputs_to_hidden_units));
% (d) hidden_layer_state = -1 ./ (1 + exp(-inputs_to_hidden_units));

%% COMPUTE STATE OF OUTPUT LAYER.
% Compute inputs to softmax.
% FILL IN CODE. Replace the line below by one of the options.
% inputs_to_softmax = zeros(vocab_size, batchsize);
% Options
inputs_to_softmax = hid_to_output_weights' * hidden_layer_state +  repmat(output_bias, 1, batchsize);
% (b) inputs_to_softmax = hid_to_output_weights' * hidden_layer_state +  repmat(output_bias, batchsize, 1);
% (c) inputs_to_softmax = hidden_layer_state * hid_to_output_weights' +  repmat(output_bias, 1, batchsize);
% (d) inputs_to_softmax = hid_to_output_weights * hidden_layer_state +  repmat(output_bias, batchsize, 1);

% Subtract maximum.
% Remember that adding or subtracting the same constant from each input to a
% softmax unit does not affect the outputs. Here we are subtracting maximum to
% make all inputs <= 0. This prevents overflows when computing their
% exponents.
inputs_to_softmax = inputs_to_softmax...
  - repmat(max(inputs_to_softmax), vocab_size, 1);

% Compute exp.
output_layer_state = exp(inputs_to_softmax);

% Normalize to get probability distribution.
output_layer_state = output_layer_state ./ repmat(...
  sum(output_layer_state, 1), vocab_size, 1);


%Number of decision makers k, i.e.
k=4;
%Number of criterias to be evaluated n i.e.
n=4;
%Information about is the given criteria cost or benefit criteria:
%Benefit=1, Cost=2, should be given as a vector of length n. i.e.
criteria=[1 1 1 2];

%Number of attributes to be ranked (suppliers in example case). i.e.
m=6;

%Criteria for selecting Fuzzy Positive Ideal Solution FPIS and Fuzzy Negative Ideal Solution FNIS.
%Three possible option are now implemented. See [1.] for more information. i.e.

ideal=3;
%Chosen similarity measure: Integer number within {1,2,3,4}
simi=4;

lingvar

WD1={VH H H H};
WD2={H VH VH H};
WD3={MH H H MH};
WD4={M M MH MH};
WD={WD1;WD2;WD3;WD4};

FDM1={G G G F; MG G MG G; F F G VG; F P G G; MG MP MG MG; G MP F G};
FDM2={MG G MG G; G MG G G; F F G VG; MG MP MG MG; F MP F MG; MG P F VG};
FDM3={G MG MG G; F MG G F; MG P F G; MG MP MG G; F MP F G; MG P MG VG};
FDM4={G MG G G; MG G G MG; G F MG G; F P G G; MG MP MG MG; MG MP F G};
FDM={FDM1;FDM2;FDM3;FDM4};

[CCS,Sstar,Sneg,Order]=Stopsis(WD,FDM,k,m,n,ideal,criteria,simi);

disp('Similarities w.r.t. attribute and FPIS:')
Sstar
disp('Similarities w.r.t. attribute and FNIS:')
Sneg

disp('Order of the attributes:')
Order
disp('Closeness values with similarity for choosing attributes:')
CCS


function a = a123 ( )

%*****************************************************************************80
%
%% A123 returns the A123 matrix.
%
%  Example:
%
%    1 2 3
%    4 5 6
%    7 8 9
%
%  Properties:
%
%    A is integral.
%
%    A is not symmetric.
%
%    A is singular.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    09 March 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Output, real A(3,3), the matrix.
%
  a = zeros ( 3, 3 );

  k = 0;
  for i = 1 : 3
    for j = 1 : 3
      k = k + 1;
      a(i,j) = k;
    end
  end

  return
end


%% The aliquot parts of a number
%
% This demo file teaches about the aliquot parts of a number,
% and how to use the functions I've provided.
%
% Author: John D'Errico
%
% e-mail: woodchips@rochester.rr.com
%
% Release: 1.0
%
% Release date: 9/21/08

%% The aliquot parts of a number are its proper divisors
% For example, the number 12 has the list of prime factors
factor(12)

%%
% but its proper divisors are given by
aliquotparts(12)

%%
% See that N will not be listed as a proper divisor of itself, not
% for any value of N. In fact, the number 1 has no proper divisors.
aliquotparts(1)

%%
% Of course, prime numbers can have only 1 as a proper divisor.
aliquotparts(17)

%% The sum of the aliquot parts, or sum of proper divisors
aliquotsum([2 4 5 6 8 12 27 135 40320])

%% aliquotsum is efficient and vectorized
% In this test, the sum of the divisors for EVERY number from 1 to 100000
% is computed in short order.
N = (1:100000)';
tic,pdsum = aliquotsum(N);toc
%%
% Show the numbers up to 100000 with the largest relative sums of their proper divisors.
[pdsum,tags] = sort(pdsum./N,'descend');
disp('         N,      sigma(N)./N')
disp([N(tags(1:10)),pdsum(1:10)])

%% You can also count the number of divisors
% This is just the sum of the zero'th powers of the divisors. Logically,
% we expect all the prime numbers to have only one divisor.
N = (1:20)';
[N,aliquotsum(N,0)]

%%
% What number no larger than 100000 has the most proper divisors?
N = (1:100000)';
d = aliquotsum(N,0);
[maxdivisors,Nmax] = max(d)
%%
% As you might expect, that number has multiple small factors
factor(Nmax)

%% Or you can form the sum of the p'th powers
% Here we will compute the sum of the squares of the aliquot parts.
N = (1:20)';
[N,aliquotsum(N,2)]

%% Perfect numbers
% Perfect numbers have their aliquot sums equal to the
% number itself.
aliquotsum([6 28 496 8128])

%% Abundant numbers
% Just under 25% of all numbers are abundant
N = 100000;
100*sum(aliquotsum(1:N)>(1:N))/N

%% Hyper-abundant numbers
% Few (roughly 2%) of numbers are hyper-abundant.
% I've defined hyper-abundancy as an aliquot sum of the number N
% that is at least twice as large as N.
N = (1:100000)';
%%
% As a percentage...
100*sum(aliquotsum(N)>(2*N))./100000

%%
% Show the numbers up to 100000 with the largest relative sums of their proper divisors.
pdsum = aliquotsum(N);
[pdsum,tags] = sort(pdsum./N,'descend');
disp('         N,      sigma(N)./N')
disp([N(tags(1:10)),pdsum(1:10)])

%% Collosally-abundant numbers?
% How hyper-abundant can they get? Is there a maximum?
N = 1000000;
pdsum = aliquotsum(1:N);
[maxsum,whichN] = max(pdsum)
%%
% It does not appear that there is any maximum value
% to this ratio. Certainly it exceeds 3. Mathworld suggests
% <http://mathworld.wolfram.com/ColossallyAbundantNumber.html |(Collossally abundant)|>
% that the ratio can get fairly large. Add 1 to get the ratio they list.
maxsum/whichN

%% Odd abundant numbers are less common than the abundant numbers in general
% The smallest odd abundant number is 945.
N = (1:2:10000)';
pdsum = aliquotsum(N);
ind = find(pdsum>N);
N(ind)'

%% The odd abundant numbers are also "less" abundant
% The third column here is the extent of the overabundancy.
% Since 1.0 there would indicate a perfect number, these odd abundants
% are all clearly just barely so. I wonder, is there a limit to the
% extent of abundancy for the odd numbers? Are any hyper-abundant,
% as I've defined it above?
N = (1:2:1000000)';
pdsum = aliquotsum(N);
maxOddAbundancy = max(pdsum./N)

%% Why are the odd abundants so rare?
% We can get some idea as to the reason why odd abundant numers
% are so rare, by looking at the factors of a highly abundant even
% number.
%
% Try 840 for example. Clearly it has multiple powers of 2 in its
% factorization.
factor(840)
%%
% 840 is also quite abundant.
aliquotsum(840)
%%
% If we look at the factors of the most highly abundant numbers,
% you will generally find many spare powers of 2. But how about
% the odd abunbdants? There are no powers of 2 allowed in an odd number,
% so pick some of the odd abundant numbers and look at their factors.
% As expected, we find some extra powers of 3, along with some other
% moderately small odd primes.
factor(945)
factor(9765)
%%
% Pick a rather large odd number that will have very many factors.
% Unfortunately, this is close to as far as aliquotsum will go, due to
% the limits of MATLAB's operating precision. I'll admit the ratio
% is starting to approach 2.
N = 3*3*3*5*5*7*7*11*13*17*19
aliquotsum(N)/N

%% Deficient numbers
% All primes are deficient, as are all pure powers of prime numbers.
% As well, all the divisors of any perfect number are known to be deficient.
aliquotsum(primes(50))

%% Maximally deficient numbers
% Naturally, the prime numbers represent the most deficient numbers
% possible, since each prime has only one divisor less than itself,
% and that is the number 1. If we ignore the prime numbers, which
% numbers are the next most deficient? Do you see a pattern?
N = setdiff(1:50,primes(50))';
[N,aliquotsum(N)]

%% Amicable numbers
% A pair of numbers is amicable if their aliquot sums are
% equal to the other member of the pair.
aliquotsum([220 284])

%% Sociable numbers form cliques, or amicable cycles
% Perfect numbers are self-amicable, with a cycle length of 1.
% Amicable pairs have a cycle length of 2.
% This cycle has length 5 before it returns to the start.
aliquotsum([12496 14288 15472 14536 14264])

%% Amicable cycles
% Perfect numbers are self-amicable. The amicablecycles function:
%
%  cycles = amicablecycles(N,L)
%
% will find all cycles that start with a number
% as large as N. The length of the cycle is L.
amicablecycles(20000,1)
%%
% Amicable pairs have a cycle length of 2.
amicablecycles(20000,2)

%%
% There are no known sociable cliques of length 3 or 4
amicablecycles(20000,3)
amicablecycles(20000,4)

%%
% The smallest cycle of length 5 starts at 12496
amicablecycles(20000,5)

%% Odd amicables
% You can force amicablecycles to look only at certain starting points.
% For example, to find the odd amicable cycles only
amicablecycles(1:2:200000,2)

%% Some interesting things to try
% What fun things can you find to do with these numbers?
% Can you find any odd perfect numbers? Perhaps a quasi-perfect
% number?
%
% Some simple problems for the student, and a few that may not be so
% simple.
%
% 1. Which numbers have many distinct divisors?
%
% 2. What is the smallest number to have exactly 31 divisors?
%
% 3. Does the product of the first k primes necessarily have the largest number of distinct divisors?
%
% 4. Can you construct a number less than 2*3*5*7*11 that has more divisors than this number?
%
% 5. For which values of non-prime numbers N is the lower bound sqrt(N)+1 realized for the aliquot sum?
%
% 6. Can you prove that sqrt(N)+1 forms a lower bound for the aliquot sum for the non-prime numbers N?
%
% 7. Is there a simple relationship for the upper bound of the aliquot sum
% as a function of N? Consider N*log(N) as a start.
%
% 8. There are many abundant numbers. As was shown above, roughly 25% of
% all numbers are abundant. Can you generate the list of all abundant
% numbers up to some value? (Start with the aliquotsum function.) What is the
% smallest abundant number?
%
% 9. The odd abundant numbers were shown to be somewhat rare, and always
% seemed to have multiple powers of 3 in their representations. Are there
% any abundants that do not have either 2 or 3 as a factor? Can this
% happen? Note that such an investigation might involve numbers that are
% too large for the tools I've provided to work with properly.
N = vpi(5)*5*5*5*5*5*7*7*7*11*13
aliquotsum(N)
%%
% 10. Can you represent numbers as the sum of two abundant numbers? Which
% numbers are not so representable? Is there a smallest number such that
% all those above it are the sum of two abundant numbers?
%
% 11. An interesting generalization of a perfect number is what I'll call the p-perfect
% number. Thus, if a perfect number is an integer such that the sum of its
% proper divisors adds up to the number itself, a p-perfect number N might
% have the sum of the p'th powers of the divisors adding up to N^p. Do any
% such numbers exist? Can you show this cannot happen? Or are there any
% numbers with the squares of the divisors that adds up to the original
% number?
%
% 12. How about p-amicability? Can you think of a way to generalize this
% concept?


% book : Signals and Systems Laboratory with MATLAB
% authors : Alex Palamides & Anastasia Veloni
%
%
%

% problem 2- graph of
% u(t+1)*u(t-1)

 t1=-5:.1:1;
 t2=1:.1:10;
 x1=zeros(size(t1));
 x2=ones(size(t2));
 t=[t1 t2];
 x=[x1 x2];
 plot(t,x);
 ylim([-0.1 1.1]);

 figure
 t=-5:.1:10;
 x=heaviside(t+1).*heaviside(t-1) ;
 plot(t,x);
 ylim([-0.1 1.1]);


% Find x that minimizes ||Ax|| under different constraints
%
% The problem can be solved with different constraints:
%  Reference: Alg 5.6, HZ2, p. 595
%   Find x that minimizes ||Ax|| subject to ||x||=1 and x=G*xHat, where G
%   has rank r
%  Reference: Alg 5.7, HZ2, p. 596
%   Find x that minimizes ||Ax|| subject to ||Cx||=1
%
% USAGE
%  x = solveLeastSqAx(A)
%  [x,xHat] = solveLeastSqAx(A,G,method)
%
% INPUTS 1 - Find x that minimizes ||Ax|| subject to ||x||=1
%  A       - constraint matrix, ||Ax|| to be minimized with ||x||=1
%
% INPUTS 2
%
% INPUTS 3 - Find x that minimizes ||Ax|| subject to ||x||=1 and x=G*xHat,
%            where G has rank r
%  A       - constraint matrix
%  G       - condition matrix
%  method  - method=2
%
% INPUTS 4 - Find x that minimizes ||Ax|| subject to ||Cx||=1
%  A       - constraint matrix
%  C       - condition matrix
%  method  - method=3
%
% OUTPUTS 1,2,4
%  x      - solution
%
% OUTPUTS 3
%  x      - solution
%  xHat   - vector such that x = G*xHat
%
% EXAMPLE
%
% See also
%
% Vincent's Structure From Motion Toolbox      Version 1.1
% Copyright (C) 2008-2011 Vincent Rabaud.  [vrabaud-at-cs.ucsd.edu]
% Please email me if you find bugs, or have suggestions or questions!
% Licensed under the GPL [see external/gpl.txt]

function [ x xHat ] = solveLeastSqAx(A,G,method)

if nargin==1; [U,D,V] = svd(A,0); x=V(:,end); return; end
if nargin==2; error('method argument required'); end

switch method
  case 1
    % Find x that minimizes ||Ax|| subject to ||x||=1
    % Reference: HZ2, Algorithm 5.4, p593
    if issparse(A)
      [ disc disc V ]=svds(A);
    else
      [ disc disc V ]=svd(A);
    end
    x = V(:,end);
  case 2
    % Find x that minimizes ||Ax|| subject to ||x||=1 and x=G*xHat, where G
    % has rank r
    % Reference: HZ2, Algorithm 5.6, p595
    % (i)
    [U,D,V] = svd(G,0);
    % (ii)
    r = rank(G); Up = U(:,1:r);
    % (iii)
    xp = solveLeastSqAx(A*U2);
    % (iv)
    x = Up*xp;
    % (v)
    if nargout==2; Vp=V(:,1:r); xHat=Vp*diag(1./diag(D(1:r,1:r)))*x2; end
  case 3
    %   Find x that minimizes ||Ax|| subject to ||Cx||=1
    %  Reference: Alg 5.7, HZ2, p. 596
    % (i)
    [U,D,V]=svd(C); Ap=A*V;
    % (ii)
    r=rank(D); A1p=Ap(:,1:r); A2p=Ap(:,r:end);
    % (iii)
    D1=D(1:r,1:r);
    % (iv)
    D1inv=diag(1./diag(D1)); A2pPinv=pinv(A2p);
    App=(A2p*A2pPinv-eye(size(A2p,1)))*A1p*D1inv;
    % (v)
    xpp=solveLeastSqAx(App);
    % (vi)
    x1p=D1inv*xpp; x2p=-A2pPinv*A1p*x1p; xp=[x1p;x2p];
    % (vii)
    x=V*xp;
end


function value = f_01_2d ( dim_num, x )

%*****************************************************************************80
%
%% F_01_2D is the 2D test function #1.
%
%  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, integer DIM_NUM, the spatial dimension.
%
%    Input, real X(DIM_NUM), the point where the function
%    is to be evaluated.
%
%    Output, real VALUE, the value of the function at X.
%
  e = 2.718281828459045;
  value = x(2) * exp ( x(1) * x(2) ) / ( e - 2.0 );

  return
end


%% Machine Learning Online Class - Exercise 2: Logistic Regression
%
%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the logistic
%  regression exercise. You will need to complete the following functions
%  in this exericse:
%
%     sigmoid.m
%     costFunction.m
%     predict.m
%     costFunctionReg.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% Load Data
%  The first two columns contains the exam scores and the third column
%  contains the label.

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);

%% ==================== Part 1: Plotting ====================
%  We start the exercise by first plotting the data to understand the
%  the problem we are working with.

fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
         'indicating (y = 0) examples.\n']);

plotData(X, y);

% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============ Part 2: Compute Cost and Gradient ============
%  In this part of the exercise, you will implement the cost and gradient
%  for logistic regression. You neeed to complete the code in
%  costFunction.m

%  Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);

% Add intercept term to x and X_test
X = [ones(m, 1) X];

% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);

% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);

fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Expected cost (approx): 0.693\n');
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');

% Compute and display cost and gradient with non-zero theta
test_theta = [-24; 0.2; 0.2];
[cost, grad] = costFunction(test_theta, X, y);

fprintf('\nCost at test theta: %f\n', cost);
fprintf('Expected cost (approx): 0.218\n');
fprintf('Gradient at test theta: \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============= Part 3: Optimizing using fminunc  =============
%  In this exercise, you will use a built-in function (fminunc) to find the
%  optimal parameters theta.

%  Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);

%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost
[theta, cost] = ...
	fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('Expected cost (approx): 0.203\n');
fprintf('theta: \n');
fprintf(' %f \n', theta);
fprintf('Expected theta (approx):\n');
fprintf(' -25.161\n 0.206\n 0.201\n');

% Plot Boundary
plotDecisionBoundary(theta, X, y);

% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============== Part 4: Predict and Accuracies ==============
%  After learning the parameters, you'll like to use it to predict the outcomes
%  on unseen data. In this part, you will use the logistic regression model
%  to predict the probability that a student with score 45 on exam 1 and
%  score 85 on exam 2 will be admitted.
%
%  Furthermore, you will compute the training and test set accuracies of
%  our model.
%
%  Your task is to complete the code in predict.m

%  Predict probability for a student with score 45 on exam 1
%  and score 85 on exam 2

prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
         'probability of %f\n'], prob);
fprintf('Expected value: 0.775 +/- 0.002\n\n');

% Compute accuracy on our training set
p = predict(theta, X);

fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
fprintf('Expected accuracy (approx): 89.0\n');
fprintf('\n');


function y = atanminuspihalf(x)
%ATANMINUSPIHALF Inverse tangent minus pi/2.
%
%   ATANMINUSPIHALF(X) is ATAN(X)-PI/2 calculated in a way that is numerically
%   better when X is large and positive.
%
%   See also TANPLUSPIHALF.

%   Author:      Peter J. Acklam
%   Time-stamp:  2003-10-13 14:55:25 +0200
%   E-mail:      pjacklam@online.no
%   URL:         http://home.online.no/~pjacklam

   % check number of input arguments
   error(nargchk(1, 1, nargin));

   y = zeros(size(x));

   k = x <= 0;
   y(k) = atan(x(k)) - (pi / 2);

   k = x > 0;
   y(k) = atan(-1 ./ x(k));


function c=ref_dft_1(f)
%REF_DFT_1  Reference DFT by doubling
%   Usage:  c=ref_dft_1(f);
%
%   REF_DFT_1(f) computes a DFT of f by upsampling f and inserting zeros
%   at the odd positions.
%
%   This is not an efficient method, it is just meant to illustrate a
%   symmetry of the DFT.

L=size(f,1);
W=size(f,2);

flong=zeros(2*L,W,assert_classname(f));
flong(1:2:end-1)=f;

fflong=fft(flong)/sqrt(L);

c=fflong(1:L,:);


function kappa = polynomialCurveCurvature(t, varargin)
%POLYNOMIALCURVECURVATURE Compute the local curvature of a polynomial curve
%
%   KAPPA = polynomialCurveCurvature(T, XCOEF, YCOEF)
%   XCOEF and YCOEF are row vectors of coefficients, in the form:
%       [a0 a1 a2 ... an]
%   KAPPA is the local curvature of the polynomial curve, computed for
%   position T. If T is a vector, KAPPA has the same length as T.
%
%   KAPPA = polynomialCurveCurvature(T, COEFS)
%   COEFS is either a 2xN matrix (one row for the coefficients of each
%   coordinate), or a cell array.
%
%   Example
%   polynomialCurveCurvature
%
%   See also
%   polynomialCurves2d, polynomialCurveLength, polynomialCurveDerivative
%
%
% ------
% Author: David Legland
% e-mail: david.legland@nantes.inra.fr
% Created: 2007-02-23
% Copyright 2007 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas.

%% Extract input parameters

% polynomial coefficients for each coordinate
var = varargin{1};
if iscell(var)
    xCoef = var{1};
    yCoef = var{2};
elseif size(var, 1)==1
    xCoef = varargin{1};
    yCoef = varargin{2};
else
    xCoef = var(1,:);
    yCoef = var(2,:);
end

%% compute derivative

% compute first derivatives of the polynomials
dx  = polynomialDerivate(xCoef);
dy  = polynomialDerivate(yCoef);

% compute second derivatives
sx  = polynomialDerivate(dx);
sy  = polynomialDerivate(dy);

% convert to polyval convention
dx  = dx(end:-1:1);
dy  = dy(end:-1:1);
sx  = sx(end:-1:1);
sy  = sy(end:-1:1);

% compute local first and second derivatives
xp  = polyval(dx, t);
yp  = polyval(dy, t);
xs  = polyval(sx, t);
ys  = polyval(sy, t);

% compute local curvature of polynomial curve
kappa  = (xp.*ys - xs.*yp) ./ power(xp.*xp + yp.*yp, 3/2);


function   r = assortativity_bin(CIJ,flag)
% ASSORTATIVITY_BIN      Assortativity coefficient
%
%   r = assortativity(CIJ,flag);
%
%   The assortativity coefficient is a correlation coefficient between the
%   degrees of all nodes on two opposite ends of a link. A positive
%   assortativity coefficient indicates that nodes tend to link to other
%   nodes with the same or similar degree.
%
%   Inputs:     CIJ,    binary directed/undirected connection matrix
%               flag,   0, undirected graph: degree/degree correlation
%                       1, directed graph: out-degree/in-degree correlation
%                       2, directed graph: in-degree/out-degree correlation
%                       3, directed graph: out-degree/out-degree correlation
%                       4, directed graph: in-degree/in-degree correlation
%
%   Outputs:    r,      assortativity coefficient
%
%   Notes: The function accepts weighted networks, but all connection
%   weights are ignored. The main diagonal should be empty. For flag 1
%   the function computes the directed assortativity described in Rubinov
%   and Sporns (2010) NeuroImage.
%
%   Reference:  Newman (2002) Phys Rev Lett 89:208701
%               Foster et al. (2010) PNAS 107:10815�10820
%
%   Olaf Sporns, Indiana University, 2007/2008
%   Vassilis Tsiaras, University of Crete, 2009
%   Murray Shanahan, Imperial College London, 2012
%   Mika Rubinov, University of Cambridge, 2012

if (flag==0)                        % undirected version
    deg = degrees_und(CIJ);
    [i,j] = find(triu(CIJ,1)>0);
    K = length(i);
    degi = deg(i);
    degj = deg(j);

else                                % directed versions
    [id,od] = degrees_dir(CIJ);
    [i,j] = find(CIJ>0);
    K = length(i);

    switch flag
        case 1
            degi = od(i);
            degj = id(j);
        case 2
            degi = id(i);
            degj = od(j);
        case 3
            degi = od(i);
            degj = od(j);
        case 4
            degi = id(i);
            degj = id(j);
    end
end

% compute assortativity
r = ( sum(degi.*degj)/K - (sum(0.5*(degi+degj))/K)^2 ) / ...
    ( sum(0.5*(degi.^2+degj.^2))/K - (sum(0.5*(degi+degj))/K)^2 );


function pp = pchipd(x,y,d,xx)
%PCHIPD  Piecewise Cubic Hermite Interpolating Polynomial with Derivatives.
%   PP = PCHIPD(X,Y,D) provides the piecewise cubic polynomial which
%   interpolates values Y and derivatives D at the sites X.  This is meant
%   to augment the built-in Matlab function PCHIP, which does not allow the
%   user to specify derivatives.
%
%   X must be a vector.
%
%   If Y and D are vectors, then Y(i) and D(i) are the value and derivative
%   to be matched at X(i).
%
%   If Y and D are matrices, then size(Y,2) == size(D,2) == length(X).
%   Also, size(Y,1) == size(D,1).  Use this for interpolating vector valued
%   functions.
%
%   YY = PCHIPD(X,Y,D,XX) is the same as YY = PPVAL(PCHIPD(X,Y,D),XX), thus
%   providing, in YY, the values of the interpolant at XX.
%
%   Example comparing SPLINE, PCHIP, and PCHIPD
%     a = -10;
%     b = 10;
%     x = linspace(a,b,7);
%     f = @(x) 1./(1+exp(-x));  % logistic function
%     df = @(x) f(x).*(1-f(x)); % derivative of the logistic function
%     t = linspace(a,b,50);
%     r = f(t);
%     p = pchip(x,f(x),t);
%     s = spline(x,f(x),t);
%     q = pchipd(x,f(x),df(x),t);
%     plot(t,r,'k',x,f(x),'o',t,p,'-',t,s,'-.',t,q,'--')
%     legend('true','data','pchip','spline','pchipd',4)
%
%   See also INTERP1, SPLINE, PCHIP, PPVAL, MKPP, UNMKPP.
%

%
% 2010-10-04 (nwh) first version
%

% check inputs
% x must be a vector
if ~isvector(x)
  error('pchipd:input_error','x must be a vector of length > 2.')
end

% get size and orient
n = length(x);
x = x(:);

% make sure x is long enough, we can't construct an interpolating
% polynomial with just one point
if n < 2
  error('pchipd:input_error','x must be a vector of length > 2.')
end

% check y and d
if isvector(y) && isvector(d) && length(y) == n && length(d) == n
  % orient
  y = y(:);
  d = d(:);
  m = 1;
elseif size(y,2) == n && size(d,2) == n && size(y,1) == size(d,1)
  m = size(y,1);
  y = y';
  d = d';
else
  error('pchipd:input_error','y and d must be vectors or matrices of same size with length(x) columns.')
end

% sort breaks & data if needed
if ~issorted(x)
  [x x_ix] = sort(x);
  if m == 1
    y = y(x_ix);
    d = d(x_ix);
  else
    y = y(x_ix,:);
    d = d(x_ix,:);
  end
end

% compute coefficients
coef = zeros(m,n-1,4);
dx = diff(x);
for i = 1:m
  dy = diff(y(:,i));
  coef(i,:,4) = y(1:end-1,i)';
  coef(i,:,3) = d(1:end-1,i)';
  coef(i,:,2) = 3*dy./(dx.^2) - (2*d(1:end-1,i)+d(2:end,i))./dx;
  coef(i,:,1) = -2*dy./(dx.^3) + (d(1:end-1,i)+d(2:end,i))./(dx.^2);
end

% create the piecewise polynomial structure
pp = mkpp(x,coef,m);

% if user requests evaluations
if nargin > 3
  pp = ppval(pp,xx);
end


function [FV2]=refinepatch(FV)
% This function "refinepatch" refines a triangular mesh with
% a spline interpolated 4-split method.
%
%   [FV2] = refinepatch(FV,options)
%
% inputs,
%   FV : Structure containing a Patch, with
%        FV.vertices the mesh vertices
%        FV.face the mesh faces (triangles), rows with each 3 vertex indices
% outputs,
%   FV2 : Structure Containing the refined patch
%
%
% Reference:
%  The spline interpolation of the face edges is done by the
%  Opposite Edge Method, described in: "Construction of Smooth Curves
%  and Surfaces from Polyhedral Models" by Leon A. Shirman
%
% How it works:
%  The tangents (normals) and velocity on the edge points of all edges
%  are calculated. Which are  later used for b-spline interpolation when
%  splitting the edges.
%
%  A tangent on an 3D line or edge is under defined and can rotate along
%  the line, thus an (virtual) opposite vertex is used to fix the tangent and
%  make it more like a surface normal.
%
%  B-spline interpolate a half way vertices between all existing vertices
%  using the velocity and tangent from the edgepoints. After splitting a
%  new facelist is constructed
%
% Speed:
%  Compile the c-functions for more speed with:
%   mex vertex_neighbours_double.c -v;
%   mex edge_tangents_double.c -v;
%
% Example:
%
% X=[-0.5000;  0.5000;  0.0000;  0.0000];
% Y=[-0.2887; -0.2887;  0.5774;  0.0000];
% Z=[ 0.0000;  0.0000;  0.0000;  0.8165];
% FV.vertices=[X Y Z];
%
% FV.faces=[2 3 4; 4 3 1; 1 2 4; 3 2 1];
%
% figure, set(gcf, 'Renderer', 'opengl'); axis equal;
% for i=1:4
%   patch(FV,'facecolor',[1 0 0]);
%   pause(2);
%   [FV]=refinepatch(FV);
% end
%
% Function is written by D.Kroon University of Twente (February 2010)

% Get the neighbour vertices of each vertice from the face list.
Ne=vertex_neighbours(FV);

% Calculate the tangents (normals) and velocity of all edges. Which is
% later used for b-spline interpolation and split of the edges
%
% A tangent on an 3D line or edge is under defined and can rotate along
% the line, thus an (virtual) opposite vertex is used to fix the tangent and
% make it more like a surface normal.
V=FV.vertices; F=FV.faces;
[ET_table,EV_table,ETV_index]=edge_tangents(V,Ne);

% B-spline interpolate a half way vertices between all existing vertices
% using the velocity and tangent from above
[V,HT_index, HT_values]=make_halfway_vertices(EV_table,ET_table,ETV_index,V,Ne);

% Make new facelist
Fnew=makenewfacelist(F,HT_index,HT_values);

FV2.vertices=V;
FV2.faces=Fnew;


function price = Price_Spread_Option_BjerksundStensland_2D(K, S0_1, S0_2, T, r, rho, sigma_1, sigma_2, q_1, q_2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% About: calcuates Price of 2D Spread option, (S_1(T) - S_2(T) - K)+, using Bjerksund & Stensland approximation
%        Note: when K = 0, this agrees with Magrabes exact formula
% Author: Justin Lars Kirkby
%
% Reference: Bjerksund, P. and Stensland, G. (2006): "Closed form spread option valuation"
%
% -----------------
% Params
% -----------------
% S0_1    = initial asset price of first asset, e.g. S0_1 = 100
% S0_2    = initial asset price of second asset, e.g. S0_2 = 100
% T       = time to maturity in years (e.g. T=1)
% r       = interest rate
% rho     = instantaneous correlation between S_1 and S_2
% sigma_1 = volatility of first asset (annualized), e.g. sigma = 0.2
% sigma_2 = volatility of second asset (annualized), e.g. sigma = 0.2
% q_1     = dividend yield of first asset, e.g. q_1 = 0.02
% q_2     = dividend yield of second asset, e.g. q_2 = 0.02
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

F1 = S0_1*exp((r-q_1)*T);
F2 = S0_2*exp((r-q_2)*T);

a = F2 + K;
b = F2 / a;
rhosigs = rho*sigma_1*sigma_2;
sig = sqrt(sigma_1^2 - 2*rhosigs*b + b^2*sigma_2^2);
sigst = sig*sqrt(T);

d1 = (log(F1/a) + (0.5*sigma_1^2 - b*rhosigs + 0.5*b^2*sigma_2^2)*T) / sigst;
d2 =  (log(F1/a) + (-0.5*sigma_1^2 + rhosigs + (0.5*b^2 - b)*sigma_2^2)*T) / sigst;
d3 = (log(F1/a) + (-0.5*sigma_1^2 + 0.5*b^2*sigma_2^2)*T) / sigst;

price = exp(-r*T)*(F1*normcdf(d1) - F2*normcdf(d2) - K*normcdf(d3));

end


function dist_signed = triangle_point_dist_signed_2d ( t, p )

%*****************************************************************************80
%
%% TRIANGLE_POINT_DIST_SIGNED_2D: signed distance ( triangle, point ) in 2D.
%
%  Discussion:
%
%    If the signed distance is:
%    0, the point is on the boundary of the triangle;
%    negative, the point is in the triangle;
%    positive, the point is outside the triangle.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 December 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real T(2,3), the triangle vertices.
%    These should be given in counter clockwise order.
%
%    Input, real P(2,1), the point which is to be checked.
%
%    Output, real DIST_SIGNED, the signed distance from the
%    point to the triangle.
%
  dim_num = 2;
%
%  Compute the signed line distances to the point.
%
  dis12 = line_exp_point_dist_signed_2d ( t(1:2,1), t(1:2,2), p );

  dis23 = line_exp_point_dist_signed_2d ( t(1:2,2), t(1:2,3), p );

  dis31 = line_exp_point_dist_signed_2d ( t(1:2,3), t(1:2,1), p );
%
%  If the point is inside the triangle, all the line distances are negative.
%  The largest (negative) line distance has the smallest magnitude,
%  and is the signed triangle distance.
%
  if ( dis12 <= 0.0 & dis23 <= 0.0 & dis31 <= 0.0 )

    dist_signed = max ( dis12, max ( dis23, dis31 ) );
%
%  If the point is outside the triangle, then we have to compute
%  the (positive) line segment distances and take the minimum.
%
  else

    dis12 = segment_point_dist_2d ( t(1:2,1), t(1:2,2), p );
    dis23 = segment_point_dist_2d ( t(1:2,2), t(1:2,3), p );
    dis31 = segment_point_dist_2d ( t(1:2,3), t(1:2,1), p );

    dist_signed = min ( dis12, min ( dis23, dis31 ) );

  end

  return
end


function [y a]=lpredict(x, np, npred, pos)
% LPREDICT estimates the values of a data set before/after the observed set.
%
% LPREDICT uses linear prediction to extrapolate data, typically a
% timeseries. Note that this is not the same as linear extrapolation.
% A window of autocorrelation coefficients is moved beyond the data
% limits to extrapolate the data. For a discussion, see Press et. al. [1].
%
% The required coefficients are derived from a call to LPC in MATLAB's
% Signal Processing Toolbox
%
% Example:
% y=LPREDICT(x, np, npred, pos)
% [y, a]=LPREDICT(x, np, npred, pos)
%      x:       the input data series as a column vector or a matrix
%                   with series organized in columns
%      np:      the number of predictor coefficients to use (>=2)
%      npred:   the number of data values to return in the output
%      pos:     a string 'pre' or 'post' (default: post)
%                   This determines whether extrapolation occurs before or
%                   after the observed series x.
%
%      y:       the output, appropriately sequenced for concatenation with
%                   input x
%      a:       the coefficients returned by LPC (organized in rows).
%                   These can be used to check the quality/stability of the
%                   fit to the observed data as described in the
%                   documenation to the LPC function.
%
% The output y is given by:
%       y(k) = -a(2)*y(k-1) - a(3)*y(k-2) - ... - a(np)*y(k-np)
%                where y(n) => x(end-n) for n<=0
%
% Note that sum(-a(2:end))is always less than unity. The output will
% therefore approach zero as npred increases. This may be a problem if x
% has a large DC offset. Subtract the the column mean(s) of x from x on
% input and add them to the output column(s) to restore DC. For a more
% accurate DC correction, see [1].
%
% To pre-pend data, the input sequence is reversed and the output is
% similarly reversed before being returned. The output may always be
% vertically concatenated with the input to extend the timeseries e.g:
%       k=(1:100)';
%       x=exp(-k/100).*sin(k/5);
%       x=[lpredict(x, 5, 100, 'pre'); x; lpredict(x, 5, 100, 'post')];
%
%
% See also LPC
%
% References:
% [1] Press et al. (1992) Numerical Recipes in C. (Ed. 2, Section 13.6).
%
% Toolboxes Required: Signal Processing
%
% Revisions:    10.07 renamed to avoid filename clash with System ID
%                     Toolbox
%                     DC correction help text corrected.
%
% -------------------------------------------------------------------------
% Author: Malcolm Lidierth 10/07
% Copyright Š The Author & King's College London 2007
% -------------------------------------------------------------------------

% ---------------Argument checks---------------
if nargin<3
    error('Not enough input arguments');
end

if np<2
    error('np must be >=2');
end

if nargin<4
    pos='post';
end
%---------------------------------------------

%------------------MATRIX--------------------
% Deal with matrix input.
% Apply function to each column via recursive calls
cols=size(x,2);
if cols>1
    y=zeros(npred, cols);
    a=zeros(cols, np+1);
    for k=1:size(x,2)
        [y(:,k) a(k,:)]=lpredict(x(:,k), np, npred, pos);
    end
    return
end
%---------------------------------------------

% ---------------MAIN FUNCTION---------------
% Order input sequence
if nargin==4 && strcmpi(pos,'pre')
    x=x(end:-1:1);
end

% Get the forward linear predictor coefficients via the LPC
% function
try
    a=lpc(x,np);
catch
    % LPC missing?
    m=lasterror();
    if strcmp(m.identifier, 'MATLAB:UndefinedFunction')
        error('Requires the LPC function from the Signal Processing Toolbox');
    else
        rethrow(lasterror);
    end
end

% Negate coefficients, and get rid of a(1)
cc=-a(2:end);

% Pre-allocate output
y=zeros(npred,1);
% Seed y with the first value
y(1)=cc*x(end:-1:end-np+1);
% Next np-1 values
for k=2:min(np,npred)
    y(k)=cc*[y(k-1:-1:1); x(end:-1:end-np+k)];
end
% Now do the rest
for k=np+1:npred
    y(k)=cc*y(k-1:-1:k-np);
end

% Order the output sequence if required
if nargin==4 && strcmpi(pos,'pre')
    y=y(end:-1:1);
end

return
end


function B = adjugate(A)

% This finds the adjugate (adjoint) of square matrix A,
% and is valid even if A is singular or complex-valued.
% With u, s, and v obtained from [u,s,v] = svd(A), it
% makes use of the identity adj(A) = det(u*v')*v*adj(s)*u',
% which holds even if A and s are singular.  The expression,
% diag(prod(reshape(s0(ix),n-1,n),1)), accomplishes the
% evaluation of adj(s), each of whose diagonal elements
% is the product of all but one of the diagonal elements
% of s.  This requires that A be n x n where n >= 2.
% Roger Stafford - 10/18/06

[m,n] = size(A);
if (m ~= n) | (n < 2)
 error('Matrix A should be size n x n with n >= 2.')
end
[u,s,v] = svd(A);
s0 = diag(s);
ix = toeplitz(ones(n-1,1),[1 zeros(1,n-1)]) ...
     + repmat((1:n-1)',1,n);
B = det(u*v')*v*diag(prod(reshape(s0(ix),n-1,n),1))*u';

% ---


% 3D rotatin about Y
function rot_y = rotMatY_3D(ty)

	rot_y = [cos(ty)	 0.0		sin(ty);
	         0.0		 	 1.0		0.0;
		    -sin(ty)	 0.0		cos(ty) ];

end


function [z, r] = fisherz(r)
% Fisher's r to z' transform, and the inverse
%
% :Outputs:
%
%   **z:**
%        z = z', treating input r as correlation
%
%  **r:**
%        treating input r as a z' score

    z = .5 .* log((1+r) ./ (1-r));     % Fisher's r-to-z transform

    if nargout > 0
        r = (exp(2.*r) - 1) ./ (exp(2.*r) + 1);    % inverse
    end
end


npts=1000;
ntaps = 5;
b =ones(1, ntaps) / ntaps; % create filter coefficients for 5- point moving average

x=(rand(npts,1)*2)-1; % raw data from -1 to +1
filtered_data=filter(b,1,x); % filter using 'b' coefficients

subplot(2,1,1); % 1st subplot
plot(x); % plot raw data
title('Raw Data');
subplot(2,1,2); % 2nd subplot
plot(filtered_data); %plot filtered data
title('Filtered Data');
xlabel('Time');

% Perform FFT on original and filtered signal
fftpts=npts; % number points in FFT
hpts=fftpts/2; % half number of FFT points
x_fft=abs(fft(x))/hpts; %scaled FFT of original signal
filtered_fft=abs(fft(filtered_data))/hpts; %scaled FFT of filtered signal

subplot(2,1,1) %1st subplot
plot(x_fft(1:hpts)); %plot first half of data points
title('Raw Data');
subplot(2,1,2) %2nd subplot
plot(filtered_fft(1:hpts));%plot first half data points
title('Filtered Data');
xlabel('Frequency');


function Q = costMat(tau, r)
%costMat constructs a cost matrix, Q for a single segment
% r: The order of the derivative subjected to optimization
% ex) r = 4: minimum snap, r = 2: minimum acceleration

% nth order poly
n = 2*r+1;

Q = zeros(n+1);
QQ = zeros(n+1,n+1,length(r));
% eg optimizing 4th derivative
% r = 4;
% row
for rr = 1:length(r)
    for i=0:size(Q,1)-1
        % column
        for j=0:size(Q,2)-1
            if i >= r(rr) && j >= r(rr)
                m = 0:r(rr)-1;
                QQ(i+1,j+1,rr) = 2*prod((i-m).*(j-m))*tau^(i+j-2*r(rr)+1)/(i+j-2*r(rr)+1);
            end
        end
    end
end

for i = 1:length(r)
    Q = Q + QQ(:,:,i);
end


function cnk = combin ( n, k )

%*****************************************************************************80
%
%% COMBIN computes the combinatorial coefficient C(N,K).
%
%  Discussion:
%
%    Real arithmetic is used, and C(N,K) is computed directly, via
%    Gamma functions, rather than recursively.
%
%    C(N,K) is the number of distinct combinations of K objects
%    chosen from a set of N distinct objects.  A combination is
%    like a set, in that order does not matter.
%
%    C(N,K) = N% / ( (N-K)% * K% )
%
%  Example:
%
%    The number of combinations of 2 things chosen from 5 is 10.
%
%    C(5,2) = ( 5 * 4 * 3 * 2 * 1 ) / ( ( 3 * 2 * 1 ) * ( 2 * 1 ) ) = 10.
%
%    The actual combinations may be represented as:
%
%      (1,2), (1,3), (1,4), (1,5), (2,3),
%      (2,4), (2,5), (3,4), (3,5), (4,5).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 January 2011
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the value of N.
%
%    Input, integer K, the value of K.
%
%    Output, real CNK, the value of C(N,K)
%
  if ( n < 0 )

    cnk = 0.0;

  elseif ( k == 0 )

    cnk = 1.0;

  elseif ( k == 1 )

    cnk = n;

  elseif ( 1 < k && k < n - 1 )

    facn = gammaln ( n + 1 );
    fack = gammaln ( k + 1 );
    facnmk = gammaln ( n - k + 1 );

    cnk = round ( exp ( facn - fack - facnmk ) );

  elseif ( k == n - 1 )

    cnk = n;

  elseif ( k == n )

    cnk = 1.0;

  else

    cnk = 0.0;

  end

  return
end


function [ delay, N_i ] = delayest_iterative(u2,u1,N_i_max,d_tol);
%[ delay, N_i ] = delayest_iterative(u2,u1,N_i_max,d_tol);
%Estimates delay using successive parabolic interpolation of the cross
%correlation function (interpolated using Nyquist sampling theorem).
%N_i_max is the maximum number of iterations
%d_tol is the tolerance to stop at (samples)

%    Copyright Travis Wiens 2009 travis.mlfx@nutaksas.com

if nargin<3
	N_i_max=20;%max iterations
end
if nargin<4
	d_tol=1e-5;%stopping tolerance
end

N_p=numel(u2);%number of points

U1=fft(u1);
U2=fft(u2);
XC=U2.*conj(U1);%circular cross correlation
xc=ifft(XC);
[tmp idx]=max(xc);%find peak

R=zeros(1,3);%three points to interpolate through
R(2)=xc(idx);

%neighbors
if idx==1
    R(1)=xc(end);
    R(3)=xc(2);
elseif idx==numel(xc)
    R(1)=xc(end-1);
    R(3)=xc(1);
else
    R(1)=xc(idx-1);
    R(3)=xc(idx+1);
end

d_R=[-1 0 1]+idx-1;%delay corresponding to R

for N_i=1:N_i_max
    [d_R_max]=crit_interp_p(R,d_R);%interpolate peak from R using parabola
    R_max=fourier_series(XC,d_R_max);%calculate new xc from Fourier coefficients
    if d_R_max<d_R(2)%replace worst value
        d_R(3)=d_R(2);
        R(3)=R(2);
    else
        d_R(1)=d_R(2);
        R(1)=R(2);
    end
    diff_d=abs(d_R(2)-d_R_max);%difference between iterations
    d_R(2)=d_R_max;
    R(2)=R_max;

    if diff_d<d_tol%check tolerance for stopping condition
        break
    end
end
delay=d_R(2);%delay


function D=sim2dis(S)
%function D=sim2dis(S);
%
%PURPOSE
%
%To transform elements of a similarity matrix S into
%dissimilarities D by D=1-S.
%
%INPUT
%
%S (matrix) MxM similarity matrix whose elements must be in [-1,1],
%    for example, absolute values of linear correlation coefficients.
%
%OUTPUT
%
% D  (matrix) MxM dissimilarity matrix
%
%SEE ALSO
%  sim2dis2
%
%USED IN
%  icassoCluster
%  icassoProjection

%COPYRIGHT NOTICE
%This function is a part of Icasso software library
%Copyright (C) 2003-2005 Johan Himberg
%
%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 any later version.
%
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%GNU General Public License for more details.
%
%You should have received a copy of the GNU General Public License
%along with this program; if not, write to the Free Software
%Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

% ver 1.2 johan 100105

if any(S(:)>1) | any(S(:)<-1),
  error('Values of similarity must be in [-1,1]');
end

D=1-S;


function [xi,w]=quadraturePoints1D(n,algorithm,c1)
%%QUADRATUREPOINTS1D Obtain quadrature points and weights to efficiently
%           numerically evaluate 1D integrals involving various weighting
%           functions. The quadrature points and weights are based off
%           properties of orthogonal polynomials. The evaluation of a
%           continuous integral using cubature points is
%           integral_lowL^upL w(x)*f(x) dx=sum_{i=1}^n w_i*f(xi(i))
%           where the equality holds up for all polynomials up to a
%           particular degree. For high-order polynomials and other
%           functions, quadrature integration is just an approximation.
%
%INPUTS: n A positive integer such that 2n-1 is the highest degree to
%          which the quadrature points are accurate. n points are returned
%          by the function.
% algorithm This specifies the type of weighting function and the range of
%          the integral. These points can generally be transformed for
%          integrals over other regions/ with other parameters. Possible
%          values are
%          0 (The default if omitted or an empty matrix is passed) The
%            weighting function is w(x)=1/(sqrt(2*pi))*exp(-x^2/2), the
%            integration interval is (-Inf,Inf). The algorithm of [2] is
%            used with parameters from Table 22.7 in Ch. 22 of [1].
%          1 w(x)=exp(-x^2) on the interval (-Inf,Inf). The algorithm of
%            [2] is used with parameters from Table 22.7 in Ch. 22 of [1].
%          2 w(x)=1 on (-1,1). The function GaussLegendrePoints1D is used.
%            Note that the transformation
%            xiNew=xi*(b-a)/2+(b+a)/2;
%            wNew=w*(b-a)/2;
%            can be used to transform the points and weights to the
%            weighting function w(y)=1 on the range (a,b).
%          3 w(x)=(1-x^2)^(c1-1/2) on (-1,1) with c1>-1/2. The algorithm of
%            [2] is used with the values from Table 22.7 of [1]. Formula 10
%            is used for the c1=0 case. This becomes numerically unstable
%            for c1 close to but not equal to zero.
%          4 w(x)=exp(-x) on (0,Inf). The algorithm of [2] is used with the
%            values from Table 22.7 of [1].
%          5 w(x)=x^c1*exp(-x) on (0,Inf). The algorithm of [2] is used
%            with the values from Table 22.7 of [1] for c1>-1.
%          6 w(x)=(1+x)^c1 on (-1,1) for c1>-1. This is a special case of
%            the Jacobi polynomials. The algorithm of [2] is used with
%            values from Table 22.7 of [1].
%          7 w(x)=x^c1 on (0,1), c1>-1. The algorithm of [2] is used. The
%            three-term recursion was derived by explicitly evaluating
%            the integrals with the desired orthogonality constraints
%            until a pattern could be identified for an arbitrary order.
%          8 w(x)=|x|^c1 on (-1,1), c1>=0 and c1 is an integer. The
%            algorithm of [2] is used. The three-term recursion was derived
%            by explicitly evaluating the integrals with the desired
%            orthogonality constraints until a pattern could be identified
%            for an arbitrary order. Separate patterns for even and odd
%            integers were found.
%          9 w(x)=|x|^c1*exp(-x^2) on (-Inf,Inf), c1>=0 and c1 is an
%            integer. The algorithm of [2] is used. The three-term
%            recursion was derived by explicitly evaluating the integrals
%            with the desired orthogonality constraints until a pattern
%            could be identified for an arbitrary order. Separate patterns
%            for even and odd integers were found.
%         10 w(x)=1/sqrt(1-x^2) on (-1,1). The formula in 25.4.38 in [1] is
%            used. Note that the transformation
%            xiNew=(b+a)/2+xi*(b-a)/2; can be used to change the
%            quadrature points to the weighting function
%            w(y)=1/sqrt((y-a)*(b-y)) on (a,b).
%         11 w(x)=sqrt(1-x^2) on (-1,1). The formula in 25.4.40 in [1] is
%            used. Note that the transformation xiNew=(b+a)/2+xi*(b-a)/2
%            can be used to change the quadrature points to the weighting
%            function w(y)=sqrt((y-a)*(b-y)) on (a,b).
%         12 w(x)=sqrt(x/(1-x)) on (0,1). The formula in 25.4.42 in [1] is
%            used. Note that the transformation xiNew=a+(b-a)*xi can be
%            used to change the quadrature points to the weighting
%            function w(x)=sqrt((x-a)/(b-x)) on (a,b).
%      c1 This is a parameter that is only used with certain algorithms
%         described above.
%
%OUTPUTS: xi A 1 X n vector containing the quadrature points.
%          w An nX1 vector of the weights associated with the quadrature
%            points. Depending on the algorithm, these may or may not sum
%            to one.
%
%In the algorithms implemented in this function, with the exception of 11
%and 12, this function obtains points by passing parameters for a three-
%point recursion to the orthoPolyZerosFromRecur function.
%
%Note that the function linCubPoints2MultiDim can be given a handle to this
%function to produce multi-dimensional cubature formula.
%
%REFERENCES:
%[1] Abramowitz, M. and Stegun, I. A. (Eds.). "Orthogonal Polynomials."
%    in Ch. 22, 25 in Handbook of Mathematical Functions with Formulas,
%    Graphs, and Mathematical Tables, 9th printing. New York: Dover, 1972.
%[2] G. H. Golub and J. H. Welsh, "Calculation of Gauss quadrature rules,"
%    Mathematics of Computation, vol. 23, pp. 221-230, 1969.
%
%August 2015 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

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

switch(algorithm)
    case 0%Weighting function w(x)=1/(sqrt(2*pi))*exp(-x^2/2)
          %on (-Inf,Inf)
        mu0=1;
        a=@(i)1;
        b=@(i)0;
        c=@(i)i-1;
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 1%Weighting function w(x)=exp(-x^2) on (-Inf,Inf)
        mu0=sqrt(pi);
        a=@(i)2;
        b=@(i)0;
        c=@(i)2*(i-1);
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 2%Weighting function w(x)=1 on (-1,1)
        [xi,w]=GaussLegendrePoints1D(n);
    case 3%Weighting function w(x)=(1-x^2)^(c1-1/2) on (-1,1) with c1>-1/2
          %and c1~=0. Given c1=0, Formula 10 is used.
        if(c1==0)
            [xi,w]=quadraturePoints1D(n,10);
            return;
        end

        mu0=(sqrt(pi)*gamma((1/2)+c1))/gamma(1+c1);
        a=@(i)2*(i+c1-1)/i;
        b=@(i)0;
        c=@(i)(i+2*c1-2)/i;
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 4%Weighting function w(x)=exp(-x) on (0,Inf)
        mu0=1;
        a=@(i)-1/i;
        b=@(i)(2*(i-1)+1)/i;
        c=@(i)(i-1)/i;
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 5%Weighting function w(x)=x^c1*exp(-x) on (0,Inf) for c1>-1.
        mu0=gamma(1+c1);
        a=@(i)(-1/i);
        b=@(i)(2*i-1+c1)/i;
        c=@(i)(i-1+c1)/i;
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 6%Weighting function w(x)=(1+x)^c1 on (-1,1) for c1>-1.
          %This uses a special case of the Jacobi polynomials.
        mu0=2^(c1+1)/(c1+1);
        a=@(i)(2*i+c1-1)*(2*i+c1)*(2*i+c1-2)/(2*i*(i+c1)*(2*i+c1-2));
        b=@(i)(2*i+c1-1)*(-c1^2)/(2*i*(i+c1)*(2*i+c1-2));
        c=@(i)2*(i-1)*(i+c1-1)*(2*i+c1)/(2*i*(i+c1)*(2*i+c1-2));
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 7%Weighting function w(x)=x^c1 on (0,1) for c1>-1
        mu0=1/(1+c1);
        a=@(k)1;
        b=@(k)(-(c1^2+(2*(k-1)+1)*c1+2*k*(k-1))/((2*(k-1)+c1)*(2*k+c1)));
        c=@(k)((k-1)^2*(c1+k-1)^2/((2*k-3+c1)*(2*k-2+c1)^2*(2*k-1+c1)));
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 8%Weighting function w(x)=|x|^c1 on (-1,1), c1>=0 and c1 is an
          %integer.
        if(mod(c1,2)==0)%Even integer
            c1=c1/2;
            mu0=2/(1+2*c1);
            a=@(k)1;
            b=@(k)0;
            c=@(k)((k-1+2*c1*mod(k+1,2))^2/((2*k-3+2*c1)*(2*k-1+2*c1)));
        else%Odd integer
            c1=(c1-1)/2;
            mu0=1/(1+c1);
            a=@(k)1;
            b=@(k)0;
            c=@(k)(((k-mod(k,2))/2+mod(k+1,2)*c1)^2/((k-1+c1)*(k+c1)));
        end
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 9%Weighting function w(x)=|x|^c1*exp(-x^2) on (-Inf,Inf), c1>=0
           %and c1 is an integer.
        if(mod(c1,2)==0)%Even integer
            c1=c1/2;
            mu0=gamma(c1+1/2);
            a=@(k)1;
            b=@(k)0;
            c=@(k)((k-1)/2+c1*mod(k+1,2));
        else%Odd integer
            c1=(c1-1)/2;
            mu0=factorial(c1);
            a=@(k)1;
            b=@(k)0;
            c=@(k)((k-mod(k,2))/2+c1*mod(k+1,2));
        end
        [xi,w]=orthoPolyZerosFromRecur(n,a,b,c,mu0);
    case 10%Weighting function w(x)=1/sqrt(1-x^2) on (-1,1) from 25.4.38 in
          %[1].
        xi=cos((2*(1:n)-1)*pi/(2*n));
        w=repmat(pi/n,[n,1]);
    case 11%Weighting function w(x)=sqrt(1-x^2) on (-1,1) from 25.4.40 in
          %[1].
        xi=cos((1:n)*pi/(n+1));
        w=(pi/(n+1))*sin((1:n)'*pi/(n+1)).^2;
    case 12%Weighting function w(x)=sqrt(x/(1-x)) on (0,1) from 25.4.42 in
          %[1].
        xi=cos((pi/2)*((2*(1:n)-1)/(2*n+1))).^2;
        w=(2*pi/(2*n+1))*xi';
    otherwise
        error('Unknown algorithm specified');
end
end

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


% book : Signals and Systems Laboratory with MATLAB
% authors : Alex Palamides & Anastasia Veloni

%Parseval's Theorem

%x(t)=exp(-t^2)

syms t w
x=exp(-t^2);
Et=int((abs(x))^2,t,-inf,inf) ;
eval(Et)
X=fourier(x,w);
Ew=(1/(2*pi))*int((abs(X))^2,w,-inf,inf);
eval(Ew)

%x(t)=exp(-t)u(t)
x=exp(-t) *heaviside(t);
Et=int((abs(x))^2,t,-inf,inf)
X=fourier(x,w);
Integ= int((abs(X))^2,w,-inf,inf);
Ew=(1/(2*pi))*Integ;
eval(Ew)


function [ w, xyz ] = tetrahedron_unit_o01 ( )

%*****************************************************************************80
%
%% TETRAHEDRON_UNIT_O01 returns a 1 point quadrature rule for the unit tetrahedron.
%
%  Discussion:
%
%    The integration region is:
%
%      0 <= X
%      0 <= Y
%      0 <= Z
%      X + Y + Z <= 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 April 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Carlos Felippa,
%    A compendium of FEM integration formulas for symbolic work,
%    Engineering Computation,
%    Volume 21, Number 8, 2004, pages 867-890.
%
%  Parameters:
%
%    Output, real W(1), the weights.
%
%    Output, real XYZ(3,1), the abscissas.
%
  w(1:1,1) = [ ...
    1.0 ];

  xyz(1:3,1:1) = [ ...
    0.25000000000000000000,  0.25000000000000000000,  0.25000000000000000000 ]';

  return
end