/* Generated by Pyrex 0.9.8.5 on Mon Jun 15 13:04:31 2009 */

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "structmember.h"
#ifndef PY_LONG_LONG
  #define PY_LONG_LONG LONG_LONG
#endif
#if PY_VERSION_HEX < 0x02050000
  typedef int Py_ssize_t;
  #define PY_SSIZE_T_MAX INT_MAX
  #define PY_SSIZE_T_MIN INT_MIN
  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
  #define PyInt_AsSsize_t(o)	PyInt_AsLong(o)
#endif
#if !defined(WIN32) && !defined(MS_WINDOWS)
  #ifndef __stdcall
    #define __stdcall
  #endif
  #ifndef __cdecl
    #define __cdecl
  #endif
#endif
#ifdef __cplusplus
#define __PYX_EXTERN_C extern "C"
#else
#define __PYX_EXTERN_C extern
#endif
#include <math.h>
#include "array_interface.h"
#include "math.h"


typedef struct {PyObject **p; int i; char *s; long n;} __Pyx_StringTabEntry; /*proto*/

static PyObject *__pyx_m;
static PyObject *__pyx_b;
static int __pyx_lineno;
static char *__pyx_filename;
static char **__pyx_f;

static void __Pyx_WriteUnraisable(char *name); /*proto*/

static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/

static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/

static PyObject *__Pyx_GetItemInt(PyObject *o, Py_ssize_t i); /*proto*/

static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/

static void __Pyx_AddTraceback(char *funcname); /*proto*/

/* Declarations from cogent.evolve._likelihood_tree */


/* Declarations from implementation of cogent.evolve._likelihood_tree */

typedef PyObject *__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType;

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_uncheckedArrayDouble(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType); /*proto*/
static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,char,int,int,int **); /*proto*/
static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,char,int,int *); /*proto*/
static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,char,int,int *,int *); /*proto*/
static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,char,int,int *,int *,int *); /*proto*/
static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,char,int,int *,int *,int *,int *); /*proto*/
static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *); /*proto*/
static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *); /*proto*/
static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *,int *); /*proto*/
static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *,int *,int *); /*proto*/
static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *); /*proto*/
static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *); /*proto*/
static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *,int *); /*proto*/
static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType,int *,int *,int *,int *); /*proto*/

static char __pyx_k1[] = "__array_struct__";
static char __pyx_k2[] = "Array required, got None";
static char __pyx_k3[] = "Unexpected array interface version %s";
static char __pyx_k4[] = "'%s' type array required, got '%s'";
static char __pyx_k5[] = "chr";
static char __pyx_k6[] = "'%s%s' type array required, got '%s%s'";
static char __pyx_k7[] = "%s dimensional array required, got %s";
static char __pyx_k8[] = "Noncontiguous array";
static char __pyx_k9[] = "Dimension %s is %s, expected %s";
static char __pyx_k10[] = "copy";
static char __pyx_k11[] = "('1', '3')";
static char __pyx_k12[] = "__version__";
static char __pyx_k13[] = "version_info";

static PyObject *__pyx_n___array_struct__;
static PyObject *__pyx_n___version__;
static PyObject *__pyx_n_chr;
static PyObject *__pyx_n_copy;
static PyObject *__pyx_n_version_info;

static PyObject *__pyx_k2p;
static PyObject *__pyx_k3p;
static PyObject *__pyx_k4p;
static PyObject *__pyx_k6p;
static PyObject *__pyx_k7p;
static PyObject *__pyx_k8p;
static PyObject *__pyx_k9p;
static PyObject *__pyx_k11p;

static __Pyx_StringTabEntry __pyx_string_tab[] = {
  {&__pyx_n___array_struct__, 1, __pyx_k1, sizeof(__pyx_k1)},
  {&__pyx_n___version__, 1, __pyx_k12, sizeof(__pyx_k12)},
  {&__pyx_n_chr, 1, __pyx_k5, sizeof(__pyx_k5)},
  {&__pyx_n_copy, 1, __pyx_k10, sizeof(__pyx_k10)},
  {&__pyx_n_version_info, 1, __pyx_k13, sizeof(__pyx_k13)},
  {&__pyx_k2p, 0, __pyx_k2, sizeof(__pyx_k2)},
  {&__pyx_k3p, 0, __pyx_k3, sizeof(__pyx_k3)},
  {&__pyx_k4p, 0, __pyx_k4, sizeof(__pyx_k4)},
  {&__pyx_k6p, 0, __pyx_k6, sizeof(__pyx_k6)},
  {&__pyx_k7p, 0, __pyx_k7, sizeof(__pyx_k7)},
  {&__pyx_k8p, 0, __pyx_k8, sizeof(__pyx_k8)},
  {&__pyx_k9p, 0, __pyx_k9, sizeof(__pyx_k9)},
  {&__pyx_k11p, 0, __pyx_k11, sizeof(__pyx_k11)},
  {0, 0, 0, 0}
};



/* Implementation of cogent.evolve._likelihood_tree */

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_uncheckedArrayDouble(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_A) {
  struct PyArrayInterface *__pyx_v_a;
  PyObject *__pyx_v_cobj;
  double *__pyx_r;
  PyObject *__pyx_1 = 0;
  Py_INCREF(__pyx_v_A);
  __pyx_v_cobj = Py_None; Py_INCREF(Py_None);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":34 */
  __pyx_1 = PyObject_GetAttr(__pyx_v_A, __pyx_n___array_struct__); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
  Py_DECREF(__pyx_v_cobj);
  __pyx_v_cobj = __pyx_1;
  __pyx_1 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":35 */
  __pyx_v_a = ((struct PyArrayInterface *)PyCObject_AsVoidPtr(__pyx_v_cobj));

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":36 */
  __pyx_r = ((double *)__pyx_v_a->data);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_1);
  __Pyx_WriteUnraisable("cogent.evolve._likelihood_tree.uncheckedArrayDouble");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_cobj);
  Py_DECREF(__pyx_v_A);
  return __pyx_r;
}

static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_A,char __pyx_v_typecode,int __pyx_v_itemsize,int __pyx_v_nd,int **__pyx_v_dims) {
  struct PyArrayInterface *__pyx_v_a;
  PyObject *__pyx_v_cobj;
  char __pyx_v_typecode2;
  int __pyx_v_dimension;
  int __pyx_v_val;
  int *__pyx_v_var;
  void *__pyx_r;
  int __pyx_1;
  PyObject *__pyx_2 = 0;
  PyObject *__pyx_3 = 0;
  PyObject *__pyx_4 = 0;
  PyObject *__pyx_5 = 0;
  PyObject *__pyx_6 = 0;
  Py_INCREF(__pyx_v_A);
  __pyx_v_cobj = Py_None; Py_INCREF(Py_None);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":43 */
  __pyx_1 = __pyx_v_A == Py_None;
  if (__pyx_1) {
    __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
    Py_INCREF(__pyx_k2p);
    PyTuple_SET_ITEM(__pyx_2, 0, __pyx_k2p);
    __pyx_3 = PyObject_CallObject(PyExc_TypeError, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    __Pyx_Raise(__pyx_3, 0, 0);
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
    goto __pyx_L2;
  }
  __pyx_L2:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":45 */
  __pyx_2 = PyObject_GetAttr(__pyx_v_A, __pyx_n___array_struct__); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; goto __pyx_L1;}
  Py_DECREF(__pyx_v_cobj);
  __pyx_v_cobj = __pyx_2;
  __pyx_2 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":46 */
  __pyx_v_a = ((struct PyArrayInterface *)PyCObject_AsVoidPtr(__pyx_v_cobj));

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":47 */
  __pyx_1 = (__pyx_v_a->version != 2);
  if (__pyx_1) {
    __pyx_3 = PyInt_FromLong(__pyx_v_a->version); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; goto __pyx_L1;}
    __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_2, 0, __pyx_3);
    __pyx_3 = 0;
    __pyx_3 = PyObject_CallObject(((PyObject *)(&PyString_Type)), __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; goto __pyx_L1;}
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    __pyx_2 = PyNumber_Remainder(__pyx_k3p, __pyx_3); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_2);
    __pyx_2 = 0;
    __pyx_2 = PyObject_CallObject(PyExc_ValueError, __pyx_3); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __Pyx_Raise(__pyx_2, 0, 0);
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; goto __pyx_L1;}
    goto __pyx_L3;
  }
  __pyx_L3:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":51 */
  __pyx_v_typecode2 = __pyx_v_a->typekind;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":52 */
  __pyx_1 = (__pyx_v_typecode2 != __pyx_v_typecode);
  if (__pyx_1) {
    __pyx_3 = __Pyx_GetName(__pyx_b, __pyx_n_chr); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    __pyx_2 = PyInt_FromLong(__pyx_v_typecode); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    __pyx_4 = PyTuple_New(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_4, 0, __pyx_2);
    __pyx_2 = 0;
    __pyx_2 = PyObject_CallObject(__pyx_3, __pyx_4); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    __pyx_3 = __Pyx_GetName(__pyx_b, __pyx_n_chr); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    __pyx_4 = PyInt_FromLong(__pyx_v_typecode2); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    __pyx_5 = PyTuple_New(1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_5, 0, __pyx_4);
    __pyx_4 = 0;
    __pyx_4 = PyObject_CallObject(__pyx_3, __pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    __pyx_3 = PyTuple_New(2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_2);
    PyTuple_SET_ITEM(__pyx_3, 1, __pyx_4);
    __pyx_2 = 0;
    __pyx_4 = 0;
    __pyx_5 = PyNumber_Remainder(__pyx_k4p, __pyx_3); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_2, 0, __pyx_5);
    __pyx_5 = 0;
    __pyx_4 = PyObject_CallObject(PyExc_TypeError, __pyx_2); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    __Pyx_Raise(__pyx_4, 0, 0);
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
    goto __pyx_L4;
  }
  __pyx_L4:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":55 */
  __pyx_1 = (__pyx_v_a->itemsize != __pyx_v_itemsize);
  if (__pyx_1) {
    __pyx_3 = __Pyx_GetName(__pyx_b, __pyx_n_chr); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_5 = PyInt_FromLong(__pyx_v_typecode); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_2, 0, __pyx_5);
    __pyx_5 = 0;
    __pyx_4 = PyObject_CallObject(__pyx_3, __pyx_2); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    __pyx_5 = PyInt_FromLong(__pyx_v_itemsize); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_3 = __Pyx_GetName(__pyx_b, __pyx_n_chr); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_2 = PyInt_FromLong(__pyx_v_typecode2); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_6 = PyTuple_New(1); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_6, 0, __pyx_2);
    __pyx_2 = 0;
    __pyx_2 = PyObject_CallObject(__pyx_3, __pyx_6); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    Py_DECREF(__pyx_6); __pyx_6 = 0;
    __pyx_3 = PyInt_FromLong(__pyx_v_a->itemsize); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    __pyx_6 = PyTuple_New(4); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_6, 0, __pyx_4);
    PyTuple_SET_ITEM(__pyx_6, 1, __pyx_5);
    PyTuple_SET_ITEM(__pyx_6, 2, __pyx_2);
    PyTuple_SET_ITEM(__pyx_6, 3, __pyx_3);
    __pyx_4 = 0;
    __pyx_5 = 0;
    __pyx_2 = 0;
    __pyx_3 = 0;
    __pyx_4 = PyNumber_Remainder(__pyx_k6p, __pyx_6); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; goto __pyx_L1;}
    Py_DECREF(__pyx_6); __pyx_6 = 0;
    __pyx_5 = PyTuple_New(1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_5, 0, __pyx_4);
    __pyx_4 = 0;
    __pyx_2 = PyObject_CallObject(PyExc_TypeError, __pyx_5); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; goto __pyx_L1;}
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    __Pyx_Raise(__pyx_2, 0, 0);
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; goto __pyx_L1;}
    goto __pyx_L5;
  }
  __pyx_L5:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":58 */
  __pyx_1 = (__pyx_v_a->nd != __pyx_v_nd);
  if (__pyx_1) {
    __pyx_3 = PyInt_FromLong(__pyx_v_nd); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; goto __pyx_L1;}
    __pyx_6 = PyInt_FromLong(__pyx_v_a->nd); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; goto __pyx_L1;}
    __pyx_4 = PyTuple_New(2); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_4, 0, __pyx_3);
    PyTuple_SET_ITEM(__pyx_4, 1, __pyx_6);
    __pyx_3 = 0;
    __pyx_6 = 0;
    __pyx_5 = PyNumber_Remainder(__pyx_k7p, __pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_2, 0, __pyx_5);
    __pyx_5 = 0;
    __pyx_3 = PyObject_CallObject(PyExc_ValueError, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    __Pyx_Raise(__pyx_3, 0, 0);
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
    goto __pyx_L6;
  }
  __pyx_L6:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":61 */
  __pyx_1 = (!(__pyx_v_a->flags & CONTIGUOUS));
  if (__pyx_1) {
    __pyx_6 = PyTuple_New(1); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; goto __pyx_L1;}
    Py_INCREF(__pyx_k8p);
    PyTuple_SET_ITEM(__pyx_6, 0, __pyx_k8p);
    __pyx_4 = PyObject_CallObject(PyExc_ValueError, __pyx_6); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; goto __pyx_L1;}
    Py_DECREF(__pyx_6); __pyx_6 = 0;
    __Pyx_Raise(__pyx_4, 0, 0);
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; goto __pyx_L1;}
    goto __pyx_L7;
  }
  __pyx_L7:;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":66 */
  for (__pyx_v_dimension = 0; __pyx_v_dimension < __pyx_v_nd; ++__pyx_v_dimension) {

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":67 */
    __pyx_v_val = (__pyx_v_a->shape[__pyx_v_dimension]);

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":68 */
    __pyx_v_var = (__pyx_v_dims[__pyx_v_dimension]);

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":69 */
    __pyx_1 = ((__pyx_v_var[0]) == 0);
    if (__pyx_1) {
      (__pyx_v_var[0]) = __pyx_v_val;
      goto __pyx_L10;
    }
    __pyx_1 = ((__pyx_v_var[0]) != __pyx_v_val);
    if (__pyx_1) {
      __pyx_5 = PyInt_FromLong(__pyx_v_dimension); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; goto __pyx_L1;}
      __pyx_2 = PyInt_FromLong(__pyx_v_val); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; goto __pyx_L1;}
      __pyx_3 = PyInt_FromLong((__pyx_v_var[0])); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; goto __pyx_L1;}
      __pyx_6 = PyTuple_New(3); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_6, 0, __pyx_5);
      PyTuple_SET_ITEM(__pyx_6, 1, __pyx_2);
      PyTuple_SET_ITEM(__pyx_6, 2, __pyx_3);
      __pyx_5 = 0;
      __pyx_2 = 0;
      __pyx_3 = 0;
      __pyx_4 = PyNumber_Remainder(__pyx_k9p, __pyx_6); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; goto __pyx_L1;}
      Py_DECREF(__pyx_6); __pyx_6 = 0;
      __pyx_5 = PyTuple_New(1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_5, 0, __pyx_4);
      __pyx_4 = 0;
      __pyx_2 = PyObject_CallObject(PyExc_ValueError, __pyx_5); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; goto __pyx_L1;}
      Py_DECREF(__pyx_5); __pyx_5 = 0;
      __Pyx_Raise(__pyx_2, 0, 0);
      Py_DECREF(__pyx_2); __pyx_2 = 0;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; goto __pyx_L1;}
      goto __pyx_L10;
    }
    /*else*/ {
    }
    __pyx_L10:;
  }

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":79 */
  __pyx_r = __pyx_v_a->data;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_2);
  Py_XDECREF(__pyx_3);
  Py_XDECREF(__pyx_4);
  Py_XDECREF(__pyx_5);
  Py_XDECREF(__pyx_6);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArray");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_cobj);
  Py_DECREF(__pyx_v_A);
  return __pyx_r;
}

static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,char __pyx_v_typecode,int __pyx_v_size,int *__pyx_v_x) {
  int *__pyx_v_dims[1];
  void *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":85 */
  (__pyx_v_dims[0]) = __pyx_v_x;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":86 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_v_a,__pyx_v_typecode,__pyx_v_size,1,__pyx_v_dims); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; goto __pyx_L1;}
  __pyx_r = __pyx_1;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArray1D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,char __pyx_v_typecode,int __pyx_v_size,int *__pyx_v_x,int *__pyx_v_y) {
  int *__pyx_v_dims[2];
  void *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":91 */
  (__pyx_v_dims[0]) = __pyx_v_x;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":92 */
  (__pyx_v_dims[1]) = __pyx_v_y;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":93 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_v_a,__pyx_v_typecode,__pyx_v_size,2,__pyx_v_dims); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; goto __pyx_L1;}
  __pyx_r = __pyx_1;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArray2D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,char __pyx_v_typecode,int __pyx_v_size,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  int *__pyx_v_dims[3];
  void *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":98 */
  (__pyx_v_dims[0]) = __pyx_v_x;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":99 */
  (__pyx_v_dims[1]) = __pyx_v_y;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":100 */
  (__pyx_v_dims[2]) = __pyx_v_z;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":101 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_v_a,__pyx_v_typecode,__pyx_v_size,3,__pyx_v_dims); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 101; goto __pyx_L1;}
  __pyx_r = __pyx_1;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArray3D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static void *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,char __pyx_v_typecode,int __pyx_v_size,int *__pyx_v_w,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  int *__pyx_v_dims[4];
  void *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":106 */
  (__pyx_v_dims[0]) = __pyx_v_w;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":107 */
  (__pyx_v_dims[1]) = __pyx_v_x;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":108 */
  (__pyx_v_dims[2]) = __pyx_v_y;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":109 */
  (__pyx_v_dims[3]) = __pyx_v_z;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":110 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray(__pyx_v_a,__pyx_v_typecode,__pyx_v_size,4,__pyx_v_dims); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; goto __pyx_L1;}
  __pyx_r = __pyx_1;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArray4D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x) {
  double *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray1D(__pyx_v_a,'f',(sizeof(double)),__pyx_v_x); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; goto __pyx_L1;}
  __pyx_r = ((double *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayDouble1D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x,int *__pyx_v_y) {
  double *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray2D(__pyx_v_a,'f',(sizeof(double)),__pyx_v_x,__pyx_v_y); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 117; goto __pyx_L1;}
  __pyx_r = ((double *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayDouble2D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  double *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray3D(__pyx_v_a,'f',(sizeof(double)),__pyx_v_x,__pyx_v_y,__pyx_v_z); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; goto __pyx_L1;}
  __pyx_r = ((double *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayDouble3D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static double *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_w,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  double *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray4D(__pyx_v_a,'f',(sizeof(double)),__pyx_v_w,__pyx_v_x,__pyx_v_y,__pyx_v_z); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; goto __pyx_L1;}
  __pyx_r = ((double *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayDouble4D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong1D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x) {
  long *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray1D(__pyx_v_a,'i',(sizeof(long)),__pyx_v_x); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 127; goto __pyx_L1;}
  __pyx_r = ((long *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayLong1D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong2D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x,int *__pyx_v_y) {
  long *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray2D(__pyx_v_a,'i',(sizeof(long)),__pyx_v_x,__pyx_v_y); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; goto __pyx_L1;}
  __pyx_r = ((long *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayLong2D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong3D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  long *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray3D(__pyx_v_a,'i',(sizeof(long)),__pyx_v_x,__pyx_v_y,__pyx_v_z); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 133; goto __pyx_L1;}
  __pyx_r = ((long *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayLong3D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static long *__pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong4D(__pyx_t_6cogent_6evolve_16_likelihood_tree_ArrayType __pyx_v_a,int *__pyx_v_w,int *__pyx_v_x,int *__pyx_v_y,int *__pyx_v_z) {
  long *__pyx_r;
  void *__pyx_1;
  Py_INCREF(__pyx_v_a);
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArray4D(__pyx_v_a,'i',(sizeof(long)),__pyx_v_w,__pyx_v_x,__pyx_v_y,__pyx_v_z); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; goto __pyx_L1;}
  __pyx_r = ((long *)__pyx_1);
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.checkArrayLong4D");
  __pyx_r = NULL;
  __pyx_L0:;
  Py_DECREF(__pyx_v_a);
  return __pyx_r;
}

static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_sumInputLikelihoods(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_sumInputLikelihoods(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_child_indexes = 0;
  PyObject *__pyx_v_result = 0;
  PyObject *__pyx_v_likelihoods = 0;
  int __pyx_v_M;
  int __pyx_v_S;
  int __pyx_v_U;
  int __pyx_v_m;
  int __pyx_v_i;
  int __pyx_v_u;
  int __pyx_v_c;
  double *__pyx_v_values_data;
  long *__pyx_v_index_data;
  double *__pyx_v_target_data;
  PyObject *__pyx_v_first;
  PyObject *__pyx_v_index;
  PyObject *__pyx_r;
  double *__pyx_1;
  PyObject *__pyx_2 = 0;
  PyObject *__pyx_3 = 0;
  long *__pyx_4;
  int __pyx_5;
  static char *__pyx_argnames[] = {"child_indexes","result","likelihoods",0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OOO", __pyx_argnames, &__pyx_v_child_indexes, &__pyx_v_result, &__pyx_v_likelihoods)) return 0;
  Py_INCREF(__pyx_v_child_indexes);
  Py_INCREF(__pyx_v_result);
  Py_INCREF(__pyx_v_likelihoods);
  __pyx_v_first = Py_None; Py_INCREF(Py_None);
  __pyx_v_index = Py_None; Py_INCREF(Py_None);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":18 */
  __pyx_v_M = 0;
  __pyx_v_S = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":19 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_v_result,(&__pyx_v_S),(&__pyx_v_M)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 19; goto __pyx_L1;}
  __pyx_v_target_data = __pyx_1;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":20 */
  __pyx_2 = PyInt_FromLong(1); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 20; goto __pyx_L1;}
  Py_DECREF(__pyx_v_first);
  __pyx_v_first = __pyx_2;
  __pyx_2 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":21 */
  __pyx_v_c = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":22 */
  __pyx_2 = PyObject_GetIter(__pyx_v_child_indexes); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 22; goto __pyx_L1;}
  for (;;) {
    __pyx_3 = PyIter_Next(__pyx_2);
    if (!__pyx_3) {
      if (PyErr_Occurred()) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 22; goto __pyx_L1;}
      break;
    }
    Py_DECREF(__pyx_v_index);
    __pyx_v_index = __pyx_3;
    __pyx_3 = 0;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":23 */
    __pyx_v_U = 0;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":24 */
    __pyx_4 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong1D(__pyx_v_index,(&__pyx_v_S)); if (__pyx_4 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 24; goto __pyx_L1;}
    __pyx_v_index_data = __pyx_4;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":25 */
    __pyx_3 = __Pyx_GetItemInt(__pyx_v_likelihoods, __pyx_v_c); if (!__pyx_3) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; goto __pyx_L1;}
    __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_3,(&__pyx_v_U),(&__pyx_v_M)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_v_values_data = __pyx_1;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":28 */
    __pyx_5 = (__pyx_v_c == 0);
    if (__pyx_5) {
      for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_S; ++__pyx_v_i) {

        /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":30 */
        __pyx_v_u = (__pyx_v_index_data[__pyx_v_i]);

        /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":31 */
        for (__pyx_v_m = 0; __pyx_v_m < __pyx_v_M; ++__pyx_v_m) {
          (__pyx_v_target_data[((__pyx_v_M * __pyx_v_i) + __pyx_v_m)]) = (__pyx_v_values_data[((__pyx_v_M * __pyx_v_u) + __pyx_v_m)]);
        }
      }
      goto __pyx_L4;
    }
    /*else*/ {
      for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_S; ++__pyx_v_i) {

        /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":35 */
        __pyx_v_u = (__pyx_v_index_data[__pyx_v_i]);

        /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":36 */
        for (__pyx_v_m = 0; __pyx_v_m < __pyx_v_M; ++__pyx_v_m) {
          (__pyx_v_target_data[((__pyx_v_M * __pyx_v_i) + __pyx_v_m)]) *= (__pyx_v_values_data[((__pyx_v_M * __pyx_v_u) + __pyx_v_m)]);
        }
      }
    }
    __pyx_L4:;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":38 */
    __pyx_v_c += 1;
  }
  Py_DECREF(__pyx_2); __pyx_2 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":39 */
  Py_INCREF(__pyx_v_result);
  __pyx_r = __pyx_v_result;
  goto __pyx_L0;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_2);
  Py_XDECREF(__pyx_3);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.sumInputLikelihoods");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_first);
  Py_DECREF(__pyx_v_index);
  Py_DECREF(__pyx_v_child_indexes);
  Py_DECREF(__pyx_v_result);
  Py_DECREF(__pyx_v_likelihoods);
  return __pyx_r;
}

static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_getTotalLogLikelihood(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_getTotalLogLikelihood(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_counts = 0;
  PyObject *__pyx_v_input_likelihoods = 0;
  PyObject *__pyx_v_mprobs = 0;
  int __pyx_v_S;
  int __pyx_v_M;
  int __pyx_v_i;
  int __pyx_v_m;
  double __pyx_v_posn;
  double __pyx_v_total;
  double *__pyx_v_likelihoods_data;
  double *__pyx_v_mprobs_data;
  double *__pyx_v_weights_data;
  PyObject *__pyx_r;
  double *__pyx_1;
  double *__pyx_2;
  PyObject *__pyx_3 = 0;
  static char *__pyx_argnames[] = {"counts","input_likelihoods","mprobs",0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OOO", __pyx_argnames, &__pyx_v_counts, &__pyx_v_input_likelihoods, &__pyx_v_mprobs)) return 0;
  Py_INCREF(__pyx_v_counts);
  Py_INCREF(__pyx_v_input_likelihoods);
  Py_INCREF(__pyx_v_mprobs);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":46 */
  __pyx_v_S = 0;
  __pyx_v_M = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":47 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_mprobs,(&__pyx_v_M)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 47; goto __pyx_L1;}
  __pyx_v_mprobs_data = __pyx_1;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":48 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_counts,(&__pyx_v_S)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 48; goto __pyx_L1;}
  __pyx_v_weights_data = __pyx_1;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":49 */
  __pyx_2 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_v_input_likelihoods,(&__pyx_v_S),(&__pyx_v_M)); if (__pyx_2 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 49; goto __pyx_L1;}
  __pyx_v_likelihoods_data = __pyx_2;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":50 */
  __pyx_v_total = 0.0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":51 */
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_S; ++__pyx_v_i) {

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":52 */
    __pyx_v_posn = 0.0;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":53 */
    for (__pyx_v_m = 0; __pyx_v_m < __pyx_v_M; ++__pyx_v_m) {
      __pyx_v_posn += ((__pyx_v_likelihoods_data[((__pyx_v_i * __pyx_v_M) + __pyx_v_m)]) * (__pyx_v_mprobs_data[__pyx_v_m]));
    }

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":55 */
    __pyx_v_total += (log(__pyx_v_posn) * (__pyx_v_weights_data[__pyx_v_i]));
  }

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":56 */
  __pyx_3 = PyFloat_FromDouble(__pyx_v_total); if (!__pyx_3) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; goto __pyx_L1;}
  __pyx_r = __pyx_3;
  __pyx_3 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_3);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.getTotalLogLikelihood");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_counts);
  Py_DECREF(__pyx_v_input_likelihoods);
  Py_DECREF(__pyx_v_mprobs);
  return __pyx_r;
}

static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_getLogSumAcrossSites(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_getLogSumAcrossSites(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_counts = 0;
  PyObject *__pyx_v_input_likelihoods = 0;
  int __pyx_v_S;
  int __pyx_v_i;
  double __pyx_v_total;
  double *__pyx_v_likelihoods_data;
  double *__pyx_v_weights_data;
  PyObject *__pyx_r;
  double *__pyx_1;
  PyObject *__pyx_2 = 0;
  static char *__pyx_argnames[] = {"counts","input_likelihoods",0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OO", __pyx_argnames, &__pyx_v_counts, &__pyx_v_input_likelihoods)) return 0;
  Py_INCREF(__pyx_v_counts);
  Py_INCREF(__pyx_v_input_likelihoods);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":62 */
  __pyx_v_S = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":63 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_counts,(&__pyx_v_S)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 63; goto __pyx_L1;}
  __pyx_v_weights_data = __pyx_1;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":64 */
  __pyx_1 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_input_likelihoods,(&__pyx_v_S)); if (__pyx_1 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; goto __pyx_L1;}
  __pyx_v_likelihoods_data = __pyx_1;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":65 */
  __pyx_v_total = 0.0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":66 */
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_S; ++__pyx_v_i) {
    __pyx_v_total += (log((__pyx_v_likelihoods_data[__pyx_v_i])) * (__pyx_v_weights_data[__pyx_v_i]));
  }

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":68 */
  __pyx_2 = PyFloat_FromDouble(__pyx_v_total); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 68; goto __pyx_L1;}
  __pyx_r = __pyx_2;
  __pyx_2 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_2);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.getLogSumAcrossSites");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_counts);
  Py_DECREF(__pyx_v_input_likelihoods);
  return __pyx_r;
}

static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_logDotReduce(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_6cogent_6evolve_16_likelihood_tree_logDotReduce(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_index = 0;
  PyObject *__pyx_v_patch_probs = 0;
  PyObject *__pyx_v_switch_probs = 0;
  PyObject *__pyx_v_plhs = 0;
  int __pyx_v_site;
  int __pyx_v_i;
  int __pyx_v_j;
  int __pyx_v_k;
  int __pyx_v_n;
  int __pyx_v_uniq;
  int __pyx_v_exponent;
  int __pyx_v_length;
  int __pyx_v_most_probable_state;
  double __pyx_v_result;
  double __pyx_v_BASE;
  double *__pyx_v_sp;
  double *__pyx_v_pl;
  double *__pyx_v_state;
  double *__pyx_v_prev;
  double *__pyx_v_tmp;
  long *__pyx_v_index_data;
  PyObject *__pyx_v_patch_probs1;
  PyObject *__pyx_v_patch_probs2;
  PyObject *__pyx_r;
  PyObject *__pyx_1 = 0;
  PyObject *__pyx_2 = 0;
  double *__pyx_3;
  double *__pyx_4;
  long *__pyx_5;
  int __pyx_6;
  PyObject *__pyx_7 = 0;
  static char *__pyx_argnames[] = {"index","patch_probs","switch_probs","plhs",0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OOOO", __pyx_argnames, &__pyx_v_index, &__pyx_v_patch_probs, &__pyx_v_switch_probs, &__pyx_v_plhs)) return 0;
  Py_INCREF(__pyx_v_index);
  Py_INCREF(__pyx_v_patch_probs);
  Py_INCREF(__pyx_v_switch_probs);
  Py_INCREF(__pyx_v_plhs);
  __pyx_v_patch_probs1 = Py_None; Py_INCREF(Py_None);
  __pyx_v_patch_probs2 = Py_None; Py_INCREF(Py_None);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":76 */
  __pyx_v_BASE = pow(2.0, 1000);

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":77 */
  __pyx_1 = PyObject_GetAttr(__pyx_v_patch_probs, __pyx_n_copy); if (!__pyx_1) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; goto __pyx_L1;}
  __pyx_2 = PyObject_CallObject(__pyx_1, 0); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; goto __pyx_L1;}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  Py_DECREF(__pyx_v_patch_probs1);
  __pyx_v_patch_probs1 = __pyx_2;
  __pyx_2 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":78 */
  __pyx_1 = PyObject_GetAttr(__pyx_v_patch_probs, __pyx_n_copy); if (!__pyx_1) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 78; goto __pyx_L1;}
  __pyx_2 = PyObject_CallObject(__pyx_1, 0); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 78; goto __pyx_L1;}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  Py_DECREF(__pyx_v_patch_probs2);
  __pyx_v_patch_probs2 = __pyx_2;
  __pyx_2 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":79 */
  __pyx_v_n = 0;
  __pyx_v_uniq = 0;
  __pyx_v_length = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":80 */
  __pyx_3 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_patch_probs1,(&__pyx_v_n)); if (__pyx_3 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 80; goto __pyx_L1;}
  __pyx_v_state = __pyx_3;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":81 */
  __pyx_3 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble1D(__pyx_v_patch_probs2,(&__pyx_v_n)); if (__pyx_3 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 81; goto __pyx_L1;}
  __pyx_v_prev = __pyx_3;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":82 */
  __pyx_4 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_v_switch_probs,(&__pyx_v_n),(&__pyx_v_n)); if (__pyx_4 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 82; goto __pyx_L1;}
  __pyx_v_sp = __pyx_4;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":83 */
  __pyx_4 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayDouble2D(__pyx_v_plhs,(&__pyx_v_uniq),(&__pyx_v_n)); if (__pyx_4 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 83; goto __pyx_L1;}
  __pyx_v_pl = __pyx_4;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":84 */
  __pyx_5 = __pyx_f_6cogent_6evolve_16_likelihood_tree_checkArrayLong1D(__pyx_v_index,(&__pyx_v_length)); if (__pyx_5 == NULL) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 84; goto __pyx_L1;}
  __pyx_v_index_data = __pyx_5;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":85 */
  __pyx_v_exponent = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":86 */
  for (__pyx_v_site = 0; __pyx_v_site < __pyx_v_length; ++__pyx_v_site) {

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":87 */
    __pyx_v_k = (__pyx_v_index_data[__pyx_v_site]);

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":88 */
    __pyx_6 = (__pyx_v_k >= __pyx_v_uniq);
    if (__pyx_6) {
      __pyx_1 = PyInt_FromLong(__pyx_v_k); if (!__pyx_1) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      __pyx_2 = PyInt_FromLong(__pyx_v_uniq); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      __pyx_7 = PyTuple_New(2); if (!__pyx_7) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_7, 0, __pyx_1);
      PyTuple_SET_ITEM(__pyx_7, 1, __pyx_2);
      __pyx_1 = 0;
      __pyx_2 = 0;
      __pyx_1 = PyTuple_New(1); if (!__pyx_1) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_1, 0, __pyx_7);
      __pyx_7 = 0;
      __pyx_2 = PyObject_CallObject(PyExc_ValueError, __pyx_1); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      Py_DECREF(__pyx_1); __pyx_1 = 0;
      __Pyx_Raise(__pyx_2, 0, 0);
      Py_DECREF(__pyx_2); __pyx_2 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; goto __pyx_L1;}
      goto __pyx_L4;
    }
    __pyx_L4:;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":90 */
    __pyx_v_tmp = __pyx_v_prev;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":91 */
    __pyx_v_prev = __pyx_v_state;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":92 */
    __pyx_v_state = __pyx_v_tmp;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":93 */
    __pyx_v_most_probable_state = 0;

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":94 */
    for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_n; ++__pyx_v_i) {

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":95 */
      (__pyx_v_state[__pyx_v_i]) = 0;

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":96 */
      for (__pyx_v_j = 0; __pyx_v_j < __pyx_v_n; ++__pyx_v_j) {
        (__pyx_v_state[__pyx_v_i]) += ((__pyx_v_prev[__pyx_v_j]) * (__pyx_v_sp[((__pyx_v_j * __pyx_v_n) + __pyx_v_i)]));
      }

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":98 */
      (__pyx_v_state[__pyx_v_i]) *= (__pyx_v_pl[((__pyx_v_k * __pyx_v_n) + __pyx_v_i)]);

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":99 */
      __pyx_6 = ((__pyx_v_state[__pyx_v_i]) > (__pyx_v_state[__pyx_v_most_probable_state]));
      if (__pyx_6) {
        __pyx_v_most_probable_state = __pyx_v_i;
        goto __pyx_L9;
      }
      __pyx_L9:;
    }

    /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":101 */
    while (1) {
      __pyx_6 = ((__pyx_v_state[__pyx_v_most_probable_state]) < 1.0);
      if (!__pyx_6) break;

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":102 */
      for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_n; ++__pyx_v_i) {
        (__pyx_v_state[__pyx_v_i]) *= __pyx_v_BASE;
      }

      /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":104 */
      __pyx_v_exponent += (-1);
    }
  }

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":105 */
  __pyx_v_result = 0.0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":106 */
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_n; ++__pyx_v_i) {
    __pyx_v_result += (__pyx_v_state[__pyx_v_i]);
  }

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":109 */
  __pyx_7 = PyFloat_FromDouble((log(__pyx_v_result) + (__pyx_v_exponent * log(__pyx_v_BASE)))); if (!__pyx_7) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 109; goto __pyx_L1;}
  __pyx_r = __pyx_7;
  __pyx_7 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_1);
  Py_XDECREF(__pyx_2);
  Py_XDECREF(__pyx_7);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree.logDotReduce");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_patch_probs1);
  Py_DECREF(__pyx_v_patch_probs2);
  Py_DECREF(__pyx_v_index);
  Py_DECREF(__pyx_v_patch_probs);
  Py_DECREF(__pyx_v_switch_probs);
  Py_DECREF(__pyx_v_plhs);
  return __pyx_r;
}

static struct PyMethodDef __pyx_methods[] = {
  {"sumInputLikelihoods", (PyCFunction)__pyx_f_6cogent_6evolve_16_likelihood_tree_sumInputLikelihoods, METH_VARARGS|METH_KEYWORDS, 0},
  {"getTotalLogLikelihood", (PyCFunction)__pyx_f_6cogent_6evolve_16_likelihood_tree_getTotalLogLikelihood, METH_VARARGS|METH_KEYWORDS, 0},
  {"getLogSumAcrossSites", (PyCFunction)__pyx_f_6cogent_6evolve_16_likelihood_tree_getLogSumAcrossSites, METH_VARARGS|METH_KEYWORDS, 0},
  {"logDotReduce", (PyCFunction)__pyx_f_6cogent_6evolve_16_likelihood_tree_logDotReduce, METH_VARARGS|METH_KEYWORDS, 0},
  {0, 0, 0, 0}
};

static void __pyx_init_filenames(void); /*proto*/

PyMODINIT_FUNC init_likelihood_tree(void); /*proto*/
PyMODINIT_FUNC init_likelihood_tree(void) {
  PyObject *__pyx_1 = 0;
  PyObject *__pyx_2 = 0;
  PyObject *__pyx_3 = 0;
  __pyx_init_filenames();
  __pyx_m = Py_InitModule4("_likelihood_tree", __pyx_methods, 0, 0, PYTHON_API_VERSION);
  if (!__pyx_m) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 1; goto __pyx_L1;};
  Py_INCREF(__pyx_m);
  __pyx_b = PyImport_AddModule("__builtin__");
  if (!__pyx_b) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 1; goto __pyx_L1;};
  if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 1; goto __pyx_L1;};
  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 1; goto __pyx_L1;};

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/../../include/numerical_pyrex.pyx":13 */
  if (PyObject_SetAttr(__pyx_m, __pyx_n___version__, __pyx_k11p) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; goto __pyx_L1;}

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":2 */
  __pyx_1 = PyInt_FromLong(2); if (!__pyx_1) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 2; goto __pyx_L1;}
  __pyx_2 = PyInt_FromLong(1); if (!__pyx_2) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 2; goto __pyx_L1;}
  __pyx_3 = PyTuple_New(2); if (!__pyx_3) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 2; goto __pyx_L1;}
  PyTuple_SET_ITEM(__pyx_3, 0, __pyx_1);
  PyTuple_SET_ITEM(__pyx_3, 1, __pyx_2);
  __pyx_1 = 0;
  __pyx_2 = 0;
  if (PyObject_SetAttr(__pyx_m, __pyx_n_version_info, __pyx_3) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 2; goto __pyx_L1;}
  Py_DECREF(__pyx_3); __pyx_3 = 0;

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":3 */
  if (PyObject_SetAttr(__pyx_m, __pyx_n___version__, __pyx_k11p) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 3; goto __pyx_L1;}

  /* "/Users/mcdonald/ResearchWork/PyCogent-v1.1/cogent/evolve/_likelihood_tree.pyx":70 */
  return;
  __pyx_L1:;
  Py_XDECREF(__pyx_1);
  Py_XDECREF(__pyx_2);
  Py_XDECREF(__pyx_3);
  __Pyx_AddTraceback("cogent.evolve._likelihood_tree");
}

static char *__pyx_filenames[] = {
  "numerical_pyrex.pyx",
  "_likelihood_tree.pyx",
};

/* Runtime support code */

static void __pyx_init_filenames(void) {
  __pyx_f = __pyx_filenames;
}

static void __Pyx_WriteUnraisable(char *name) {
    PyObject *old_exc, *old_val, *old_tb;
    PyObject *ctx;
    PyErr_Fetch(&old_exc, &old_val, &old_tb);
    ctx = PyString_FromString(name);
    PyErr_Restore(old_exc, old_val, old_tb);
    if (!ctx)
        ctx = Py_None;
    PyErr_WriteUnraisable(ctx);
}

static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
    Py_XINCREF(type);
    Py_XINCREF(value);
    Py_XINCREF(tb);
    /* First, check the traceback argument, replacing None with NULL. */
    if (tb == Py_None) {
        Py_DECREF(tb);
        tb = 0;
    }
    else if (tb != NULL && !PyTraceBack_Check(tb)) {
        PyErr_SetString(PyExc_TypeError,
            "raise: arg 3 must be a traceback or None");
        goto raise_error;
    }
    /* Next, replace a missing value with None */
    if (value == NULL) {
        value = Py_None;
        Py_INCREF(value);
    }
    #if PY_VERSION_HEX < 0x02050000
    if (!PyClass_Check(type))
    #else
    if (!PyType_Check(type))
    #endif
    {
        /* Raising an instance.  The value should be a dummy. */
        if (value != Py_None) {
            PyErr_SetString(PyExc_TypeError,
                "instance exception may not have a separate value");
            goto raise_error;
        }
        /* Normalize to raise <class>, <instance> */
        Py_DECREF(value);
        value = type;
        #if PY_VERSION_HEX < 0x02050000
            if (PyInstance_Check(type)) {
                type = (PyObject*) ((PyInstanceObject*)type)->in_class;
                Py_INCREF(type);
            }
            else {
                PyErr_SetString(PyExc_TypeError,
                    "raise: exception must be an old-style class or instance");
                goto raise_error;
            }
        #else
            type = (PyObject*) type->ob_type;
            Py_INCREF(type);
            if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
                PyErr_SetString(PyExc_TypeError,
                    "raise: exception class must be a subclass of BaseException");
                goto raise_error;
            }
        #endif
    }
    PyErr_Restore(type, value, tb);
    return;
raise_error:
    Py_XDECREF(value);
    Py_XDECREF(type);
    Py_XDECREF(tb);
    return;
}

static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
    PyObject *result;
    result = PyObject_GetAttr(dict, name);
    if (!result)
        PyErr_SetObject(PyExc_NameError, name);
    return result;
}

static PyObject *__Pyx_GetItemInt(PyObject *o, Py_ssize_t i) {
    PyTypeObject *t = o->ob_type;
    PyObject *r;
    if (t->tp_as_sequence && t->tp_as_sequence->sq_item)
        r = PySequence_GetItem(o, i);
    else {
        PyObject *j = PyInt_FromLong(i);
        if (!j)
            return 0;
        r = PyObject_GetItem(o, j);
        Py_DECREF(j);
    }
    return r;
}

static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
    while (t->p) {
        *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
        if (!*t->p)
            return -1;
        if (t->i)
            PyString_InternInPlace(t->p);
        ++t;
    }
    return 0;
}

#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

static void __Pyx_AddTraceback(char *funcname) {
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    PyObject *py_globals = 0;
    PyObject *empty_tuple = 0;
    PyObject *empty_string = 0;
    PyCodeObject *py_code = 0;
    PyFrameObject *py_frame = 0;
    
    py_srcfile = PyString_FromString(__pyx_filename);
    if (!py_srcfile) goto bad;
    py_funcname = PyString_FromString(funcname);
    if (!py_funcname) goto bad;
    py_globals = PyModule_GetDict(__pyx_m);
    if (!py_globals) goto bad;
    empty_tuple = PyTuple_New(0);
    if (!empty_tuple) goto bad;
    empty_string = PyString_FromString("");
    if (!empty_string) goto bad;
    py_code = PyCode_New(
        0,            /*int argcount,*/
        0,            /*int nlocals,*/
        0,            /*int stacksize,*/
        0,            /*int flags,*/
        empty_string, /*PyObject *code,*/
        empty_tuple,  /*PyObject *consts,*/
        empty_tuple,  /*PyObject *names,*/
        empty_tuple,  /*PyObject *varnames,*/
        empty_tuple,  /*PyObject *freevars,*/
        empty_tuple,  /*PyObject *cellvars,*/
        py_srcfile,   /*PyObject *filename,*/
        py_funcname,  /*PyObject *name,*/
        __pyx_lineno,   /*int firstlineno,*/
        empty_string  /*PyObject *lnotab*/
    );
    if (!py_code) goto bad;
    py_frame = PyFrame_New(
        PyThreadState_Get(), /*PyThreadState *tstate,*/
        py_code,             /*PyCodeObject *code,*/
        py_globals,          /*PyObject *globals,*/
        0                    /*PyObject *locals*/
    );
    if (!py_frame) goto bad;
    py_frame->f_lineno = __pyx_lineno;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    Py_XDECREF(empty_tuple);
    Py_XDECREF(empty_string);
    Py_XDECREF(py_code);
    Py_XDECREF(py_frame);
}
