function diff = diff_compute ( chain_num, gen_index, gen_num, jump_dim, ...
  jump_num, pair_num, par_num, r, z ) 

%*****************************************************************************80
%
%% DIFF_COMPUTE computes the differential evolution.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 January 2018
%
%  Author:
%
%    Original FORTRAN90 version by Guannan Zhang.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Jasper Vrugt, CJF ter Braak, CGH Diks, Bruce Robinson, James Hyman, 
%    Dave Higdon,
%    Accelerating Markov Chain Monte Carlo Simulation by Differential 
%    Evolution with Self-Adaptive Randomized Subspace Sampling,
%    International Journal of Nonlinear Sciences and Numerical Simulation,
%    Volume 10, Number 3, March 2009, pages 271-288.
%
%  Input parameters:
%
%    Input, integer CHAIN_NUM, the total number of chains.
%    3 <= CHAIN_NUM.
%
%    Input, integer GEN_INDEX, the index of the current generation.
%    1 <= GEN_INDEX <= GEN_NUM.
%
%    Input, integer GEN_NUM, the total number of generations.
%    2 <= GEN_NUM.
%
%    Input, integer JUMP_DIM(JUMP_NUM), the dimensions in which
%    a jump is to be made.
%
%    Input, integer JUMP_NUM, the number of dimensions in which
%    a jump will be made.  0 <= JUMP_NUM <= PAR_NUM.
%
%    Input, integer PAIR_NUM, the number of pairs of 
%    crossover chains.
%    0 <= PAIR_NUM.
%
%    Input, integer PAR_NUM, the total number of parameters.
%    1 <= PAR_NUM.
%
%    Input, integer R(2,PAIR_NUM), pairs of chains used
%    to compute differences.
%
%    Input, real Z(PAR_NUM,CHAIN_NUM,GEN_NUM), the Markov chain 
%    sample data.
%
%  Output parameters:
%
%    Output, real DIFF(PAR_NUM), the vector of pair differences.
%

%
%  Produce the difference of the pairs used for population evolution.
%
  diff = zeros ( par_num, 1 );

  for pair = 1 : pair_num
    for j = 1 : jump_num
      k = jump_dim(j);
      diff(k) = diff(k) ...
        + ( z(k,r(1,pair),gen_index-1) - z(k,r(2,pair),gen_index-1) );
    end
  end

  return
end
