function D = delsq(G)
%DELSQ	Construct five-point finite difference Laplacian.
%	delsq(G) is the sparse form of the two-dimensional,
%	5-point discrete negative Laplacian on the grid G.
%	The grid G can be generated by NUMGRID or NESTED.
%
%	See also NUMGRID, DEL2, DELSQDEMO.

%	C. Moler, 7-16-91.
%	Copyright (c) 1984-93 by The MathWorks, Inc.

[m,n] = size(G);

% Indices of interior points
p = find(G);

% Connect interior points to themselves with 4's.
i = G(p);
j = G(p);
s = 4*ones(size(p));

% for k = north, east, south, west
for k = [-1 m 1 -m]
   % Possible neighbors in k-th direction
   Q = G(p+k);
   % Index of points with interior neighbors
   q = find(Q);
   % Connect interior points to neighbors with -1's.
   i = [i; G(p(q))];
   j = [j; Q(q)];
   s = [s; -ones(length(q),1)];
end
D = sparse(i,j,s);
