function [ P, L, U ] = r8mat_to_plu ( A )

%*****************************************************************************80
%
%% r8mat_to_plu() determines the PLU factorization of a matrix.
%
%  Discussion:
%
%    The desired factorization is A = P' * L * U
%    * P is a permutation matrix;
%    * L is a unit lower triangular matrix;
%    * U is an upper triangular matrix.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 January 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(m,n), the system matrix.
%
%  Output:
%
%    global real P(m,m), L(m,m), U(m,n), the PLU factors.
%
  [ m, n ] = size ( A );

  P = eye(m);
  L = eye(m);
  U = A;

  info = 0;

  for j = 1 : n - 1
%
%  Pivoting.
%
    [ v, p ] = max ( abs ( U(j:n,j) ) );
    p = p + j - 1;

    if ( v == 0.0 )
      continue;
    end

    if ( p ~= j )
      T      = P(j,:);
      P(j,:) = P(p,:);
      P(p,:) = T;

      T      = U(j,:);
      U(j,:) = U(p,:);
      U(p,:) = T;

      T          = L(j,1:j-1);
      L(j,1:j-1) = L(p,1:j-1);
      L(p,1:j-1) = T;
    end
%
%  Elimination.
%
    for i = j + 1 : n
      s = U(i,j) / U(j,j);
      U(i,:) = U(i,:) - s * U(j,:);
      U(i,j) = 0.0;
      L(i,j) = s;
    end

  end

  return
end

