// file: $isip/class/stat/UniformModel/uni_07.cc
// version: $Id: uni_07.cc 6394 2001-02-26 16:24:31Z hamaker $
//

// isip include files
//
#include "UniformModel.h"

// method: init
//
// arguments: none
//
// return: a boolean value
//
// this method initializes the distribution
//
boolean UniformModel::init() {

  // if we are not in the precompute mode, we don't need to do anything.
  //
  if (mode_d != PRECOMPUTE) {
    is_valid_d = true;
    return true;
  }

  // compute the scale factor
  //
  scale_d = initScale();

  // exit gracefully
  //
  return (is_valid_d = true);
}

// method: initScale
//
// arguments: none
//
// return: a boolean value
//
// this method computes the scale factor for the distribution
// (see getLikelihood() for a more detailed explanation).
//
float UniformModel::initScale() {

  // check the arguments
  //
  long len = min_d.length();

  if (len != max_d.length()) {
    Error::handle(name(), L"initScale", ERR, __FILE__, __LINE__);
    return 0;
  }

  // compute the surface area of the region of support for the distribution
  //
  float sum = 1.0;

  for (long i = 0; i < len; i++) {
    if(!max_d(i).eq(min_d(i))) 
      sum *= max_d(i) - min_d(i);
  }

  // set the scale factor to the inverse of this number and return the
  // scale
  //
  return 1.0 / sum;
}
