%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    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: [R,U,G] = RUGmatrices(A0,A1,A2,n)
%
% This program iteratively computes the sequence of R matrices,
% given A2, A1, and A0.  
% Here, the generator matrix of the 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}
%        n: level that the QBD process starts repeating
%        epsilon: the error bound (epsilon = 1e-6: recommended).
%
% OUTPUT: R: cells for R{i}, R matrices
%         U: cells for U{i}, U matrices
%         G: cells for G{i}, Gmatrices
%
%
% author: Takayuki Osogami
%         Department of Computer Science
%         Carnegie Mellon University
%         osogami@cs.cmu.edu
% date: April 28, 2004

function [R,U,G] = RUGmatrices(A0,A1,A2,n,epsilon)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate G{n}, U{n}, and R{n} of the repeating part %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  G{n} = Gmatrix(A0{n},A1{n},A2{n},epsilon);
  U{n} = A1{n} + A0{n} * G{n};
  R{n} = A0{n} * inv(-U{n});


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate G, U, and R of the non-repeating part %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  for i = 1:n-2

    j = n - i;

    U{j} = A1{j} + A0{j} * G{j+1};
    Tmp = inv(-U{j});
    G{j} = Tmp * A2{j};
    R{j} = A0{j-1} * Tmp;

    clear Tmp;

  end

  U{1} = A1{1} + A0{1} * G{2};

