## Copyright (C) 2011-2025 L. Markowsky <lmarkowsky@gmail.com>
##
## This file is part of the fuzzy-logic-toolkit.
##
## The fuzzy-logic-toolkit is free software; you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3 of
## the License, or (at your option) any later version.
##
## The fuzzy-logic-toolkit is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the fuzzy-logic-toolkit; see the file COPYING.  If not,
## see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{vpc} =} partition_coeff (@var{soft_partition})
##
## Return the partition coefficient for a given soft partition.
##
## The argument to partition_coeff is:
## @itemize @w
## @item
## @var{soft_partition}: the membership degree of each input data point in each cluster
## @end itemize
##
## The return value is:
## @itemize @w
## @item
## @var{vpc}: the partition coefficient for the given soft partition
## @end itemize
##
## To run demonstration code that uses this function, type "@t{demo fcm}"
## or "@t{demo gustafson_kessel}" (without the quotation marks) at the
## Octave prompt.
##
## For more information about the @var{soft_partition} matrix, please see the
## documentation for function fcm.
##
## @seealso{fcm, gustafson_kessel, partition_entropy, xie_beni_index}
##
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit partition coefficient cluster
## Directory:     fuzzy-logic-toolkit/inst/
## Filename:      partition_coeff.m
## Last-Modified: 13 Jun 2024

##----------------------------------------------------------------------
## Note: This function is an implementation of Equation 13.9 (corrected
##       -- the equation in the book omits the exponent 2) in
##       Fuzzy Logic: Intelligence, Control and Information, by J. Yen
##       and R. Langari, Prentice Hall, 1999, page 384 (International
##       Edition). 
##----------------------------------------------------------------------

function vpc = partition_coeff (soft_partition)

  ## If partition_coeff was called with an incorrect number of
  ## arguments, or the argument does not have the correct type,
  ## print an error message and halt.

  if (nargin != 1)
    error ("partition_coeff requires 1 argument\n");
  elseif (!(is_real_matrix (soft_partition) &&
            (min (min (soft_partition)) >= 0) &&
            (max (max (soft_partition)) <= 1)))
    error ("partition_coeff's argument must be a matrix of reals 0.0-1.0\n");
  endif

  ## Compute and return the partition coefficient.

  soft_part_sqr = soft_partition .* soft_partition;
  vpc = (sum (sum (soft_part_sqr))) / columns (soft_partition);

endfunction

## Test input validation
%!error <partition_coeff requires 1 argument>
%! partition_coeff()
%!error <partition_coeff: function called with too many inputs>
%! partition_coeff(1, 2)
%!error <partition_coeff's argument must be a matrix of reals 0.0-1.0>
%! partition_coeff([-1 2])

