// -*- C++ -*-
// $Id: 
#include "CLHEP/GenericFunctions/Variable.hh"
#include "CLHEP/GenericFunctions/Power.hh"
#include <gsl/gsl_sf_legendre.h>
#include <cmath>
#include <signal.h>
#include <assert.h>


namespace Genfun {

FUNCTION_OBJECT_IMP(AssociatedLegendre)

// This is the product n (n-2) (n-4)... 
inline double dfactorial (int n) {
  if (n<=1) return 1.0;
  else return n*dfactorial(n-2);
}
//
inline
AssociatedLegendre::AssociatedLegendre(unsigned int l, unsigned int m):
  AbsFunction(),
  _l(l),
  _m(m)
{
  assert(m<=l);
}

inline
AssociatedLegendre::~AssociatedLegendre() {
}

inline
AssociatedLegendre::AssociatedLegendre(const AssociatedLegendre & right):
AbsFunction(),
_l(right._l),
_m(right._m)
{
}

inline
unsigned int AssociatedLegendre::l() const {
  return _l;
}

inline
unsigned int AssociatedLegendre::m() const {
  return _m;
}


inline
double AssociatedLegendre::operator() (double x) const {
  gsl_sf_result result;
  int status = gsl_sf_legendre_Plm_e (_l, _m, x, &result);
  
  if (status!=0) {
    std::cerr << "Warning, GSL function gsl_sf_bessel_Yn_impl" 
	      << " return code" << status << std::endl;
    raise(SIGFPE);
  }
  return result.val;
}

} // end namespace Genfun
