function L = laplacian5_torus ( A, dx, dy )

%*****************************************************************************80
%
%% laplacian5_torus uses a 5 point Laplacian stencil on a torus.
%
%  Discussion:
%
%    1) Assumes the region is a rectangular torus;
%    2) Assumes the mesh is uniform in both X and Y directions.
%    3) Assumes the spacing is dx in both directions.
%
%    The computation is done using for loops.  It could be speeded up
%    by using vectorized MATLAB statements.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 June 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(NXP,NYP): function values, sampled on a regular grid.
%
%    real DX, DY: the grid spacing in the X and Y directions.
%
%  Output:
%
%    real L(NXP,NYP): the values of the Laplacian, as estimated by the
%    5 point periodic stencil.
%
  [ nxp, nyp ] = size ( A );
  L = zeros ( nxp, nyp );

  for j = 1 : nyp

    jp1 = j + 1;
    if ( nyp < jp1 )
      jp1 = 1;
    end
    jm1 = j - 1;
    if ( jm1 < 1 )
      jm1 = nyp;
    end

    for i = 1 : nxp

      ip1 = i + 1;
      if ( nxp < ip1 )
        ip1 = 1;
      end
      im1 = i - 1;
      if ( im1 < 1 )
        im1 = nxp;
      end

      L(i,j) = ( A(ip1,j) - 2.0 * A(i,j) + A(im1,j) ) / dx^2 ...
             + ( A(i,jp1) - 2.0 * A(i,j) + A(i,jm1) ) / dy^2;
    end
  end

  return
end

