function rank = vector_rank ( d, b, a )

%*****************************************************************************80
%
%% vector_rank() ranks a vector.
%
%  Example:
%
%    d = 3,
%    b = 3
%    A = ( 2, 1, 3 )
%
%    RANK = 12
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer d: the vector dimension.
%
%    integer b: the upper limit for the array values.
%
%    integer a(d): the vector to be ranked.
%
%  Output:
%
%    integer RANK, the rank of the vector, or -1 if A is not legal.
%
  rank = -1;

  for i = 1 : d
    if ( a(i) < 1 | b < a(i) )
      return;
    end
  end

  rank = 0;
  for i = 1 : d
    rank = b * rank + a(i);
  end

  rank = 1;
  range = 1;
  for i = 1 : d
    rank = rank + ( a(d+1-i) - 1 ) * range;
    range = range * b;
  end

  return
end

