function L = laplacian5_torus_vector ( A, dx, dy )

%*****************************************************************************80
%
%% laplacian5_torus_vector() uses a 5 point Laplacian stencil on a 2D 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.
%
%    Vectorized MATLAB statements are used for efficiency.
%
%    This code would be much simpler if we could simply ignore the last
%    row and column of data, which the periodic boundary conditions
%    identify with the first row and column.
%
%    To calculate effectively, we have to temporarily suppress the last
%    row and column, do our calculation, and then copy the repeated
%    information.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 September 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(NXP,NYP): function values, sampled on a regular 2D 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 );

  A2 = A(1:nxp-1,1:nyp-1);

  f_xx = (        circshift ( A2, [  1,  0 ] ) ... 
         - 2.0 .* circshift ( A2, [  0,  0 ] ) ...
                + circshift ( A2, [ -1,  0 ] ) ) / dx^2;

  f_yy = (        circshift ( A2, [  0,  1 ] ) ...
         - 2.0 .* circshift ( A2, [  0,  0 ] ) ...
                + circshift ( A2, [  0, -1 ] ) ) / dy^2;

  L2 = f_xx + f_yy;

  L = [ L2,      L2(:,1); ...
        L2(1,:), L2(1,1) ];
  
  return
end

