text:
/* specfunc/legendre_con.c
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
 * Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/* Author:  G. Jungman */

#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_poly.h>
#include <gsl/gsl_sf_exp.h>
#include <gsl/gsl_sf_trig.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_sf_ellint.h>
#include <gsl/gsl_sf_pow_int.h>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_sf_hyperg.h>
#include <gsl/gsl_sf_legendre.h>

#include "error.h"
#include "legendre.h"

#define Root_2OverPi_  0.797884560802865355879892
#define locEPS         (1000.0*GSL_DBL_EPSILON)

/*-*-*-*-*-*-*-*-*-*-*-* Private Section *-*-*-*-*-*-*-*-*-*-*-*/

#define RECURSE_LARGE  (1.0e-5*GSL_DBL_MAX)
#define RECURSE_SMALL  (1.0e+5*GSL_DBL_MIN)

/* Continued fraction for f_{ell+1}/f_ell
 * f_ell := P^{-mu-ell}_{-1/2 + I tau}(x),  x < 1.0
 *
 * Uses standard CF method from Temme's book.
 */
static
int
conicalP_negmu_xlt1_CF1(const double mu, const int ell, const double tau,
                        const double x, gsl_sf_result * result)
{
  const double RECUR_BIG = GSL_SQRT_DBL_MAX;
  const int maxiter = 5000;
  int n = 1;
  double xi = x/(sqrt(1.0-x)*sqrt(1.0+x));
  double Anm2 = 1.0;
  double Bnm2 = 0.0;
  double Anm1 = 0.0;
  double Bnm1 = 1.0;
  double a1 = 1.0;
  double b1 = 2.0*(mu + ell + 1.0) * xi;
  double An = b1*Anm1 + a1*Anm2;
  double Bn = b1*Bnm1 + a1*Bnm2;
  double an, bn;
  double fn = An/Bn;

  while(n < maxiter) {
    double old_fn;
    double del;
    n++;
    Anm2 = Anm1;
    Bnm2 = Bnm1;
    Anm1 = An;
    Bnm1 = Bn;
    an = tau*tau + (mu - 0.5 + ell + n)*(mu - 0.5 + ell + n);
    bn = 2.0*(ell + mu + n) * xi;
    An = bn*Anm1 + an*Anm2;
    Bn = bn*Bnm1 + an*Bnm2;

    if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
      An /= RECUR_BIG;
      Bn /= RECUR_BIG;
      Anm1 /= RECUR_BIG;
      Bnm1 /= RECUR_BIG;
      Anm2 /= RECUR_BIG;
      Bnm2 /= RECUR_BIG;
    }

    old_fn = fn;
    fn = An/Bn;
    del = old_fn/fn;

    if(fabs(del - 1.0) < 2.0*GSL_DBL_EPSILON) break;
  }

  result->val = fn;
  result->err = 4.0 * GSL_DBL_EPSILON * (sqrt(n) + 1.0) * fabs(fn);

  if(n >= maxiter)
    GSL_ERROR ("error", GSL_EMAXITER);
  else
    return GSL_SUCCESS;
}

/* Continued fraction for f_{ell+1}/f_ell
 * f_ell := P^{-mu-ell}_{-1/2 + I tau}(x),  x >= 1.0
 *
 * Uses Gautschi (Euler) equivalent series.
 */
static
int
conicalP_negmu_xgt1_CF1(const double mu, const int ell, const double tau,
                        const double x, gsl_sf_result * result)
{
  const int maxk = 20000;
  const double gamma = 1.0-1.0/(x*x);
  const double pre = sqrt(x-1.0)*sqrt(x+1.0) / (x*(2.0*(ell+mu+1.0)));
  double tk   = 1.0;
  double sum  = 1.0;
  double rhok = 0.0;
  int k;

  for(k=1; k<maxk; k++) {
    double tlk = 2.0*(ell + mu + k);
    double l1k = (ell + mu - 0.5 + 1.0 + k);
    double ak = -(tau*tau + l1k*l1k)/(tlk*(tlk+2.0)) * gamma;
    rhok = -ak*(1.0 + rhok)/(1.0 + ak*(1.0 + rhok));
    tk  *= rhok;
    sum += tk;
    if(fabs(tk/sum) < GSL_DBL_EPSILON) break;
  }

  result->val  = pre * sum;
  result->err  = fabs(pre * tk);
  result->err += 2.0 * GSL_DBL_EPSILON * (sqrt(k) + 1.0) * fabs(pre*sum);

  if(k >= maxk)
    GSL_ERROR ("error", GSL_EMAXITER);
  else
    return GSL_SUCCESS;
}

/* Implementation of large negative mu asymptotic
 * [Dunster, Proc. Roy. Soc. Edinburgh 119A, 311 (1991), p. 326]
 */

inline
static double olver_U1(double beta2, double p)
{
  return (p-1.0)/(24.0*(1.0+beta2)) * (3.0 + beta2*(2.0 + 5.0*p*(1.0+p)));
}

inline
static double olver_U2(double beta2, double p)
{
  double beta4 = beta2*beta2;
  double p2    = p*p;
  double poly1 =  4.0*beta4 + 84.0*beta2 - 63.0;
  double poly2 = 16.0*beta4 + 90.0*beta2 - 81.0;
  double poly3 = beta2*p2*(97.0*beta2 - 432.0 + 77.0*p*(beta2-6.0) - 385.0*beta2*p2*(1.0 + p));
  return (1.0-p)/(1152.0*(1.0+beta2)) * (poly1 + poly2 + poly3);
}

static const double U3c1[] = {   -1307.0,   -1647.0,    3375.0,    3675.0 };
static const double U3c2[] = {   29366.0,   35835.0, -252360.0, -272630.0,
                                276810.0,  290499.0 };
static const double U3c3[] = {  -29748.0,   -8840.0, 1725295.0, 1767025.0,
                              -7313470.0, -754778.0, 6309875.0, 6480045.0 };
static const double U3c4[] = {    2696.0,    -16740.0,   -524250.0,  -183975.0,
                              14670540.0,  14172939.0, -48206730.0, -48461985.0,
                              36756720.0,  37182145.0 };
static const double U3c5[] = {       9136.0,      22480.0,     12760.0,
                                  -252480.0,   -4662165.0,   -1705341.0,
                                 92370135.0,   86244015.0, -263678415.0,
                               -260275015.0, 185910725.0,  185910725.0 };

#if 0
static double olver_U3(double beta2, double p)
{
  double beta4 = beta2*beta2;
  double beta6 = beta4*beta2;
  double opb2s = (1.0+beta2)*(1.0+beta2);
  double den   = 39813120.0 * opb2s*opb2s;
  double poly1 = gsl_poly_eval(U3c1, 4, p);
  double poly2 = gsl_poly_eval(U3c2, 6, p);
  double poly3 = gsl_poly_eval(U3c3, 8, p);
  double poly4 = gsl_poly_eval(U3c4, 10, p);
  double poly5 = gsl_poly_eval(U3c5, 12, p);

  return (p-1.0)*(     1215.0*poly1 + 324.0*beta2*poly2
                 + 54.0*beta4*poly3 +  12.0*beta6*poly4
                 + beta4*beta4*poly5
                 ) / den;
}
#endif /* 0 */

/* Large negative mu asymptotic
 * P^{-mu}_{-1/2 + I tau}, mu -> Inf
 * |x| < 1
 *
 * [Dunster, Proc. Roy. Soc. Edinburgh 119A, 311 (1991), p. 326]
 */
int
gsl_sf_conicalP_xlt1_large_neg_mu_e(double mu, double tau, double x,
                                       gsl_sf_result * result, double * ln_multiplier)
{
  double beta  = tau/mu;
  double beta2 = beta*beta;
  double S     = beta * acos((1.0-beta2)/(1.0+beta2));
  double p     = x/sqrt(beta2*(1.0-x*x) + 1.0);
  gsl_sf_result lg_mup1;
  int lg_stat = gsl_sf_lngamma_e(mu+1.0, &lg_mup1);
  double ln_pre_1 =  0.5*mu*(S - log(1.0+beta2) + log((1.0-p)/(1.0+p))) - lg_mup1.val;
  double ln_pre_2 = -0.25 * log(1.0 + beta2*(1.0-x));
  double ln_pre_3 = -tau * atan(p*beta);
  double ln_pre = ln_pre_1 + ln_pre_2 + ln_pre_3;
  double sum   = 1.0 - olver_U1(beta2, p)/mu + olver_U2(beta2, p)/(mu*mu);

  if(sum == 0.0) {
    result->val = 0.0;
    result->err = 0.0;
    *ln_multiplier = 0.0;
    return GSL_SUCCESS;
  }
  else {
    int stat_e = gsl_sf_exp_mult_e(ln_pre, sum, result);
    if(stat_e != GSL_SUCCESS) {
      result->val = sum;
      result->err = 2.0 * GSL_DBL_EPSILON * fabs(sum);
      *ln_multiplier = ln_pre;
    }
    else {
      *ln_multiplier = 0.0;
    }
    return lg_stat;
  }
}

/* Implementation of large tau asymptotic
 *
 * A_n^{-mu}, B_n^{-mu}  [Olver, p.465, 469]
 */

inline
static double olver_B0_xi(double mu, double xi)
{
  return (1.0 - 4.0*mu*mu)/(8.0*xi) * (1.0/tanh(xi) - 1.0/xi);
}

static double olver_A1_xi(double mu, double xi, double x)
{
  double B = olver_B0_xi(mu, xi);
  double psi;
  if(fabs(x - 1.0) < GSL_ROOT4_DBL_EPSILON) {
    double y = x - 1.0;
    double s = -1.0/3.0 + y*(2.0/15.0 - y *(61.0/945.0 - 452.0/14175.0*y));
    psi = (4.0*mu*mu - 1.0)/16.0 * s;
  }
  else {
    psi = (4.0*mu*mu - 1.0)/16.0 * (1.0/(x*x-1.0) - 1.0/(xi*xi));
  }
  return 0.5*xi*xi*B*B + (mu+0.5)*B - psi + mu/6.0*(0.25 - mu*mu);
}

inline
static double olver_B0_th(double mu, double theta)
{
  return -(1.0 - 4.0*mu*mu)/(8.0*theta) * (1.0/tan(theta) - 1.0/theta);
}

static double olver_A1_th(double mu, double theta, double x)
{
  double B = olver_B0_th(mu, theta);
  double psi;
  if(fabs(x - 1.0) < GSL_ROOT4_DBL_EPSILON) {
    double y = 1.0 - x;
    double s = -1.0/3.0 + y*(2.0/15.0 - y *(61.0/945.0 - 452.0/14175.0*y));
    psi = (4.0*mu*mu - 1.0)/16.0 * s;
  }
  else {
    psi = (4.0*mu*mu - 1.0)/16.0 * (1.0/(x*x-1.0) + 1.0/(theta*theta));
  }
  return -0.5*theta*theta*B*B + (mu+0.5)*B - psi + mu/6.0*(0.25 - mu*mu);
}

/* Large tau uniform asymptotics
 * P^{-mu}_{-1/2 + I tau}
 * 1 < x
 * tau -> Inf
 * [Olver, p. 469]
 */
int
gsl_sf_conicalP_xgt1_neg_mu_largetau_e(const double mu, const double tau,
                                          const double x, double acosh_x,
                                          gsl_sf_result * result, double * ln_multiplier)
{
  double xi = acosh_x;
  double ln_xi_pre;
  double ln_pre;
  double sumA, sumB, sum;
  double arg;
  gsl_sf_result J_mup1;
  gsl_sf_result J_mu;
  double J_mum1;

  if(xi < GSL_ROOT4_DBL_EPSILON) {
    ln_xi_pre = -xi*xi/6.0;           /* log(1.0 - xi*xi/6.0) */
  }
  else {
    gsl_sf_result lnshxi;
    gsl_sf_lnsinh_e(xi, &lnshxi);
    ln_xi_pre = log(xi) - lnshxi.val;     /* log(xi/sinh(xi) */
  }

  ln_pre = 0.5*ln_xi_pre - mu*log(tau);

  arg = tau*xi;

  gsl_sf_bessel_Jnu_e(mu + 1.0,   arg, &J_mup1);
  gsl_sf_bessel_Jnu_e(mu,         arg, &J_mu);
  J_mum1 = -J_mup1.val + 2.0*mu/arg*J_mu.val;      /* careful of mu < 1 */

  sumA = 1.0 - olver_A1_xi(-mu, xi, x)/(tau*tau);
  sumB = olver_B0_xi(-mu, xi);
  sum  = J_mu.val * sumA - xi/tau * J_mum1 * sumB;

  if(sum == 0.0) {
    result->val = 0.0;
    result->err = 0.0;
    *ln_multiplier = 0.0;
    return GSL_SUCCESS;
  }
  else {
    int stat_e = gsl_sf_exp_mult_e(ln_pre, sum, result);
    if(stat_e != GSL_SUCCESS) {
      result->val = sum;
      result->err = 2.0 * GSL_DBL_EPSILON * fabs(sum);
      *ln_multiplier = ln_pre;
    }
    else {
      *ln_multiplier = 0.0;
    }
    return GSL_SUCCESS;
  }
}

/* Large tau uniform asymptotics
 * P^{-mu}_{-1/2 + I tau}
 * -1 < x < 1
 * tau -> Inf
 * [Olver, p. 473]
 */
int
gsl_sf_conicalP_xlt1_neg_mu_largetau_e(const double mu, const double tau,
                                          const double x, const double acos_x,
                                          gsl_sf_result * result, double * ln_multiplier)
{
  double theta = acos_x;
  double ln_th_pre;
  double ln_pre;
  double sumA, sumB, sum, sumerr;
  double arg;
  gsl_sf_result I_mup1, I_mu;
  double I_mum1;

  if(theta < GSL_ROOT4_DBL_EPSILON) {
    ln_th_pre = theta*theta/6.0;   /* log(1.0 + theta*theta/6.0) */
  }
  else {
    ln_th_pre = log(theta/sin(theta));
  }

  ln_pre = 0.5 * ln_th_pre - mu * log(tau);

  arg = tau*theta;
  gsl_sf_bessel_Inu_e(mu + 1.0,   arg, &I_mup1);
  gsl_sf_bessel_Inu_e(mu,         arg, &I_mu);
  I_mum1 = I_mup1.val + 2.0*mu/arg * I_mu.val; /* careful of mu < 1 */

  sumA = 1.0 - olver_A1_th(-mu, theta, x)/(tau*tau);
  sumB = olver_B0_th(-mu, theta);
  sum  = I_mu.val * sumA - theta/tau * I_mum1 * sumB;
  sumerr  = fabs(I_mu.err * sumA);
  sumerr += fabs(I_mup1.err * theta/tau * sumB);
  sumerr += fabs(I_mu.err   * theta/tau * sumB * 2.0 * mu/arg);

  if(sum == 0.0) {
    result->val = 0.0;
    result->err = 0.0;
    *ln_multiplier = 0.0;
    return GSL_SUCCESS;
  }
  else {
    int stat_e = gsl_sf_exp_mult_e(ln_pre, sum, result);
    if(stat_e != GSL_SUCCESS) {
      result->val  = sum;
      result->err  = sumerr;
      result->err += GSL_DBL_EPSILON * fabs(sum);
      *ln_multiplier = ln_pre;
    }
    else {
      *ln_multiplier = 0.0;
    }
    return GSL_SUCCESS;
  }
}

/* Hypergeometric function which appears in the
 * large x expansion below:
 *
 *   2F1(1/4 - mu/2 - I tau/2, 3/4 - mu/2 - I tau/2, 1 - I tau, y)
 *
 * Note that for the usage below y = 1/x^2;
 */
static
int
conicalP_hyperg_large_x(const double mu, const double tau, const double y,
                        double * reF, double * imF)
{
  const int kmax = 1000;
  const double re_a = 0.25 - 0.5*mu;
  const double re_b = 0.75 - 0.5*mu;
  const double re_c = 1.0;
  const double im_a = -0.5*tau;
  const double im_b = -0.5*tau;
  const double im_c = -tau;

  double re_sum = 1.0;
  double im_sum = 0.0;
  double re_term = 1.0;
  double im_term = 0.0;
  int k;

  for(k=1; k<=kmax; k++) {
    double re_ak = re_a + k - 1.0;
    double re_bk = re_b + k - 1.0;
    double re_ck = re_c + k - 1.0;
    double im_ak = im_a;
    double im_bk = im_b;
    double im_ck = im_c;
    double den = re_ck*re_ck + im_ck*im_ck;
    double re_multiplier = ((re_ak*re_bk - im_ak*im_bk)*re_ck + im_ck*(im_ak*re_bk + re_ak*im_bk)) / den;
    double im_multiplier = ((im_ak*re_bk + re_ak*im_bk)*re_ck - im_ck*(re_ak*re_bk - im_ak*im_bk)) / den;
    double re_tmp = re_multiplier*re_term - im_multiplier*im_term;
    double im_tmp = im_multiplier*re_term + re_multiplier*im_term;
    double asum = fabs(re_sum) + fabs(im_sum);
    re_term = y/k * re_tmp;
    im_term = y/k * im_tmp;
    if(fabs(re_term/asum) < GSL_DBL_EPSILON && fabs(im_term/asum) < GSL_DBL_EPSILON) break;
    re_sum += re_term;
    im_sum += im_term;
  }

  *reF = re_sum;
  *imF = im_sum;

  if(k == kmax)
    GSL_ERROR ("error", GSL_EMAXITER);
  else
    return GSL_SUCCESS;
}

/* P^{mu}_{-1/2 + I tau}
 * x->Inf
 */
int
gsl_sf_conicalP_large_x_e(const double mu, const double tau, const double x,
                             gsl_sf_result * result, double * ln_multiplier)
{
  /* 2F1 term
   */
  double y = ( x < 0.5*GSL_SQRT_DBL_MAX ? 1.0/(x*x) : 0.0 );
  double reF, imF;
  int stat_F = conicalP_hyperg_large_x(mu, tau, y, &reF, &imF);

  /* f = Gamma(+i tau)/Gamma(1/2 - mu + i tau)
   * FIXME: shift so it's better for tau-> 0
   */
  gsl_sf_result lgr_num, lgth_num;
  gsl_sf_result lgr_den, lgth_den;
  int stat_gn = gsl_sf_lngamma_complex_e(0.0,tau,&lgr_num,&lgth_num);
  int stat_gd = gsl_sf_lngamma_complex_e(0.5-mu,tau,&lgr_den,&lgth_den);

  double angle = lgth_num.val - lgth_den.val + atan2(imF,reF);

  double lnx   = log(x);
  double lnxp1 = log(x+1.0);
  double lnxm1 = log(x-1.0);
  double lnpre_const = 0.5*M_LN2 - 0.5*M_LNPI;
  double lnpre_comm = (mu-0.5)*lnx - 0.5*mu*(lnxp1 + lnxm1);
  double lnpre_err  =   GSL_DBL_EPSILON * (0.5*M_LN2 + 0.5*M_LNPI)
                      + GSL_DBL_EPSILON * fabs((mu-0.5)*lnx)
                      + GSL_DBL_EPSILON * fabs(0.5*mu)*(fabs(lnxp1)+fabs(lnxm1));

  /*  result = pre*|F|*|f| * cos(angle - tau * (log(x)+M_LN2))
   */
  gsl_sf_result cos_result;
  int stat_cos = gsl_sf_cos_e(angle + tau*(log(x) + M_LN2), &cos_result);
  int status = GSL_ERROR_SELECT_4(stat_cos, stat_gd, stat_gn, stat_F);
  if(cos_result.val == 0.0) {
    result->val = 0.0;
    result->err = 0.0;
    return status;
  }
  else {
    double lnFf_val = 0.5*log(reF*reF+imF*imF) + lgr_num.val - lgr_den.val;
    double lnFf_err = lgr_num.err + lgr_den.err + GSL_DBL_EPSILON * fabs(lnFf_val);
    double lnnoc_val = lnpre_const + lnpre_comm + lnFf_val;
    double lnnoc_err = lnpre_err + lnFf_err + GSL_DBL_EPSILON * fabs(lnnoc_val);
    int stat_e = gsl_sf_exp_mult_err_e(lnnoc_val, lnnoc_err,
                                          cos_result.val, cos_result.err,
                                          result);
    if(stat_e == GSL_SUCCESS) {
      *ln_multiplier = 0.0;
    }
    else {
      result->val  = cos_result.val;
      result->err  = cos_result.err;
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      *ln_multiplier = lnnoc_val;
    }
    return status;
  }
}

/* P^{mu}_{-1/2 + I tau}  first hypergeometric representation
 * -1 < x < 1
 * This is more effective for |x| small, however it will work w/o
 * reservation for any x < 0 because everything is positive
 * definite in that case.
 *
 * [Kolbig,   (3)] (note typo in args of gamma functions)
 * [Bateman, (22)] (correct form)
 */
static
int
conicalP_xlt1_hyperg_A(double mu, double tau, double x, gsl_sf_result * result)
{
  double x2 = x*x;
  double err_amp = 1.0 + 1.0/(GSL_DBL_EPSILON + fabs(1.0-fabs(x)));
  double pre_val = M_SQRTPI / pow(0.5*sqrt(1-x2), mu);
  double pre_err = err_amp * GSL_DBL_EPSILON * (fabs(mu)+1.0) * fabs(pre_val) ;
  gsl_sf_result ln_g1, ln_g2, arg_g1, arg_g2;
  gsl_sf_result F1, F2;
  gsl_sf_result pre1, pre2;
  double t1_val, t1_err;
  double t2_val, t2_err;

  int stat_F1 = gsl_sf_hyperg_2F1_conj_e(0.25 - 0.5*mu, 0.5*tau, 0.5, x2, &F1);
  int stat_F2 = gsl_sf_hyperg_2F1_conj_e(0.75 - 0.5*mu, 0.5*tau, 1.5, x2, &F2);
  int status = GSL_ERROR_SELECT_2(stat_F1, stat_F2);

  gsl_sf_lngamma_complex_e(0.75 - 0.5*mu, -0.5*tau, &ln_g1, &arg_g1);
  gsl_sf_lngamma_complex_e(0.25 - 0.5*mu, -0.5*tau, &ln_g2, &arg_g2);

  gsl_sf_exp_err_e(-2.0*ln_g1.val, 2.0*ln_g1.err, &pre1);
  gsl_sf_exp_err_e(-2.0*ln_g2.val, 2.0*ln_g2.err, &pre2);
  pre2.val *= -2.0*x;
  pre2.err *=  2.0*fabs(x);
  pre2.err +=  GSL_DBL_EPSILON * fabs(pre2.val);

  t1_val = pre1.val * F1.val;
  t1_err = fabs(pre1.val) * F1.err + pre1.err * fabs(F1.val);
  t2_val = pre2.val * F2.val;
  t2_err = fabs(pre2.val) * F2.err + pre2.err * fabs(F2.val);

  result->val  = pre_val * (t1_val + t2_val);
  result->err  = pre_val * (t1_err + t2_err);
  result->err += pre_err * fabs(t1_val + t2_val);
  result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);

  return status;
}

/* P^{mu}_{-1/2 + I tau}
 * defining hypergeometric representation
 * [Abramowitz+Stegun, 8.1.2]
 * 1 < x < 3
 * effective for x near 1
 *
 */
#if 0
static
int
conicalP_def_hyperg(double mu, double tau, double x, double * result)
{
  double F;
  int stat_F = gsl_sf_hyperg_2F1_conj_renorm_e(0.5, tau, 1.0-mu, 0.5*(1.0-x), &F);
  *result = pow((x+1.0)/(x-1.0), 0.5*mu) * F;
  return stat_F;
}
#endif /* 0 */

/* P^{mu}_{-1/2 + I tau}  second hypergeometric representation
 * [Zhurina+Karmazina, (3.1)]
 * -1 < x < 3
 * effective for x near 1
 *
 */
#if 0
static
int
conicalP_xnear1_hyperg_C(double mu, double tau, double x, double * result)
{
  double ln_pre, arg_pre;
  double ln_g1, arg_g1;
  double ln_g2, arg_g2;
  double F;

  int stat_F = gsl_sf_hyperg_2F1_conj_renorm_e(0.5+mu, tau, 1.0+mu, 0.5*(1.0-x), &F);

  gsl_sf_lngamma_complex_e(0.5+mu, tau, &ln_g1, &arg_g1);
  gsl_sf_lngamma_complex_e(0.5-mu, tau, &ln_g2, &arg_g2);

  ln_pre  = mu*M_LN2 - 0.5*mu*log(fabs(x*x-1.0)) + ln_g1 - ln_g2;
  arg_pre = arg_g1 - arg_g2;

  *result = exp(ln_pre) * F;
  return stat_F;
}
#endif /* 0 */

/* V0, V1 from Kolbig, m = 0
 */
static
int
conicalP_0_V(const double t, const double f, const double tau, const double sgn,
             double * V0, double * V1)
{
  double C[8];
  double T[8];
  double H[8];
  double V[12];
  int i;
  T[0] = 1.0;
  H[0] = 1.0;
  V[0] = 1.0;
  for(i=1; i<=7; i++) {
    T[i] = T[i-1] * t;
    H[i] = H[i-1] * (t*f);
  }
  for(i=1; i<=11; i++) {
    V[i] = V[i-1] * tau;
  }

  C[0] = 1.0;
  C[1] = (H[1]-1.0)/(8.0*T[1]);
  C[2] = (9.0*H[2] + 6.0*H[1] - 15.0 - sgn*8.0*T[2])/(128.0*T[2]);
  C[3] = 5.0*(15.0*H[3] + 27.0*H[2] + 21.0*H[1] - 63.0 - sgn*T[2]*(16.0*H[1]+24.0))/(1024.0*T[3]);
  C[4] = 7.0*(525.0*H[4] + 1500.0*H[3] + 2430.0*H[2] + 1980.0*H[1] - 6435.0
              + 192.0*T[4] - sgn*T[2]*(720.0*H[2]+1600.0*H[1]+2160.0)
              ) / (32768.0*T[4]);
  C[5] = 21.0*(2835.0*H[5] + 11025.0*H[4] + 24750.0*H[3] + 38610.0*H[2]
               + 32175.0*H[1] - 109395.0 + T[4]*(1984.0*H[1]+4032.0)
               - sgn*T[2]*(4800.0*H[3]+15120.0*H[2]+26400.0*H[1]+34320.0)
               ) / (262144.0*T[5]);
  C[6] = 11.0*(218295.0*H[6] + 1071630.0*H[5] + 3009825.0*H[4] + 6142500.0*H[3]
               + 9398025.0*H[2] + 7936110.0*H[1] - 27776385.0
               + T[4]*(254016.0*H[2]+749952.0*H[1]+1100736.0)
               - sgn*T[2]*(441000.0*H[4] + 1814400.0*H[3] + 4127760.0*H[2]
                         + 6552000.0*H[1] + 8353800.0 + 31232.0*T[4]
                         )
               ) / (4194304.0*T[6]);

  *V0 = C[0] + (-4.0*C[3]/T[1]+C[4])/V[4]
             + (-192.0*C[5]/T[3]+144.0*C[6]/T[2])/V[8]
             + sgn * (-C[2]/V[2]
                      + (-24.0*C[4]/T[2]+12.0*C[5]/T[1]-C[6])/V[6]
                      + (-1920.0*C[6]/T[4])/V[10]
                      );
  *V1 = C[1]/V[1] + (8.0*(C[3]/T[2]-C[4]/T[1])+C[5])/V[5]
                  + (384.0*C[5]/T[4] - 768.0*C[6]/T[3])/V[9]
                  + sgn * ((2.0*C[2]/T[1]-C[3])/V[3]
                           + (48.0*C[4]/T[3]-72.0*C[5]/T[2] + 18.0*C[6]/T[1])/V[7]
                           + (3840.0*C[6]/T[5])/V[11]
                           );

  return GSL_SUCCESS;
}

/* V0, V1 from Kolbig, m = 1
 */
static
int
conicalP_1_V(const double t, const double f, const double tau, const double sgn,
             double * V0, double * V1)
{
  double Cm1;
  double C[8];
  double T[8];
  double H[8];
  double V[12];
  int i;
  T[0] = 1.0;
  H[0] = 1.0;
  V[0] = 1.0;
  for(i=1; i<=7; i++) {
    T[i] = T[i-1] * t;
    H[i] = H[i-1] * (t*f);
  }
  for(i=1; i<=11; i++) {
    V[i] = V[i-1] * tau;
  }

  Cm1  = -1.0;
  C[0] = 3.0*(1.0-H[1])/(8.0*T[1]);
  C[1] = (-15.0*H[2]+6.0*H[1]+9.0+sgn*8.0*T[2])/(128.0*T[2]);
  C[2] = 3.0*(-35.0*H[3] - 15.0*H[2] + 15.0*H[1] + 35.0 + sgn*T[2]*(32.0*H[1]+8.0))/(1024.0*T[3]);
  C[3] = (-4725.0*H[4] - 6300.0*H[3] - 3150.0*H[2] + 3780.0*H[1] + 10395.0
          -1216.0*T[4] + sgn*T[2]*(6000.0*H[2]+5760.0*H[1]+1680.0)) / (32768.0*T[4]);
  C[4] = 7.0*(-10395.0*H[5] - 23625.0*H[4] - 28350.0*H[3] - 14850.0*H[2]
              +19305.0*H[1] + 57915.0 - T[4]*(6336.0*H[1]+6080.0)
              + sgn*T[2]*(16800.0*H[3] + 30000.0*H[2] + 25920.0*H[1] + 7920.0)
              ) / (262144.0*T[5]);
  C[5] = (-2837835.0*H[6] - 9168390.0*H[5] - 16372125.0*H[4] - 18918900*H[3]
          -10135125.0*H[2] + 13783770.0*H[1] + 43648605.0
          -T[4]*(3044160.0*H[2] + 5588352.0*H[1] + 4213440.0)
          +sgn*T[2]*(5556600.0*H[4] + 14817600.0*H[3] + 20790000.0*H[2]
                     + 17297280.0*H[1] + 5405400.0 + 323072.0*T[4]
                     )
          ) / (4194304.0*T[6]);
  C[6] = 0.0;

  *V0 = C[0] + (-4.0*C[3]/T[1]+C[4])/V[4]
             + (-192.0*C[5]/T[3]+144.0*C[6]/T[2])/V[8]
             + sgn * (-C[2]/V[2]
                      + (-24.0*C[4]/T[2]+12.0*C[5]/T[1]-C[6])/V[6]
                      );
  *V1 = C[1]/V[1] + (8.0*(C[3]/T[2]-C[4]/T[1])+C[5])/V[5]
                  + (384.0*C[5]/T[4] - 768.0*C[6]/T[3])/V[9]
                  + sgn * (Cm1*V[1] + (2.0*C[2]/T[1]-C[3])/V[3]
                           + (48.0*C[4]/T[3]-72.0*C[5]/T[2] + 18.0*C[6]/T[1])/V[7]
                           );

  return GSL_SUCCESS;
}

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

/* P^0_{-1/2 + I lambda}
 */
int
gsl_sf_conicalP_0_e(const double lambda, const double x, gsl_sf_result * result)
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0) {
    DOMAIN_ERROR(result);
  }
  else if(x == 1.0) {
    result->val = 1.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else if(lambda == 0.0) {
    gsl_sf_result K;
    int stat_K;
    if(x < 1.0) {
      const double th = acos(x);
      const double s  = sin(0.5*th);
      stat_K = gsl_sf_ellint_Kcomp_e(s, GSL_MODE_DEFAULT, &K);
      result->val  = 2.0/M_PI * K.val;
      result->err  = 2.0/M_PI * K.err;
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      return stat_K;
    }
    else {
      const double xi = acosh(x);
      const double c  = cosh(0.5*xi);
      const double t  = tanh(0.5*xi);
      stat_K = gsl_sf_ellint_Kcomp_e(t, GSL_MODE_DEFAULT, &K);
      result->val  = 2.0/M_PI / c * K.val;
      result->err  = 2.0/M_PI / c * K.err;
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      return stat_K;
    }
  }
  else if(   (x <= 0.0 && lambda < 1000.0)
          || (x <  0.1 && lambda < 17.0)
          || (x <  0.2 && lambda < 5.0 )
    ) {
    return conicalP_xlt1_hyperg_A(0.0, lambda, x, result);
  }
  else if(   (x <= 0.2 && lambda < 17.0)
          || (x <= 1.5 && lambda < 20.0)
    ) {
    return gsl_sf_hyperg_2F1_conj_e(0.5, lambda, 1.0, (1.0-x)/2, result);
  }
  else if(1.5 < x && lambda < GSL_MAX(x,20.0)) {
    gsl_sf_result P;
    double lm;
    int stat_P = gsl_sf_conicalP_large_x_e(0.0, lambda, x,
                                              &P, &lm
                                              );
    int stat_e = gsl_sf_exp_mult_err_e(lm, 2.0*GSL_DBL_EPSILON * fabs(lm),
                                          P.val, P.err,
                                          result);
    return GSL_ERROR_SELECT_2(stat_e, stat_P);
  }
  else {
    double V0, V1;
    if(x < 1.0) {
      double th  = acos(x);
      double sth = sqrt(1.0-x*x);  /* sin(th) */
      gsl_sf_result I0, I1;
      int stat_I0 = gsl_sf_bessel_I0_scaled_e(th * lambda, &I0);
      int stat_I1 = gsl_sf_bessel_I1_scaled_e(th * lambda, &I1);
      int stat_I  = GSL_ERROR_SELECT_2(stat_I0, stat_I1);
      int stat_V  = conicalP_0_V(th, x/sth, lambda, -1.0, &V0, &V1);
      double bessterm = V0 * I0.val + V1 * I1.val;
      double besserr  = fabs(V0) * I0.err + fabs(V1) * I1.err;
      double arg1 = th*lambda;
      double sqts = sqrt(th/sth);
      int stat_e = gsl_sf_exp_mult_err_e(arg1, 4.0 * GSL_DBL_EPSILON * fabs(arg1),
                                            sqts * bessterm, sqts * besserr,
                                            result);
      return GSL_ERROR_SELECT_3(stat_e, stat_V, stat_I);
    }
    else {
      double sh = sqrt(x-1.0)*sqrt(x+1.0);  /* sinh(xi)      */
      double xi = log(x + sh);              /* xi = acosh(x) */
      gsl_sf_result J0, J1;
      int stat_J0 = gsl_sf_bessel_J0_e(xi * lambda, &J0);
      int stat_J1 = gsl_sf_bessel_J1_e(xi * lambda, &J1);
      int stat_J  = GSL_ERROR_SELECT_2(stat_J0, stat_J1);
      int stat_V  = conicalP_0_V(xi, x/sh, lambda, 1.0, &V0, &V1);
      double bessterm = V0 * J0.val + V1 * J1.val;
      double besserr  = fabs(V0) * J0.err + fabs(V1) * J1.err;
      double pre_val = sqrt(xi/sh);
      double pre_err = 2.0 * fabs(pre_val);
      result->val  = pre_val * bessterm;
      result->err  = pre_val * besserr;
      result->err += pre_err * fabs(bessterm);
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      return GSL_ERROR_SELECT_2(stat_V, stat_J);
    }
  }
}

/* P^1_{-1/2 + I lambda}
 */
int
gsl_sf_conicalP_1_e(const double lambda, const double x, gsl_sf_result * result)
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0) {
    DOMAIN_ERROR(result);
  }
  else if(lambda == 0.0) {
    gsl_sf_result K, E;
    int stat_K, stat_E;
    if(x == 1.0) {
      result->val = 0.0;
      result->err = 0.0;
      return GSL_SUCCESS;
    }
    else if(x < 1.0) {
      if(1.0-x < GSL_SQRT_DBL_EPSILON) {
        double err_amp = GSL_MAX_DBL(1.0, 1.0/(GSL_DBL_EPSILON + fabs(1.0-x)));
        result->val = 0.25/M_SQRT2 * sqrt(1.0-x) * (1.0 + 5.0/16.0 * (1.0-x));
        result->err = err_amp * 3.0 * GSL_DBL_EPSILON * fabs(result->val);
        return GSL_SUCCESS;
      }
      else {
        const double th = acos(x);
        const double s  = sin(0.5*th);
        const double c2 = 1.0 - s*s;
        const double sth = sin(th);
        const double pre = 2.0/(M_PI*sth);
        stat_K = gsl_sf_ellint_Kcomp_e(s, GSL_MODE_DEFAULT, &K);
        stat_E = gsl_sf_ellint_Ecomp_e(s, GSL_MODE_DEFAULT, &E);
        result->val  = pre * (E.val - c2 * K.val);
        result->err  = pre * (E.err + fabs(c2) * K.err);
        result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
        return GSL_ERROR_SELECT_2(stat_K, stat_E);
      }
    }
    else {
      if(x-1.0 < GSL_SQRT_DBL_EPSILON) {
        double err_amp = GSL_MAX_DBL(1.0, 1.0/(GSL_DBL_EPSILON + fabs(1.0-x)));
        result->val = -0.25/M_SQRT2 * sqrt(x-1.0) * (1.0 - 5.0/16.0 * (x-1.0));
        result->err = err_amp * 3.0 * GSL_DBL_EPSILON * fabs(result->val);
        return GSL_SUCCESS;
      }
      else {
        const double xi = acosh(x);
        const double c  = cosh(0.5*xi);
        const double t  = tanh(0.5*xi);
        const double sxi = sinh(xi);
        const double pre = 2.0/(M_PI*sxi) * c;
        stat_K = gsl_sf_ellint_Kcomp_e(t, GSL_MODE_DEFAULT, &K);
        stat_E = gsl_sf_ellint_Ecomp_e(t, GSL_MODE_DEFAULT, &E);
        result->val  = pre * (E.val - K.val);
        result->err  = pre * (E.err + K.err);
        result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
        return GSL_ERROR_SELECT_2(stat_K, stat_E);
      }
    }
  }
  else if(   (x <= 0.0 && lambda < 1000.0)
          || (x <  0.1 && lambda < 17.0)
          || (x <  0.2 && lambda < 5.0 )
    ) {
    return conicalP_xlt1_hyperg_A(1.0, lambda, x, result);
  }
  else if(   (x <= 0.2 && lambda < 17.0)
          || (x <  1.5 && lambda < 20.0)
    ) {
    const double arg = fabs(x*x - 1.0);
    const double sgn = GSL_SIGN(1.0 - x);
    const double pre = 0.5*(lambda*lambda + 0.25) * sgn * sqrt(arg);
    gsl_sf_result F;
    int stat_F = gsl_sf_hyperg_2F1_conj_e(1.5, lambda, 2.0, (1.0-x)/2, &F);
    result->val  = pre * F.val;
    result->err  = fabs(pre) * F.err;
    result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    return stat_F;
  }
  else if(1.5 <= x && lambda < GSL_MAX(x,20.0)) {
    gsl_sf_result P;
    double lm;
    int stat_P = gsl_sf_conicalP_large_x_e(1.0, lambda, x,
                                              &P, &lm
                                              );
    int stat_e = gsl_sf_exp_mult_err_e(lm, 2.0 * GSL_DBL_EPSILON * fabs(lm),
                                          P.val, P.err,
                                          result);
    return GSL_ERROR_SELECT_2(stat_e, stat_P);
  }
  else {
    double V0, V1;
    if(x < 1.0) {
      const double sqrt_1mx = sqrt(1.0 - x);
      const double sqrt_1px = sqrt(1.0 + x);
      const double th  = acos(x);
      const double sth = sqrt_1mx * sqrt_1px;  /* sin(th) */
      gsl_sf_result I0, I1;
      int stat_I0 = gsl_sf_bessel_I0_scaled_e(th * lambda, &I0);
      int stat_I1 = gsl_sf_bessel_I1_scaled_e(th * lambda, &I1);
      int stat_I  = GSL_ERROR_SELECT_2(stat_I0, stat_I1);
      int stat_V  = conicalP_1_V(th, x/sth, lambda, -1.0, &V0, &V1);
      double bessterm = V0 * I0.val + V1 * I1.val;
      double besserr  =  fabs(V0) * I0.err + fabs(V1) * I1.err
                       + 2.0 * GSL_DBL_EPSILON * fabs(V0 * I0.val)
                       + 2.0 * GSL_DBL_EPSILON * fabs(V1 * I1.val);
      double arg1 = th * lambda;
      double sqts = sqrt(th/sth);
      int stat_e = gsl_sf_exp_mult_err_e(arg1, 2.0 * GSL_DBL_EPSILON * fabs(arg1),
                                            sqts * bessterm, sqts * besserr,
                                            result);
      result->err *= 1.0/sqrt_1mx;
      return GSL_ERROR_SELECT_3(stat_e, stat_V, stat_I);
    }
    else {
      const double sqrt_xm1 = sqrt(x - 1.0);
      const double sqrt_xp1 = sqrt(x + 1.0);
      const double sh = sqrt_xm1 * sqrt_xp1;  /* sinh(xi)      */
      const double xi = log(x + sh);          /* xi = acosh(x) */
      const double xi_lam = xi * lambda;
      gsl_sf_result J0, J1;
      const int stat_J0 = gsl_sf_bessel_J0_e(xi_lam, &J0);
      const int stat_J1 = gsl_sf_bessel_J1_e(xi_lam, &J1);
      const int stat_J  = GSL_ERROR_SELECT_2(stat_J0, stat_J1);
      const int stat_V  = conicalP_1_V(xi, x/sh, lambda, 1.0, &V0, &V1);
      const double bessterm = V0 * J0.val + V1 * J1.val;
      const double besserr  = fabs(V0) * J0.err + fabs(V1) * J1.err
                       + 512.0 * 2.0 * GSL_DBL_EPSILON * fabs(V0 * J0.val)
                       + 512.0 * 2.0 * GSL_DBL_EPSILON * fabs(V1 * J1.val)
                       + GSL_DBL_EPSILON * fabs(xi_lam * V0 * J1.val)
                       + GSL_DBL_EPSILON * fabs(xi_lam * V1 * J0.val);
      const double pre = sqrt(xi/sh);
      result->val  = pre * bessterm;
      result->err  = pre * besserr * sqrt_xp1 / sqrt_xm1;
      result->err += 4.0 * GSL_DBL_EPSILON * fabs(result->val);
      return GSL_ERROR_SELECT_2(stat_V, stat_J);
    }
  }
}

/* P^{1/2}_{-1/2 + I lambda} (x)
 * [Abramowitz+Stegun 8.6.8, 8.6.12]
 * checked OK [GJ] Fri May  8 12:24:36 MDT 1998
 */
int gsl_sf_conicalP_half_e(const double lambda, const double x,
                              gsl_sf_result * result
                              )
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0) {
    DOMAIN_ERROR(result);
  }
  else if(x < 1.0) {
    double err_amp = 1.0 + 1.0/(GSL_DBL_EPSILON + fabs(1.0-fabs(x)));
    double ac  = acos(x);
    double den = sqrt(sqrt(1.0-x)*sqrt(1.0+x));
    result->val  = Root_2OverPi_ / den * cosh(ac * lambda);
    result->err  = err_amp * 3.0 * GSL_DBL_EPSILON * fabs(result->val);
    result->err *= fabs(ac * lambda) + 1.0;
    return GSL_SUCCESS;
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else {
    /* x > 1 */
    double err_amp = 1.0 + 1.0/(GSL_DBL_EPSILON + fabs(1.0-fabs(x)));
    double sq_term = sqrt(x-1.0)*sqrt(x+1.0);
    double ln_term = log(x + sq_term);
    double den = sqrt(sq_term);
    double carg_val = lambda * ln_term;
    double carg_err = 2.0 * GSL_DBL_EPSILON * fabs(carg_val);
    gsl_sf_result cos_result;
    int stat_cos = gsl_sf_cos_err_e(carg_val, carg_err, &cos_result);
    result->val  = Root_2OverPi_ / den * cos_result.val;
    result->err  = err_amp * Root_2OverPi_ / den * cos_result.err;
    result->err += 4.0 * GSL_DBL_EPSILON * fabs(result->val);
    return stat_cos;
  }
}

/* P^{-1/2}_{-1/2 + I lambda} (x)
 * [Abramowitz+Stegun 8.6.9, 8.6.14]
 * checked OK [GJ] Fri May  8 12:24:43 MDT 1998
 */
int gsl_sf_conicalP_mhalf_e(const double lambda, const double x, gsl_sf_result * result)
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0) {
    DOMAIN_ERROR(result);
  }
  else if(x < 1.0) {
    double ac  = acos(x);
    double den = sqrt(sqrt(1.0-x)*sqrt(1.0+x));
    double arg = ac * lambda;
    double err_amp = 1.0 + 1.0/(GSL_DBL_EPSILON + fabs(1.0-fabs(x)));
    if(fabs(arg) < GSL_SQRT_DBL_EPSILON) {
      result->val  = Root_2OverPi_ / den * ac;
      result->err  = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      result->err *= err_amp;
    }
    else {
      result->val  = Root_2OverPi_ / (den*lambda) * sinh(arg);
      result->err  = GSL_DBL_EPSILON * (fabs(arg)+1.0) * fabs(result->val);
      result->err *= err_amp;
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    }
    return GSL_SUCCESS;
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else {
    /* x > 1 */
    double sq_term = sqrt(x-1.0)*sqrt(x+1.0);
    double ln_term = log(x + sq_term);
    double den = sqrt(sq_term);
    double arg_val = lambda * ln_term;
    double arg_err = 2.0 * GSL_DBL_EPSILON * fabs(arg_val);
    if(arg_val < GSL_SQRT_DBL_EPSILON) {
      result->val = Root_2OverPi_ / den * ln_term;
      result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
      return GSL_SUCCESS;
    }
    else {
      gsl_sf_result sin_result;
      int stat_sin = gsl_sf_sin_err_e(arg_val, arg_err, &sin_result);
      result->val  = Root_2OverPi_ / (den*lambda) * sin_result.val;
      result->err  = Root_2OverPi_ / fabs(den*lambda) * sin_result.err;
      result->err += 3.0 * GSL_DBL_EPSILON * fabs(result->val);
      return stat_sin;
    }
  }
}

int gsl_sf_conicalP_sph_reg_e(const int l, const double lambda,
                                 const double x,
                                 gsl_sf_result * result
                                 )
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0 || l < -1) {
    DOMAIN_ERROR(result);
  }
  else if(l == -1) {
    return gsl_sf_conicalP_half_e(lambda, x, result);
  }
  else if(l == 0) {
    return gsl_sf_conicalP_mhalf_e(lambda, x, result);
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else if(x < 0.0) {
    double c = 1.0/sqrt(1.0-x*x);
    gsl_sf_result r_Pellm1;
    gsl_sf_result r_Pell;
    int stat_0 = gsl_sf_conicalP_half_e(lambda, x, &r_Pellm1);  /* P^( 1/2) */
    int stat_1 = gsl_sf_conicalP_mhalf_e(lambda, x, &r_Pell);   /* P^(-1/2) */
    int stat_P = GSL_ERROR_SELECT_2(stat_0, stat_1);
    double Pellm1 = r_Pellm1.val;
    double Pell   = r_Pell.val;
    double Pellp1;
    int ell;

    for(ell=0; ell<l; ell++) {
      double d = (ell+1.0)*(ell+1.0) + lambda*lambda;
      Pellp1 = (Pellm1 - (2.0*ell+1.0)*c*x * Pell) / d;
      Pellm1 = Pell;
      Pell   = Pellp1;
    }

    result->val  = Pell;
    result->err  = (0.5*l + 1.0) * GSL_DBL_EPSILON * fabs(Pell);
    result->err += GSL_DBL_EPSILON * l * fabs(result->val);
    return stat_P;
  }
  else if(x < 1.0) {
    const double xi = x/(sqrt(1.0-x)*sqrt(1.0+x));
    gsl_sf_result rat;
    gsl_sf_result Phf;
    int stat_CF1 = conicalP_negmu_xlt1_CF1(0.5, l, lambda, x, &rat);
    int stat_Phf = gsl_sf_conicalP_half_e(lambda, x, &Phf);
    double Pellp1 = rat.val * GSL_SQRT_DBL_MIN;
    double Pell   = GSL_SQRT_DBL_MIN;
    double Pellm1;
    int ell;

    for(ell=l; ell>=0; ell--) {
      double d = (ell+1.0)*(ell+1.0) + lambda*lambda;
      Pellm1 = (2.0*ell+1.0)*xi * Pell + d * Pellp1;
      Pellp1 = Pell;
      Pell   = Pellm1;
    }

    result->val  = GSL_SQRT_DBL_MIN * Phf.val / Pell;
    result->err  = GSL_SQRT_DBL_MIN * Phf.err / fabs(Pell);
    result->err += fabs(rat.err/rat.val) * (l + 1.0) * fabs(result->val);
    result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);

    return GSL_ERROR_SELECT_2(stat_Phf, stat_CF1);
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else {
    /* x > 1.0 */

    const double xi = x/sqrt((x-1.0)*(x+1.0));
    gsl_sf_result rat;
    int stat_CF1 = conicalP_negmu_xgt1_CF1(0.5, l, lambda, x, &rat);
    int stat_P;
    double Pellp1 = rat.val * GSL_SQRT_DBL_MIN;
    double Pell   = GSL_SQRT_DBL_MIN;
    double Pellm1;
    int ell;

    for(ell=l; ell>=0; ell--) {
      double d = (ell+1.0)*(ell+1.0) + lambda*lambda;
      Pellm1 = (2.0*ell+1.0)*xi * Pell - d * Pellp1;
      Pellp1 = Pell;
      Pell   = Pellm1;
    }

    if(fabs(Pell) > fabs(Pellp1)){
      gsl_sf_result Phf;
      stat_P = gsl_sf_conicalP_half_e(lambda, x, &Phf);
      result->val  =       GSL_SQRT_DBL_MIN * Phf.val / Pell;
      result->err  = 2.0 * GSL_SQRT_DBL_MIN * Phf.err / fabs(Pell);
      result->err += 2.0 * fabs(rat.err/rat.val) * (l + 1.0) * fabs(result->val);
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    }
    else {
      gsl_sf_result Pmhf;
      stat_P = gsl_sf_conicalP_mhalf_e(lambda, x, &Pmhf);
      result->val  =       GSL_SQRT_DBL_MIN * Pmhf.val / Pellp1;
      result->err  = 2.0 * GSL_SQRT_DBL_MIN * Pmhf.err / fabs(Pellp1);
      result->err += 2.0 * fabs(rat.err/rat.val) * (l + 1.0) * fabs(result->val);
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    }

    return GSL_ERROR_SELECT_2(stat_P, stat_CF1);
  }
}

int gsl_sf_conicalP_cyl_reg_e(const int m, const double lambda,
                                 const double x,
                                 gsl_sf_result * result
                                 )
{
  /* CHECK_POINTER(result) */

  if(x <= -1.0 || m < -1) {
    DOMAIN_ERROR(result);
  }
  else if(m == -1) {
    return gsl_sf_conicalP_1_e(lambda, x, result);
  }
  else if(m == 0) {
    return gsl_sf_conicalP_0_e(lambda, x, result);
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else if(x < 0.0) {
    double c = 1.0/sqrt(1.0-x*x);
    gsl_sf_result r_Pkm1;
    gsl_sf_result r_Pk;
    int stat_0 = gsl_sf_conicalP_1_e(lambda, x, &r_Pkm1);  /* P^1 */
    int stat_1 = gsl_sf_conicalP_0_e(lambda, x, &r_Pk);    /* P^0 */
    int stat_P = GSL_ERROR_SELECT_2(stat_0, stat_1);
    double Pkm1 = r_Pkm1.val;
    double Pk   = r_Pk.val;
    double Pkp1;
    int k;

    for(k=0; k<m; k++) {
      double d = (k+0.5)*(k+0.5) + lambda*lambda;
      Pkp1 = (Pkm1 - 2.0*k*c*x * Pk) / d;
      Pkm1 = Pk;
      Pk   = Pkp1;
    }

    result->val  = Pk;
    result->err  = (m + 2.0) * GSL_DBL_EPSILON * fabs(Pk);
    result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);

    return stat_P;
  }
  else if(x < 1.0) {
    const double xi = x/(sqrt(1.0-x)*sqrt(1.0+x));
    gsl_sf_result rat;
    gsl_sf_result P0;
    int stat_CF1 = conicalP_negmu_xlt1_CF1(0.0, m, lambda, x, &rat);
    int stat_P0  = gsl_sf_conicalP_0_e(lambda, x, &P0);
    double Pkp1 = rat.val * GSL_SQRT_DBL_MIN;
    double Pk   = GSL_SQRT_DBL_MIN;
    double Pkm1;
    int k;

    for(k=m; k>0; k--) {
      double d = (k+0.5)*(k+0.5) + lambda*lambda;
      Pkm1 = 2.0*k*xi * Pk + d * Pkp1;
      Pkp1 = Pk;
      Pk   = Pkm1;
    }

    result->val  = GSL_SQRT_DBL_MIN * P0.val / Pk;
    result->err  = 2.0 * GSL_SQRT_DBL_MIN * P0.err / fabs(Pk);
    result->err += 2.0 * fabs(rat.err/rat.val) * (m + 1.0) * fabs(result->val);
    result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);

    return GSL_ERROR_SELECT_2(stat_P0, stat_CF1);
  }
  else if(x == 1.0) {
    result->val = 0.0;
    result->err = 0.0;
    return GSL_SUCCESS;
  }
  else {
    /* x > 1.0 */

    const double xi = x/sqrt((x-1.0)*(x+1.0));
    gsl_sf_result rat;
    int stat_CF1 = conicalP_negmu_xgt1_CF1(0.0, m, lambda, x, &rat);
    int stat_P;
    double Pkp1 = rat.val * GSL_SQRT_DBL_MIN;
    double Pk   = GSL_SQRT_DBL_MIN;
    double Pkm1;
    int k;

    for(k=m; k>-1; k--) {
      double d = (k+0.5)*(k+0.5) + lambda*lambda;
      Pkm1 = 2.0*k*xi * Pk - d * Pkp1;
      Pkp1 = Pk;
      Pk   = Pkm1;
    }

    if(fabs(Pk) > fabs(Pkp1)){
      gsl_sf_result P1;
      stat_P = gsl_sf_conicalP_1_e(lambda, x, &P1);
      result->val  = GSL_SQRT_DBL_MIN * P1.val / Pk;
      result->err  = 2.0 * GSL_SQRT_DBL_MIN * P1.err / fabs(Pk);
      result->err += 2.0 * fabs(rat.err/rat.val) * (m+2.0) * fabs(result->val);
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    }
    else {
      gsl_sf_result P0;
      stat_P = gsl_sf_conicalP_0_e(lambda, x, &P0);
      result->val  = GSL_SQRT_DBL_MIN * P0.val / Pkp1;
      result->err  = 2.0 * GSL_SQRT_DBL_MIN * P0.err / fabs(Pkp1);
      result->err += 2.0 * fabs(rat.err/rat.val) * (m+2.0) * fabs(result->val);
      result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
    }

    return GSL_ERROR_SELECT_2(stat_P, stat_CF1);
  }
}

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

#include "eval.h"

double gsl_sf_conicalP_0(const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_0_e(lambda, x, &result));
}

double gsl_sf_conicalP_1(const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_1_e(lambda, x, &result));
}

double gsl_sf_conicalP_half(const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_half_e(lambda, x, &result));
}

double gsl_sf_conicalP_mhalf(const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_mhalf_e(lambda, x, &result));
}

double gsl_sf_conicalP_sph_reg(const int l, const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_sph_reg_e(l, lambda, x, &result));
}

double gsl_sf_conicalP_cyl_reg(const int m, const double lambda, const double x)
{
  EVAL_RESULT(gsl_sf_conicalP_cyl_reg_e(m, lambda, x, &result));
}

ext: c

max_stars_repo_name: ielomariala/Hex-Game

max_issues_repo_name: Brian-ning/HMNE

max_forks_repo_name: Brian-ning/HMNE


text:
/**
  * THE BIG PICTURE
  *
  * This file:
  * 1) (libmtm.a, actually) loads and parses the input matrix
  * 2) iterates through a sequence of feature pairs specified in one of
  *    a variety of ways
  * 3) for each pair it calls a high-level analysis method that selects
  *    and executes an appropriate covariate analysis.
  * 4) routes the analysis results to either
  *    a) immediate output using a specified formatter or
  *    b) a cache to be post-processed (for FDR control) and subsequently
  *       output.
  *
  * Implementation notes:
  *   1) If row labels are present then, by default, they are parsed to
  *      infer the statistical class of the row. Moreover, the parser is
  *      one for "TCGA" format by default. This is so that Sheila et al.'s
  *      scripts need not pass an option for something they consider
  *      "standard." In order to minimize the danger of mis-interpreted
  *      row labels a conservative definition is used (by the
  *      mtm_sclass_by_prefix function). The first two characters of the row
  *      label must match /[BCDFNO][[:punct:]]/.
  *      Otherwise, mtm_sclass_by_prefix returns MTM_STATCLASS_UNKNOWN,
  *      and the class is inferred from the data content of the row.
  * Audit:
  *   1) All error exits return -1. When used as webservice proper status
  *      returns is the least we can do...
  *   2) stdout is ONLY used for data output (at verbosity 0)
  *   3) Analysis failures (because of degeneracy) still return something
  *      the web service can deal with. Striving for data-as-error,
  *      instead of a separate error channel.
  *   4) Insure mtm_resort is called before named rows are used.
  *   5) Every statistical test must fully initialize 1st 4 members of
  *      struct Statistic
  *   6) Casts, ALL casts!
  */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <alloca.h>

#include <gsl/gsl_errno.h>

#include "mtmatrix.h"
#include "mtheader.h"
#include "mtsclass.h"
#include "mterror.h"
#include "featpair.h"
#include "stattest.h"
#include "analysis.h"
#include "varfmt.h"
#include "fixfmt.h"
#include "limits.h"
#include "version.h"

#ifdef HAVE_LUA
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#endif

/***************************************************************************
 * externs
 */

extern int mtm_sclass_by_prefix( const char *token );

extern int get_base10_ints( FILE *fp, int *index, int n );

/***************************************************************************
 * Globals & statics
 */

static const char *MAGIC_SUFFIX         = "-www";
static const char *MAGIC_FORMAT_ID_STD  = "std";
static const char *MAGIC_FORMAT_ID_TCGA = "tcga";

static const char *TYPE_PARSER_INFER    = "auto";

const char *AUTHOR_EMAIL = "rkramer@systemsbiology.org";

/**
  * False Discovery Rate control state.
  */

static double arg_q_value = 0.0;
#define USE_FDR_CONTROL (arg_q_value > 0.0)

/**
  * This is used simply to preclude bloating the FDR cache with results
  * that can't possibly be relevant to the final BH-calculated p-value
  * threshold for FDR.
  * I'm setting this high for safety since it's just an optimization, but
  * it could probably be MUCH smaller.
  */
static double opt_fdr_cache_threshold = 0.5;

// No default on opt_script because looking for a "default.lua" script
// or any *.lua file invites all sorts of confusion with the defaults
// and precedence of other row selection methods.
static const char *opt_script          = NULL;
static bool        opt_header          = true;
static bool        opt_row_labels      = true;
static const char *opt_type_parser     = NULL;
static const char *opt_preproc_matrix  = NULL; // ...or optarg
static       char *opt_single_pair     = NULL; // non-const because it's split

static const char *opt_pairlist_source = NULL;

static const char *NO_ROW_LABELS       = "matrix has no row labels";
static bool        opt_by_name         = false;
#ifdef HAVE_LUA
static const char *DEFAULT_COROUTINE   = "pair_generator";
#endif
static const char *opt_coroutine       = NULL;
static bool        opt_dry_run         = false;
static const char *opt_format          = "tcga";
static bool     opt_warnings_are_fatal = false;

/**
  * Primary output and critical error messages.
  */
#define V_ESSENTIAL (1)

/**
  * Non-critical information that may, nonetheless, be helpful.
  */
#define V_WARNINGS  (2)

/**
  * Purely informational.
  */
#define V_INFO      (3)

static int opt_verbosity = V_ESSENTIAL;

#ifdef _DEBUG
static bool  dbg_silent               = false;
#endif

#define GLOBAL
GLOBAL unsigned  arg_min_cell_count   = 5;
GLOBAL unsigned  arg_min_mixb_count   = 1;
GLOBAL unsigned  arg_min_sample_count = 2; // < 2 NEVER makes sense
#undef  GLOBAL

static const char *opt_na_regex       = NULL; // must be initialized in main

static double      opt_p_value        = 1.0;

/**
  * Eventually it is (was) intended to support delegating row label
  * interpretation--in particular inference of a row's statistical class
  * based on its row label--to a user-provided Lua function.
  * Currently, only the default interpreter built into libmtm is actually
  * supported, which interprets row labels according to "ISB conventions."
  */
static MTM_ROW_LABEL_INTERPRETER _interpret_row_label = mtm_sclass_by_prefix;

static unsigned opt_status_mask       = COVAN_E_MASK;

#ifdef HAVE_LUA
static lua_State *_L = NULL;
/**
  * Delegate inference of a row's statistical class to a user-provided
  * function.
  */
int _lua_statclass_inference( const char *label ) {
	fprintf( stderr, "interpret %s\n", label );
	return MTM_STATCLASS_UNKNOWN;
}

static void _freeLua() {
	lua_close( _L );
}

/**
  * <source> may be literal Lua code or a filename reference.
  */
static int _load_script( const char *source, lua_State *state ) {

	struct stat info;
	return (access( source, R_OK ) == 0) && (stat( source, &info ) == 0)
		? luaL_dofile(   state, source )
		: luaL_dostring( state, source );
}

#endif

static FILE *_fp_output = NULL;
static void (*_emit)( EMITTER_SIG ) = format_tcga;

static bool _sigint_received = false;

/**
  * Records the number of pairwise tests COMPLETED WITHOUT ERROR
  * but not emitted (because they didn't pass the p-value threshold
  * for emission.
  *
  * This is NOT used when FDR control is active in pairwise; rather it is
  * for use in FDR control calculations *outside* the context of pairwise.
  *
  * Note that the total number of tests *attempted* is *not* separately
  * counted because it is assumed that that is known to the caller who
  * set up the run, after all.
  */
static unsigned _insignificant = 0;
static unsigned _untested      = 0;

/**
  * The binary matrix is accessed at runtime through this variable.
  * Notice, in particular, that the analysis code (analysis.cpp)
  * only has access to pairs of rows, NOT the whole matrix.
  * The matrix' structure and implementation is strictly encapsulated
  * within this file, and code in here dolls out just what analysis
  * requires: row pairs.
  */
static struct mtm_matrix _matrix;

static void _freeMatrix( void ) {
	_matrix.destroy( &_matrix );
}

static void _interrupt( int n ) {
	_sigint_received = true;
}

/***************************************************************************
 * Pipeline
 * A) explicit pairs
 *    1. row pair -> analysis -> filtering -> output
 * B) all-pairs or Lua generated pair lists
 *    1. row pair -> analysis -> filtering -> output
 *    2. row pair -> analysis -> fdr aggregation -> re-processing ->output
 */

/**
  * Analysis results on feature pairs are passed EITHER to:
  * 1. an emitter for immediate output (single pair only)
  * 2. a filter (and then possibly to an emitter)
  * 3. an aggregator (to be filtered and stored for later emission)
  *
  * All three of these methods must have the same signature.
  */
#define ANALYSIS_FN_SIG const struct feature_pair *pair

typedef void (*ANALYSIS_FN)( ANALYSIS_FN_SIG );

/***************************************************************************
 * Encapsulates all the decision making regarding actual emission of
 * results.
 */
static void _filter( ANALYSIS_FN_SIG ) {

	struct CovariateAnalysis covan;
	memset( &covan, 0, sizeof(covan) );
	covan_exec( pair, &covan );

	// One last thing to check before filtering to insure corner cases
	// don't fall through the following conditionals....

	if( ! ( isfinite( covan.result.probability ) && fpclassify( covan.result.probability ) != FP_SUBNORMAL ) ) {
		covan.result.probability = 1.0;
		covan.status             = COVAN_E_MATH;
		// The assumption is that if a NaN shows up in the result, it was
		// triggered by an un"pre"detected degeneracy, and so the pair was
		// in fact untestable (how it will be interpreted below).
	}

#ifdef _DEBUG
	if( ! dbg_silent ) {
#endif

		if( ( covan.status & opt_status_mask ) == 0 ) {
			if( covan.result.probability <= opt_p_value )
				_emit( pair, &covan, _fp_output );
			else
				_insignificant += 1;
		} else
			_untested += 1;

#ifdef _DEBUG
	}
#endif
}

static ANALYSIS_FN _analyze = _filter;

static void _error_handler(const char * reason,
                        const char * file,
                        int line,
                        int gsl_errno) {
	fprintf( stderr,
		"#GSL error(%d): %s\n"
		"#GSL error  at: %s:%d\n",
		gsl_errno, reason, file, line );
}

static FILE *_fdr_cache_fp = NULL;

/***************************************************************************
  * FDR processing
  * This currently involves some redundant computation in the interest of
  * simplicity. Since the whole point of FDR is to limit output, though, the
  * actual runtime cost should be bearabe. Specifically...
  *
  * 1. Two passes are made over the (selected) pairs.
  * 2. The offsets and p-value of the result of the first pass are cached
  *    in a tmp file.
  * 3. After completion, the p-value appropriate for the given q-value is
  *    determined and results from the 1st pass with sufficiently low
  *    p-values are recalculated and, this time, fully emitted.
  *
  * TODO: Optimization: Pairs with very high p-values (i.e. *clearly*
  * uninteresting pairs) can be omitted from the cache even in the first
  * pass and merely counted as tests since their recomputation will *almost*
  * certainly, depending ultimately on the threshold, not be required.
  */

struct FDRCacheRecord {
	double p;
	unsigned a,b;
} __attribute__((packed));
typedef struct FDRCacheRecord FDRCacheRecord_t;
typedef const FDRCacheRecord_t FDRCACHERECORD_T;

static int _cmp_fdr_cache_records( const void *pvl, const void *pvr ) {

	FDRCACHERECORD_T *l = (FDRCACHERECORD_T*)pvl;
	FDRCACHERECORD_T *r = (FDRCACHERECORD_T*)pvr;

	if( l->p == r->p )
		return  0;
	else
		return (l->p < r->p) ? -1 : +1;
}

static int _fdr_uncached_count = 0;

/**
  * Analyze the pair and cache just the offsets and p-value of the result
  * for possible recalculation during post-processing--after FDR control
  * has calculated an appropriate p-value threshold from the q-value.
  */
static void _fdr_cache( ANALYSIS_FN_SIG ) {

	struct CovariateAnalysis covan;
	memset( &covan, 0, sizeof(covan) );

	covan_exec( pair, &covan );

	// Failed tests (for reasons of one kind of degeneracy or another)
	// do not contribute to the calculation of the p-value threshold.

	if( covan.status == 0
		&& isfinite( covan.result.probability ) ) {

		// ...then, whatever the p-value, the test was at least
		// successfully *executed*.

		if( covan.result.probability <= opt_fdr_cache_threshold ) {
			struct FDRCacheRecord rec = {
				.p = covan.result.probability,
				.a = pair->l.offset,
				.b = pair->r.offset
			};
			fwrite( &rec, sizeof(rec), 1, _fdr_cache_fp );
		} else
			_fdr_uncached_count += 1;
	}
}

/**
  * This implements the Benjamini-Hochberg algorithm as described on
  * page 49 of "Large-Scale Inference", Bradley Efron, Cambridge.
  * "...for a fixed value of q in (0,1), let i_max be the largest
  *  index for which
  *                     p_(i) <= (i/N)q
  *
  *  ...and reject H_{0(i)}, the null hypothesis corresponding to
  *  p_(i), if
  *                         i <= i_max,
  *
  *  ...accepting H_{0(i)} otherwise."
  *
  * [...And i is 1-based in this notation!]
  */
static void _fdr_postprocess( FILE *cache, double Q, FILE *final_output, bool minimal_output ) {

	const unsigned CACHED_COUNT
		= ftell( cache ) / sizeof(struct FDRCacheRecord);
	const unsigned TESTED_COUNT
		= CACHED_COUNT
		+ _fdr_uncached_count;

	struct FDRCacheRecord *prec, *sortbuf
		= calloc( CACHED_COUNT, sizeof(struct FDRCacheRecord) );

	const double RATIO
		= Q/TESTED_COUNT;
	int i = 0;

	// Load and sort the cached test records...

	rewind( cache );
	fread( sortbuf, sizeof(struct FDRCacheRecord), CACHED_COUNT, cache );
	qsort( sortbuf, CACHED_COUNT, sizeof(struct FDRCacheRecord), _cmp_fdr_cache_records );

	// ...and recompute the full statistics of all earlier tests that
	// pass the now-established p-value threshold.

	prec = sortbuf;
	if( minimal_output ) {

		while( prec->p <= (i+1)*RATIO && i < CACHED_COUNT)
			fprintf( final_output, "%d\t%d\t%.3e\n", prec->a, prec->b, prec->p );

	} else {

		while( prec->p <= (i+1)*RATIO && i < CACHED_COUNT) {

			struct feature_pair fpair;
			struct CovariateAnalysis covan;
			memset( &covan, 0, sizeof(covan) );

			fpair.l.offset = prec->a;
			fpair.r.offset = prec->b;
			fetch_by_offset( &_matrix, &fpair );

			covan_exec( &fpair, &covan );

			// At this point emission is unconditional; FDR control has
			// already filtered all that will be filtered...

			_emit( &fpair, &covan, final_output );

			if( _sigint_received ) {
				time_t now = time(NULL);
				fprintf( stderr, "# FDR postprocess interrupted @ %s", ctime(&now) );
				break;
			}

			prec += 1;
			i    += 1;
		}
	}

	// However, the preceding loop exited

	if( opt_verbosity >= V_WARNINGS ) {
		if( i > 0 )
			fprintf( final_output, "# max p-value %.3f\n", sortbuf[i-1].p );
		else
			fprintf( final_output, "# no values passed FDR control\n" );
	}
	if( sortbuf )
		free( sortbuf );
}

/***************************************************************************
  * Row selection iterators
  * Each of these 5 methods takes arguments specific to the
  * source of input pairs and uses file static state for output.
  * All but the first delegate to the _analyze function which may be a
  * filter on immediate-mode output or a caching function for FDR.
  */

// BEGIN:RSI

static int /*ANCP*/ _analyze_cross_product(
		const struct mtm_matrix_header *hdr, FILE *fp[] ) {

	bool completed = true;
	struct feature_pair fpair;
	struct mtm_row *rrid;

	/**
	  * Location of left data won't change: same buffer, same offset for
	  * duration of iteration.
	  */
	fpair.l.data
		= calloc( _matrix.columns, sizeof(mtm_int_t) );

	/**
	  * TODO: I actually could enumerate the disk-resident matrix'
	  * row names just as I do the descriptors. In general, the
	  * pervasive assumption throughout this source that the input
	  * is exactly one matrix needs to be revisited.
	  */
	fpair.l.name = NULL;

	if( fpair.l.data == NULL )
		return -1;

	assert( ! _matrix.lexigraphic_order /* should be row order */ );

	for(fpair.l.offset = 0;
		fpair.l.offset < hdr->rows;
		fpair.l.offset++ ) {

		/**
		  * Read the "left" feature's data and descriptor
		  */

		if( fread( (void*)fpair.l.data, sizeof(mtm_int_t), hdr->columns,  fp[0] ) != hdr->columns )
			break;
		if( fread( &fpair.l.desc, sizeof(struct mtm_descriptor), 1, fp[1] ) != 1 )
			break;

		/**
		  * RAM-resident matrix is *fully* reset for each row of disk-
		  * resident matrix...
		  */

		fpair.r.data = _matrix.data;
		rrid         = _matrix.row_map; // may be NULL

		for(fpair.r.offset = 0;
			fpair.r.offset < _matrix.rows;
			fpair.r.offset++ ) {

			fpair.r.name = rrid ? rrid->string : "";
			fpair.r.desc = _matrix.desc[ fpair.r.offset ];

			_analyze( &fpair );

			if( _sigint_received ) {
				time_t now = time(NULL);
				fprintf( stderr, "# main analysis loop interrupted @ %s", ctime(&now) );
				completed = false;
				break;
			}

			fpair.r.data += _matrix.columns;
			if( rrid ) rrid += 1;

		} // inner for
	}

	if( ferror( fp[0] ) || ferror( fp[1] ) ) {
		warn( "reading row %d of preprocessed matrix", fpair.l.offset );
		completed = false;
	}

	if( fpair.l.data )
		free( (void*)fpair.l.data );

	return completed ? 0 : -1;
}

static bool _is_integer( const char *pc ) {
	while( *pc ) if( ! isdigit(*pc++) ) return false;
	return true;
}

static int _analyze_single_pair( const char *csv, const bool HAVE_ROW_LABELS ) {

	static const char *MISSING_MSG
		= "you specified row %s for a matrix without row names.\n";

	int econd;
	char *right, *left = alloca( strlen(csv)+1 );
	struct CovariateAnalysis covan;
	struct feature_pair pair;

	memset( &pair,  0, sizeof(pair) );
	memset( &covan, 0, sizeof(covan) );

	// Split the string in two...

	strcpy( left, csv );
	right = strchr( left, ',' );
	if( NULL == right )
		errx( -1, "missing comma separator in feature pair \"%s\"", left );
	*right++ = '\0';

	// Lookup each part.(They need not be the same format.)

	if( _is_integer( left ) ) {
		pair.l.offset = atoi( left );
		mtm_resort_rowmap( &_matrix, MTM_RESORT_BYROWOFFSET );
		econd = mtm_fetch_by_offset( &_matrix, &(pair.l) );
	} else
	if( HAVE_ROW_LABELS ) {
		pair.l.name   = left;
		if( mtm_resort_rowmap( &_matrix, MTM_RESORT_LEXIGRAPHIC ) )
			errx( -1, NO_ROW_LABELS );
		econd = mtm_fetch_by_name( &_matrix, &(pair.l) );
	} else
		errx( -1, MISSING_MSG, left );

	if( _is_integer( right ) ) {
		pair.r.offset = atoi( right );
		mtm_resort_rowmap( &_matrix, MTM_RESORT_BYROWOFFSET );
		econd = mtm_fetch_by_offset( &_matrix, &(pair.r) );
	} else
	if( HAVE_ROW_LABELS ) {
		pair.r.name = right;
		if( mtm_resort_rowmap( &_matrix, MTM_RESORT_LEXIGRAPHIC ) )
			errx( -1, NO_ROW_LABELS );
		econd = mtm_fetch_by_name( &_matrix, &(pair.r) );
	} else
		errx( -1, MISSING_MSG, right );

	covan_exec( &pair, &covan );

	_emit( &pair, &covan, _fp_output );

	return 0;
}

static int /*ANAM*/ _analyze_named_pair_list( FILE *fp ) {

	struct feature_pair fpair;

	size_t blen = 0;
	ssize_t llen;
	char *right, *left = NULL;

	while( ( llen = getline( &left, &blen, fp ) ) > 0 ) {

		// Trim off the newline char.

		if( left[llen-1] == '\n' )
			left[--llen] = '\0'; // Strip the NL

		// Parse the two names out of the line.

		right = strchr( left, '\t' );
		if( right )
			*right++ = '\0';
		else {
			fprintf( stderr, "error: no tab found in '%s'.\n", left );
			if( opt_warnings_are_fatal )
				break;
			else
				continue; // no reason we -can't- continue
		}

		fpair.l.name  = left;
		fpair.r.name = right;

		if( fetch_by_name( &_matrix, &fpair ) ) {

			warnx( "error: one or both of...\n"
				"\t1) %s\n"
				"\t2) %s\n"
				"\t...not found.\n",
				fpair.l.name,
				fpair.r.name );
			if( opt_warnings_are_fatal )
				break;
			else
				continue; // no reason we -can't- continue
		} else
			_analyze( &fpair );
	}

	if( left )
		free( left );

	return 0;
}

static int /*ANUM*/ _analyze_pair_list( FILE *fp ) {

	struct feature_pair fpair;

	int arr[2];

	while( get_base10_ints( fp, arr, 2 ) == 2 ) {

		fpair.l.offset = arr[0];
		fpair.r.offset = arr[1];

		if( fetch_by_offset( &_matrix, &fpair ) ) {
			warnx( "error: one of row indices (%d,%d) not in [0,%d)\n"
				"\tjust before byte offset %ld in the stream.\n"
				"\tAborting...\n",
				fpair.l.offset,
				fpair.r.offset,
				_matrix.rows,
				ftell( fp ) );
			if( opt_warnings_are_fatal )
				break;
			else
				continue; // no reason we -can't- continue
		} else
			_analyze( &fpair );
	}
	return 0;
}

#ifdef HAVE_LUA
static int /*ALUA*/ _analyze_generated_pair_list( lua_State *state ) {

	struct feature_pair fpair;
	int isnum, lua_status;

	lua_getglobal( _L, opt_coroutine );

	assert( ! lua_isnil( _L, -1 ) /* because it was checked early */ );

	do {

		lua_pushnumber( _L, _matrix.rows );
		lua_status = lua_resume( _L, NULL, 1 );

		if( lua_status == LUA_YIELD ) {

			fpair.r.offset = lua_tonumberx( _L, -1, &isnum );
			fpair.l.offset = lua_tonumberx( _L, -2, &isnum );
			lua_pop( _L, 2 );

			if( fetch_by_offset( &_matrix, &fpair ) ) {
				warnx( "one or both of %s-generated row indices (%d,%d) not in [0,%d)\n",
					opt_coroutine,
					fpair.l.offset,
					fpair.r.offset,
					_matrix.rows );
				if( opt_warnings_are_fatal )
					break;
				else
					continue; // no reason we -can't- continue
			} else
				_analyze( &fpair );

		} else
		if( lua_status == LUA_OK )
			break;
		else { // some sort of error occurred.
			fputs( lua_tostring( _L, -1 ), stderr );
		}

		if( _sigint_received ) {
			time_t now = time(NULL);
			fprintf( stderr, "analysis loop interrupted @ %s", ctime(&now) );
			break;
		}

	} while( lua_status == LUA_YIELD );

	return 0;
}
#endif

/**
  * This clause serves the primary use case motivating this
  * application: FAST, EXHAUSTIVE (n-choose-2) pairwise analysis.
  * As a result, the coding of this iteration schema is quite
  * different from the others. In particular:
  * 1. Since I -know- the order of feature pair evaluation I can
  *    preclude lots of useless work by entirely skipping outer loop
  * features with univariate degeneracy. This isn't feasible in
  * the other iteration sc
  * I'm dispensing with "pretty" and doing the
  * pointer arithmetic locally to eek out maximum possible speed.
  * In particular, I set up lots of ptrs below to obviate
  * redundant pointer arithmetic--only do fixed additions.
  */
static int /*AALL*/ _analyze_all_pairs( void ) {

	bool completed = true;
	struct feature_pair fpair;

	struct mtm_row *lrid, *rrid;

	assert( ! _matrix.lexigraphic_order /* should be row order */ );

	fpair.l.data = _matrix.data;
	lrid         = _matrix.row_map; // may be NULL

	for(fpair.l.offset = 0;
		fpair.l.offset < _matrix.rows;
		fpair.l.offset++ ) {

		fpair.l.name = lrid ? lrid->string : "";
		fpair.l.desc = _matrix.desc[ fpair.l.offset ];

		fpair.r.data = fpair.l.data + _matrix.columns;
		rrid = lrid ? lrid + 1 : NULL;

		for(fpair.r.offset = fpair.l.offset+1;
			fpair.r.offset < _matrix.rows;
			fpair.r.offset++ ) {

			fpair.r.name = rrid ? rrid->string : "";
			fpair.r.desc = _matrix.desc[ fpair.r.offset ];

			_analyze( &fpair );

			if( _sigint_received ) {
				time_t now = time(NULL);
				fprintf( stderr, "# main analysis loop interrupted @ %s", ctime(&now) );
				completed = false;
				break;
			}

			fpair.r.data += _matrix.columns;
			if( rrid ) rrid += 1;

		} // inner for

		fpair.l.data += _matrix.columns;
		if( lrid ) lrid += 1;
	}
	return completed ? 0 : -1;
}

// END:RSI

/**
  * Initializations for which static initialization can't/shouldn't
  * be relied upon. Common to executable and Python extension.
  */
static void _jit_initialization( void ) {
	opt_na_regex = mtm_default_NA_regex;
	memset( &_matrix,  0, sizeof(struct mtm_matrix) );
	gsl_set_error_handler( _error_handler );
}

/***************************************************************************
  * Online help
  */

static const char *_YN( bool y ) {
	return y ? "yes" : "no";
}

#define USAGE_SHORT false
#define USAGE_LONG  true

static void _print_usage( const char *exename, FILE *fp, bool exhaustive ) {

	extern const char *USAGE_UNABRIDGED;
	extern const char *USAGE_ABRIDGED;

	char debug_state[64];
	debug_state[0] = 0;
#ifdef _DEBUG
	sprintf( debug_state, "(DEBUG) Compiled %s %s", __DATE__,__TIME__ );
#endif

	if( exhaustive )
		fprintf( fp, USAGE_UNABRIDGED,
			exename, VER_MAJOR, VER_MINOR, VER_PATCH, VER_TAG, debug_state,
			exename,
			exename,
			TYPE_PARSER_INFER,
			opt_na_regex,
#ifdef HAVE_LUA
			DEFAULT_COROUTINE,
#endif
			arg_min_cell_count,
			arg_min_mixb_count,
			arg_min_sample_count,

			opt_p_value,
			opt_status_mask,
			opt_format,
			MAGIC_FORMAT_ID_STD, MAGIC_FORMAT_ID_TCGA,
			_YN(opt_warnings_are_fatal),
			opt_verbosity,
			MAGIC_SUFFIX,
			MAX_CATEGORY_COUNT,
			MTM_MAX_MISSING_VALUES,
			AUTHOR_EMAIL );
	else
		fprintf( fp, USAGE_ABRIDGED,
			exename, VER_MAJOR, VER_MINOR, VER_PATCH, VER_TAG, debug_state,
			exename,
			opt_p_value,
		  	AUTHOR_EMAIL );
}

int main( int argc, char *argv[] ) {

	int exit_status = EXIT_SUCCESS;

	const char *i_file = NULL;
	const char *o_file = NULL;
	FILE *fp           = NULL;

	if( argc < 2 ) { // absolute minimum args: <executable name> <input matrix>
		_print_usage( argv[0], stdout, USAGE_SHORT );
		exit( EXIT_SUCCESS );
	}

	_jit_initialization();

	do {

		static const char *CHAR_OPTIONS
#ifdef HAVE_LUA
			= "s:hrt:N:C:P:n:x:c:DM:p:f:q:v:?X";
#else
			= "hrt:N:C:P:n:x:DM:p:f:q:v:?X";
#endif

		static struct option LONG_OPTIONS[] = {
#ifdef HAVE_LUA
			{"script",        required_argument,  0,'s'},
#endif
			{"no-header",     no_argument,        0,'h'},
			{"no-row-labels", no_argument,        0,'r'},
			{"type-parser",   required_argument,  0,'t'},
			{"na-regex",      required_argument,  0,'N'},

			{"crossprod",     required_argument,  0,'C'},
			{"pair",          required_argument,  0,'P'},
			{"by-name",       required_argument,  0,'n'},
			{"by-index",      required_argument,  0,'x'},
#ifdef HAVE_LUA
			{"coroutine",     required_argument,  0,'c'},
#endif
			{"dry-run",       no_argument,        0,'D'},

			{"min-ct-cell",   required_argument,  0, 256 }, // no short equivalents
			{"min-mx-cell",   required_argument,  0, 257 }, // no short equivalents
			{"min-samples",   required_argument,  0,'M'},

			{"p-value",       required_argument,  0,'p'},
			{"format",        required_argument,  0,'f'},
			{"fdr",           required_argument,  0,'q'},
			{"verbosity",     required_argument,  0,'v'},
#ifdef _DEBUG
			{"debug",         required_argument,  0, 258 }, // no short equivalents
#endif
			{"help",          no_argument,        0,'?'},
			{ NULL,           0,                  0, 0 }
		};

		int arg_index = 0;
		const int c
			= getopt_long( argc, argv, CHAR_OPTIONS, LONG_OPTIONS, &arg_index );

		switch (c) {

		case 's': // script
			opt_script      = optarg;
			break;
		case 'h': // no-header
			opt_header      = false;
			break;
		case 'r': // no-row-labels
			opt_row_labels  = false;
			break;
		case 't': // type-parser
			opt_type_parser = optarg;
			break;
		case 'N': // na-regex
			opt_na_regex    = optarg;
			break;
		case 'C': // cross-product of matrices
			opt_preproc_matrix = optarg;
			break;
		case 'P': // pair
			opt_single_pair = optarg;
			break;
		case 'n': // by-name
			opt_pairlist_source = optarg;
			opt_by_name         = true;
			break;
		case 'x': // by-index
			opt_pairlist_source = optarg;
			opt_by_name         = false;
			break;
		case 'c': // coroutine
			opt_coroutine       = optarg;
			break;
		case 'D': // dry-run
			opt_dry_run         = true;
			break;

		////////////////////////////////////////////////////////////////////
		case 256: // ...because I haven't defined a short form for this
			arg_min_cell_count = atoi( optarg );
			break;

		case 257: // ...because I haven't defined a short form for this
			arg_min_mixb_count = atoi( optarg );
			break;

		case 'M':
			arg_min_sample_count = atoi( optarg );
			if( arg_min_sample_count < 2 ) {
				warnx( "Seriously...%d samples is acceptable?\n"
					"I don't think so... ;)\n",
					arg_min_sample_count );
				exit( EXIT_FAILURE );
			}
			break;
		////////////////////////////////////////////////////////////////////

		case 'p':
			opt_p_value = atof( optarg );
			if( ! ( 0 < opt_p_value ) ) {
				warnx( "specified p-value %.3f will preclude all output.\n",
					opt_p_value );
				abort();
			} else
			if( ! ( opt_p_value < 1.0 ) ) {
				warnx( "p-value %.3f will filter nothing.\n"
					"\tIs this really what you want?\n",
					opt_p_value );
			}
			break;

		case 'J': // JSON format
		case 'f': // tabular format
			// Check for magic-value strings first
			if( strcmp( MAGIC_FORMAT_ID_STD, optarg ) == 0 )
				_emit = format_standard;
			else
			if( strcmp( MAGIC_FORMAT_ID_TCGA, optarg ) == 0 )
				_emit = format_tcga;
			else {
				const char *specifier
					= emit_config( optarg, c=='J' ? FORMAT_JSON : FORMAT_TABULAR );
				if( specifier ) {
					errx( -1, "invalid specifier \"%s\"", specifier );
				}
				_emit = emit_exec;
			}
			break;

		case 'q':
			arg_q_value = atof( optarg );
			_analyze = _fdr_cache;
			break;

		case 'v': // verbosity
			opt_verbosity = atoi( optarg );
			break;

		case '?': // help
			_print_usage( argv[0], stdout, USAGE_SHORT );
			exit( EXIT_SUCCESS );
			break;

		case 'X': // help
			_print_usage( argv[0], stdout, USAGE_LONG );
			exit( EXIT_SUCCESS );
			break;

#ifdef _DEBUG
		case 258:
			if( strchr( optarg, 'X' ) != NULL ) {
				// Turn OFF all filtering to turn on "exhaustive"
				// output mode.
				opt_status_mask = 0;
				opt_p_value     = 1.0;
			}
			dbg_silent     = strchr( optarg, 'S' ) != NULL;
			break;
#endif
		case -1: // ...signals no more options.
			break;
		default:
			printf ("error: unknown option: %c\n", c );
			exit( EXIT_FAILURE );
		}
		if( -1 == c ) break;

	} while( true );

#ifdef HAVE_LUA

	if( opt_script ) {

		/**
		  * Create a Lua state machine and load/run the Lua script from file
		  * or command line before anything else.
		  */

		_L = luaL_newstate();
		if( _L ) {
			atexit( _freeLua ); // ...so lua_close needed NOWHERE else.
			luaL_openlibs( _L );
			if( _load_script( opt_script, _L ) != LUA_OK ) {
				errx( -1, "in \"%s\": %s",
					opt_script, lua_tostring( _L,-1) );
			}
		} else
			errx( -1, "failed creating Lua statespace" );

		/**
		  * Fail early!
		  * If the user provided a coroutine name, check for its presence.
		  * Otherwise, see if the default coroutine is present.
		  * Otherwise, Lua is not being used to generate pairs.
		  */

		if( opt_coroutine ) {

			lua_getglobal( _L, opt_coroutine );
			if( lua_isnil( _L, -1 ) ) {
				errx( -1, "pair generator coroutine \"%s\" not defined in \"%s\"",
					opt_coroutine, opt_script );
			}

		} else {

			lua_getglobal( _L, DEFAULT_COROUTINE );

			if( ! lua_isnil( _L, -1 ) )
				opt_coroutine = DEFAULT_COROUTINE;

			// Non-existence of DEFAULT_COROUTINE is not an error;
			// we assume user does NOT intend Lua to generate the pairs.
		}

		lua_pop( _L, 1 ); // clean the stack

		/**
		  * Similarly verify NOW that the a specified type parser is present.
		  */
		if( opt_type_parser ) {
			lua_getglobal( _L, opt_type_parser );
			if( lua_isnil( _L, -1 ) ) {
				errx( -1, "pair generator coroutine \"%s\" not defined in \"%s\"",
					opt_type_parser, opt_script );
			}
			lua_pop( _L, 1 ); // clean the stack
		}

		/**
		  * Load Lua script and execute a dry-run if applicable.
		  * This is in advance of other argument validation since other
		  * arguments will be ignored...
		  */

		if( opt_dry_run && opt_coroutine != NULL ) {

			const int COUNT
				= getenv("DRY_RUN_COUNT")
				? atoi( getenv("DRY_RUN_COUNT") )
				: 8;

			int isnum, lua_status = LUA_YIELD;

			lua_getglobal( _L, opt_coroutine );
			if( lua_isnil( _L, -1 ) )
				errx( -1, "%s not defined (in Lua's global namespace)",
					opt_coroutine );

			while( lua_status == LUA_YIELD ) {
				lua_pushnumber( _L, COUNT );
				lua_status = lua_resume( _L, NULL, 1 );
				if( lua_status <= LUA_YIELD /* OK == 0, YIELD == 1*/ ) {
					// Only output coroutine.yield'ed values...
					if( lua_status == LUA_YIELD ) {
						const int b = lua_tonumberx( _L, -1, &isnum );
						const int a = lua_tonumberx( _L, -2, &isnum );
						lua_pop( _L, 2 );
						fprintf( stdout, "%d %d\n", a, b );
					}
				} else
					fputs( lua_tostring( _L, -1 ), stderr );
			}

			exit( EXIT_SUCCESS );
		}
	} else
	if( opt_coroutine ) {
		errx( -1, "coroutine specified (\"%s\") but no script", opt_coroutine );
	}

#endif

	/**
	  * Catch simple argument inconsistencies and fail early!
	  */

	if( opt_single_pair ) {
		if( USE_FDR_CONTROL ) {
			warnx( "FDR is senseless on a single pair.\n" );
			if( opt_warnings_are_fatal )
				abort();
			else
				arg_q_value = 0.0;
		}
	}

	/**
	  * The last two positional arguments are expected to be filenames
	  * ... <filename1>
	  */

	static const char *NAME_STDIN  = "stdin";
	static const char *NAME_STDOUT = "stdout";

	switch( argc - optind ) {

	case 0: // input MUST be stdin, output stdout
		i_file = NAME_STDIN;
		o_file = NAME_STDOUT;
		break;

	case 1:
		if( access( argv[ optind ], R_OK ) == 0 ) {
			i_file = argv[ optind++ ];
			o_file = NAME_STDOUT;
		} else {
			i_file = NAME_STDIN;
			o_file = argv[ optind++ ];
		}
		break;

	case 2:
	default:
		i_file = argv[ optind++ ];
		o_file = argv[ optind++ ];
		if( (argc-optind) > 0 && opt_verbosity >= V_ESSENTIAL ) {
			// This behavior of ignoring "extra" arguments is implemented
			// ONLY so that this executable plays nicely with a job control
			// system which uses extra command line args NOT intended for
			// the executable. TODO: Revisit this!
			fprintf( stderr,
				"warning: ignoring %d trailing positional arguments.\n", argc-optind );
			while( optind < argc )
				fprintf( stderr, "\t\"%s\"\n", argv[optind++] );
			if( opt_warnings_are_fatal )
				exit( EXIT_FAILURE );
		}
	}

	if( opt_pairlist_source != NULL
			&& strcmp( opt_pairlist_source, i_file ) == 0 ) {
		errx( -1, "error: stdin specified (or implied) for both pair list and the input matrix\n" );
	}

	/**
	  * Emit intentions...
	  */

	if( opt_verbosity >= V_INFO ) {

#define MAXLEN_FS 75

		char feature_selection[MAXLEN_FS+1];
		feature_selection[MAXLEN_FS] = 0; // insure NUL termination

		/**
		  * Following conditional cascade must exactly match the larger
		  * one below in order to accurately report feature selection method.
		  */
		if( opt_single_pair ) {
			strncpy( feature_selection, opt_single_pair, MAXLEN_FS );
		} else
		if( opt_pairlist_source ) {
			snprintf( feature_selection,
				MAXLEN_FS,
				"by %s in %s",
				opt_by_name ? "name" : "offset", opt_pairlist_source );
		} else {
#ifdef HAVE_LUA
			if( opt_coroutine )
				snprintf( feature_selection,
					MAXLEN_FS,
					"%s in %s",
					opt_coroutine, opt_script /* may be literal source */ );
			else
#endif
				strncpy( feature_selection, "all-pairs", MAXLEN_FS );
		}

		fprintf( stderr,
			"       input: %s\n"
			"      select: %s\n"
			"      output: %s\n"
#ifdef _DEBUG
			"       debug: silent: %s\n"
#endif
			,i_file,
			feature_selection,
			o_file
#ifdef _DEBUG
			, _YN( dbg_silent )
#endif
			);
	}

	/**
	  * Load the input matrix.
	  */

	fp = strcmp( i_file, NAME_STDIN )
		? fopen( i_file, "r" )
		: stdin;
	if( fp ) {

		const unsigned int FLAGS
			= ( opt_header ? MTM_MATRIX_HAS_HEADER : 0 )
			| ( opt_row_labels ? MTM_MATRIX_HAS_ROW_NAMES : 0 )
			| ( opt_verbosity & MTM_VERBOSITY_MASK);

		const int econd
			= mtm_parse( fp,
				FLAGS,
				opt_na_regex,
				MAX_CATEGORY_COUNT,
				opt_row_labels ? _interpret_row_label : NULL,
				NULL, // ...since no persistent binary matrix is needed.
				&_matrix );
		fclose( fp );

		if( econd )
			errx( -1, "mtm_parse returned (%d)", econd );
		else
			atexit( _freeMatrix );
	}

	if( opt_dry_run ) { // a second possible
		exit( EXIT_SUCCESS );
	}

	if( SIG_ERR == signal( SIGINT, _interrupt ) ) {
		warn( "failed installing interrupt handler\n"
			"\tCtrl-C will terminated gracelessly\n" );
	}

	/**
	  * Choose and open, if necessary, an output stream.
	  */

	_fp_output
		= (strcmp( o_file, NAME_STDOUT ) == 0 )
		? stdout
		: fopen( o_file, "w" );

	if( NULL == _fp_output ) {
		err( -1, "opening output file \"%s\"", o_file );
	}

	if( covan_init( _matrix.columns ) ) {
		err( -1, "error: covan_init(%d)\n", _matrix.columns );
	} else
		atexit( covan_fini );

	if( opt_verbosity >= V_INFO )
		fprintf( _fp_output, "# %d rows/features X %d columns/samples\n", _matrix.rows, _matrix.columns );

	if( USE_FDR_CONTROL ) {
		_fdr_cache_fp = tmpfile();
		_fdr_uncached_count = 0;
		if( NULL == _fdr_cache_fp )
			err( -1, "creating a temporary file" );
	}

	/**
	  * Here the main decision is made regarding feature selection.
	  * In order of precedence:
	  * 0. cross-product of matrices
	  * 1. single pair
	  * 2. explicit pairs (by name or by offset)
	  * 3. Lua-generated offsets
	  * 4. all-pairs
	  */

	if( opt_preproc_matrix ) {
		FILE *ppm[2];
		ppm[0] = fopen( opt_preproc_matrix, "r" );
		if( ppm[0] ) {

			struct mtm_matrix_header hdr;

			/**
			  * Open a second stream on the file to access the
			  * descriptor table.
			  */
			ppm[1] = fopen( opt_preproc_matrix, "r" );

			/**
			  * Read the header and verify compatibility
			  */

			if( mtm_load_header( ppm[0], &hdr ) != MTM_OK )
				err( -1, "failed reading preprocessed matrix' (%s) header",
					opt_preproc_matrix );

			// Verify file is the preprocessed matrix the user thinks it is.

			if( strcmp( hdr.sig, MTM_SIGNATURE ) )
				errx( -1, "%s has wrong signature."
					"Are you sure this is a preprocessed matrix",
					opt_preproc_matrix );

			// Verify equality of columns

			if( _matrix.columns != hdr.columns )
				errx( -1, "processed matrix has %d columns; other has %d",
					hdr.columns, _matrix.columns );

			// Skip past the header to the row data.

			if( fseek( ppm[0], hdr.section[ S_DATA ].offset, SEEK_SET ) )
				err( -1, "failed seeking to start of data" );

			// Skip to the descriptor table in the 2nd stream.

			if( fseek( ppm[1], hdr.section[ S_DESC ].offset, SEEK_SET ) )
				err( -1, "failed seeking to start of data" );

			_analyze_cross_product( &hdr, ppm );

			fclose( ppm[1] );
			fclose( ppm[0] );
		}
	} else
	if( opt_single_pair ) {

		_analyze_single_pair( opt_single_pair, opt_row_labels );

	} else
	if( opt_pairlist_source ) {

		FILE *fp
			= strcmp(opt_pairlist_source,NAME_STDIN)
			? fopen( opt_pairlist_source, "r" )
			: stdin;

		if( opt_by_name ) {
			if( mtm_resort_rowmap( &_matrix, MTM_RESORT_LEXIGRAPHIC ) )
				errx( -1, NO_ROW_LABELS );
		}

		if( fp ) {
			const int econd
				= opt_by_name
				? _analyze_named_pair_list( fp )
				: _analyze_pair_list( fp );
			if( econd )
				warn( "error (%d) analyzing%s pair list",
					econd, opt_by_name ? " named" : "" );
			fclose( fp );
		} else
			warn( "opening \"%s\"", opt_pairlist_source );

	} else {

#ifdef HAVE_LUA
		if( opt_coroutine )
			_analyze_generated_pair_list( _L );
		else
#endif
			_analyze_all_pairs();
	}

	// Post process results if FDR is in effect and the 1st pass was
	// allowed to complete. (Post-processing involves a repetition of
	// analysis FOR A SUBSET of the original input.)

	if( USE_FDR_CONTROL && (! _sigint_received) ) {
		_fdr_postprocess( _fdr_cache_fp, arg_q_value, _fp_output, opt_preproc_matrix != NULL );
	} else
	if( opt_verbosity >= V_ESSENTIAL ) {
		fprintf( _fp_output,
				"# %d filtered for insignificance\n"
				"# %d filtered for some sort of degeneracy\n",
				_insignificant,
				_untested );
		// ...which does not apply in FDR control context.
	}

	if( _fdr_cache_fp )
		fclose( _fdr_cache_fp );

	if( _fp_output )
		fclose( _fp_output );

	return exit_status;
}

max_stars_repo_name: IlyaLab/kramtools

max_issues_repo_name: IlyaLab/kramtools

max_forks_repo_name: IlyaLab/kramtools


text:
/* statistics/gsl_statistics_ulong.h
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Jim Davies, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __GSL_STATISTICS_ULONG_H__
#define __GSL_STATISTICS_ULONG_H__

#include <stddef.h>
#include <gsl/gsl_types.h>

#undef __BEGIN_DECLS
#undef __END_DECLS
#ifdef __cplusplus
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
#else
# define __BEGIN_DECLS /* empty */
# define __END_DECLS /* empty */
#endif

__BEGIN_DECLS

GSL_EXPORT double gsl_stats_ulong_mean (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_variance (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_sd (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_variance_with_fixed_mean (const unsigned long data[], const size_t stride, const size_t n, const double mean);
GSL_EXPORT double gsl_stats_ulong_sd_with_fixed_mean (const unsigned long data[], const size_t stride, const size_t n, const double mean);
GSL_EXPORT double gsl_stats_ulong_absdev (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_skew (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_kurtosis (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT double gsl_stats_ulong_lag1_autocorrelation (const unsigned long data[], const size_t stride, const size_t n);

GSL_EXPORT double gsl_stats_ulong_covariance (const unsigned long data1[], const size_t stride1,const unsigned long data2[], const size_t stride2, const size_t n);

GSL_EXPORT double gsl_stats_ulong_variance_m (const unsigned long data[], const size_t stride, const size_t n, const double mean);
GSL_EXPORT double gsl_stats_ulong_sd_m (const unsigned long data[], const size_t stride, const size_t n, const double mean);
GSL_EXPORT double gsl_stats_ulong_absdev_m (const unsigned long data[], const size_t stride, const size_t n, const double mean);
GSL_EXPORT double gsl_stats_ulong_skew_m_sd (const unsigned long data[], const size_t stride, const size_t n, const double mean, const double sd);
GSL_EXPORT double gsl_stats_ulong_kurtosis_m_sd (const unsigned long data[], const size_t stride, const size_t n, const double mean, const double sd);
GSL_EXPORT double gsl_stats_ulong_lag1_autocorrelation_m (const unsigned long data[], const size_t stride, const size_t n, const double mean);

GSL_EXPORT double gsl_stats_ulong_covariance_m (const unsigned long data1[], const size_t stride1,const unsigned long data2[], const size_t stride2, const size_t n, const double mean1, const double mean2);

GSL_EXPORT double gsl_stats_ulong_pvariance (const unsigned long data1[], const size_t stride1, const size_t n1, const unsigned long data2[], const size_t stride2, const size_t n2);
GSL_EXPORT double gsl_stats_ulong_ttest (const unsigned long data1[], const size_t stride1, const size_t n1, const unsigned long data2[], const size_t stride2, const size_t n2);

GSL_EXPORT unsigned long gsl_stats_ulong_max (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT unsigned long gsl_stats_ulong_min (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT void gsl_stats_ulong_minmax (unsigned long * min, unsigned long * max, const unsigned long data[], const size_t stride, const size_t n);

GSL_EXPORT size_t gsl_stats_ulong_max_index (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT size_t gsl_stats_ulong_min_index (const unsigned long data[], const size_t stride, const size_t n);
GSL_EXPORT void gsl_stats_ulong_minmax_index (size_t * min_index, size_t * max_index, const unsigned long data[], const size_t stride, const size_t n);

GSL_EXPORT double gsl_stats_ulong_median_from_sorted_data (const unsigned long sorted_data[], const size_t stride, const size_t n) ;
GSL_EXPORT double gsl_stats_ulong_quantile_from_sorted_data (const unsigned long sorted_data[], const size_t stride, const size_t n, const double f) ;

__END_DECLS

#endif /* __GSL_STATISTICS_ULONG_H__ */

ext: h

max_stars_repo_name: dynaryu/vaws

max_issues_repo_name: dynaryu/vaws

max_forks_repo_name: dynaryu/vaws


text:
/*
 * C version of Diffusive Nested Sampling (DNest4) by Brendon J. Brewer
 *
 * Yan-Rong Li, liyanrong@mail.ihep.ac.cn
 * Jun 30, 2016
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <mpi.h>
#include <gsl/gsl_rng.h>

#include "dnest.h"
#include "model1.h"

int which_level_update;
int num_params;

DNestFptrSet *fptrset_thismodel;

void model1()
{
  int i, argc=0, narg=6;
  char **argv;

  argv = malloc(narg*sizeof(char *));
  for(i=0; i<narg; i++)
  {
    argv[i] = malloc(200*sizeof(char));
  }
  strcpy(argv[argc++], "dnest");
  strcpy(argv[argc++], "-s");
  strcpy(argv[argc++], "restart_dnest1.txt");
  strcpy(argv[argc++], "-l"); //level-dependnet sampling
  strcpy(argv[argc++], "-g");
  strcpy(argv[argc++], "1");

  /* setup szie of modeltype, which is used for dnest */
  num_params = 20;

  /* allocate memory */
  fptrset_thismodel = dnest_malloc_fptrset();

  /* setup functions used for dnest*/
  fptrset_thismodel->from_prior = from_prior_thismodel;
  fptrset_thismodel->log_likelihoods_cal = log_likelihoods_cal_thismodel;
  fptrset_thismodel->log_likelihoods_cal_initial = log_likelihoods_cal_thismodel;
  fptrset_thismodel->log_likelihoods_cal_restart = log_likelihoods_cal_thismodel;
  fptrset_thismodel->perturb = perturb_thismodel;
  fptrset_thismodel->print_particle = print_particle_thismodel;
  fptrset_thismodel->restart_action = restart_action_model1;

  /* run dnest */
  dnest(argc, argv, fptrset_thismodel, num_params, NULL, NULL, NULL, "./", "OPTIONS1", NULL, NULL);

  /* free memory */
  dnest_free_fptrset(fptrset_thismodel);

  for(i=0; i<narg; i++)
    free(argv[i]);
  free(argv);
}

/*====================================================*/
/* users responsible for following struct definitions */

void from_prior_thismodel(void *model)
{
  int i;
  double *params = (double *)model;
  for(i=0; i<num_params; i++)
  {
    params[i] = -0.5 + dnest_rand();
  }
}

double log_likelihoods_cal_thismodel(const void *model)
{
  double *params = (double *)model;
  double logL;
  const double u = 0.01;
	const double v = 0.1;
	const double C = log(1.0/sqrt(2*M_PI));

	double logl1 = num_params*(C - log(u));
	double logl2 = num_params*(C - log(v));

  int i;
	for(i=0; i<num_params; i++)
	{
		logl1 += -0.5*pow(( params[i] - 0.031)/u, 2);
		logl2 += -0.5*pow(  params[i]/v, 2);
	}
	logl1 += log(100.);

  double max = fmax(logl1, logl2);

  logL = log( exp(logl1-max) + exp(logl2-max) ) + max;

  return logL;
}

double perturb_thismodel(void *model)
{
  double *params = (double *)model;
  double logH = 0.0, width, limit1, limit2;
  int which = dnest_rand_int(num_params), which_level;
  int size_levels;

  which_level_update = dnest_get_which_level_update();
  size_levels = dnest_get_size_levels();
  which_level = which_level_update > (size_levels - 20)?(size_levels-20):which_level_update;
  if(which_level > 0)
  {
    limit1 = limits[(which_level-1) * num_params *2 + which *2 ];
    limit2 = limits[(which_level-1) * num_params *2 + which *2 + 1];
  }
  else
  {
    limit1 = -0.5;
    limit2 = 0.5;
  }

  width = (limit2 - limit1);
	params[which] += width * dnest_randh();
	dnest_wrap(&params[which], limit1, limit2);
  return logH;
}

void print_particle_thismodel(FILE *fp, const void *model)
{
  int i;
  double *params = (double *)model;
  for(i=0; i<num_params; i++)
  {
    fprintf(fp, "%f ", params[i] );
  }
  fprintf(fp, "\n");
}

/*========================================================*/

void restart_action_model1(int iflag)
{
  return;
}

max_stars_repo_name: LiyrAstroph/CDNest