function [ cr_dis, cr_ups ] = cr_dis_update ( chain_index, chain_num, ...
  cr_dis, cr_index, cr_num, cr_ups, gen_index, gen_num, par_num, z ) 

%*****************************************************************************80
%
%% CR_DIS_UPDATE updates the CR distance.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 January 2018
%
%  Author:
%
%    John Burkardt
%
%  Input parameters:
%
%    Input, integer CHAIN_INDEX, the index of the chain.
%    1 <= CHAIN_INDEX <= CHAIN_NUM.
%
%    Input, integer CHAIN_NUM, the total number of chains.
%    3 <= CHAIN_NUM.
%
%    Input, real CR_DIS(CR_NUM), the CR distances.
%
%    Input, integer CR_INDEX, the index of the CR.
%    1 <= CR_INDEX <= CR_NUM.
%
%    Input, integer CR_NUM, the total number of CR values.
%    1 <= CR_NUM.
%
%    Input, integer CR_UPS(CR_NUM), the number of updates 
%    for each CR.
%
%    Input, integer GEN_INDEX, the index of the generation.
%    1 <= GEN_INDEX <= GEN_NUM.
%
%    Input, integer GEN_NUM, the total number of generations.
%    2 <= GEN_NUM.
%
%    Input, integer PAR_NUM, the total number of parameters.
%    1 <= PAR_NUM.
%
%    Input, real Z(PAR_NUM,CHAIN_NUM,GEN_NUM), the Markov chain 
%    sample data.
%
%  Input parameters:
%
%    Output, real CR_DIS(CR_NUM), the CR distances.
%
%    Output, integer CR_UPS(CR_NUM), the number of updates 
%    for each CR.
%

%
%  Compute the standard deviations.
%
  std = std_compute ( chain_num, gen_index, gen_num, par_num, z );
%
%  Increment the update count.
%
  cr_ups(cr_index) = cr_ups(cr_index) + 1;
%
%  Update the CR distance.
%
  for i = 1 : par_num 

    cr_dis(cr_index) = cr_dis(cr_index) ...
      + ( ( z(i,chain_index,gen_index) - z(i,chain_index,gen_index-1) ) ...
      / std(i) ) ^ 2;

  end

  return
end
