/* specfunc/hermite.c
 *
 * Copyright (C) 2011, 2012, 2013, 2014, 2019 Konrad Griessinger (konradg(at)gmx.net)
 * Copyright (C) 2019 Patrick Alken
 *
 * 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/*----------------------------------------------------------------------*
 * "The purpose of computing is insight, not numbers." - R.W. Hamming   *
 * Hermite polynomials, Hermite functions                               *
 * and their respective arbitrary derivatives                           *
 *----------------------------------------------------------------------*/

/* TODO:
 * - array functions for derivatives of Hermite functions
 * - asymptotic approximation for derivatives of Hermite functions
 * - refine existing asymptotic approximations, especially around x=sqrt(2*n+1) or x=sqrt(2*n+1)*sqrt(2), respectively
 */

#include <config.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_airy.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sf_pow_int.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_sf_hermite.h>

#include "error.h"
#include "eval.h"

#define pow2(n) (gsl_sf_pow_int(2,n))
#define RND(x)  ((double) ((x >= 0) ? (int) (x + 0.5) : (int) (x - 0.5)))

/* evaluates the probabilists' Hermite polynomial of order n at position x */
int
gsl_sf_hermite_prob_e(const int n, const double x, gsl_sf_result * result)
{
  if (n < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (n == 0)
    {
      result->val = 1.;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (n == 1)
    {
      result->val = x;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (x == 0.0)
    {
      if (GSL_IS_ODD(n))
        {
          result->val = 0.0;
          result->err = 0.0;
          return GSL_SUCCESS;
        }
      else
        {
          /* for n even, He_n(0) = (-1)^{n/2} (n - 1)!! */

          int status = GSL_SUCCESS;

          if (n - 1 > GSL_SF_DOUBLEFACT_NMAX)  /* test if (n - 1)!! will overflow */
            {
              status = GSL_EOVRFLW;
              result->val = GSL_IS_ODD(n / 2) ? GSL_NEGINF : GSL_POSINF;
              result->err = GSL_POSINF;
            }
          else
            {
              gsl_sf_doublefact_e(n - 1, result);
              if (GSL_IS_ODD(n / 2))
                result->val = -result->val;
            }

          return status;
        }
    }
  else
    {
      /* upward recurrence: He_{n+1} = x He_n - n He_{n-1} */

      int status = GSL_SUCCESS;
      const double abs_x = fabs(x);
      const double thresh1 = abs_x > 1.0 ? 0.9 * GSL_DBL_MAX / abs_x : GSL_DBL_MAX;
      const double thresh2 = 0.9 * GSL_DBL_MAX;

      double p_n0 = 1.0;    /* He_0(x) */
      double p_n1 = x;      /* He_1(x) */
      double p_n = p_n1;

      double e_n0 = GSL_DBL_EPSILON;
      double e_n1 = fabs(x)*GSL_DBL_EPSILON;
      double e_n = e_n1;

      int j;

      for (j = 1; j < n; j++)
        {
          if (fabs(p_n1) > thresh1 ||    /* test if x*p_n1 will overflow */
              fabs(p_n0) > thresh2 / j)  /* test if j*p_n0 will overflow */
            {
              status = GSL_EOVRFLW;
              break;
            }

          p_n  = x*p_n1 - j*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;

          e_n  = fabs(x)*e_n1+j*e_n0;
          e_n0 = e_n1;
          e_n1 = e_n;
        }

      result->val = p_n;
      result->err = e_n + fabs(result->val)*GSL_DBL_EPSILON;

      return status;
    }
}

double gsl_sf_hermite_prob(const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_prob_e(n, x, &result));
}

/* Evaluates the m-th derivative of the probabilists' Hermite polynomial of order n at position x.
 * The direct formula He^{(m)}_n = n!/(n-m)!*He_{n-m}(x) (where He_j(x) is the j-th probabilists' Hermite polynomial and He^{(m)}_j(x) its m-th derivative) is employed. */
int
gsl_sf_hermite_prob_deriv_e(const int m, const int n, const double x, gsl_sf_result * result)
{
  if (n < 0 || m < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (n < m)
    {
      result->val = 0.;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else
    {
      int status;
      double f = gsl_sf_choose(n,m)*gsl_sf_fact(m);
      gsl_sf_result He;

      status = gsl_sf_hermite_prob_e(n - m, x, &He);
      if (status == GSL_SUCCESS)
        {
          result->val = He.val*f;
          result->err = He.err*f + GSL_DBL_EPSILON*fabs(result->val);
        }
      else
        {
          result->val = He.val;
          result->err = GSL_POSINF;
        }

      return status;
    }
}

double
gsl_sf_hermite_prob_deriv(const int m, const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_prob_deriv_e(m, n, x, &result));
}

/* evaluates the physicists' Hermite polynomial of order n at position x */
int
gsl_sf_hermite_e(const int n, const double x, gsl_sf_result * result)
{
  if (n < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (n == 0)
    {
      result->val = 1.;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (n == 1)
    {
      result->val = 2.0*x;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (x == 0.0)
    {
      if (GSL_IS_ODD(n))
        {
          result->val = 0.0;
          result->err = 0.0;
          return GSL_SUCCESS;
        }
      else
        {
          /* for n even, H_n(0) = (-2)^{n/2} (n - 1)!! */

          int status = GSL_SUCCESS;
          int m = n >> 1;

          if (n - 1 > GSL_SF_DOUBLEFACT_NMAX)  /* test if (n - 1)!! will overflow */
            {
              status = GSL_EOVRFLW;
              result->val = GSL_IS_ODD(m) ? GSL_NEGINF : GSL_POSINF;
              result->err = GSL_POSINF;
            }
          else
            {
              double f = gsl_pow_int(2.0, m);

              gsl_sf_doublefact_e(n - 1, result);

              if (result->val > 0.9 * GSL_DBL_MAX / f) /* test if 2^{n/2} * (n-1)!! will overflow */
                {
                  status = GSL_EOVRFLW;
                  result->val = GSL_IS_ODD(m) ? GSL_NEGINF : GSL_POSINF;
                  result->err = GSL_POSINF;
                }
              else
                {
                  result->val *= f;
                  result->err *= f;
                  if (GSL_IS_ODD(m))
                    result->val = -result->val;
                }
            }

          return status;
        }
    }
  else
    {
      /* upward recurrence: H_{n+1} = 2x H_n - 2n H_{n-1} */

      int status = GSL_SUCCESS;
      const double two_x = 2.0 * x;
      const double abs_two_x = fabs(two_x);
      const double thresh1 = abs_two_x > 1.0 ? 0.9 * GSL_DBL_MAX / abs_two_x : GSL_DBL_MAX;
      const double thresh2 = 0.9 * GSL_DBL_MAX / 2.0;

      double p_n0 = 1.0;    /* H_0(x) */
      double p_n1 = two_x;  /* H_1(x) */
      double p_n = p_n1;

      double e_n0 = GSL_DBL_EPSILON;
      double e_n1 = 2.*fabs(x)*GSL_DBL_EPSILON;
      double e_n = e_n1;

      int j;

      for (j = 1; j <= n - 1; j++)
        {
          if (fabs(p_n1) > thresh1 ||    /* test if 2*x*p_n1 will overflow */
              fabs(p_n0) > thresh2 / j)  /* test if 2*j*p_n0 will overflow */
            {
              status = GSL_EOVRFLW;
              break;
            }

          p_n  = two_x*p_n1 - 2.0*j*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;

          e_n  = 2.*(fabs(x)*e_n1+j*e_n0);
          e_n0 = e_n1;
          e_n1 = e_n;
        }

      result->val = p_n;
      result->err = e_n + fabs(result->val)*GSL_DBL_EPSILON;

      return status;
    }
}

double
gsl_sf_hermite(const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_e(n, x, &result));
}

/* Evaluates the m-th derivative of the physicists' Hermite polynomial of order n at position x.
 * The direct formula H^{(m)}_n = 2**m*n!/(n-m)!*H_{n-m}(x) (where H_j(x) is the j-th physicists' Hermite polynomial and H^{(m)}_j(x) its m-th derivative) is employed. */
int
gsl_sf_hermite_deriv_e(const int m, const int n, const double x, gsl_sf_result * result)
{
  if (n < 0 || m < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (n < m)
    {
      result->val = 0.;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else
    {
      int status;
      double f = gsl_sf_choose(n,m)*gsl_sf_fact(m)*pow2(m);
      gsl_sf_result H;

      status = gsl_sf_hermite_e(n - m, x, &H);
      if (status == GSL_SUCCESS)
        {
          result->val = H.val*f;
          result->err = H.err*f + GSL_DBL_EPSILON*fabs(result->val);
        }
      else
        {
          result->val = H.val;
          result->err = GSL_POSINF;
        }

      return status;
    }
}

double
gsl_sf_hermite_deriv(const int m, const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_deriv_e(m, n, x, &result));
}

/* evaluates the Hermite function of order n at position x */
int
gsl_sf_hermite_func_e(const int n, const double x, gsl_sf_result * result)
{
  if (n < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (x == 0.0)
    {
      if (GSL_IS_ODD(n))
        {
          result->val = 0.;
          result->err = 0.;
          return GSL_SUCCESS;
        }
      else
        {
          double f = (GSL_IS_ODD(n / 2) ? -1.0 : 1.0);
          int j;

          for(j = 1; j < n; j += 2)
            f *= sqrt(j / (j + 1.0));

          result->val = f / sqrt(M_SQRTPI);
          result->err = GSL_DBL_EPSILON * fabs(result->val);
          return GSL_SUCCESS;
        }
    }
  else if (n == 0)
    {
      result->val = exp(-0.5 * x * x) / sqrt(M_SQRTPI);
      result->err = GSL_DBL_EPSILON * fabs(result->val);
      return GSL_SUCCESS;
    }
  else if (n == 1)
    {
      result->val = M_SQRT2 * x * exp(-0.5 * x * x) / sqrt(M_SQRTPI);
      result->err = GSL_DBL_EPSILON * fabs(result->val);
      return GSL_SUCCESS;
    }
  else
    {
      /*
       * This algorithm is based on the modified recurrence algorithm
       * found in the appendix of:
       *
       * B. Bunck, BIT Numerical Mathematics, 49, 281 (2009)
       *
       * Numerical tests showed that this algorithm is more stable for
       * large x (x > 40) than the standard recurrence relation.
       * Accuracy is comparable to the recurrence relation method
       * for small x and all n.
       *
       * See:
       *
       * https://scicomp.stackexchange.com/questions/30896/generate-high-n-quantum-harmonic-oscillator-states-numerically
       *
       * for further discussion.
       */

      double hi2 = 1.0 / sqrt(M_SQRTPI); /* \hat{h}_0 */
      double hi1 = M_SQRT2 * x * hi2;    /* \hat{h}_1 */
      double hi = 0.0;
      double sum_log_scale = 0.0;
      double abshi;
      int i;

      for (i = 2; i <= n; ++i)
        {
          hi = sqrt(2.0 / i) * x * hi1 - sqrt((i - 1.0) / i) * hi2;
          hi2 = hi1;
          hi1 = hi;

          abshi = fabs(hi);
          if (abshi > 1.0)
            {
              double log_scale = RND(log(abshi));
              double scale = exp(-log_scale);

              hi *= scale;
              hi1 *= scale;
              hi2 *= scale;
              sum_log_scale += log_scale;
            }
        }

      result->val = hi * exp(-0.5 * x * x + sum_log_scale);
      result->err = n * GSL_DBL_EPSILON * fabs(result->val);

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_func(const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_func_e(n, x, &result));
}

/*
 * This algorithm is based on the contour integral algorithm of:
 *
 * B. Bunck, BIT Numerical Mathematics, 49, 281 (2009)
 *
 * It has O(sqrt(n)) complexity
 */

int
gsl_sf_hermite_func_fast_e(const int n, const double x, gsl_sf_result * result)
{
  if (n < 1000 || x == 0.0)
    {
      /* for small n, the recurrence method is faster and more accurate */
      return gsl_sf_hermite_func_e(n, x, result);
    }
  else
    {
      size_t j;
      const double k = sqrt(0.5*n);
      const size_t steps = (size_t) ceil(6.211 * sqrt(n));
      const double dt = M_PI/steps;
      const double invn2 = 1.0/(n*n);
      double ex, ex_e, cs, cs_e, sn, sn2, t;
      gsl_sf_result lngamma;

      if (n < 36)
        {
          gsl_sf_lnfact_e(n, &lngamma);
          lngamma.val *= 0.5;
          lngamma.err *= 0.5;
          t = 0.5*n*log(n) + 0.25*M_LNPI;
          cs = 0.5*n;
          lngamma.val += cs - t;
          lngamma.err += (cs + t)*GSL_DBL_EPSILON;
        }
      else
        {
          /* approximate ln(gamma_{n,k}) using Stirling's formula */
          lngamma.val = 0.25*log(2*n);
          lngamma.err = (lngamma.val  + ((((invn2/3360 + 1.0/2520)*invn2 + 1.0/720)*invn2) + 1.0/24)/n)*GSL_DBL_EPSILON;
          lngamma.val -= ((((invn2/3360 - 1.0/2520)*invn2 + 1.0/720)*invn2) - 1.0/24)/n;
        }

      ex = exp(lngamma.val - n - 0.5*x*x - 2*x*k);
      cs = (GSL_IS_ODD(n) ? -1 : 1);
      result->val = 0.5*ex*cs;
      result->err = 0.5*ex*(lngamma.err + (n + 0.5*x*x + fabs(2*x*k) + 1)*GSL_DBL_EPSILON);
      ex = exp(lngamma.val - n - 0.5*x*x + 2*x*k);
      result->val += 0.5*ex;
      result->err += 0.5*ex*(lngamma.err + (n + 0.5*x*x + fabs(2*x*k) + 1)*GSL_DBL_EPSILON);

      for (j = 1; j < steps; j++)
        {
          t = j*dt;
          cs = cos(t);
          ex = exp(lngamma.val - 0.5*x*x + (2*x*k - n*cs)*cs);
          ex_e = ex*(lngamma.err + GSL_DBL_EPSILON*(1 + 0.5*x*x + (fabs(2*x*k) + fabs(n*cs))*fabs(cs)));
          sn = sin(t);
          sn2 = sin(2*t);
          cs = cos(2*x*k*sn - 0.5*n*sn2 - n*t);
          cs_e = GSL_MIN(1.0+fabs(cs), GSL_DBL_EPSILON*(fabs(cs) + (fabs(2*x*k*sn) + fabs(0.5*n*sn2) + n*t)*fabs(sin(2*x*k*sn - 0.5*n*sn2 - n*t))));
          result->val += ex*cs;
          result->err += ex*cs_e + ex_e*fabs(cs) + GSL_DBL_EPSILON*fabs(ex*cs);
        }

      result->val *= M_1_PI*dt;
      result->err = M_1_PI*dt*result->err + GSL_DBL_EPSILON*fabs(result->val);

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_func_fast(const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_func_fast_e(n, x, &result));
}

/* Evaluates all probabilists' Hermite polynomials up to order nmax at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */
int
gsl_sf_hermite_prob_array(const int nmax, const double x, double * result_array)
{
  if (nmax < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (nmax == 0)
    {
      result_array[0] = 1.0;
      return GSL_SUCCESS;
    }
  else if (nmax == 1)
    {
      result_array[0] = 1.0;
      result_array[1] = x;
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence: He_{n+1} = x He_n - n He_{n-1} */

      int status = GSL_SUCCESS;
      const double abs_x = fabs(x);
      const double thresh1 = abs_x > 1.0 ? 0.9 * GSL_DBL_MAX / abs_x : GSL_DBL_MAX;
      const double thresh2 = 0.9 * GSL_DBL_MAX;

      double p_n0 = 1.0;    /* He_0(x) */
      double p_n1 = x;      /* He_1(x) */
      double p_n = p_n1;

      int j;

      result_array[0] = 1.0;
      result_array[1] = x;

      for (j = 1; j < nmax; j++)
        {
          if (fabs(p_n1) > thresh1 ||    /* test if x*p_n1 will overflow */
              fabs(p_n0) > thresh2 / j)  /* test if j*p_n0 will overflow */
            {
              status = GSL_EOVRFLW;
              break;
            }

          p_n  = x*p_n1 - j*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;

          result_array[j + 1] = p_n;
        }

      return status;
   }
}

/* Evaluates the m-th derivative of all probabilists' Hermite polynomials up to order nmax at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */

int
gsl_sf_hermite_prob_array_deriv(const int m, const int nmax, const double x, double * result_array)
{
  if (nmax < 0 || m < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (m == 0)
    {
      gsl_sf_hermite_prob_array(nmax, x, result_array);
      return GSL_SUCCESS;
    }
  else if (nmax < m)
    {
      int j;

      for (j = 0; j <= nmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (nmax == m)
    {
      int j;

      for (j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[nmax] = gsl_sf_fact(m);
      return GSL_SUCCESS;
    }
  else if (nmax == m + 1)
    {
      int j;

      for (j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[nmax-1] = gsl_sf_fact(m);
      result_array[nmax] = result_array[nmax-1]*(m+1)*x;
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence: He^{(m)}_{n+1} = (n+1)/(n-m+1)*(x He^{(m)}_n - n He^{(m)}_{n-1}) */

      double p_n0 = gsl_sf_fact(m);    /* He^{(m)}_{m}(x) */
      double p_n1 = p_n0*(m+1)*x;      /* He^{(m)}_{m+1}(x) */
      double p_n = p_n1;
      int j;

      for (j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[m] = p_n0;
      result_array[m + 1] = p_n1;

      for (j = m + 1; j <= nmax - 1; j++)
        {
          p_n  = (x*p_n1 - j*p_n0) * (j + 1.0) / (j - m + 1.0);
          p_n0 = p_n1;
          p_n1 = p_n;
          result_array[j + 1] = p_n;
        }

      return GSL_SUCCESS;
    }
}

/* Evaluates all derivatives (starting from 0) up to the mmax-th derivative of the probabilists' Hermite polynomial of order n at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */

int
gsl_sf_hermite_prob_deriv_array(const int mmax, const int n, const double x, double * result_array)
{
  if (n < 0 || mmax < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (n == 0)
    {
      int j;

      result_array[0] = 1.0;
      for (j = 1; j <= mmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (n == 1 && mmax > 0)
    {
      int j;

      result_array[0] = x;
      result_array[1] = 1.0;

      for(j=2; j <= mmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (mmax == 0)
    {
      result_array[0] = gsl_sf_hermite_prob(n,x);
      return GSL_SUCCESS;
    }
  else if (mmax == 1)
    {
      result_array[0] = gsl_sf_hermite_prob(n,x);
      result_array[1] = n*gsl_sf_hermite_prob(n-1,x);
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence */

      int k = GSL_MAX_INT(0, n - mmax);
      double p_n0 = gsl_sf_hermite_prob(k,x);        /* He_k(x) */
      double p_n1 = gsl_sf_hermite_prob(k+1,x);      /* He_{k+1}(x) */
      double p_n  = p_n1;
      int j;

      for(j=n+1; j <= mmax; j++)
        result_array[j] = 0.0;

      result_array[GSL_MIN_INT(n,mmax)] = p_n0;
      result_array[GSL_MIN_INT(n,mmax)-1] = p_n1;

      for (j = GSL_MIN_INT(mmax,n)-1; j > 0; j--)
        {
          k++;
          p_n  = x*p_n1-k*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;
          result_array[j - 1] = p_n;
        }

      p_n = 1.0;
      for (j = 1; j <= GSL_MIN_INT(n, mmax); j++)
        {
          p_n  = p_n*(n-j+1);
          result_array[j] = p_n*result_array[j];
        }

      return GSL_SUCCESS;
    }
}

/* Evaluates the series sum_{j=0}^n a_j*He_j(x) with He_j being the j-th probabilists' Hermite polynomial.
 * For improved numerical stability the Clenshaw algorithm (Clenshaw, C. W. (July 1955). "A note on the summation of Chebyshev series". Mathematical Tables and other Aids to Computation 9 (51): 118–110.) adapted to probabilists' Hermite polynomials is used. */

int
gsl_sf_hermite_prob_series_e(const int n, const double x, const double * a, gsl_sf_result * result)
{
  if(n < 0) {
    DOMAIN_ERROR(result);
  }
  else if(n == 0) {
    result->val = a[0];
    result->err = 0.;
    return GSL_SUCCESS;
  }
  else if(n == 1) {
    result->val = a[0]+a[1]*x;
    result->err = 2.*GSL_DBL_EPSILON * (fabs(a[0]) + fabs(a[1]*x)) ;
    return GSL_SUCCESS;
  }
  else {
    /* downward recurrence: b_n = a_n + x b_{n+1} - (n+1) b_{n+2} */

    double b0 = 0.;
    double b1 = 0.;
    double btmp = 0.;

    double e0 = 0.;
    double e1 = 0.;
    double etmp = e1;

    int j;

    for(j=n; j >= 0; j--){
      btmp = b0;
      b0  = a[j]+x*b0-(j+1)*b1;
      b1 = btmp;

      etmp = e0;
      e0  = (GSL_DBL_EPSILON*fabs(a[j])+fabs(x)*e0+(j+1)*e1);
      e1 = etmp;
    }

    result->val = b0;
    result->err = e0 + fabs(b0)*GSL_DBL_EPSILON;
    return GSL_SUCCESS;
  }
}

double
gsl_sf_hermite_prob_series(const int n, const double x, const double * a)
{
  EVAL_RESULT(gsl_sf_hermite_prob_series_e(n, x, a, &result));
}

/* Evaluates all physicists' Hermite polynomials up to order nmax at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */
int
gsl_sf_hermite_array(const int nmax, const double x, double * result_array)
{
  if(nmax < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (nmax == 0)
    {
      result_array[0] = 1.0;
      return GSL_SUCCESS;
    }
  else if (nmax == 1)
    {
      result_array[0] = 1.0;
      result_array[1] = 2.0*x;
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence: H_{n+1} = 2x H_n - 2n H_{n-1} */

      int status = GSL_SUCCESS;
      const double two_x = 2.0 * x;
      const double abs_two_x = fabs(two_x);
      const double thresh1 = abs_two_x > 1.0 ? 0.9 * GSL_DBL_MAX / abs_two_x : GSL_DBL_MAX;
      const double thresh2 = 0.9 * GSL_DBL_MAX / 2.0;

      double p_n0 = 1.0;    /* H_0(x) */
      double p_n1 = two_x;  /* H_1(x) */
      double p_n = p_n1;

      int j;

      result_array[0] = 1.0;
      result_array[1] = 2.0*x;

      for (j = 1; j < nmax; j++)
        {
          if (fabs(p_n1) > thresh1 ||    /* test if 2*x*p_n1 will overflow */
              fabs(p_n0) > thresh2 / j)  /* test if 2*j*p_n0 will overflow */
            {
              status = GSL_EOVRFLW;
            }

          p_n  = two_x*p_n1 - 2.0*j*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;

          result_array[j + 1] = p_n;
        }

      return status;
    }
}

/* Evaluates the m-th derivative of all physicists' Hermite polynomials up to order nmax at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */
int
gsl_sf_hermite_array_deriv(const int m, const int nmax, const double x, double * result_array)
{
  if (nmax < 0 || m < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (m == 0)
    {
      gsl_sf_hermite_array(nmax, x, result_array);
      return GSL_SUCCESS;
    }
  else if (nmax < m)
    {
      int j;

      for(j = 0; j <= nmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (nmax == m)
    {
      int j;

      for(j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[nmax] = pow2(m)*gsl_sf_fact(m);

      return GSL_SUCCESS;
    }
  else if (nmax == m + 1)
    {
      int j;

      for(j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[nmax-1] = pow2(m)*gsl_sf_fact(m);
      result_array[nmax] = result_array[nmax-1]*2*(m+1)*x;
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence: H^{(m)}_{n+1} = 2(n+1)/(n-m+1)*(x H^{(m)}_n - n H^{(m)}_{n-1}) */

      double p_n0 = pow2(m)*gsl_sf_fact(m);  /* H^{(m)}_{m}(x) */
      double p_n1 = p_n0*2*(m+1)*x;          /* H^{(m)}_{m+1}(x) */
      double p_n;
      int j;

      for(j = 0; j < m; j++)
        result_array[j] = 0.0;

      result_array[m] = p_n0;
      result_array[m+1] = p_n1;

      for (j = m + 1; j < nmax; ++j)
        {
          p_n  = (x*p_n1 - j*p_n0) * 2 * (j + 1.0) / (j - m + 1.0);
          p_n0 = p_n1;
          p_n1 = p_n;
          result_array[j + 1] = p_n;
        }

      return GSL_SUCCESS;
    }
}

/* Evaluates all derivatives (starting from 0) up to the mmax-th derivative of the physicists' Hermite polynomial of order n at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */
int
gsl_sf_hermite_deriv_array(const int mmax, const int n, const double x, double * result_array)
{
  if (n < 0 || mmax < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (n == 0)
    {
      int j;

      result_array[0] = 1.0;
      for(j = 1; j <= mmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (n == 1 && mmax > 0)
    {
      int j;

      result_array[0] = 2*x;
      result_array[1] = 1.0;
      for (j = 2; j <= mmax; j++)
        result_array[j] = 0.0;

      return GSL_SUCCESS;
    }
  else if (mmax == 0)
    {
      result_array[0] = gsl_sf_hermite(n,x);
      return GSL_SUCCESS;
    }
  else if (mmax == 1)
    {
      result_array[0] = gsl_sf_hermite(n,x);
      result_array[1] = 2*n*gsl_sf_hermite(n - 1, x);
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence */

      int k = GSL_MAX_INT(0, n - mmax);
      double p_n0 = gsl_sf_hermite(k, x);        /* H_k(x) */
      double p_n1 = gsl_sf_hermite(k + 1, x);    /* H_{k+1}(x) */
      double p_n  = p_n1;
      int j;

      for (j = n + 1; j <= mmax; j++)
        result_array[j] = 0.0;

      result_array[GSL_MIN_INT(n,mmax)] = p_n0;
      result_array[GSL_MIN_INT(n,mmax)-1] = p_n1;

      for (j = GSL_MIN_INT(mmax, n) - 1; j > 0; j--)
        {
          k++;
          p_n  = 2*x*p_n1 - 2*k*p_n0;
          p_n0 = p_n1;
          p_n1 = p_n;
          result_array[j - 1] = p_n;
        }

      p_n = 1.0;
      for (j = 1; j <= GSL_MIN_INT(n,mmax); j++)
        {
          p_n *= 2.0 * (n - j + 1.0);
          result_array[j] *= p_n;
        }

      return GSL_SUCCESS;
    }
}

/* Evaluates the series sum_{j=0}^n a_j*H_j(x) with H_j being the j-th physicists' Hermite polynomial.
 * For improved numerical stability the Clenshaw algorithm (Clenshaw, C. W. (July 1955). "A note on the summation of Chebyshev series". Mathematical Tables and other Aids to Computation 9 (51): 118–110.) adapted to physicists' Hermite polynomials is used. */
int
gsl_sf_hermite_series_e(const int n, const double x, const double * a, gsl_sf_result * result)
{
  if(n < 0) {
    DOMAIN_ERROR(result);
  }
  else if(n == 0) {
    result->val = a[0];
    result->err = 0.;
    return GSL_SUCCESS;
  }
  else if(n == 1) {
    result->val = a[0]+a[1]*2.*x;
    result->err = 2.*GSL_DBL_EPSILON * (fabs(a[0]) + fabs(a[1]*2.*x)) ;
    return GSL_SUCCESS;
  }
  else {
    /* downward recurrence: b_n = a_n + 2x b_{n+1} - 2(n+1) b_{n+2} */

    double b0 = 0.;
    double b1 = 0.;
    double btmp = 0.;

    double e0 = 0.;
    double e1 = 0.;
    double etmp = e1;

    int j;

    for(j=n; j >= 0; j--){
      btmp = b0;
      b0  = a[j]+2.*x*b0-2.*(j+1)*b1;
      b1 = btmp;

      etmp = e0;
      e0  = (GSL_DBL_EPSILON*fabs(a[j])+fabs(2.*x)*e0+2.*(j+1)*e1);
      e1 = etmp;
    }

    result->val = b0;
    result->err = e0 + fabs(b0)*GSL_DBL_EPSILON;
    return GSL_SUCCESS;
  }
}

double
gsl_sf_hermite_series(const int n, const double x, const double * a)
{
  EVAL_RESULT(gsl_sf_hermite_series_e(n, x, a, &result));
}

/* Evaluates all Hermite functions up to order nmax at position x. The results are stored in result_array.
 * Since all polynomial orders are needed, upward recurrence is employed. */
int
gsl_sf_hermite_func_array(const int nmax, const double x, double * result_array)
{
  if (nmax < 0)
    {
      GSL_ERROR ("domain error", GSL_EDOM);
    }
  else if (nmax == 0)
    {
      result_array[0] = exp(-0.5*x*x)/sqrt(M_SQRTPI);
      return GSL_SUCCESS;
    }
  else if (nmax == 1)
    {
      result_array[0] = exp(-0.5*x*x)/sqrt(M_SQRTPI);
      result_array[1] = result_array[0]*M_SQRT2*x;
      return GSL_SUCCESS;
    }
  else
    {
      /* upward recurrence: Psi_{n+1} = sqrt(2/(n+1))*x Psi_n - sqrt(n/(n+1)) Psi_{n-1} */

      const double arg = -0.5 * x * x;
      double hi2 = 1.0 / sqrt(M_SQRTPI);
      double hi1 = M_SQRT2 * x * hi2;
      double hi = 0.0;
      double sum_log_scale = 0.0;
      double abshi;
      int i;

      result_array[0] = exp(arg) * hi2;
      result_array[1] = result_array[0] * M_SQRT2 * x;

      for (i = 2; i <= nmax; ++i)
        {
          hi = sqrt(2.0 / i) * x * hi1 - sqrt((i - 1.0) / i) * hi2;
          hi2 = hi1;
          hi1 = hi;

          abshi = fabs(hi);
          if (abshi > 1.0)
            {
              double log_scale = RND(log(abshi));
              double scale = exp(-log_scale);

              hi *= scale;
              hi1 *= scale;
              hi2 *= scale;
              sum_log_scale += log_scale;
            }

          result_array[i] = hi * exp(arg + sum_log_scale);
        }

      return GSL_SUCCESS;
    }
}

/* Evaluates the series sum_{j=0}^n a_j*Psi_j(x) with Psi_j being the j-th Hermite function.
 * For improved numerical stability the Clenshaw algorithm (Clenshaw, C. W. (July 1955). "A note on the summation of Chebyshev series". Mathematical Tables and other Aids to Computation 9 (51): 118–110.) adapted to Hermite functions is used. */

int
gsl_sf_hermite_func_series_e(const int n, const double x, const double * a, gsl_sf_result * result)
{
  if (n < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (n == 0)
    {
      result->val = a[0]*exp(-0.5*x*x)/sqrt(M_SQRTPI);
      result->err = GSL_DBL_EPSILON*fabs(result->val);
      return GSL_SUCCESS;
    }
  else if (n == 1)
    {
      result->val = (a[0]+a[1]*M_SQRT2*x)*exp(-0.5*x*x)/sqrt(M_SQRTPI);
      result->err = 2.*GSL_DBL_EPSILON*(fabs(a[0])+fabs(a[1]*M_SQRT2*x))*exp(-0.5*x*x)/sqrt(M_SQRTPI);
      return GSL_SUCCESS;
    }
  else
    {
      /* downward recurrence: b_n = a_n + sqrt(2/(n+1))*x b_{n+1} - sqrt((n+1)/(n+2)) b_{n+2} */

      double b0 = 0.;
      double b1 = 0.;
      double btmp = 0.;

      double e0 = 0.;
      double e1 = 0.;
      double etmp = e1;

      int j;

      for (j = n; j >= 0; j--)
        {
          btmp = b0;
          b0  = a[j]+sqrt(2./(j+1))*x*b0-sqrt((j+1.)/(j+2.))*b1;
          b1 = btmp;

          etmp = e0;
          e0  = (GSL_DBL_EPSILON*fabs(a[j])+sqrt(2./(j+1))*fabs(x)*e0+sqrt((j+1.)/(j+2.))*e1);
          e1 = etmp;
        }

      result->val = b0*exp(-0.5*x*x)/sqrt(M_SQRTPI);
      result->err = e0 + fabs(result->val)*GSL_DBL_EPSILON;

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_func_series(const int n, const double x, const double * a)
{
  EVAL_RESULT(gsl_sf_hermite_func_series_e(n, x, a, &result));
}

/* Evaluates the m-th derivative of the Hermite function of order n at position x.
 * A summation including upward recurrences is used. */

int
gsl_sf_hermite_func_der_e(const int m, const int n, const double x, gsl_sf_result * result)
{
  if(m < 0 || n < 0)
    {
      DOMAIN_ERROR(result);
    }
  else if (m == 0)
    {
      return gsl_sf_hermite_func_e(n, x, result);
    }
  else if (m == 1)
    {
      double hi2 = 1.0 / sqrt(M_SQRTPI);
      double hi1 = M_SQRT2 * x * hi2;
      double hi = 0.0;
      double sum_log_scale = 0.0;
      double abshi;
      int i;

      for (i = 2; i <= n; ++i)
        {
          hi = sqrt(2.0 / i) * x * hi1 - sqrt((i - 1.0) / i) * hi2;
          hi2 = hi1;
          hi1 = hi;

          abshi = fabs(hi);
          if (abshi > 1.0)
            {
              double log_scale = RND(log(abshi));
              double scale = exp(-log_scale);

              hi *= scale;
              hi1 *= scale;
              hi2 *= scale;
              sum_log_scale += log_scale;
            }
        }

      /* psi'_n(x) = sqrt(2 n) psi_{n-1} - x psi_n */
      result->val = (sqrt(2.0*n) * hi2 - x * hi) * exp(-0.5 * x * x + sum_log_scale);
      result->err = n * GSL_DBL_EPSILON * fabs(result->val);

      return GSL_SUCCESS;
    }
  else
    {
      int j;
      double r,er,b;
      double h0 = 1.;
      double h1 = x;
      double eh0 = GSL_DBL_EPSILON;
      double eh1 = GSL_DBL_EPSILON;
      double p0 = 1.;
      double p1 = M_SQRT2*x;
      double ep0 = GSL_DBL_EPSILON;
      double ep1 = M_SQRT2*GSL_DBL_EPSILON;
      double f = 1.;

      for (j=GSL_MAX_INT(1,n-m+1);j<=n;j++)
        f *= sqrt(2.*j);

      if (m > n)
        {
          f = (GSL_IS_ODD(m-n)?-f:f);

          for (j=0;j<GSL_MIN_INT(n,m-n);j++)
            f *= (m-j)/(j+1.);
        }

      for (j=1;j<=m-n;j++)
        {
          b = x*h1-j*h0;
          h0 = h1;
          h1 = b;

          b  = (fabs(x)*eh1+j*eh0);
          eh0 = eh1;
          eh1 = b;
        }

      b = 0.;
      for (j=1;j<=n-m;j++)
        {
          b = (M_SQRT2*x*p1-sqrt(j)*p0)/sqrt(j+1.);
          p0 = p1;
          p1 = b;

          b  = (M_SQRT2*fabs(x)*ep1+sqrt(j)*ep0)/sqrt(j+1.);
          ep0 = ep1;
          ep1 = b;
        }

      b = 0.;
      r = 0.;
      er = 0.;
      for (j=GSL_MAX_INT(0,m-n);j<=m;j++)
        {
          r += f*h0*p0;
          er += eh0*fabs(f*p0)+ep0*fabs(f*h0)+GSL_DBL_EPSILON*fabs(f*h0*p0);

          b = x*h1-(j+1.)*h0;
          h0 = h1;
          h1 = b;

          b  = 0.5*(fabs(x)*eh1+(j+1.)*eh0);
          eh0 = eh1;
          eh1 = b;

          b = (M_SQRT2*x*p1-sqrt(n-m+j+1.)*p0)/sqrt(n-m+j+2.);
          p0 = p1;
          p1 = b;

          b  = 0.5*(M_SQRT2*fabs(x)*ep1+sqrt(n-m+j+1.)*ep0)/sqrt(n-m+j+2.);
          ep0 = ep1;
          ep1 = b;

          f *= -(m-j)/(j+1.)/sqrt(n-m+j+1.)*M_SQRT1_2;
        }

      result->val = r*exp(-0.5*x*x)/sqrt(M_SQRTPI);
      result->err = er*fabs(exp(-0.5*x*x)/sqrt(M_SQRTPI)) + GSL_DBL_EPSILON*fabs(result->val);

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_func_der(const int m, const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_func_der_e(m, n, x, &result));
}

static double
H_zero_init(const int n, const int k)
{
  double p = 1., x = 1., y = 1.;
  if (k == 1 && n > 50) {
    x = (GSL_IS_ODD(n)?1./sqrt((n-1)/6.):1./sqrt(0.5*n));
  }
  else {
    p = -0.7937005259840997373758528196*gsl_sf_airy_zero_Ai(n/2-k+1);
    x = sqrt(2*n+1.);
    y = pow(2*n+1.,1/6.);
    x = x - p/y - 0.1*p*p/(x*y*y) + (9/280. - p*p*p*11/350.)/(x*x*x) + (p*277/12600. - gsl_sf_pow_int(p,4)*823/63000.)/gsl_sf_pow_int(x,4)/y;
  }
  p = acos(x/sqrt(2*n+1.));
  y = M_PI*(-2*(n/2-k)-1.5)/(n+0.5);
  if(gsl_fcmp(y,sin(2.*p)-2*p,GSL_SQRT_DBL_EPSILON)==0) return x; /* initial approx sufficiently accurate */
  if (y > -GSL_DBL_EPSILON) return sqrt(2*n+1.);
  if (p < GSL_DBL_EPSILON) p = GSL_DBL_EPSILON;
  if (p > M_PI_2) p = M_PI_2;
  if (sin(2.*p)-2*p > y){
    x = GSL_MAX((sin(2.*p)-2*p-y)/4.,GSL_SQRT_DBL_EPSILON);
    do{
      x *= 2.;
      p += x;
    } while (sin(2.*p)-2*p > y);
  }
  do {
    x = p;
    p -= (sin(2.*p)-2.*p-y)/(2.*cos(2.*p)-2.);
    if (p<0.||p>M_PI_2) p = M_PI_2;
  } while (gsl_fcmp(x,p,100*GSL_DBL_EPSILON)!=0);
  return sqrt(2*n+1.)*cos(p);
}

/* lookup table for the positive zeros of the probabilists' Hermite polynomials of order 3 through 20 */
static double He_zero_tab[99] = {
  1.73205080756887729352744634151,
  0.741963784302725857648513596726,
  2.33441421833897723931751226721,
  1.35562617997426586583052129087,
  2.85697001387280565416230426401,
  0.616706590192594152193686099399,
  1.88917587775371067550566789858,
  3.32425743355211895236183546247,
  1.154405394739968127239597758838,
  2.36675941073454128861885646856,
  3.75043971772574225630392202571,
  0.539079811351375108072461918694,
  1.63651904243510799922544657297,
  2.80248586128754169911301080618,
  4.14454718612589433206019783917,
  1.023255663789132524828148225810,
  2.07684797867783010652215614374,
  3.20542900285646994336567590292,
  4.51274586339978266756667884317,
  0.484935707515497653046233483105,
  1.46598909439115818325066466416,
  2.48432584163895458087625118368,
  3.58182348355192692277623675546,
  4.85946282833231215015516494660,
  0.928868997381063940144111999584,
  1.87603502015484584534137013967,
  2.86512316064364499771968407254,
  3.93616660712997692868589612142,
  5.18800122437487094818666404539,
  0.444403001944138945299732445510,
  1.34037519715161672153112945211,
  2.25946445100079912386492979448,
  3.22370982877009747166319001956,
  4.27182584793228172295999293076,
  5.50090170446774760081221630899,
  0.856679493519450033897376121795,
  1.72541837958823916151095838741,
  2.62068997343221478063807762201,
  3.56344438028163409162493844661,
  4.59139844893652062705231872720,
  5.80016725238650030586450565322,
  0.412590457954601838167454145167,
  1.24268895548546417895063983219,
  2.08834474570194417097139675101,
  2.96303657983866750254927123447,
  3.88692457505976938384755016476,
  4.89693639734556468372449782879,
  6.08740954690129132226890147034,
  0.799129068324547999424888414207,
  1.60671006902872973652322479373,
  2.43243682700975804116311571682,
  3.28908242439876638890856229770,
  4.19620771126901565957404160583,
  5.19009359130478119946445431715,
  6.36394788882983831771116094427,
  0.386760604500557347721047189801,
  1.16382910055496477419336819907,
  1.95198034571633346449212362880,
  2.76024504763070161684598142269,
  3.60087362417154828824902745506,
  4.49295530252001124266582263095,
  5.47222570594934308841242925805,
  6.63087819839312848022981922233,
  0.751842600703896170737870774614,
  1.50988330779674075905491513417,
  2.28101944025298889535537879396,
  3.07379717532819355851658337833,
  3.90006571719800990903311840097,
  4.77853158962998382710540812497,
  5.74446007865940618125547815768,
  6.88912243989533223256205432938,
  0.365245755507697595916901619097,
  1.09839551809150122773848360538,
  1.83977992150864548966395498992,
  2.59583368891124032910545091458,
  3.37473653577809099529779309480,
  4.18802023162940370448450911428,
  5.05407268544273984538327527397,
  6.00774591135959752029303858752,
  7.13946484914647887560975631213,
  0.712085044042379940413609979021,
  1.42887667607837287134157901452,
  2.15550276131693514033871248449,
  2.89805127651575312007902775275,
  3.66441654745063847665304033851,
  4.46587262683103133615452574019,
  5.32053637733603803162823765939,
  6.26289115651325170419416064557,
  7.38257902403043186766326977122,
  0.346964157081355927973322447164,
  1.04294534880275103146136681143,
  1.74524732081412671493067861704,
  2.45866361117236775131735057433,
  3.18901481655338941485371744116,
  3.94396735065731626033176813604,
  4.73458133404605534390170946748,
  5.57873880589320115268040332802,
  6.51059015701365448636289263918,
  7.61904854167975829138128156060
};

/*
 * Computes the s-th zero the probabilists' Hermite polynomial of order n.
 * A Newton iteration using a continued fraction representation adapted from:
 *
 * [E.T. Whittaker (1914), On the continued fractions which represent the
 * functions of Hermite and other functions defined by differential equations,
 * Proceedings of the Edinburgh Mathematical Society, 32, 65-74]
 *
 * is performed with the initial approximation from
 *
 * [Arpad Elbert and Martin E. Muldoon, Approximations for zeros of Hermite
 * functions, pp. 117-126 in D. Dominici and R. S. Maier, eds, "Special Functions
 * and Orthogonal Polynomials", Contemporary Mathematics, vol 471 (2008)]
 *
 * refined via the bisection method.
 */

int
gsl_sf_hermite_prob_zero_e(const int n, const int s, gsl_sf_result * result)
{
  if (n <= 0 || s < 0 || s > n/2)
    {
      DOMAIN_ERROR(result);
    }
  else if (s == 0)
    {
      if (GSL_IS_ODD(n) == 1)
        {
          result->val = 0.;
          result->err = 0.;
          return GSL_SUCCESS;
        }
      else
        {
          DOMAIN_ERROR(result);
        }
    }
  else if (n == 2)
    {
      result->val = 1.;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (n < 21)
    {
      result->val = He_zero_tab[(GSL_IS_ODD(n)?n/2:0)+((n/2)*(n/2-1))+s-2];
      result->err = GSL_DBL_EPSILON*(result->val);
      return GSL_SUCCESS;
    }
  else
    {
      double d = 1., x = 1., x0 = 1.;
      int j;
      x = H_zero_init(n,s) * M_SQRT2;
      do
        {
          x0 = x;
          d = 0.;
          for (j=1; j<n; j++)
            d = j/(x-d);

          x -= (x-d)/n;

          /* gsl_fcmp can be used since the smallest zero approaches 1/sqrt(n) or 1/sqrt((n-1)/3.)
           * for large n and thus all zeros are non-zero (except for the trivial case handled above) */
        }
      while (gsl_fcmp(x, x0, 10*GSL_DBL_EPSILON) != 0);

      result->val = x;
      result->err = 2*GSL_DBL_EPSILON*x + fabs(x-x0);

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_prob_zero(const int n, const int s)
{
  EVAL_RESULT(gsl_sf_hermite_prob_zero_e(n, s, &result));
}

/* lookup table for the positive zeros of the physicists' Hermite polynomials of order 3 through 20 */
static double H_zero_tab[99] = {
  1.22474487139158904909864203735,
  0.524647623275290317884060253835,
  1.65068012388578455588334111112,
  0.958572464613818507112770593893,
  2.02018287045608563292872408814,
  0.436077411927616508679215948251,
  1.335849074013696949714895282970,
  2.35060497367449222283392198706,
  0.816287882858964663038710959027,
  1.67355162876747144503180139830,
  2.65196135683523349244708200652,
  0.381186990207322116854718885584,
  1.157193712446780194720765779063,
  1.98165675669584292585463063977,
  2.93063742025724401922350270524,
  0.723551018752837573322639864579,
  1.46855328921666793166701573925,
  2.26658058453184311180209693284,
  3.19099320178152760723004779538,
  0.342901327223704608789165025557,
  1.03661082978951365417749191676,
  1.75668364929988177345140122011,
  2.53273167423278979640896079775,
  3.43615911883773760332672549432,
  0.656809566882099765024611575383,
  1.32655708449493285594973473558,
  2.02594801582575533516591283121,
  2.78329009978165177083671870152,
  3.66847084655958251845837146485,
  0.314240376254359111276611634095,
  0.947788391240163743704578131060,
  1.59768263515260479670966277090,
  2.27950708050105990018772856942,
  3.02063702512088977171067937518,
  3.88972489786978191927164274724,
  0.605763879171060113080537108602,
  1.22005503659074842622205526637,
  1.85310765160151214200350644316,
  2.51973568567823788343040913628,
  3.24660897837240998812205115236,
  4.10133759617863964117891508007,
  0.291745510672562078446113075799,
  0.878713787329399416114679311861,
  1.47668273114114087058350654421,
  2.09518325850771681573497272630,
  2.74847072498540256862499852415,
  3.46265693360227055020891736115,
  4.30444857047363181262129810037,
  0.565069583255575748526020337198,
  1.13611558521092066631913490556,
  1.71999257518648893241583152515,
  2.32573248617385774545404479449,
  2.96716692790560324848896036355,
  3.66995037340445253472922383312,
  4.49999070730939155366438053053,
  0.273481046138152452158280401965,
  0.822951449144655892582454496734,
  1.38025853919888079637208966969,
  1.95178799091625397743465541496,
  2.54620215784748136215932870545,
  3.17699916197995602681399455926,
  3.86944790486012269871942409801,
  4.68873893930581836468849864875,
  0.531633001342654731349086553718,
  1.06764872574345055363045773799,
  1.61292431422123133311288254454,
  2.17350282666662081927537907149,
  2.75776291570388873092640349574,
  3.37893209114149408338327069289,
  4.06194667587547430689245559698,
  4.87134519367440308834927655662,
  0.258267750519096759258116098711,
  0.776682919267411661316659462284,
  1.30092085838961736566626555439,
  1.83553160426162889225383944409,
  2.38629908916668600026459301424,
  2.96137750553160684477863254906,
  3.57376906848626607950067599377,
  4.24811787356812646302342016090,
  5.04836400887446676837203757885,
  0.503520163423888209373811765050,
  1.01036838713431135136859873726,
  1.52417061939353303183354859367,
  2.04923170985061937575050838669,
  2.59113378979454256492128084112,
  3.15784881834760228184318034120,
  3.76218735196402009751489394104,
  4.42853280660377943723498532226,
  5.22027169053748216460967142500,
  0.245340708300901249903836530634,
  0.737473728545394358705605144252,
  1.23407621539532300788581834696,
  1.73853771211658620678086566214,
  2.25497400208927552308233334473,
  2.78880605842813048052503375640,
  3.34785456738321632691492452300,
  3.94476404011562521037562880052,
  4.60368244955074427307767524898,
  5.38748089001123286201690041068
};

/*
 * Computes the s-th zero the physicists' Hermite polynomial of order n, thus also
 * the s-th zero of the Hermite function of order n. A Newton iteration using a continued
 * fraction representation adapted from:
 *
 * [E.T. Whittaker (1914), On the continued fractions which represent the functions of Hermite
 * and other functions defined by differential equations, Proceedings of the Edinburgh Mathematical
 * Society, 32, 65-74]
 *
 * An initial approximation is used from:
 *
 * [Arpad Elbert and Martin E. Muldoon, Approximations for zeros of Hermite functions,
 * pp. 117-126 in D. Dominici and R. S. Maier, eds, "Special Functions and Orthogonal Polynomials",
 * Contemporary Mathematics, vol 471 (2008)]
 *
 * which is refined via the bisection method.
 */

int
gsl_sf_hermite_zero_e(const int n, const int s, gsl_sf_result * result)
{
  if (n <= 0 || s < 0 || s > n/2)
    {
      DOMAIN_ERROR(result);
    }
  else if (s == 0)
    {
      if (GSL_IS_ODD(n) == 1)
        {
          result->val = 0.;
          result->err = 0.;
          return GSL_SUCCESS;
        }
      else
        {
          DOMAIN_ERROR(result);
        }
    }
  else if (n == 2)
    {
      result->val = M_SQRT1_2;
      result->err = 0.;
      return GSL_SUCCESS;
    }
  else if (n < 21)
    {
      result->val = H_zero_tab[(GSL_IS_ODD(n)?n/2:0)+((n/2)*(n/2-1))+s-2];
      result->err = GSL_DBL_EPSILON*(result->val);
      return GSL_SUCCESS;
    }
  else
    {
      double d = 1., x = 1., x0 = 1.;
      int j;

      x = H_zero_init(n,s);
      do
        {
          x0 = x;
          d = 0.;

          for (j=1; j<n; j++)
            d = 2*j/(2.*x-d);

          x -= (2*x-d)*0.5/n;

        /* gsl_fcmp can be used since the smallest zero approaches 1/sqrt(n) or 1/sqrt((n-1)/3.)
         * for large n and thus all zeros are non-zero (except for the trivial case handled above) */
        }
      while (gsl_fcmp(x, x0, 10*GSL_DBL_EPSILON) != 0);

      result->val = x;
      result->err = 2*GSL_DBL_EPSILON*x + fabs(x-x0);

      return GSL_SUCCESS;
    }
}

double
gsl_sf_hermite_zero(const int n, const int s)
{
  EVAL_RESULT(gsl_sf_hermite_zero_e(n, s, &result));
}

int
gsl_sf_hermite_func_zero_e(const int n, const int s, gsl_sf_result * result)
{
  return gsl_sf_hermite_zero_e(n, s, result);
}

double
gsl_sf_hermite_func_zero(const int n, const int s)
{
  EVAL_RESULT(gsl_sf_hermite_func_zero_e(n, s, &result));
}

#ifndef GSL_DISABLE_DEPRECATED

int
gsl_sf_hermite_phys_e(const int n, const double x, gsl_sf_result * result)
{
  return gsl_sf_hermite_e(n, x, result);
}

double
gsl_sf_hermite_phys(const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_phys_e(n, x, &result));
}

int
gsl_sf_hermite_phys_der_e(const int m, const int n, const double x, gsl_sf_result * result)
{
  return gsl_sf_hermite_deriv_e(m, n, x, result);
}

double
gsl_sf_hermite_phys_der(const int m, const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_phys_der_e(m, n, x, &result));
}

int
gsl_sf_hermite_phys_array(const int nmax, const double x, double * result_array)
{
  return gsl_sf_hermite_array(nmax, x, result_array);
}

int
gsl_sf_hermite_phys_series_e(const int n, const double x, const double * a, gsl_sf_result * result)
{
  return gsl_sf_hermite_series_e(n, x, a, result);
}

double
gsl_sf_hermite_phys_series(const int n, const double x, const double * a)
{
  EVAL_RESULT(gsl_sf_hermite_phys_series_e(n, x, a, &result));
}

int
gsl_sf_hermite_phys_array_der(const int m, const int nmax, const double x, double * result_array)
{
  return gsl_sf_hermite_array_deriv(m, nmax, x, result_array);
}

int
gsl_sf_hermite_phys_der_array(const int mmax, const int n, const double x, double * result_array)
{
  return gsl_sf_hermite_deriv_array(mmax, n, x, result_array);
}

int
gsl_sf_hermite_phys_zero_e(const int n, const int s, gsl_sf_result * result)
{
  return gsl_sf_hermite_zero_e(n, s, result);
}

double
gsl_sf_hermite_phys_zero(const int n, const int s)
{
  EVAL_RESULT(gsl_sf_hermite_zero_e(n, s, &result));
}

int
gsl_sf_hermite_prob_array_der(const int m, const int nmax, const double x, double * result_array)
{
  return gsl_sf_hermite_prob_array_deriv(m, nmax, x, result_array);
}

int
gsl_sf_hermite_prob_der_array(const int mmax, const int n, const double x, double * result_array)
{
  return gsl_sf_hermite_prob_deriv_array(mmax, n, x, result_array);
}

int
gsl_sf_hermite_prob_der_e(const int m, const int n, const double x, gsl_sf_result * result)
{
  return gsl_sf_hermite_prob_deriv_e(m, n, x, result);
}

double
gsl_sf_hermite_prob_der(const int m, const int n, const double x)
{
  EVAL_RESULT(gsl_sf_hermite_prob_deriv_e(m, n, x, &result));
}

#endif /* !GSL_DISABLE_DEPRECATED */


/****************************************************************
  wigner_gsl.h / wigner_gsl_twice.h

  Defines Wigner coupling and recoupling symbols as wrappers for GSL
  angular momentum functions:

    - wigner_gsl.h -- takes HalfInt angular momentum arguments (RECOMMENDED)
    - wigner_gsl_twice.h -- takes integer "twice value" angular momentum arguments

  Naming convention:
    - Function names *not* ending in '2' accept HalfInt arguments J.
    - Function names ending in '2' accept integer arguments 2*J.

  See, e.g., appendix to de Shalit and Talmi for underlying formulas.

  DOCUMENTATION: See wigner_gsl.h (rather than wigner_gsl_twice.h) for
  more complete function comments.

  Language: C++

  Mark A. Caprio
  University of Notre Dame

  + 02/16/10 (mac): Created.
  + 11/13/15 (mac): Add unitary 6J for (12)3-(13)2 recoupling
    and Racah reduction factor.
  + 02/27/16 (mac): Update includes for restructured header files.
  + 03/8/16 (mac): Enclose in namespace.
  + 06/8/16 (mac): Update #define guard directive.
  + 06/21/16 (mac): Remove Racah reduction factor. Update comments.
  + 10/18/16 (mac): Update Unitary6J comment. Rename wigner2_gsl.h to
    wigner_gsl_twice.h.
  + 04/28/18 (mac): Restore missing Hat2 and ParitySign2 to
    wigner_gsl_twice.h.
  + 04/10/20 (pjf): Replace assertions with exceptions.

****************************************************************/

#ifndef WIGNER2_GSL_H_
#define WIGNER2_GSL_H_

#include <stdexcept>

#include <gsl/gsl_sf_coupling.h>

#include "am.h"

namespace am {

  // angular momentum hat symbol

  inline
    double Hat2(int two_j)
  {
    return sqrt(static_cast<double>(two_j+1));
  }

  // phase sign (-)^sum

  inline
    int ParitySign2(int two_sum)
  {
    if ((two_sum%2)!=0) throw std::domain_error("two_sum not even");
    int remainder = abs(two_sum/2) % 2;
    int sign = 1 - 2*remainder;
    return sign;
  }

  // Wigner3J(ja,jb,jc,ma,mb,mc)
  //   returns Wigner 3-J symbol
  //   wrapper for gsl_sf_coupling_3j

  inline
    double Wigner3J2(
		     int two_ja, int two_jb, int two_jc,
		     int two_ma, int two_mb, int two_mc
		     )
  {
    return gsl_sf_coupling_3j(
			      two_ja, two_jb, two_jc,
			      two_ma, two_mb, two_mc
			      );
  }

  // ClebschGordan(ja,ma,jb,mb,jc,mc)
  //   returns Clebsch-Gordan coefficient
  //   wrapper for gsl_sf_coupling_3j

  inline
    double ClebschGordan2(
			  int two_ja, int two_ma,
			  int two_jb, int two_mb,
			  int two_jc, int two_mc
			  )
  {
    return Hat2(two_jc)*ParitySign2(two_ja-two_jb+two_mc)
      *gsl_sf_coupling_3j(
			  two_ja, two_jb, two_jc,
			  two_ma, two_mb, -two_mc
			  );
  }

  // Wigner6J(ja,jb,jc,jd,je,jf)
  //   returns Wigner 6-J symbol
  //   wrapper for gsl_sf_coupling_6j

  inline
    double Wigner6J2(
		     int two_ja, int two_jb, int two_jc,
		     int two_jd, int two_je, int two_jf
		     )
  {
    return gsl_sf_coupling_6j(
			      two_ja, two_jb, two_jc,
			      two_jd, two_je, two_jf
			      );
  }

  // Unitary6J(ja,jb,jc,jd,je,jf)
  //   wrapper for gsl_sf_coupling_6j
  //   returns unitary recoupling symbol for (12)3-1(23) recoupling

  inline
    double Unitary6J2(
		      int two_ja, int two_jb, int two_jc,
		      int two_jd, int two_je, int two_jf
		      )
  {
    return ParitySign2(two_ja+two_jb+two_jd+two_je)*Hat2(two_jc)*Hat2(two_jf)*gsl_sf_coupling_6j(
												 two_ja, two_jb, two_jc,
												 two_jd, two_je, two_jf
												 );
  }

  // Wigner9J(ja,jb,jc,jd,je,jf,jg,jh,ji)
  //   returns Wigner 9-J symbol
  //   wrapper for gsl_sf_coupling_9j

  inline
    double Wigner9J2(
		     int two_ja, int two_jb, int two_jc,
		     int two_jd, int two_je, int two_jf,
		     int two_jg, int two_jh, int two_ji
		     )
  {
    return gsl_sf_coupling_9j(
			      two_ja, two_jb, two_jc,
			      two_jd, two_je, two_jf,
			      two_jg, two_jh, two_ji
			      );
  }

  // Unitary9J(ja,jb,jc,jd,je,jf,jg,jh,ji)
  //   returns unitary 9-J symbol
  //   wrapper for gsl_sf_coupling_9j

  inline
    double Unitary9J2(
		      int two_ja, int two_jb, int two_jc,
		      int two_jd, int two_je, int two_jf,
		      int two_jg, int two_jh, int two_ji
		      )
  {
    return Hat2(two_jc)*Hat2(two_jf)*Hat2(two_jg)*Hat2(two_jh)
      *gsl_sf_coupling_9j(
			  two_ja, two_jb, two_jc,
			  two_jd, two_je, two_jf,
			  two_jg, two_jh, two_ji
			  );
  }

} // namespace
#endif


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gbpLib.h>
#include <gbpMath.h>
#include <gbpCosmo_core.h>
#include <gbpCosmo_NFW_etc.h>
#include <gsl/gsl_sf_expint.h>

// Cole and Lacey (1996) V_c(r) profile
double V_circ_NFW(double r, double M_vir, double z, int mode, cosmo_info **cosmo) {
    double c_vir;
    double R_vir;
    double V2_c_characteristic;
    double g_c;
    double r_o;
    double x;
    double r_val = 0.;

    set_NFW_params(M_vir, z, mode, cosmo, &c_vir, &R_vir);

    r_o = R_vir / c_vir;
    x   = r / r_o;
    if(x > 0.) {
        V2_c_characteristic = G_NEWTON * M_vir / R_vir;
        g_c                 = 1. / (log(1. + c_vir) - c_vir / (1. + c_vir));
        r_val               = sqrt(V2_c_characteristic * g_c * (log(1. + x) - x / (1. + x)) * c_vir / x);
    }
    return (r_val);
}


/* cdf/betainv.c
 *
 * Copyright (C) 2004 Free Software Foundation, Inc.
 * Copyright (C) 2006, 2007 Brian Gough
 * Written by Jason H. Stover.
 * Modified for GSL by Brian Gough
 *
 * 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 3 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 * Invert the Beta distribution.
 *
 * References:
 *
 * Roger W. Abernathy and Robert P. Smith. "Applying Series Expansion
 * to the Inverse Beta Distribution to Find Percentiles of the
 * F-Distribution," ACM Transactions on Mathematical Software, volume
 * 19, number 4, December 1993, pages 474-480.
 *
 * G.W. Hill and A.W. Davis. "Generalized asymptotic expansions of a
 * Cornish-Fisher type," Annals of Mathematical Statistics, volume 39,
 * number 8, August 1968, pages 1264-1273.
 */

#include <config.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>

#include "error.h"

static double
bisect (double x, double P, double a, double b, double xtol, double Ptol)
{
  double x0 = 0, x1 = 1, Px;

  while (fabs(x1 - x0) > xtol) {
    Px = gsl_cdf_beta_P (x, a, b);
    if (fabs(Px - P) < Ptol) {
      /* return as soon as approximation is good enough, including on
         the first iteration */
      return x;
    } else if (Px < P) {
      x0 = x;
    } else if (Px > P) {
      x1 = x;
    }
    x = 0.5 * (x0 + x1);
  }
  return x;
}

double
gsl_cdf_beta_Pinv (const double P, const double a, const double b)
{
  double x, mean;

  if (P < 0.0 || P > 1.0)
    {
      CDF_ERROR ("P must be in range 0 < P < 1", GSL_EDOM);
    }

  if (a < 0.0)
    {
      CDF_ERROR ("a < 0", GSL_EDOM);
    }

  if (b < 0.0)
    {
      CDF_ERROR ("b < 0", GSL_EDOM);
    }

  if (P == 0.0)
    {
      return 0.0;
    }

  if (P == 1.0)
    {
      return 1.0;
    }

  if (P > 0.5)
    {
      return gsl_cdf_beta_Qinv (1 - P, a, b);
    }

  mean = a / (a + b);

  if (P < 0.1)
    {
      /* small x */

      double lg_ab = gsl_sf_lngamma (a + b);
      double lg_a = gsl_sf_lngamma (a);
      double lg_b = gsl_sf_lngamma (b);

      double lx = (log (a) + lg_a + lg_b - lg_ab + log (P)) / a;
      if (lx <= 0) {
        x = exp (lx);             /* first approximation */
        x *= pow (1 - x, -(b - 1) / a);   /* second approximation */
      } else {
        x = mean;
      }

      if (x > mean)
        x = mean;
    }
  else
    {
      /* Use expected value as first guess */
      x = mean;
    }

  /* Do bisection to get closer */
  x = bisect (x, P, a, b, 0.01, 0.01);

  {
    double lambda, dP, phi;
    unsigned int n = 0;

  start:
    dP = P - gsl_cdf_beta_P (x, a, b);
    phi = gsl_ran_beta_pdf (x, a, b);

    if (dP == 0.0 || n++ > 64)
      goto end;

    lambda = dP / GSL_MAX (2 * fabs (dP / x), phi);

    {
      double step0 = lambda;
      double step1 = -((a - 1) / x - (b - 1) / (1 - x)) * lambda * lambda / 2;

      double step = step0;

      if (fabs (step1) < fabs (step0))
        {
          step += step1;
        }
      else
        {
          /* scale back step to a reasonable size when too large */
          step *= 2 * fabs (step0 / step1);
        };

      if (x + step > 0 && x + step < 1)
        {
          x += step;
        }
      else
        {
          x = sqrt (x) * sqrt (mean);   /* try a new starting point */
        }

      if (fabs (step0) > 1e-10 * x)
        goto start;
    }

  end:

    if (fabs(dP) > GSL_SQRT_DBL_EPSILON * P)
      {
        GSL_ERROR_VAL("inverse failed to converge", GSL_EFAILED, GSL_NAN);
      }

    return x;
  }
}

double
gsl_cdf_beta_Qinv (const double Q, const double a, const double b)
{

  if (Q < 0.0 || Q > 1.0)
    {
      CDF_ERROR ("Q must be inside range 0 < Q < 1", GSL_EDOM);
    }

  if (a < 0.0)
    {
      CDF_ERROR ("a < 0", GSL_EDOM);
    }

  if (b < 0.0)
    {
      CDF_ERROR ("b < 0", GSL_EDOM);
    }

  if (Q == 0.0)
    {
      return 1.0;
    }

  if (Q == 1.0)
    {
      return 0.0;
    }

  if (Q > 0.5)
    {
      return gsl_cdf_beta_Pinv (1 - Q, a, b);
    }
  else
    {
      return 1 - gsl_cdf_beta_Pinv (Q, b, a);
    };
}


/* multifit/multilinear.c
 *
 * Copyright (C) 2000 Brian Gough
 *
 * 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.
 */

#include <config.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_multifit.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>

/* Fit
 *
 *  y = X c
 *
 *  where X is an M x N matrix of M observations for N variables.
 *
 */

int
gsl_multifit_linear (const gsl_matrix * X,
		     const gsl_vector * y,
		     gsl_vector * c,
		     gsl_matrix * cov,
		     double *chisq, gsl_multifit_linear_workspace * work)
{
  if (X->size1 != y->size)
    {
      GSL_ERROR
	("number of observations in y does not match rows of matrix X",
	 GSL_EBADLEN);
    }
  else if (X->size2 != c->size)
    {
      GSL_ERROR ("number of parameters c does not match columns of matrix X",
		 GSL_EBADLEN);
    }
  else if (cov->size1 != cov->size2)
    {
      GSL_ERROR ("covariance matrix is not square", GSL_ENOTSQR);
    }
  else if (c->size != cov->size1)
    {
      GSL_ERROR
	("number of parameters does not match size of covariance matrix",
	 GSL_EBADLEN);
    }
  else if (X->size1 != work->n || X->size2 != work->p)
    {
      GSL_ERROR
	("size of workspace does not match size of observation matrix",
	 GSL_EBADLEN);
    }
  else
    {
      const size_t n = X->size1;
      const size_t p = X->size2;

      size_t i, j;

      gsl_matrix *A = work->A;
      gsl_matrix *Q = work->Q;
      gsl_matrix *QSI = work->QSI;
      gsl_vector *S = work->S;
      gsl_vector *xt = work->xt;
      gsl_vector *D = work->D;

      /* Copy X to workspace,  A <= X */

      gsl_matrix_memcpy (A, X);

      /* Balance the columns of the matrix A */

      gsl_linalg_balance_columns (A, D);

      /* Decompose A into U S Q^T */

      gsl_linalg_SV_decomp_mod (A, QSI, Q, S, xt);

      /* Solve y = A c for c */

      gsl_blas_dgemv (CblasTrans, 1.0, A, y, 0.0, xt);

      /* Scale the matrix Q,  Q' = Q S^-1 */

      gsl_matrix_memcpy (QSI, Q);

      for (j = 0; j < p; j++)
	{
	  gsl_vector_view column = gsl_matrix_column (QSI, j);
	  double alpha = gsl_vector_get (S, j);
	  if (alpha != 0)
	    alpha = 1.0 / alpha;
	  gsl_vector_scale (&column.vector, alpha);
	}

      gsl_vector_set_zero (c);

      gsl_blas_dgemv (CblasNoTrans, 1.0, QSI, xt, 0.0, c);

      /* Unscale the balancing factors */

      gsl_vector_div (c, D);

      /* Compute chisq, from residual r = y - X c */

      {
	double s2 = 0, r2 = 0;

	for (i = 0; i < n; i++)
	  {
	    double yi = gsl_vector_get (y, i);
	    gsl_vector_const_view row = gsl_matrix_const_row (X, i);
	    double y_est, ri;
	    gsl_blas_ddot (&row.vector, c, &y_est);
	    ri = yi - y_est;
	    r2 += ri * ri;
	  }

	s2 = r2 / (n - p);

	*chisq = r2;

	/* Form variance-covariance matrix cov = s2 * (Q S^-1) (Q S^-1)^T */

	for (i = 0; i < p; i++)
	  {
	    gsl_vector_view row_i = gsl_matrix_row (QSI, i);
	    double d_i = gsl_vector_get (D, i);

	    for (j = i; j < p; j++)
	      {
		gsl_vector_view row_j = gsl_matrix_row (QSI, j);
		double d_j = gsl_vector_get (D, j);
		double s;

		gsl_blas_ddot (&row_i.vector, &row_j.vector, &s);

		gsl_matrix_set (cov, i, j, s * s2 / (d_i * d_j));
		gsl_matrix_set (cov, j, i, s * s2 / (d_i * d_j));
	      }
	  }
      }

      return GSL_SUCCESS;
    }
}

int
gsl_multifit_wlinear (const gsl_matrix * X,
		      const gsl_vector * w,
		      const gsl_vector * y,
		      gsl_vector * c,
		      gsl_matrix * cov,
		      double *chisq, gsl_multifit_linear_workspace * work)
{
  if (X->size1 != y->size)
    {
      GSL_ERROR
	("number of observations in y does not match rows of matrix X",
	 GSL_EBADLEN);
    }
  else if (X->size2 != c->size)
    {
      GSL_ERROR ("number of parameters c does not match columns of matrix X",
		 GSL_EBADLEN);
    }
  else if (w->size != y->size)
    {
      GSL_ERROR ("number of weights does not match number of observations",
		 GSL_EBADLEN);
    }
  else if (cov->size1 != cov->size2)
    {
      GSL_ERROR ("covariance matrix is not square", GSL_ENOTSQR);
    }
  else if (c->size != cov->size1)
    {
      GSL_ERROR
	("number of parameters does not match size of covariance matrix",
	 GSL_EBADLEN);
    }
  else if (X->size1 != work->n || X->size2 != work->p)
    {
      GSL_ERROR
	("size of workspace does not match size of observation matrix",
	 GSL_EBADLEN);
    }
  else
    {
      const size_t n = X->size1;
      const size_t p = X->size2;

      size_t i, j;

      gsl_matrix *A = work->A;
      gsl_matrix *Q = work->Q;
      gsl_matrix *QSI = work->QSI;
      gsl_vector *S = work->S;
      gsl_vector *t = work->t;
      gsl_vector *xt = work->xt;
      gsl_vector *D = work->D;

      /* Scale X,  A = sqrt(w) X */

      gsl_matrix_memcpy (A, X);

      for (i = 0; i < n; i++)
	{
	  double wi = gsl_vector_get (w, i);

	  if (wi < 0)
	    wi = 0;

	  {
	    gsl_vector_view row = gsl_matrix_row (A, i);
	    gsl_vector_scale (&row.vector, sqrt (wi));
	  }
	}

      /* Balance the columns of the matrix A */

      gsl_linalg_balance_columns (A, D);

      /* Decompose A into U S Q^T */

      gsl_linalg_SV_decomp_mod (A, QSI, Q, S, xt);

      /* Solve sqrt(w) y = A c for c, by first computing t = sqrt(w) y */

      for (i = 0; i < n; i++)
	{
	  double wi = gsl_vector_get (w, i);
	  double yi = gsl_vector_get (y, i);
	  if (wi < 0)
	    wi = 0;
	  gsl_vector_set (t, i, sqrt (wi) * yi);
	}

      gsl_blas_dgemv (CblasTrans, 1.0, A, t, 0.0, xt);

      /* Scale the matrix Q,  Q' = Q S^-1 */

      gsl_matrix_memcpy (QSI, Q);

      for (j = 0; j < p; j++)
	{
	  gsl_vector_view column = gsl_matrix_column (QSI, j);
	  double alpha = gsl_vector_get (S, j);
	  if (alpha != 0)
	    alpha = 1.0 / alpha;
	  gsl_vector_scale (&column.vector, alpha);
	}

      gsl_vector_set_zero (c);

      /* Solution */

      gsl_blas_dgemv (CblasNoTrans, 1.0, QSI, xt, 0.0, c);

      /* Unscale the balancing factors */

      gsl_vector_div (c, D);

      /* Form covariance matrix cov = (Q S^-1) (Q S^-1)^T */

      for (i = 0; i < p; i++)
	{
	  gsl_vector_view row_i = gsl_matrix_row (QSI, i);
	  double d_i = gsl_vector_get (D, i);

	  for (j = i; j < p; j++)
	    {
	      gsl_vector_view row_j = gsl_matrix_row (QSI, j);
	      double d_j = gsl_vector_get (D, j);
	      double s;

	      gsl_blas_ddot (&row_i.vector, &row_j.vector, &s);

	      gsl_matrix_set (cov, i, j, s / (d_i * d_j));
	      gsl_matrix_set (cov, j, i, s / (d_i * d_j));
	    }
	}

      /* Compute chisq, from residual r = y - X c */

      {
	double r2 = 0;

	for (i = 0; i < n; i++)
	  {
	    double yi = gsl_vector_get (y, i);
	    double wi = gsl_vector_get (w, i);
	    gsl_vector_const_view row = gsl_matrix_const_row (X, i);
	    double y_est, ri;
	    gsl_blas_ddot (&row.vector, c, &y_est);
	    ri = yi - y_est;
	    r2 += wi * ri * ri;
	  }

	*chisq = r2;
      }

      return GSL_SUCCESS;
    }
}


/* multifit/multilinear.c
 *
 * Copyright (C) 2000, 2007, 2010 Brian Gough
 * Copyright (C) 2013, 2015 Patrick Alken
 *
 * 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

//#include <config.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_multifit.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>

#include "linear_common.c"

static int multifit_linear_svd (const gsl_matrix * X,
                                const int balance,
                                gsl_multifit_linear_workspace * work);

int
gsl_multifit_linear (const gsl_matrix * X,
                     const gsl_vector * y,
                     gsl_vector * c,
                     gsl_matrix * cov,
                     double *chisq, gsl_multifit_linear_workspace * work)
{
  size_t rank;
  int status = gsl_multifit_linear_tsvd(X, y, GSL_DBL_EPSILON, c, cov, chisq, &rank, work);

  return status;
}

/*
gsl_multifit_linear_tsvd()
  Solve linear least squares system with truncated SVD
Inputs: X     - least squares matrix, n-by-p
        y     - right hand side vector, n-by-1
        tol   - tolerance for singular value truncation; if
                s_j <= tol * s_0
                then it is discarded from series expansion
        c     - (output) solution vector, p-by-1
        cov   - (output) covariance matrix, p-by-p
        chisq - (output) cost function chi^2
        rank  - (output) effective rank (number of singular values
                used in solution)
        work  - workspace
*/

int
gsl_multifit_linear_tsvd (const gsl_matrix * X,
                          const gsl_vector * y,
                          const double tol,
                          gsl_vector * c,
                          gsl_matrix * cov,
                          double * chisq,
                          size_t * rank,
                          gsl_multifit_linear_workspace * work)
{
  const size_t n = X->size1;
  const size_t p = X->size2;

  if (y->size != n)
    {
      GSL_ERROR("number of observations in y does not match matrix",
                GSL_EBADLEN);
    }
  else if (p != c->size)
    {
      GSL_ERROR ("number of parameters c does not match matrix",
                 GSL_EBADLEN);
    }
  else if (tol <= 0)
    {
      GSL_ERROR ("tolerance must be positive", GSL_EINVAL);
    }
  else
    {
      int status;
      double rnorm = 0.0, snorm;

      /* compute balanced SVD */
      status = gsl_multifit_linear_bsvd (X, work);
      if (status)
        return status;

      status = multifit_linear_solve (X, y, tol, -1.0, rank,
                                      c, &rnorm, &snorm, work);

      *chisq = rnorm * rnorm;

      /* variance-covariance matrix cov = s2 * (Q S^-1) (Q S^-1)^T */
      {
        double r2 = rnorm * rnorm;
        double s2 = r2 / (double)(n - *rank);
        size_t i, j;
        gsl_matrix_view QSI = gsl_matrix_submatrix(work->QSI, 0, 0, p, p);
        gsl_vector_view D = gsl_vector_subvector(work->D, 0, p);

        for (i = 0; i < p; i++)
          {
            gsl_vector_view row_i = gsl_matrix_row (&QSI.matrix, i);
            double d_i = gsl_vector_get (&D.vector, i);

            for (j = i; j < p; j++)
              {
                gsl_vector_view row_j = gsl_matrix_row (&QSI.matrix, j);
                double d_j = gsl_vector_get (&D.vector, j);
                double s;

                gsl_blas_ddot (&row_i.vector, &row_j.vector, &s);

                gsl_matrix_set (cov, i, j, s * s2 / (d_i * d_j));
                gsl_matrix_set (cov, j, i, s * s2 / (d_i * d_j));
              }
          }
      }

      return status;
    }
}

/*
gsl_multifit_linear_svd()
  Perform SVD decomposition of the matrix X and store in work without
balancing
*/

int
gsl_multifit_linear_svd (const gsl_matrix * X,
                         gsl_multifit_linear_workspace * work)
{
  /* do not balance by default */
  int status = multifit_linear_svd(X, 0, work);

  return status;
}

/*
gsl_multifit_linear_bsvd()
  Perform SVD decomposition of the matrix X and store in work with
balancing
*/

int
gsl_multifit_linear_bsvd (const gsl_matrix * X,
                          gsl_multifit_linear_workspace * work)
{
  int status = multifit_linear_svd(X, 1, work);

  return status;
}

size_t
gsl_multifit_linear_rank(const double tol, const gsl_multifit_linear_workspace * work)
{
  double s0 = gsl_vector_get (work->S, 0);
  size_t rank = 0;
  size_t j;

  for (j = 0; j < work->p; j++)
    {
      double sj = gsl_vector_get (work->S, j);

      if (sj > tol * s0)
        ++rank;
    }

  return rank;
}

/* Estimation of values for given x */

int
gsl_multifit_linear_est (const gsl_vector * x,
                         const gsl_vector * c,
                         const gsl_matrix * cov, double *y, double *y_err)
{

  if (x->size != c->size)
    {
      GSL_ERROR ("number of parameters c does not match number of observations x",
         GSL_EBADLEN);
    }
  else if (cov->size1 != cov->size2)
    {
      GSL_ERROR ("covariance matrix is not square", GSL_ENOTSQR);
    }
  else if (c->size != cov->size1)
    {
      GSL_ERROR ("number of parameters c does not match size of covariance matrix cov",
         GSL_EBADLEN);
    }
  else
    {
      size_t i, j;
      double var = 0;

      gsl_blas_ddot(x, c, y);       /* y = x.c */

      /* var = x' cov x */

      for (i = 0; i < x->size; i++)
        {
          const double xi = gsl_vector_get (x, i);
          var += xi * xi * gsl_matrix_get (cov, i, i);

          for (j = 0; j < i; j++)
            {
              const double xj = gsl_vector_get (x, j);
              var += 2 * xi * xj * gsl_matrix_get (cov, i, j);
            }
        }

      *y_err = sqrt (var);

      return GSL_SUCCESS;
    }
}

/*
gsl_multifit_linear_rcond()
  Return reciprocal condition number of LS matrix;
gsl_multifit_linear_svd() must first be called to
compute the SVD of X and its reciprocal condition number
*/

double
gsl_multifit_linear_rcond (const gsl_multifit_linear_workspace * w)
{
  return w->rcond;
}

/*
gsl_multifit_linear_residuals()
  Compute vector of residuals from fit
Inputs: X - design matrix
        y - rhs vector
        c - fit coefficients
        r - (output) where to store residuals
*/

int
gsl_multifit_linear_residuals (const gsl_matrix *X, const gsl_vector *y,
                               const gsl_vector *c, gsl_vector *r)
{
  if (X->size1 != y->size)
    {
      GSL_ERROR
        ("number of observations in y does not match rows of matrix X",
         GSL_EBADLEN);
    }
  else if (X->size2 != c->size)
    {
      GSL_ERROR ("number of parameters c does not match columns of matrix X",
                 GSL_EBADLEN);
    }
  else if (y->size != r->size)
    {
      GSL_ERROR ("number of observations in y does not match number of residuals",
                 GSL_EBADLEN);
    }
  else
    {
      /* r = y - X c */
      gsl_vector_memcpy(r, y);
      gsl_blas_dgemv(CblasNoTrans, -1.0, X, c, 1.0, r);

      return GSL_SUCCESS;
    }
} /* gsl_multifit_linear_residuals() */

/* Perform a SVD decomposition on the least squares matrix X = U S Q^T
 *
 * Inputs: X       - least squares matrix
 *         balance - 1 to perform column balancing
 *         work    - workspace
 *
 * Notes:
 * 1) On output,
 *    work->A contains the matrix U
 *    work->Q contains the matrix Q
 *    work->S contains the vector of singular values
 * 2) The matrix X may have smaller dimensions than the workspace
 *    in the case of stdform2() - but the dimensions cannot be larger
 * 3) On output, work->n and work->p are set to the dimensions of X
 * 4) On output, work->rcond is set to the reciprocal condition number of X
 */

static int
multifit_linear_svd (const gsl_matrix * X,
                     const int balance,
                     gsl_multifit_linear_workspace * work)
{
  const size_t n = X->size1;
  const size_t p = X->size2;

  if (n > work->nmax || p > work->pmax)
    {
      GSL_ERROR("observation matrix larger than workspace", GSL_EBADLEN);
    }
  else
    {
      gsl_matrix_view A = gsl_matrix_submatrix(work->A, 0, 0, n, p);
      gsl_matrix_view Q = gsl_matrix_submatrix(work->Q, 0, 0, p, p);
      gsl_matrix_view QSI = gsl_matrix_submatrix(work->QSI, 0, 0, p, p);
      gsl_vector_view S = gsl_vector_subvector(work->S, 0, p);
      gsl_vector_view xt = gsl_vector_subvector(work->xt, 0, p);
      gsl_vector_view D = gsl_vector_subvector(work->D, 0, p);

      /* Copy X to workspace,  A <= X */

      gsl_matrix_memcpy (&A.matrix, X);

      /* Balance the columns of the matrix A if requested */

      if (balance)
        {
          gsl_linalg_balance_columns (&A.matrix, &D.vector);
        }
      else
        {
          gsl_vector_set_all (&D.vector, 1.0);
        }

      /* decompose A into U S Q^T */
      gsl_linalg_SV_decomp_mod (&A.matrix, &QSI.matrix, &Q.matrix,
                                &S.vector, &xt.vector);

      /* compute reciprocal condition number rcond = smin / smax */
      {
        double smin, smax;
        gsl_vector_minmax(&S.vector, &smin, &smax);
        work->rcond = smin / smax;
      }

      work->n = n;
      work->p = p;

      return GSL_SUCCESS;
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_errno.h>

#include "ccl.h"

//Spline creator
//n     -> number of points
//x     -> x-axis
//y     -> f(x)-axis
//y0,yf -> values of f(x) to use beyond the interpolation range
ccl_f1d_t *ccl_f1d_t_new(int n,double *x,double *y,double y0,double yf,
			 ccl_f1d_extrap_t extrap_lo_type,
			 ccl_f1d_extrap_t extrap_hi_type, int *status)
{
  ccl_f1d_t *spl=malloc(sizeof(ccl_f1d_t));
  if(spl==NULL) {
    *status = CCL_ERROR_MEMORY;
    return NULL;
  }

  spl->spline=gsl_spline_alloc(gsl_interp_akima,n);
  if (spl->spline == NULL) {
    *status = CCL_ERROR_MEMORY;
    free(spl);
    return NULL;
  }
  int parstatus=gsl_spline_init(spl->spline,x,y,n);
  if(parstatus) {
    *status = CCL_ERROR_SPLINE;
    gsl_spline_free(spl->spline);
    free(spl);
    return NULL;
  }

  if(*status==0) {
    spl->y0=y0;
    spl->yf=yf;
    spl->x_ini=x[0];
    spl->x_end=x[n-1];
    spl->y_ini=y[0];
    spl->y_end=y[n-1];
    spl->extrap_lo_type=extrap_lo_type;
    spl->extrap_hi_type=extrap_hi_type;
  }

  // Compute derivatives
  // Low-end
  if(spl->extrap_lo_type == ccl_f1d_extrap_const) {
    spl->der_lo = 0;
  }
  else if(spl->extrap_lo_type == ccl_f1d_extrap_linx_liny) {
    spl->der_lo = (y[1]-y[0])/(x[1]-x[0]);
  }
  else if(spl->extrap_lo_type == ccl_f1d_extrap_linx_logy) {
    if(y[1]*y[0]<=0)
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_lo = log(y[1]/y[0])/(x[1]-x[0]);
  }
  else if(spl->extrap_lo_type == ccl_f1d_extrap_logx_liny) {
    if(x[1]*x[0]<=0)
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_lo = (y[1]-y[0])/log(x[1]/x[0]);
  }
  else if(spl->extrap_lo_type == ccl_f1d_extrap_logx_logy) {
    if((y[1]*y[0]<=0) || (x[1]*x[0]<=0))
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_lo = log(y[1]/y[0])/log(x[1]/x[0]);
  }
  else // No extrapolation
    spl->der_lo = 0;

  // High-end
  if(spl->extrap_hi_type == ccl_f1d_extrap_const) {
    spl->der_hi = 0;
  }
  else if(spl->extrap_hi_type == ccl_f1d_extrap_linx_liny) {
    spl->der_hi = (y[n-1]-y[n-2])/(x[n-1]-x[n-2]);
  }
  else if(spl->extrap_hi_type == ccl_f1d_extrap_linx_logy) {
    if(y[n-1]*y[n-2]<=0)
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_hi = log(y[n-1]/y[n-2])/(x[n-1]-x[n-2]);
  }
  else if(spl->extrap_hi_type == ccl_f1d_extrap_logx_liny) {
    if(x[n-1]*x[n-2]<=0)
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_hi = (y[n-1]-y[n-2])/log(x[n-1]/x[n-2]);
  }
  else if(spl->extrap_hi_type == ccl_f1d_extrap_logx_logy) {
    if((y[n-1]*y[n-2]<=0) || (x[n-1]*x[n-2]<=0))
      *status = CCL_ERROR_SPLINE;
    else
      spl->der_hi = log(y[n-1]/y[n-2])/log(x[n-1]/x[n-2]);
  }
  else // No extrapolation
    spl->der_hi = 0;

  // If one of the derivatives could not be calculated
  // then return NULL.
  if (*status){
    ccl_f1d_t_free(spl);
    spl = NULL;
  }

  return spl;
}

//Evaluates spline at x checking for bound errors
double ccl_f1d_t_eval(ccl_f1d_t *spl,double x)
{
  // Extrapolation
  // Lin_x, lin_y
  // f(x) = f_n + (x-x_n) * der
  //   der = (f_n - f_{n-1}) / (x_n - x_{n-1})
  //

  // Log_x, lin_y
  // f(x) = f_n + log(x/x_n) * der
  // der = (f_n - f_{n-1}) / log(x_n/x_{n-1})

  // Lin_x, log_y
  // f(x) = f_n * exp[ (x - x_n) * der ]
  // der = log(f_n/f_{n-1})/(x_n-x_{n-1})

  // Log_x, log_y
  // f(x) = f_n * exp[ log(x/x_n) * der ]
  // der = log(f_n/f_{n-1})/log(x_n/x_{n-1})
  if(x<=spl->x_ini) {
    if(spl->extrap_lo_type==ccl_f1d_extrap_const)
      return spl->y0;
    else if(spl->extrap_lo_type==ccl_f1d_extrap_linx_liny) {
      return spl->y_ini + spl->der_lo * (x - spl->x_ini);
    }
    else if(spl->extrap_lo_type==ccl_f1d_extrap_logx_liny) {
      return spl->y_ini + spl->der_lo * log(x/spl->x_ini);
    }
    else if(spl->extrap_lo_type==ccl_f1d_extrap_linx_logy) {
      return spl->y_ini * exp(spl->der_lo * (x - spl->x_ini));
    }
    else if(spl->extrap_lo_type==ccl_f1d_extrap_logx_logy) {
      return spl->y_ini * pow(x/spl->x_ini, spl->der_lo);
    }
    else {
      ccl_raise_gsl_warning(CCL_ERROR_SPLINE_EV,
                            "ccl_f1d.c: ccl_f1d_t_eval(): "
                            "x-value below range.");
      return NAN;
    }
  }
  else if(x>=spl->x_end) {
    if(spl->extrap_hi_type==ccl_f1d_extrap_const)
      return spl->yf;
    else if(spl->extrap_hi_type==ccl_f1d_extrap_linx_liny) {
      return spl->y_end + spl->der_hi * (x - spl->x_end);
    }
    else if(spl->extrap_hi_type==ccl_f1d_extrap_logx_liny) {
      return spl->y_end + spl->der_hi * log(x/spl->x_end);
    }
    else if(spl->extrap_hi_type==ccl_f1d_extrap_linx_logy) {
      return spl->y_end * exp(spl->der_hi * (x - spl->x_end));
    }
    else if(spl->extrap_hi_type==ccl_f1d_extrap_logx_logy) {
      return spl->y_end * pow(x/spl->x_end, spl->der_hi);
    }
    else {
      ccl_raise_gsl_warning(CCL_ERROR_SPLINE_EV,
                            "ccl_f1d.c: ccl_f1d_t_eval(): "
                            "x-value above range.");
      return NAN;
    }
  }
  else {
    double y;
    int stat=gsl_spline_eval_e(spl->spline,x,NULL,&y);
    if (stat!=GSL_SUCCESS) {
      ccl_raise_gsl_warning(stat, "ccl_f1d.c: ccl_f1d_t_eval(): "
                            "x-value outside range.");
      return NAN;
    }
    return y;
  }
}

//Spline destructor
void ccl_f1d_t_free(ccl_f1d_t *spl)
{
  if (spl != NULL) {
    gsl_spline_free(spl->spline);
  }
  free(spl);
}


#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main( int argc, const char *argv[]  ) {
        double x = 5.0 ;
            double y = gsl_sf_bessel_J0( x  ) ;
                printf("J0(%g) = % .18e\n", x, y ) ;
                    return 0 ;

}


#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_vegas.h>
#include "timer.c"
#include "timer.h"

double g (double *t, size_t dim, void *params);

void timer_start(void);
double timer_stop(void);

int main (void)
{
  double tvegas, tmonte;
  double res, err;
  double distmin = 1.001;
  double distmax = 4.;
  double dist = distmin;
  int np = 20;
  double nt = (distmax - distmin) / ((double) np - 1.);
  size_t dim = 6;
  double x1[] = { 0., 0., 0., 0., 0., 0. };
  double xu[] = { 1., 1., 1., 1., 1., 1. };

  double vegas[20], dipole[20], distance[20], monte[20];
  int f = 0;
  gsl_rng *r = gsl_rng_alloc (gsl_rng_taus2);
  unsigned long seed = 1UL;

  gsl_rng_set (r, seed);

  size_t calls = 1500000;

  //Vegas Integration

  gsl_monte_function G = { &g, dim, &dist };

  gsl_monte_vegas_state *sv = gsl_monte_vegas_alloc (dim);

  gsl_monte_vegas_init (sv);

  timer_start();

  do
    {

      gsl_monte_vegas_integrate (&G, x1, xu, dim, calls / 5, r, sv, &res,
				 &err);
      do
	{
	  gsl_monte_vegas_integrate (&G, x1, xu, dim, calls, r, sv, &res,
				     &err);
	  fflush (stdout);
	}
      while (fabs (gsl_monte_vegas_chisq (sv) - 1.) > .2);
      vegas[f] = res;
      f++;
      dist = dist + nt;
    }
  while (dist <= distmax);

  tvegas = timer_stop();

  gsl_monte_vegas_free (sv);

  // Montecarlo Integration

  double sum = 0.;
  double x[6];
  long i, j, l;

  l = 1E6;
  dist = distmin;

  timer_start();

  for (j = 0; j < np; j++)
    {
      sum = 0.;
      for (i = 0; i < l; i++)
	{
	  for (int k = 0; k < (int) dim; k++)
	    {
	      x[k] = gsl_rng_uniform (r);
	    }
	  sum += g (x, dim, &dist);
	}

      res = sum / (double) l;
      monte[j] = res;
      dist += nt;
    }

  tmonte =  timer_stop();

  gsl_rng_free (r);
  dist = distmin;

  for (int y = 0; y < np; y++)
    {
      dipole[y] = 2. / pow (dist, 3);
      distance[y] = dist;
      dist = dist + nt;
    }

  printf
    ("#    Dist               Vegas           Monte       Dipolapprox\n");
  for (int q = 0; q < np; q++)
    {
      double dd = distance[q];
      double vv = fabs (vegas[q]);
      double hh = fabs (monte[q]);
      double di = fabs (dipole[q]);
      printf ("   %.6f         %.6f       %.6f      %.6f\n", dd, vv, hh, di);
    }

  printf("\n\n");
  printf("Time for Vegas  = %f\n", tvegas);
  printf("Time for Monte  = %f\n", tmonte);
  printf("Speed up Factor = %f\n", tvegas/tmonte);

  return 0;
}