%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    Copyright (c) 2004.  Takayuki Osogami.  All Rights Reserved.   %
%                                                                   %
% Permission to use, copy, modify, and distribute this source code  %
% and its documentation for any purpose, without fee, and without   %
% a written agreement, is hereby granted, provided that the above   %
% copyright notice, this paragraph and the following two paragraphs %
% appear in all copies, modifications, and distributions.           %
% Created by Takayuki Osogami, Department of Computer Science,      %
% Carnegie Mellon University.                                       %
%                                                                   %
% IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR ANY       %
% DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS   %
% SOURCE CODE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN    %
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        %
%                                                                   %
% THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,      %
% BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY     %
% AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOURCE CODE AND        %
% ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS         %
% PROVIDED "AS IS."  THE AUTHOR HAS NO OBLIGATION TO PROVIDE        %
% MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: [EE] = Neuts3(A0,A1,A2,G,n,k,epsilon)
%
% Given a QBD process,  Neuts3 returns
% the first three moments of the first passage time
% from level k to level k-1.
% Here, the generator matrix of the input QBD process is
%
% [ A1{1} A0{1}                                    ]
% [ A2{2} A1{2} A0{2}                              ]
% [       A2{3} A1{3} A0{3}                        ]
% [              :     :                           ]
% [                        A2{n} A1{n} A0{n}       ]
% [                              A2{n} A1{n} A0{n} ]
% [                                     :     :    ]
%
% Note that the QBD process repeats after level n.
%
% We define cells, A0, A1, and A2:
% A0 = {A0{1} A0{2} ... A0{n}}
% A1 = {A1{1} A1{2} ... A1{n}}
% A2 = {A2{1} A2{2} ... A2{n}},
%
% INPUT: A0,A1,A2: cells for A0{i}, A1{i}, A2{i}
%        G: a cell for G matrices, G{1}, ..., G{n}
%        n: level that the QBD process starts repeating
%        k: calculate first passage time from level k to level k-1
%        epsilon: the error bound (epsilon = 1e-6: recommended).
%
% OUTPUT: EE: a cell for EE{1}, EE{2}, EE{3}
%             EE{h} is a matrix of the h-th moment, where (i,j) element
%             denotes the first passage time from phase i in level k to
%             level k-1 given that phase j is the state to reach in level k-1
%
%
% author: Takayuki Osogami
%         Department of Computer Science
%         Carnegie Mellon University
%         osogami@cs.cmu.edu
% date: July 19, 2004

function [EE] = Neuts3(A0,A1,A2,G,n,k,epsilon)

%%%%%%%%%%%%%%%%%%%%
%% repeating part %%
%%%%%%%%%%%%%%%%%%%%

[H] = homoNeuts(A0{n},A1{n},A2{n},G{n},epsilon);

if( k >= n )

  EE = H;

else

  %%%%%%%%%%%%%%%%%%%%%%%%
  %% non-repeating part %%
  %%%%%%%%%%%%%%%%%%%%%%%%

  num = n - k;
  for i = 1:num

    m = n - i;
    [H] = nonhomoNeuts(A0{m},A1{m},A2{m},G{m},G{m+1},H,epsilon);

  end

  EE = H;

end
