function x = i4mat_rref2_solve ( m, n1, n2, a )

%*****************************************************************************80
%
%% i4mat_rref2_solve() solves an integer RREF linear system.
%
%  Discussion:
%
%    An MxN1 integer matrix A has been brought to reduced row echelon form (RREF).
%
%    The RREF procedure has also been applied "passively" to right hand vectors
%    stored in columns N1+1 through N1+N2.  These columns are passive only in
%    the sense that they did not participate in the pivot search.
%
%    If an original right hand side vector b represents a consistent linear 
%    system A*x=b, then this function will determine the solution vector x.
%
%    In cases where the original right hand side was not consistent, then
%    the vector x returned is not a solution.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    20 September 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, the number of rows in A.
%
%    integer N1, N2, the number of active and passive columns in A.
%
%    integer A(M,N1+N2), a matrix containing a reduced row echelon
%    form matrix in columns 1 through N1, and N2 right hand sides in columns
%    N1+1 through N1+N2 which have also gone through the RREF process.
%
%  Output:
%
%    real X(N2,1), the "solutions" to the original system A*x=b.
%
  x = zeros ( n1, n2 );

  piv = 1;
  row = 1;

  while ( piv <= n1 )

    if ( a(row,piv) ~= 0 )
      x(piv,1:n2) = a(row,n1+1:n1+n2) / a(row,piv);
      row = row + 1;
      if ( m < row )
        break;
      end
    end

    piv = piv + 1;

  end

  return  
end
