function [ nst, ist, jst, vst ] = dif2_st ( m, n )

%*****************************************************************************80
%
%% dif2_st() sets up the second difference matrix in Sparse Triplet (ST) format.
%
%  Discussion:
%
%    The diagonal entries are listed first.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    17 February 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N: the number of rows and columns.
%    N defaults to M.
%
%  Output:
%
%    integer NST: the number of nonzeros.
%
%    integer IST(NST), JST(NST): the row and column indices.
%
%    real VST(NST): the values.
%
  if ( nargin == 1 )
    n = m;
  end

  ndiag = min ( m, n );
  nsub = min ( m, n - 1 );
  nsup = min ( m - 1, n );

  nst = nsub + ndiag + nsup;

  ist = zeros ( nst, 1 );
  ist(1:ndiag) = 1:ndiag;
  ist(ndiag+1:ndiag+nsub) = 2:nsub+1;
  ist(ndiag+nsub+1:ndiag+nsub+nsup) = 1:nsup;

  jst = zeros ( nst, 1 );
  jst(1:ndiag) = 1:ndiag;
  jst(ndiag+1:ndiag+nsub) = 1:nsub;
  jst(ndiag+nsub+1:ndiag+nsub+nsup) = 2:nsup+1;

  vst = zeros ( nst, 1 );
  vst(1:ndiag) = 2.0;
  vst(ndiag+1:ndiag+nsub) = -1.0;
  vst(ndiag+nsub+1:ndiag+nsub+nsup) = -1.0;

  return
end

