function x = plut_solve ( P, L, U, b )
%
%  function x = plut_solve ( P, L, U, b )
%
%  Purpose:
%
%    PLUT_SOLVE solves a transposed linear system A'*x=b, given the PLU factors of A.
%
%  Discussion:
%
%    A is assumed to be a square nonsingular matrix.
%
%    The PLU factors of A might come from the routine GE_PP, for instance.
%
%    The order of the system, N, is determined by the rows of B.
%    The number of right hand sides, NRHS, is determined by the columns of B.
%
%    It's best to think of the code in terms of the steps:
%
%      1: Solve U' * z = b
%      2: Solve L' * y = z.
%      3: Solve P' * x = y.
%
%    where separate variables X, Y and Z are used.  However, it turns
%    out that we can actually use the same vector X for all three
%    variables X, Y and Z, so that's what we do here.
%
%  Modified:
%
%    22 February 2000
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real P(N,N), the permutation factor.
%
%    Input, real L(N,N), the unit lower triangular factor.
%
%    Input, real U(N,N), the upper triangular factor.
%
%    Input, real B(N,NRHS), the right hand side.  
%
%    Output, real X(N), the solution.
%
[ n, nrhs ] = size ( b );

x = b;
%
%  Step 1: Solve U' * z = b.
%
for k = 1 : nrhs
  for j = 1 : n
    x(j,k) = x(j,k) / U(j,j);
    for i = j+1 : n
      x(i,k) = x(i,k) - U(j,i) * x(j,k);
    end
  end
end
%
%  Step 2: Solve L' * y = z.
%
for k = 1 : nrhs
  for j = n : -1 : 1
    for i = 1 : j-1
      x(i,k) = x(i,k) - L(j,i) * x(j,k);
    end
  end
end
%
%  Step 3: Solve P' * x = y
%
x = P * x;
