// -*- C++ -*-
// $Id: 
#include "gsl/gsl_sf_bessel.h"
#include <cmath>
#include <signal.h>
#include <assert.h>


#define GF_DBL_EPSILON        2.2204460492503131e-16


namespace Genfun {
namespace IntegralOrder {

FUNCTION_OBJECT_IMP(Bessel)

inline
Bessel::Bessel(Type type, unsigned int order):
  _type(type),_order(order)
{
}

inline
Bessel::~Bessel() {
}

inline
Bessel::Bessel(const Bessel & right):
  _type(right._type),
  _order(right._order)
{
}

inline
double Bessel::operator() (double x) const {
  gsl_sf_result result;
  if (_type==J) {
    int status = gsl_sf_bessel_Jn_e(_order, x,  &result);
    if (status!=0) {
      std::cerr << "Warning, GSL function gsl_sf_bessel_Jn_impl" 
		<< " return code" << status << std::endl;
      raise(SIGFPE);
    }
    return result.val;
  }
  else if (_type==Y) {
    int status = gsl_sf_bessel_Yn_e(_order, 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;
  }
  else {
    return 0;
  }
}

} // end namespace IntegralOrder

namespace FractionalOrder {

FUNCTION_OBJECT_IMP(Bessel)

inline
Bessel::Bessel(Type type):
  _type(type),
  _order("Order", 0.0,-10,10)
{
}

inline
Bessel::~Bessel() {
}

inline
Bessel::Bessel(const Bessel & right):
  _type(right._type),
  _order(right._order)
{
}


inline
Parameter & Bessel::order() {
  return _order;
}

inline
const Parameter & Bessel::order() const {
  return _order;
}


inline
double Bessel::operator() (double x) const {
  gsl_sf_result result;
  if (_type==J) {
    int status = gsl_sf_bessel_Jnu_e(_order.getValue(), x,  &result);
    if (status!=0) {
      std::cerr << "Warning, GSL function gsl_sf_bessel_Jnu_impl" 
		<< " return code" << status << std::endl;
      raise(SIGFPE);
    }
    return result.val;
  }
  else if (_type==Y) {
    int status = gsl_sf_bessel_Ynu_e(_order.getValue(), x,  &result);
    if (status!=0) {
      std::cerr << "Warning, GSL function gsl_sf_bessel_Ynu_impl" 
		<< " return code" << status << std::endl;
      raise(SIGFPE);
    }
    return result.val;
  }
  return result.val;
}


} // end namespace FractionalOrder

} // end namespace Genfun
