#include <iostream>
#include <iomanip>
using namespace std;

#include "MpIeee.hh"
#include "CMpIeee.hh"
#include "ArithmosIO.hh"

/* specfunc/expint3.c
 * 
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
 * 
 * This program 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 2 of the License, or (at
 * your option) any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Author: G. Jungman */

#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_expint.h>

#include "error.h"

#include "chebyshev.h"
#include "cheb_eval.c"

static MpIeee expint3_data[24] =  {
  MpIeee( "1.269198414221126014" ),
 -MpIeee( "0.248846446384140982" ),
  MpIeee( "0.80526220717231041e-01" ),
 -MpIeee( "0.25772733251968330e-01" ),
  MpIeee( "0.7599878873073774e-02" ),
 -MpIeee( "0.2030695581940405e-02" ),
  MpIeee( "0.490834586699330e-03" ),
 -MpIeee( "0.107682239142021e-03" ),
  MpIeee( "0.21551726264290e-04" ),
 -MpIeee( "0.3956705137384e-05" ),
  MpIeee( "0.6699240933896e-06" ),
 -MpIeee( "0.105132180807e-06" ),
  MpIeee( "0.15362580199e-07" ),
 -MpIeee( "0.20990960364e-08" ),
  MpIeee( "0.2692109538e-09" ),
 -MpIeee( "0.325195242e-10" ),
  MpIeee( "0.37114816e-11" ),
 -MpIeee( "0.4013652e-12" ),
  MpIeee( "0.412334e-13" ),
 -MpIeee( "0.40338e-14" ),
  MpIeee( "0.3766e-15" ),
 -MpIeee( "0.336e-16" ),
  MpIeee( "0.29e-17" ),
 -MpIeee( "0.2e-18" )
};
static cheb_series expint3_cs = {
  expint3_data,
  23,
  -1.0, 1.0,
  15
};

static MpIeee expint3a_data[23] =  {
   MpIeee( "1.9270464955068273729" ),
  -MpIeee( "0.349293565204813805e-01" ),
   MpIeee( "0.14503383718983009e-02" ),
  -MpIeee( "0.8925336718327903e-04" ),
   MpIeee( "0.70542392191184e-05" ),
  -MpIeee( "0.6671727454761e-06" ),
   MpIeee( "0.724267589982e-07" ),
  -MpIeee( "0.87825825606e-08" ),
   MpIeee( "0.11672234428e-08" ),
  -MpIeee( "0.1676631281e-09" ),
   MpIeee( "0.257550158e-10" ),
  -MpIeee( "0.41957888e-11" ),
   MpIeee( "0.7201041e-12" ),
  -MpIeee( "0.1294906e-12" ),
   MpIeee( "0.24287e-13" ),
  -MpIeee( "0.47331e-14" ),
   MpIeee( "0.95531e-15" ),
  -MpIeee( "0.1991e-15" ),
   MpIeee( "0.428e-16" ),
  -MpIeee( "0.94e-17" ),
   MpIeee( "0.21e-17" ),
  -MpIeee( "0.5e-18" ),
   MpIeee( "0.1e-18" )
};
static cheb_series expint3a_cs = {
  expint3a_data,
  22,
  -1.0, 1.0,
  10
};


/*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/

int  gsl_sf_expint_3_e(const MpIeee x, gsl_sf_result * result)
{
  const MpIeee val_infinity=  0.892979511569249211;

  /* CHECK_POINTER(result) */

  if(x < 0.0) {
    DOMAIN_ERROR(result);
  }
  else if(x < 1.6*GSL_ROOT3_DBL_EPSILON) {
    result->val = x;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else if(x <= 2.0) {
    const MpIeee t=  x*x*x/4.0 - 1.0;
    gsl_sf_result result_c;
    cheb_eval_e(&expint3_cs, t, &result_c);
    result->val = x * result_c.val;
    result->err = x * result_c.err;
    return GSL_SUCCESS;
  }
  else if(x < pow(-GSL_LOG_DBL_EPSILON, 1.0/3.0)) {
    const MpIeee t=  16.0/(x*x*x) - 1.0;
    const MpIeee s=  exp(-x*x*x)/(3.0*x*x);
    gsl_sf_result result_c;
    cheb_eval_e(&expint3a_cs, t, &result_c);
    result->val = val_infinity - result_c.val * s;
    result->err = val_infinity * GSL_DBL_EPSILON + s * result_c.err;
    return GSL_SUCCESS;
  }
  else {
    result->val = val_infinity;
    result->err = val_infinity * GSL_DBL_EPSILON;
    return GSL_SUCCESS;
  }
}


/*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/

#include "eval.h"

MpIeee gsl_sf_expint_3(MpIeee x)
{
  EVAL_RESULT(gsl_sf_expint_3_e(x, &result));
}
