function P = poisson_rb_reordering(m)

% This routine generates the permutation matrix P for reordering the
% variables in the discrete 2D Poisson problem to achieve red-black
% ordering.  Specifically, if u is the vector of variables with the
% standard ordering (R, B, R, B, ..., R), then P*u is the vector of
% variables with red-black ordering (R, ..., R, B, ..., B). Additionally,  
% if A is the discrete Laplacian matrix for the standard ordering, then
% P*A*P' is the corresponding matrix for red-black ordering.
%
% The Poisson problem is assumed to be discretized using centered
% differences on an mxm evenly spaced grid on [0,1]x[0,1].
%
% Input: 
%  m = number of interior grid points in each direction. 
%
% Output: 
%   P = permutation matrix (sparse)
% 
% NOTE: m must be odd.
%

if 2\(m+1) ~= round(2\(m+1)), 
  error('Input m must be odd.');
end

n = m^2;

P = speye(n,n); 

P = [P(1:2:n,:);P(2:2:n-1,:)];
