//  Copyright John Maddock 2006-7.
//  Copyright Paul A. Bristow 2007.

//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_MATH_SF_GAMMA_HPP
#define BOOST_MATH_SF_GAMMA_HPP

#ifdef _MSC_VER
#pragma once
#endif

#include <boost/config.hpp>
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable: 4127 4701)
//  // For lexical_cast, until fixed in 1.35?
//  // conditional expression is constant &
//  // Potentially uninitialized local variable 'name' used
#endif
#include <boost/lexical_cast.hpp>
#ifdef BOOST_MSVC
# pragma warning(pop)
#endif
#include <boost/math/tools/series.hpp>
#include <boost/math/tools/fraction.hpp>
#include <boost/math/tools/precision.hpp>
#include <boost/math/tools/promotion.hpp>
#include <boost/math/policies/error_handling.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/math/special_functions/math_fwd.hpp>
#include <boost/math/special_functions/log1p.hpp>
#include <boost/math/special_functions/trunc.hpp>
#include <boost/math/special_functions/powm1.hpp>
#include <boost/math/special_functions/sqrt1pm1.hpp>
#include <boost/math/special_functions/lanczos.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/math/special_functions/detail/igamma_large.hpp>
#include <boost/math/special_functions/detail/unchecked_factorial.hpp>
#include <boost/math/special_functions/detail/lgamma_small.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/assert.hpp>
#include <boost/mpl/greater.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/greater.hpp>

#include <boost/config/no_tr1/cmath.hpp>
#include <algorithm>

#ifdef BOOST_MATH_INSTRUMENT
#include <iostream>
#include <iomanip>
#include <typeinfo>
#endif

#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
# pragma warning(disable: 4127) // conditional expression is constant.
# pragma warning(disable: 4100) // unreferenced formal parameter.
// Several variables made comments,
// but some difficulty as whether referenced on not may depend on macro values.
// So to be safe, 4100 warnings suppressed.
// TODO - revisit this?
#endif

namespace boost{ namespace math{

namespace detail{

template <class T>
inline bool is_odd(T v, const boost::true_type&)
{
   int i = static_cast<int>(v);
   return i&1;
}
template <class T>
inline bool is_odd(T v, const boost::false_type&)
{
   // Oh dear can't cast T to int!
   BOOST_MATH_STD_USING
   T modulus = v - 2 * floor(v/2);
   return static_cast<bool>(modulus != 0);
}
template <class T>
inline bool is_odd(T v)
{
   return is_odd(v, ::boost::is_convertible<T, int>());
}

template <class T>
T sinpx(T z)
{
   // Ad hoc function calculates x * sin(pi * x),
   // taking extra care near when x is near a whole number.
   BOOST_MATH_STD_USING
   int sign = 1;
   if(z < 0)
   {
      z = -z;
   }
   else
   {
      sign = -sign;
   }
   T fl = floor(z);
   T dist;
   if(is_odd(fl))
   {
      fl += 1;
      dist = fl - z;
      sign = -sign;
   }
   else
   {
      dist = z - fl;
   }
   BOOST_ASSERT(fl >= 0);
   if(dist > 0.5)
      dist = 1 - dist;
   T result = sin(dist*boost::math::constants::pi<T>());
   return sign*z*result;
} // template <class T> T sinpx(T z)
//
// tgamma(z), with Lanczos support:
//
template <class T, class Policy, class L>
T gamma_imp(T z, const Policy& pol, const L& l)
{
   BOOST_MATH_STD_USING

   T result = 1;

#ifdef BOOST_MATH_INSTRUMENT
   static bool b = false;
   if(!b)
   {
      std::cout << "tgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
      b = true;
   }
#endif
   static const char* function = "boost::math::tgamma<%1%>(%1%)";

   if(z <= 0)
   {
      if(floor(z) == z)
         return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
      if(z <= -20)
      {
         result = gamma_imp(T(-z), pol, l) * sinpx(z);
         BOOST_MATH_INSTRUMENT_VARIABLE(result);
         if((fabs(result) < 1) && (tools::max_value<T>() * fabs(result) < boost::math::constants::pi<T>()))
            return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
         result = -boost::math::constants::pi<T>() / result;
         if(result == 0)
            return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
         if((boost::math::fpclassify)(result) == (int)FP_SUBNORMAL)
            return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", result, pol);
         BOOST_MATH_INSTRUMENT_VARIABLE(result);
         return result;
      }

      // shift z to > 1:
      while(z < 0)
      {
         result /= z;
         z += 1;
      }
   }
   BOOST_MATH_INSTRUMENT_VARIABLE(result);
   if((floor(z) == z) && (z < max_factorial<T>::value))
   {
      result *= unchecked_factorial<T>(itrunc(z, pol) - 1);
      BOOST_MATH_INSTRUMENT_VARIABLE(result);
   }
   else
   {
      result *= L::lanczos_sum(z);
      BOOST_MATH_INSTRUMENT_VARIABLE(result);
      BOOST_MATH_INSTRUMENT_VARIABLE(tools::log_max_value<T>());
      if(z * log(z) > tools::log_max_value<T>())
      {
         // we're going to overflow unless this is done with care:
         T zgh = (z + static_cast<T>(L::g()) - boost::math::constants::half<T>());
         BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
         if(log(zgh) * z / 2 > tools::log_max_value<T>())
            return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
         T hp = pow(zgh, (z / 2) - T(0.25));
         BOOST_MATH_INSTRUMENT_VARIABLE(hp);
         result *= hp / exp(zgh);
         BOOST_MATH_INSTRUMENT_VARIABLE(result);
         if(tools::max_value<T>() / hp < result)
            return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
         result *= hp;
         BOOST_MATH_INSTRUMENT_VARIABLE(result);
      }
      else
      {
         T zgh = (z + static_cast<T>(L::g()) - boost::math::constants::half<T>());
         BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
         BOOST_MATH_INSTRUMENT_VARIABLE(pow(zgh, z - boost::math::constants::half<T>()));
         BOOST_MATH_INSTRUMENT_VARIABLE(exp(zgh));
         result *= pow(zgh, z - boost::math::constants::half<T>()) / exp(zgh);
         BOOST_MATH_INSTRUMENT_VARIABLE(result);
      }
   }
   return result;
}
//
// lgamma(z) with Lanczos support:
//
template <class T, class Policy, class L>
T lgamma_imp(T z, const Policy& pol, const L& l, int* sign = 0)
{
#ifdef BOOST_MATH_INSTRUMENT
   static bool b = false;
   if(!b)
   {
      std::cout << "lgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
      b = true;
   }
#endif

   BOOST_MATH_STD_USING

   static const char* function = "boost::math::lgamma<%1%>(%1%)";

   T result = 0;
   int sresult = 1;
   if(z <= 0)
   {
      // reflection formula:
      if(floor(z) == z)
         return policies::raise_pole_error<T>(function, "Evaluation of lgamma at a negative integer %1%.", z, pol);

      T t = sinpx(z);
      z = -z;
      if(t < 0)
      {
         t = -t;
      }
      else
      {
         sresult = -sresult;
      }
      result = log(boost::math::constants::pi<T>()) - lgamma_imp(z, pol, l) - log(t);
   }
   else if(z < 15)
   {
      typedef typename policies::precision<T, Policy>::type precision_type;
      typedef typename mpl::if_<
         mpl::and_<
            mpl::less_equal<precision_type, mpl::int_<64> >,
            mpl::greater<precision_type, mpl::int_<0> >
         >,
         mpl::int_<64>,
         typename mpl::if_<
            mpl::and_<
               mpl::less_equal<precision_type, mpl::int_<113> >,
               mpl::greater<precision_type, mpl::int_<0> >
            >,
            mpl::int_<113>, mpl::int_<0> >::type
          >::type tag_type;
      result = lgamma_small_imp<T>(z, z - 1, z - 2, tag_type(), pol, l);
   }
   else if((z >= 3) && (z < 100))
   {
      // taking the log of tgamma reduces the error, no danger of overflow here:
      result = log(gamma_imp(z, pol, l));
   }
   else
   {
      // regular evaluation:
      T zgh = static_cast<T>(z + L::g() - boost::math::constants::half<T>());
      result = log(zgh) - 1;
      result *= z - 0.5f;
      result += log(L::lanczos_sum_expG_scaled(z));
   }

   if(sign)
      *sign = sresult;
   return result;
}

//
// Incomplete gamma functions follow:
//
template <class T>
struct upper_incomplete_gamma_fract
{
private:
   T z, a;
   int k;
public:
   typedef std::pair<T,T> result_type;

   upper_incomplete_gamma_fract(T a1, T z1)
      : z(z1-a1+1), a(a1), k(0)
   {
   }

   result_type operator()()
   {
      ++k;
      z += 2;
      return result_type(k * (a - k), z);
   }
};

template <class T>
inline T upper_gamma_fraction(T a, T z, T eps)
{
   // Multiply result by z^a * e^-z to get the full
   // upper incomplete integral.  Divide by tgamma(z)
   // to normalise.
   upper_incomplete_gamma_fract<T> f(a, z);
   return 1 / (z - a + 1 + boost::math::tools::continued_fraction_a(f, eps));
}

template <class T>
struct lower_incomplete_gamma_series
{
private:
   T a, z, result;
public:
   typedef T result_type;
   lower_incomplete_gamma_series(T a1, T z1) : a(a1), z(z1), result(1){}

   T operator()()
   {
      T r = result;
      a += 1;
      result *= z/a;
      return r;
   }
};

template <class T, class Policy>
inline T lower_gamma_series(T a, T z, const Policy& pol, T init_value = 0)
{
   // Multiply result by ((z^a) * (e^-z) / a) to get the full
   // lower incomplete integral. Then divide by tgamma(a)
   // to get the normalised value.
   lower_incomplete_gamma_series<T> s(a, z);
   boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
   T factor = policies::get_epsilon<T, Policy>();
   T result = boost::math::tools::sum_series(s, factor, max_iter, init_value);
   policies::check_series_iterations("boost::math::detail::lower_gamma_series<%1%>(%1%)", max_iter, pol);
   return result;
}

//
// Fully generic tgamma and lgamma use the incomplete partial
// sums added together:
//
template <class T, class Policy>
T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos& l)
{
   static const char* function = "boost::math::tgamma<%1%>(%1%)";
   BOOST_MATH_STD_USING
   if((z <= 0) && (floor(z) == z))
      return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
   if(z <= -20)
   {
      T result = gamma_imp(-z, pol, l) * sinpx(z);
      if((fabs(result) < 1) && (tools::max_value<T>() * fabs(result) < boost::math::constants::pi<T>()))
         return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
      result = -boost::math::constants::pi<T>() / result;
      if(result == 0)
         return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
      if((boost::math::fpclassify)(result) == (int)FP_SUBNORMAL)
         return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", result, pol);
      return result;
   }
   //
   // The upper gamma fraction is *very* slow for z < 6, actually it's very
   // slow to converge everywhere but recursing until z > 6 gets rid of the
   // worst of it's behaviour.
   //
   T prefix = 1;
   while(z < 6)
   {
      prefix /= z;
      z += 1;
   }
   BOOST_MATH_INSTRUMENT_CODE(prefix);
   if((floor(z) == z) && (z < max_factorial<T>::value))
   {
      prefix *= unchecked_factorial<T>(itrunc(z, pol) - 1);
   }
   else
   {
      prefix = prefix * pow(z / boost::math::constants::e<T>(), z);
      BOOST_MATH_INSTRUMENT_CODE(prefix);
      T sum = detail::lower_gamma_series(z, z, pol) / z;
      BOOST_MATH_INSTRUMENT_CODE(sum);
      sum += detail::upper_gamma_fraction(z, z, ::boost::math::policies::get_epsilon<T, Policy>());
      BOOST_MATH_INSTRUMENT_CODE(sum);
      if(fabs(tools::max_value<T>() / prefix) < fabs(sum))
         return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
      BOOST_MATH_INSTRUMENT_CODE((sum * prefix));
      return sum * prefix;
   }
   return prefix;
}

template <class T, class Policy>
T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos& l, int*sign)
{
   BOOST_MATH_STD_USING

   static const char* function = "boost::math::lgamma<%1%>(%1%)";
   T result = 0;
   int sresult = 1;
   if(z <= 0)
   {
      if(floor(z) == z)
         return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
      T t = detail::sinpx(z);
      z = -z;
      if(t < 0)
      {
         t = -t;
      }
      else
      {
         sresult = -sresult;
      }
      result = log(boost::math::constants::pi<T>()) - lgamma_imp(z, pol, l, 0) - log(t);
   }
   else if((z != 1) && (z != 2))
   {
      T limit = (std::max)(z+1, T(10));
      T prefix = z * log(limit) - limit;
      T sum = detail::lower_gamma_series(z, limit, pol) / z;
      sum += detail::upper_gamma_fraction(z, limit, ::boost::math::policies::get_epsilon<T, Policy>());
      result = log(sum) + prefix;
   }
   if(sign)
      *sign = sresult;
   return result;
}
//
// This helper calculates tgamma(dz+1)-1 without cancellation errors,
// used by the upper incomplete gamma with z < 1:
//
template <class T, class Policy, class L>
T tgammap1m1_imp(T dz, Policy const& pol, const L& l)
{
   BOOST_MATH_STD_USING

   typedef typename policies::precision<T,Policy>::type precision_type;

   typedef typename mpl::if_<
      mpl::or_<
         mpl::less_equal<precision_type, mpl::int_<0> >,
         mpl::greater<precision_type, mpl::int_<113> >
      >,
      typename mpl::if_<
         is_same<L, lanczos::lanczos24m113>,
         mpl::int_<113>,
         mpl::int_<0>
      >::type,
      typename mpl::if_<
         mpl::less_equal<precision_type, mpl::int_<64> >,
         mpl::int_<64>, mpl::int_<113> >::type
       >::type tag_type;

   T result;
   if(dz < 0)
   {
      if(dz < -0.5)
      {
         // Best method is simply to subtract 1 from tgamma:
         result = boost::math::tgamma(1+dz, pol) - 1;
         BOOST_MATH_INSTRUMENT_CODE(result);
      }
      else
      {
         // Use expm1 on lgamma:
         result = boost::math::expm1(-boost::math::log1p(dz, pol)
            + lgamma_small_imp<T>(dz+2, dz + 1, dz, tag_type(), pol, l));
         BOOST_MATH_INSTRUMENT_CODE(result);
      }
   }
   else
   {
      if(dz < 2)
      {
         // Use expm1 on lgamma:
         result = boost::math::expm1(lgamma_small_imp<T>(dz+1, dz, dz-1, tag_type(), pol, l), pol);
         BOOST_MATH_INSTRUMENT_CODE(result);
      }
      else
      {
         // Best method is simply to subtract 1 from tgamma:
         result = boost::math::tgamma(1+dz, pol) - 1;
         BOOST_MATH_INSTRUMENT_CODE(result);
      }
   }

   return result;
}

template <class T, class Policy>
inline T tgammap1m1_imp(T dz, Policy const& pol,
                 const ::boost::math::lanczos::undefined_lanczos& l)
{
   BOOST_MATH_STD_USING // ADL of std names
   //
   // There should be a better solution than this, but the
   // algebra isn't easy for the general case....
   // Start by subracting 1 from tgamma:
   //
   T result = gamma_imp(1 + dz, pol, l) - 1;
   BOOST_MATH_INSTRUMENT_CODE(result);
   //
   // Test the level of cancellation error observed: we loose one bit
   // for each power of 2 the result is less than 1.  If we would get
   // more bits from our most precise lgamma rational approximation,
   // then use that instead:
   //
   BOOST_MATH_INSTRUMENT_CODE((dz > -0.5));
   BOOST_MATH_INSTRUMENT_CODE((dz < 2));
   BOOST_MATH_INSTRUMENT_CODE((ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34));
   if((dz > -0.5) && (dz < 2) && (ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34))
   {
      result = tgammap1m1_imp(dz, pol, boost::math::lanczos::lanczos24m113());
      BOOST_MATH_INSTRUMENT_CODE(result);
   }
   return result;
}

//
// Series representation for upper fraction when z is small:
//
template <class T>
struct small_gamma2_series
{
   typedef T result_type;

   small_gamma2_series(T a_, T x_) : result(-x_), x(-x_), apn(a_+1), n(1){}

   T operator()()
   {
      T r = result / (apn);
      result *= x;
      result /= ++n;
      apn += 1;
      return r;
   }

private:
   T result, x, apn;
   int n;
};
//
// calculate power term prefix (z^a)(e^-z) used in the non-normalised
// incomplete gammas:
//
template <class T, class Policy>
T full_igamma_prefix(T a, T z, const Policy& pol)
{
   BOOST_MATH_STD_USING

   T prefix;
   T alz = a * log(z);

   if(z >= 1)
   {
      if((alz < tools::log_max_value<T>()) && (-z > tools::log_min_value<T>()))
      {
         prefix = pow(z, a) * exp(-z);
      }
      else if(a >= 1)
      {
         prefix = pow(z / exp(z/a), a);
      }
      else
      {
         prefix = exp(alz - z);
      }
   }
   else
   {
      if(alz > tools::log_min_value<T>())
      {
         prefix = pow(z, a) * exp(-z);
      }
      else if(z/a < tools::log_max_value<T>())
      {
         prefix = pow(z / exp(z/a), a);
      }
      else
      {
         prefix = exp(alz - z);
      }
   }
   //
   // This error handling isn't very good: it happens after the fact
   // rather than before it...
   //
   if((boost::math::fpclassify)(prefix) == (int)FP_INFINITE)
      policies::raise_overflow_error<T>("boost::math::detail::full_igamma_prefix<%1%>(%1%, %1%)", "Result of incomplete gamma function is too large to represent.", pol);

   return prefix;
}
//
// Compute (z^a)(e^-z)/tgamma(a)
// most if the error occurs in this function:
//
template <class T, class Policy, class L>
T regularised_gamma_prefix(T a, T z, const Policy& pol, const L& l)
{
   BOOST_MATH_STD_USING
   T agh = a + static_cast<T>(L::g()) - T(0.5);
   T prefix;
   T d = ((z - a) - static_cast<T>(L::g()) + T(0.5)) / agh;

   if(a < 1)
   {
      //
      // We have to treat a < 1 as a special case because our Lanczos
      // approximations are optimised against the factorials with a > 1,
      // and for high precision types especially (128-bit reals for example)
      // very small values of a can give rather eroneous results for gamma
      // unless we do this:
      //
      // TODO: is this still required?  Lanczos approx should be better now?
      //
      if(z <= tools::log_min_value<T>())
      {
         // Oh dear, have to use logs, should be free of cancellation errors though:
         return exp(a * log(z) - z - lgamma_imp(a, pol, l));
      }
      else
      {
         // direct calculation, no danger of overflow as gamma(a) < 1/a
         // for small a.
         return pow(z, a) * exp(-z) / gamma_imp(a, pol, l);
      }
   }
   else if((fabs(d*d*a) <= 100) && (a > 150))
   {
      // special case for large a and a ~ z.
      prefix = a * boost::math::log1pmx(d, pol) + z * static_cast<T>(0.5 - L::g()) / agh;
      prefix = exp(prefix);
   }
   else
   {
      //
      // general case.
      // direct computation is most accurate, but use various fallbacks
      // for different parts of the problem domain:
      //
      T alz = a * log(z / agh);
      T amz = a - z;
      if(((std::min)(alz, amz) <= tools::log_min_value<T>()) || ((std::max)(alz, amz) >= tools::log_max_value<T>()))
      {
         T amza = amz / a;
         if(((std::min)(alz, amz)/2 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/2 < tools::log_max_value<T>()))
         {
            // compute square root of the result and then square it:
            T sq = pow(z / agh, a / 2) * exp(amz / 2);
            prefix = sq * sq;
         }
         else if(((std::min)(alz, amz)/4 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/4 < tools::log_max_value<T>()) && (z > a))
         {
            // compute the 4th root of the result then square it twice:
            T sq = pow(z / agh, a / 4) * exp(amz / 4);
            prefix = sq * sq;
            prefix *= prefix;
         }
         else if((amza > tools::log_min_value<T>()) && (amza < tools::log_max_value<T>()))
         {
            prefix = pow((z * exp(amza)) / agh, a);
         }
         else
         {
            prefix = exp(alz + amz);
         }
      }
      else
      {
         prefix = pow(z / agh, a) * exp(amz);
      }
   }
   prefix *= sqrt(agh / boost::math::constants::e<T>()) / L::lanczos_sum_expG_scaled(a);
   return prefix;
}
//
// And again, without Lanczos support:
//
template <class T, class Policy>
T regularised_gamma_prefix(T a, T z, const Policy& pol, const lanczos::undefined_lanczos&)
{
   BOOST_MATH_STD_USING

   T limit = (std::max)(T(10), a);
   T sum = detail::lower_gamma_series(a, limit, pol) / a;
   sum += detail::upper_gamma_fraction(a, limit, ::boost::math::policies::get_epsilon<T, Policy>());

   if(a < 10)
   {
      // special case for small a:
      T prefix = pow(z / 10, a);
      prefix *= exp(10-z);
      if(0 == prefix)
      {
         prefix = pow((z * exp((10-z)/a)) / 10, a);
      }
      prefix /= sum;
      return prefix;
   }

   T zoa = z / a;
   T amz = a - z;
   T alzoa = a * log(zoa);
   T prefix;
   if(((std::min)(alzoa, amz) <= tools::log_min_value<T>()) || ((std::max)(alzoa, amz) >= tools::log_max_value<T>()))
   {
      T amza = amz / a;
      if((amza <= tools::log_min_value<T>()) || (amza >= tools::log_max_value<T>()))
      {
         prefix = exp(alzoa + amz);
      }
      else
      {
         prefix = pow(zoa * exp(amza), a);
      }
   }
   else
   {
      prefix = pow(zoa, a) * exp(amz);
   }
   prefix /= sum;
   return prefix;
}
//
// Upper gamma fraction for very small a:
//
template <class T, class Policy>
inline T tgamma_small_upper_part(T a, T x, const Policy& pol, T* pgam = 0, bool invert = false, T* pderivative = 0)
{
   BOOST_MATH_STD_USING  // ADL of std functions.
   //
   // Compute the full upper fraction (Q) when a is very small:
   //
   T result;
   result = boost::math::tgamma1pm1(a, pol);
   if(pgam)
      *pgam = (result + 1) / a;
   T p = boost::math::powm1(x, a, pol);
   result -= p;
   result /= a;
   detail::small_gamma2_series<T> s(a, x);
   boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>() - 10;
   p += 1;
   if(pderivative)
      *pderivative = p / (*pgam * exp(x));
   T init_value = invert ? *pgam : 0;
   result = -p * tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, (init_value - result) / p);
   policies::check_series_iterations("boost::math::tgamma_small_upper_part<%1%>(%1%, %1%)", max_iter, pol);
   if(invert)
      result = -result;
   return result;
}
//
// Upper gamma fraction for integer a:
//
template <class T, class Policy>
inline T finite_gamma_q(T a, T x, Policy const& pol, T* pderivative = 0)
{
   //
   // Calculates normalised Q when a is an integer:
   //
   BOOST_MATH_STD_USING
   T e = exp(-x);
   T sum = e;
   if(sum != 0)
   {
      T term = sum;
      for(unsigned n = 1; n < a; ++n)
      {
         term /= n;
         term *= x;
         sum += term;
      }
   }
   if(pderivative)
   {
      *pderivative = e * pow(x, a) / boost::math::unchecked_factorial<T>(itrunc(T(a - 1), pol));
   }
   return sum;
}
//
// Upper gamma fraction for half integer a:
//
template <class T, class Policy>
T finite_half_gamma_q(T a, T x, T* p_derivative, const Policy& pol)
{
   //
   // Calculates normalised Q when a is a half-integer:
   //
   BOOST_MATH_STD_USING
   T e = boost::math::erfc(sqrt(x), pol);
   if((e != 0) && (a > 1))
   {
      T term = exp(-x) / sqrt(constants::pi<T>() * x);
      term *= x;
      static const T half = T(1) / 2;
      term /= half;
      T sum = term;
      for(unsigned n = 2; n < a; ++n)
      {
         term /= n - half;
         term *= x;
         sum += term;
      }
      e += sum;
      if(p_derivative)
      {
         *p_derivative = 0;
      }
   }
   else if(p_derivative)
   {
      // We'll be dividing by x later, so calculate derivative * x:
      *p_derivative = sqrt(x) * exp(-x) / constants::root_pi<T>();
   }
   return e;
}
//
// Main incomplete gamma entry point, handles all four incomplete gamma's:
//
template <class T, class Policy>
T gamma_incomplete_imp(T a, T x, bool normalised, bool invert,
                       const Policy& pol, T* p_derivative)
{
   static const char* function = "boost::math::gamma_p<%1%>(%1%, %1%)";
   if(a <= 0)
      policies::raise_domain_error<T>(function, "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
   if(x < 0)
      policies::raise_domain_error<T>(function, "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);

   BOOST_MATH_STD_USING

   typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;

   T result = 0; // Just to avoid warning C4701: potentially uninitialized local variable 'result' used

   BOOST_ASSERT((p_derivative == 0) || (normalised == true));

   bool is_int, is_half_int;
   bool is_small_a = (a < 30) && (a <= x + 1);
   if(is_small_a)
   {
      T fa = floor(a);
      is_int = (fa == a);
      is_half_int = is_int ? false : (fabs(fa - a) == 0.5f);
   }
   else
   {
      is_int = is_half_int = false;
   }

   int eval_method;

   if(is_int && (x > 0.6))
   {
      // calculate Q via finite sum:
      invert = !invert;
      eval_method = 0;
   }
   else if(is_half_int && (x > 0.2))
   {
      // calculate Q via finite sum for half integer a:
      invert = !invert;
      eval_method = 1;
   }
   else if(x < 0.5)
   {
      //
      // Changeover criterion chosen to give a changeover at Q ~ 0.33
      //
      if(-0.4 / log(x) < a)
      {
         eval_method = 2;
      }
      else
      {
         eval_method = 3;
      }
   }
   else if(x < 1.1)
   {
      //
      // Changover here occurs when P ~ 0.75 or Q ~ 0.25:
      //
      if(x * 0.75f < a)
      {
         eval_method = 2;
      }
      else
      {
         eval_method = 3;
      }
   }
   else
   {
      //
      // Begin by testing whether we're in the "bad" zone
      // where the result will be near 0.5 and the usual
      // series and continued fractions are slow to converge:
      //
      bool use_temme = false;
      if(normalised && std::numeric_limits<T>::is_specialized && (a > 20))
      {
         T sigma = fabs((x-a)/a);
         if((a > 200) && (policies::digits<T, Policy>() <= 113))
         {
            //
            // This limit is chosen so that we use Temme's expansion
            // only if the result would be larger than about 10^-6.
            // Below that the regular series and continued fractions
            // converge OK, and if we use Temme's method we get increasing
            // errors from the dominant erfc term as it's (inexact) argument
            // increases in magnitude.
            //
            if(20 / a > sigma * sigma)
               use_temme = true;
         }
         else if(policies::digits<T, Policy>() <= 64)
         {
            // Note in this zone we can't use Temme's expansion for
            // types longer than an 80-bit real:
            // it would require too many terms in the polynomials.
            if(sigma < 0.4)
               use_temme = true;
         }
      }
      if(use_temme)
      {
         eval_method = 5;
      }
      else
      {
         //
         // Regular case where the result will not be too close to 0.5.
         //
         // Changeover here occurs at P ~ Q ~ 0.5
         // Note that series computation of P is about x2 faster than continued fraction
         // calculation of Q, so try and use the CF only when really necessary, especially
         // for small x.
         //
         if(x - (1 / (3 * x)) < a)
         {
            eval_method = 2;
         }
         else
         {
            eval_method = 4;
            invert = !invert;
         }
      }
   }

   switch(eval_method)
   {
   case 0:
      {
         result = finite_gamma_q(a, x, pol, p_derivative);
         if(normalised == false)
            result *= boost::math::tgamma(a, pol);
         break;
      }
   case 1:
      {
         result = finite_half_gamma_q(a, x, p_derivative, pol);
         if(normalised == false)
            result *= boost::math::tgamma(a, pol);
         if(p_derivative && (*p_derivative == 0))
            *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
         break;
      }
   case 2:
      {
         // Compute P:
         result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
         if(p_derivative)
            *p_derivative = result;
         if(result != 0)
         {
            T init_value = 0;
            if(invert)
            {
               init_value = -a * (normalised ? 1 : boost::math::tgamma(a, pol)) / result;
            }
            result *= detail::lower_gamma_series(a, x, pol, init_value) / a;
            if(invert)
            {
               invert = false;
               result = -result;
            }
         }
         break;
      }
   case 3:
      {
         // Compute Q:
         invert = !invert;
         T g;
         result = tgamma_small_upper_part(a, x, pol, &g, invert, p_derivative);
         invert = false;
         if(normalised)
            result /= g;
         break;
      }
   case 4:
      {
         // Compute Q:
         result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
         if(p_derivative)
            *p_derivative = result;
         if(result != 0)
            result *= upper_gamma_fraction(a, x, policies::get_epsilon<T, Policy>());
         break;
      }
   case 5:
      {
         //
         // Use compile time dispatch to the appropriate
         // Temme asymptotic expansion.  This may be dead code
         // if T does not have numeric limits support, or has
         // too many digits for the most precise version of
         // these expansions, in that case we'll be calling
         // an empty function.
         //
         typedef typename policies::precision<T, Policy>::type precision_type;

         typedef typename mpl::if_<
            mpl::or_<mpl::equal_to<precision_type, mpl::int_<0> >,
            mpl::greater<precision_type, mpl::int_<113> > >,
            mpl::int_<0>,
            typename mpl::if_<
               mpl::less_equal<precision_type, mpl::int_<53> >,
               mpl::int_<53>,
               typename mpl::if_<
                  mpl::less_equal<precision_type, mpl::int_<64> >,
                  mpl::int_<64>,
                  mpl::int_<113>
               >::type
            >::type
         >::type tag_type;

         result = igamma_temme_large(a, x, pol, static_cast<tag_type const*>(0));
         if(x >= a)
            invert = !invert;
         if(p_derivative)
            *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
         break;
      }
   }

   if(normalised && (result > 1))
      result = 1;
   if(invert)
   {
      T gam = normalised ? 1 : boost::math::tgamma(a, pol);
      result = gam - result;
   }
   if(p_derivative)
   {
      //
      // Need to convert prefix term to derivative:
      //
      if((x < 1) && (tools::max_value<T>() * x < *p_derivative))
      {
         // overflow, just return an arbitrarily large value:
         *p_derivative = tools::max_value<T>() / 2;
      }

      *p_derivative /= x;
   }

   return result;
}

//
// Ratios of two gamma functions:
//
template <class T, class Policy, class L>
T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const L&)
{
   BOOST_MATH_STD_USING
   T zgh = z + L::g() - constants::half<T>();
   T result;
   if(fabs(delta) < 10)
   {
      result = exp((constants::half<T>() - z) * boost::math::log1p(delta / zgh, pol));
   }
   else
   {
      result = pow(zgh / (zgh + delta), z - constants::half<T>());
   }
   result *= pow(constants::e<T>() / (zgh + delta), delta);
   result *= L::lanczos_sum(z) / L::lanczos_sum(z + delta);
   return result;
}
//
// And again without Lanczos support this time:
//
template <class T, class Policy>
T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const lanczos::undefined_lanczos&)
{
   BOOST_MATH_STD_USING
   //
   // The upper gamma fraction is *very* slow for z < 6, actually it's very
   // slow to converge everywhere but recursing until z > 6 gets rid of the
   // worst of it's behaviour.
   //
   T prefix = 1;
   T zd = z + delta;
   while((zd < 6) && (z < 6))
   {
      prefix /= z;
      prefix *= zd;
      z += 1;
      zd += 1;
   }
   if(delta < 10)
   {
      prefix *= exp(-z * boost::math::log1p(delta / z, pol));
   }
   else
   {
      prefix *= pow(z / zd, z);
   }
   prefix *= pow(constants::e<T>() / zd, delta);
   T sum = detail::lower_gamma_series(z, z, pol) / z;
   sum += detail::upper_gamma_fraction(z, z, ::boost::math::policies::get_epsilon<T, Policy>());
   T sumd = detail::lower_gamma_series(zd, zd, pol) / zd;
   sumd += detail::upper_gamma_fraction(zd, zd, ::boost::math::policies::get_epsilon<T, Policy>());
   sum /= sumd;
   if(fabs(tools::max_value<T>() / prefix) < fabs(sum))
      return policies::raise_overflow_error<T>("boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)", "Result of tgamma is too large to represent.", pol);
   return sum * prefix;
}

template <class T, class Policy>
T tgamma_delta_ratio_imp(T z, T delta, const Policy& pol)
{
   BOOST_MATH_STD_USING

   if(z <= 0)
      policies::raise_domain_error<T>("boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got a=%1%).", z, pol);
   if(z+delta <= 0)
      policies::raise_domain_error<T>("boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got b=%1%).", z+delta, pol);

   if(floor(delta) == delta)
   {
      if(floor(z) == z)
      {
         //
         // Both z and delta are integers, see if we can just use table lookup
         // of the factorials to get the result:
         //
         if((z <= max_factorial<T>::value) && (z + delta <= max_factorial<T>::value))
         {
            return unchecked_factorial<T>((unsigned)itrunc(z, pol) - 1) / unchecked_factorial<T>((unsigned)itrunc(T(z + delta), pol) - 1);
         }
      }
      if(fabs(delta) < 20)
      {
         //
         // delta is a small integer, we can use a finite product:
         //
         if(delta == 0)
            return 1;
         if(delta < 0)
         {
            z -= 1;
            T result = z;
            while(0 != (delta += 1))
            {
               z -= 1;
               result *= z;
            }
            return result;
         }
         else
         {
            T result = 1 / z;
            while(0 != (delta -= 1))
            {
               z += 1;
               result /= z;
            }
            return result;
         }
      }
   }
   typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
   return tgamma_delta_ratio_imp_lanczos(z, delta, pol, lanczos_type());
}

template <class T, class Policy>
T gamma_p_derivative_imp(T a, T x, const Policy& pol)
{
   //
   // Usual error checks first:
   //
   if(a <= 0)
      policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
   if(x < 0)
      policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);
   //
   // Now special cases:
   //
   if(x == 0)
   {
      return (a > 1) ? 0 :
         (a == 1) ? 1 : policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
   }
   //
   // Normal case:
   //
   typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
   T f1 = detail::regularised_gamma_prefix(a, x, pol, lanczos_type());
   if((x < 1) && (tools::max_value<T>() * x < f1))
   {
      // overflow:
      return policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
   }

   f1 /= x;

   return f1;
}

template <class T, class Policy>
inline typename tools::promote_args<T>::type
   tgamma(T z, const Policy& /* pol */, const mpl::true_)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;
   return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma<%1%>(%1%)");
}

template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   tgamma(T1 a, T2 z, const Policy&, const mpl::false_)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;
   return policies::checked_narrowing_cast<result_type, forwarding_policy>(
      detail::gamma_incomplete_imp(static_cast<value_type>(a),
      static_cast<value_type>(z), false, true,
      forwarding_policy(), static_cast<value_type*>(0)), "boost::math::tgamma<%1%>(%1%, %1%)");
}

template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   tgamma(T1 a, T2 z, const mpl::false_ tag)
{
   return tgamma(a, z, policies::policy<>(), tag);
}

} // namespace detail

template <class T>
inline typename tools::promote_args<T>::type
   tgamma(T z)
{
   return tgamma(z, policies::policy<>());
}

template <class T, class Policy>
inline typename tools::promote_args<T>::type
   lgamma(T z, int* sign, const Policy&)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;
   return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::lgamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type(), sign), "boost::math::lgamma<%1%>(%1%)");
}

template <class T>
inline typename tools::promote_args<T>::type
   lgamma(T z, int* sign)
{
   return lgamma(z, sign, policies::policy<>());
}

template <class T, class Policy>
inline typename tools::promote_args<T>::type
   lgamma(T x, const Policy& pol)
{
   return ::boost::math::lgamma(x, 0, pol);
}

template <class T>
inline typename tools::promote_args<T>::type
   lgamma(T x)
{
   return ::boost::math::lgamma(x, 0, policies::policy<>());
}

template <class T, class Policy>
inline typename tools::promote_args<T>::type
   tgamma1pm1(T z, const Policy& /* pol */)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<typename remove_cv<result_type>::type, forwarding_policy>(detail::tgammap1m1_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma1pm1<%!%>(%1%)");
}

template <class T>
inline typename tools::promote_args<T>::type
   tgamma1pm1(T z)
{
   return tgamma1pm1(z, policies::policy<>());
}

//
// Full upper incomplete gamma:
//
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   tgamma(T1 a, T2 z)
{
   //
   // Type T2 could be a policy object, or a value, select the
   // right overload based on T2:
   //
   typedef typename policies::is_policy<T2>::type maybe_policy;
   return detail::tgamma(a, z, maybe_policy());
}
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   tgamma(T1 a, T2 z, const Policy& pol)
{
   return detail::tgamma(a, z, pol, mpl::false_());
}
//
// Full lower incomplete gamma:
//
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   tgamma_lower(T1 a, T2 z, const Policy&)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(
      detail::gamma_incomplete_imp(static_cast<value_type>(a),
      static_cast<value_type>(z), false, false,
      forwarding_policy(), static_cast<value_type*>(0)), "tgamma_lower<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   tgamma_lower(T1 a, T2 z)
{
   return tgamma_lower(a, z, policies::policy<>());
}
//
// Regularised upper incomplete gamma:
//
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   gamma_q(T1 a, T2 z, const Policy& /* pol */)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(
      detail::gamma_incomplete_imp(static_cast<value_type>(a),
      static_cast<value_type>(z), true, true,
      forwarding_policy(), static_cast<value_type*>(0)), "gamma_q<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   gamma_q(T1 a, T2 z)
{
   return gamma_q(a, z, policies::policy<>());
}
//
// Regularised lower incomplete gamma:
//
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   gamma_p(T1 a, T2 z, const Policy&)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(
      detail::gamma_incomplete_imp(static_cast<value_type>(a),
      static_cast<value_type>(z), true, false,
      forwarding_policy(), static_cast<value_type*>(0)), "gamma_p<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   gamma_p(T1 a, T2 z)
{
   return gamma_p(a, z, policies::policy<>());
}

// ratios of gamma functions:
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   tgamma_delta_ratio(T1 z, T2 delta, const Policy& /* pol */)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_delta_ratio_imp(static_cast<value_type>(z), static_cast<value_type>(delta), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   tgamma_delta_ratio(T1 z, T2 delta)
{
   return tgamma_delta_ratio(z, delta, policies::policy<>());
}
template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   tgamma_ratio(T1 a, T2 b, const Policy&)
{
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_delta_ratio_imp(static_cast<value_type>(a), static_cast<value_type>(static_cast<value_type>(b) - static_cast<value_type>(a)), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   tgamma_ratio(T1 a, T2 b)
{
   return tgamma_ratio(a, b, policies::policy<>());
}

template <class T1, class T2, class Policy>
inline typename tools::promote_args<T1, T2>::type
   gamma_p_derivative(T1 a, T2 x, const Policy&)
{
   BOOST_FPU_EXCEPTION_GUARD
   typedef typename tools::promote_args<T1, T2>::type result_type;
   typedef typename policies::evaluation<result_type, Policy>::type value_type;
   typedef typename policies::normalise<
      Policy,
      policies::promote_float<false>,
      policies::promote_double<false>,
      policies::discrete_quantile<>,
      policies::assert_undefined<> >::type forwarding_policy;

   return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_p_derivative_imp(static_cast<value_type>(a), static_cast<value_type>(x), forwarding_policy()), "boost::math::gamma_p_derivative<%1%>(%1%, %1%)");
}
template <class T1, class T2>
inline typename tools::promote_args<T1, T2>::type
   gamma_p_derivative(T1 a, T2 x)
{
   return gamma_p_derivative(a, x, policies::policy<>());
}

} // namespace math
} // namespace boost

#ifdef BOOST_MSVC
# pragma warning(pop)
#endif

#include <boost/math/special_functions/detail/igamma_inverse.hpp>
#include <boost/math/special_functions/detail/gamma_inva.hpp>
#include <boost/math/special_functions/erf.hpp>

#endif // BOOST_MATH_SF_GAMMA_HPP


/*
 * RotationScalarExpressionNode.cpp
 *
 *  Created on: Oct 5, 2014
 *      Author: hannes
 */

#include <Eigen/Core>
#include <aslam/backend/RotationScalarExpressionNode.hpp>

namespace aslam {
namespace backend {

    Eigen::Matrix3d RotationScalarExpressionNode::toRotationMatrixImplementation() const
    {
      const double s = _s->toScalar(), cs = std::cos(s), ss = std::sin(s);
      Eigen::Matrix3d ret;
      ret.setZero();

      ret(_axis, _axis) = 1;
      ret((_axis + 1) % 3, (_axis + 1) % 3) = cs;
      ret((_axis + 2) % 3, (_axis + 2) % 3) = cs;
      ret((_axis + 1) % 3, (_axis + 2) % 3) = -ss;
      ret((_axis + 2) % 3, (_axis + 1) % 3) = ss;
      return  ret;
    }

    void RotationScalarExpressionNode::evaluateJacobiansImplementation(JacobianContainer & outJacobians) const
    {
      /*
       * Assume R to be the current rotation value of this exp.
       * J_v (Phi_R^{-1} R(s(v))) = J_v  ( log ( R(s(v) * R^{-1})) = e_axis
       */
      _s->evaluateJacobians(outJacobians, -Eigen::Vector3d::Unit(_axis));
    }

    void RotationScalarExpressionNode::getDesignVariablesImplementation(DesignVariable::set_t & designVariables) const
    {
      _s->getDesignVariables(designVariables);
    }
}
}


/*
 * @Author: Thyssen Wen
 * @Date: 2021-03-18 22:46:09
 * @LastEditors: Thyssen Wen
 * @LastEditTime: 2021-03-23 22:07:43
 * @Description: 测试自动微分功能
 * @FilePath: /SKYNet/demo/autograd/autograd.cpp
 */
#include "baseOption.h"
#include "iostream"
#include "vector"
#include <armadillo>

int main(){
    static const double PI = 3.1415926;

    Eigen::ArrayXXf a = Eigen::ArrayXXf::Zero(1,1);
    a << PI;
    Eigen::ArrayXXf da = Eigen::ArrayXXf::Zero(1,1);
    da << 1;
    Eigen::ArrayXXf b = Eigen::ArrayXXf::Zero(1,1);
    a << 2;
    Eigen::ArrayXXf db = Eigen::ArrayXXf::Zero(1,1);
    da << 0;
    Eigen::ArrayXXf c = Eigen::ArrayXXf::Zero(1,1);
    a << 1;
    Eigen::ArrayXXf dc = Eigen::ArrayXXf::Zero(1,1);
    da << 0;

    std::vector<SKYImage::baseOption> x;
    x.emplace_back(a,da);
    x.emplace_back(b,db);
    x.emplace_back(c,dc);

    SKYImage::baseOption y1 = cos(x[0]);
    SKYImage::baseOption y2 = sin(x[0]);
    SKYImage::baseOption y3 = x[1]*y1;
    SKYImage::baseOption y4 = x[2]*y2;
    SKYImage::baseOption y5 = x[1]*y2;
    SKYImage::baseOption y6 = x[2]*y1;

    SKYImage::baseOption z1 = y3 + y4;
    SKYImage::baseOption z2 = y6 - y5;

    std::cout << "x = [" << std::endl << x[0].value << "," << x[1].value << ","
        << x[2].value << "]" << std::endl;
    std::cout << "z = [" << z1.value << "," << z2.value << "]" << std::endl;
    std::cout << "[dz1/dx0, dz2/dx0] = [" << z1.dvalue << "," << z2.dvalue << "]" << std::endl;

}


// 01 Frame includes
#include "cgalthreadsubdivision.h"

// C++ includes
#include <fstream>
#include <iostream>
#include <string.h>

// VTK includes
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>

// CGAL includes
#include <CGAL/Timer.h>
#include <CGAL/Surface_mesh.h>
#include <boost/lexical_cast.hpp>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/subdivision_method_3.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>

CGALThreadSubdivision::CGALThreadSubdivision(QObject *parent) :
    CGALThread(parent) {
    this->surface_ = nullptr;
}

CGALThreadSubdivision::~CGALThreadSubdivision() {
}

void CGALThreadSubdivision::doWork() {
    this->SetResult(false);
    QFile::remove("subdivision.off");
    if (STL2OFF("subdivision.off")) {
        if (CGALFunctionSubdivision()) {
            OFF2STL("subdivision.off");
            this->SetResult(true);
        } else {
            this->SetResult(false);
        }
    }
}

void CGALThreadSubdivision::SetSurface(const vtkSmartPointer<vtkPolyData> value) {
    this->surface_ = value;
}

vtkSmartPointer<vtkPolyData> CGALThreadSubdivision::GetSurface() {
    return this->surface_;
}

void CGALThreadSubdivision::run() {
    this->InitialResult();
    this->doWork();
}

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/self_intersections.h>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Simple_cartesian<double>         Kernel;
typedef CGAL::Surface_mesh<Kernel::Point_3>             PolygonMesh;

typedef boost::graph_traits<PolygonMesh>::face_descriptor face_descriptor;
namespace PMP = CGAL::Polygon_mesh_processing;

bool CGALThreadSubdivision::CGALFunctionSubdivision() {
//    typedef CGAL::Simple_cartesian<double>         Kernel;
//    typedef CGAL::Surface_mesh<Kernel::Point_3>    PolygonMesh;
//    using namespace std;
//    using namespace CGAL;
//    namespace params = CGAL::parameters;
//    PolygonMesh pmesh;
//    std::ifstream in("subdivision.off");
//    if (in.fail()) {
//        qWarning() << "Could not open input file ";
//        return false;
//    }
//    in >> pmesh;
//    Subdivision_method_3::CatmullClark_subdivision(
//        pmesh, params::number_of_iterations(1));
//    Timer t;
//    t.start();
//    Subdivision_method_3::CatmullClark_subdivision(
//        pmesh, params::number_of_iterations(1));
//    std::cerr << "Done (" << t.time() << " s)" << std::endl;
//    std::ofstream out("subdivision.off");
//    out << pmesh;
//    out.close();
//    return true;

    const char *filename =  "subdivision.off";
    std::ifstream input(filename);
    PolygonMesh mesh;
    if (!input || !(input >> mesh) || !CGAL::is_triangle_mesh(mesh)) {
        std::cerr << "Not a valid input file." << std::endl;
        return 1;
    }
    bool intersecting = PMP::does_self_intersect(mesh,
                        PMP::parameters::vertex_point_map(
                            get(CGAL::vertex_point, mesh)));
    std::cout
            << (intersecting ?
                "There are self-intersections." :
                "There is no self-intersection.")
            << std::endl;
    std::vector<std::pair<face_descriptor, face_descriptor> > intersected_tris;
    PMP::self_intersections(mesh, std::back_inserter(intersected_tris));
    std::cout << intersected_tris.size()
              << " pairs of triangles intersect." << std::endl;

    return false;
}

bool CGALThreadSubdivision::STL2OFF(const QString off_filename) {
    qDebug();
    if (off_filename.isEmpty()) {
        return false;
    }
    if (this->surface_ == nullptr) {
        return false;
    }
    double x[3];
    QFile file(off_filename);
    if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
        QTextStream stream(&file);
        stream.seek(file.size());
        stream << "OFF" << "\n";
        if (this->surface_->GetNumberOfPoints() == 0) {
            qDebug() << "vtkPolyData 为空";
            file.close();
            return false;
        }
        stream << this->surface_->GetNumberOfPoints() << " "
               << this->surface_->GetNumberOfCells() << " 0\n";
        for (qint32 ww = 0; ww < this->surface_->GetNumberOfPoints() ; ww++) {
            this->surface_->GetPoint(ww, x);
            stream << x[0] << " " << x[1] << " " << x[2] << "\n";
        }
        for (qint32 ww = 0; ww < this->surface_->GetNumberOfCells() ; ww++) {
            stream << this->surface_->GetCell(ww)->GetNumberOfPoints() << " ";
            for (qint32 i = 0; i <
                    this->surface_->GetCell(ww)->GetNumberOfPoints(); i++) {
                stream << this->surface_->GetCell(ww)->GetPointId(i) << " ";
            }
            stream << "\n";
        }
    }
    file.close();
    return true;
}

void CGALThreadSubdivision::OFF2STL(const QString off_filename) {
    std::string inputFilename = off_filename.toLocal8Bit().data();
    std::ifstream fin(inputFilename.c_str());
    if (this->surface_ == nullptr) {
        return;
    }
    this->surface_ = vtkSmartPointer<vtkPolyData>::Take(CustomReader(fin));
    fin.close();
}

vtkPolyData *CGALThreadSubdivision::CustomReader(istream &infile) {
    qDebug();
    char buf[1000];
    infile.getline(buf, 1000);
    if (strcmp(buf, "off") == 0 || strcmp(buf, "OFF") == 0) {
        vtkIdType number_of_points, number_of_triangles, number_of_lines;
        infile >> number_of_points >> number_of_triangles >> number_of_lines;
        vtkSmartPointer<vtkPoints> points
            = vtkSmartPointer<vtkPoints>::New();
        points->SetNumberOfPoints(number_of_points);
        for (vtkIdType i = 0; i < number_of_points; i++) {
            double x, y, z;
            infile >> x >> y >> z;
            points->SetPoint(i, x, y, z);
        }
        vtkSmartPointer<vtkCellArray> polys
            = vtkSmartPointer<vtkCellArray>::New();
        int n;
        vtkIdType type;
        for (vtkIdType i = 0; i < number_of_triangles; i++) {
            infile >> n;
            polys->InsertNextCell(n);
            for (; n > 0; n--) {
                infile >> type;
                polys->InsertCellPoint(type);
            }
        }
        vtkPolyData *polydata = vtkPolyData::New();
        polydata->SetPoints(points);
        polydata->SetPolys(polys);
        return polydata;
    }
    vtkPolyData *polydata = vtkPolyData::New();
    return polydata;
}


/** \file HypreSolver.h */

#pragma once

#ifdef MTL_HAS_HYPRE

// MTL4 headers
#include <boost/numeric/itl/itl.hpp>
#include <boost/numeric/mtl/mtl.hpp>

// AMDiS headers
#include "MTL4Types.hpp"
#include "solver/LinearSolver.hpp"
#include "solver/ITL_Preconditioner.hpp"
#include "solver/itl/hypre.hpp"

namespace AMDiS
{

  struct Hypre_Runner : public RunnerBase<MTLTypes::MTLMatrix, MTLTypes::MTLVector>
  {
    using MatrixType = MTLTypes::MTLMatrix;
    using VectorType = MTLTypes::MTLVector;
    using Super      = RunnerBase<MatrixType, VectorType>;

    /** Interface to the HYPRE BoomerAMG solver [...]
     * Parameters provided by AMDiS:
     *
     * [solver]->cycle mode:
     * 	1...V-cycle
     *	2...W-cycle
     *
     * [solver]->interpolation type:
     *  0...classical modified interpolation
     *	1...LS interpolation (for use with GSMG)
     * 	2...classical modified interpolation for hyperbolic PDEs
     * 	3...direct interpolation (with separation of weights)
     * 	4...multipass interpolation
     * 	5...multipass interpolation (with separation of weights)
     * 	6...extended+i interpolation
     * 	7...extended+i (if no common C neighbor) interpolation
     * 	8...standard interpolation
     * 	9...standard interpolation (with separation of weights)
     * 	10..classical block interpolation (for use with nodal systems version only)
     * 	11..classical block interpolation (for use with nodal systems version only)
     * 		with diagonalized diagonal blocks
     * 	12..FF interpolation
     * 	13..FF1 interpolation
     * 	14..extended interpolation
     *
     *  [solver]->info:
     * 	0...no printout (default)
     * 	1...print setup information
     * 	2...print solve information
     * 	3...print both setup and solve information
     *
     *  [solver]->relax type:
     * 	0...Jacobi
     * 	1...Gauss-Seidel, sequential (very slow!)
     * 	2...Gauss-Seidel, interior points in parallel, boundary sequential (slow!)
     * 	3...hybrid Gauss-Seidel or SOR, forward solve
     * 	4...hybrid Gauss-Seidel or SOR, backward solve
     * 	5...hybrid chaotic Gauss-Seidel (works only with OpenMP)
     * 	6...hybrid symmetric Gauss-Seidel or SSOR
     * 	9...Gaussian elimination (only on coarsest level)
     * */
    Hypre_Runner(LinearSolverInterface* oemPtr)
      : oem(*oemPtr),
        useTransposed(false),
        solverCreated(false)
    {
      int cycleMode = -1, interpolation = -1, relaxation = -1;
      Parameters::get(oem.getName() + "->cycle mode", cycleMode);
      Parameters::get(oem.getName() + "->interpolation type", interpolation);
      Parameters::get(oem.getName() + "->relax type", relaxation);

      config.maxIter(oem.getMaxIterations());
      config.interpolationType(interpolation);
      config.relaxType(relaxation);
      config.cycle(cycleMode);
      config.tolerance(oem.getRelative());
      config.printLevel(oem.getInfo());
    }

    /// Destructor.
    ~Hypre_Runner()
    {
      exit();
    }

    /// Implementation of \ref RunnerBase::init()
    virtual void init(SolverMatrix<Matrix<DOFMatrix*>> const& A,
                      MatrixType const& mtlMatrix) override
    {
      setTransposed(typename MatrixType::orientation());
      // TODO: copy matrix directly from DOFMatrix to HYPRE matrix (?)
      hypreMatrix.init(mtlMatrix);
      HYPRE_IJMatrixGetObject(hypreMatrix, (void**) &matrix);
      HYPRE_BoomerAMGCreate(&solver);

      mtl::dense_vector<double> swap(1, 0.0);
      mtl::HypreParVector x(swap);
      HYPRE_BoomerAMGSetup(solver, matrix, x, x);

      solverCreated = true;
    }

    /// Implementation of \ref RunnerBase::solve()
    virtual int solve(MatrixType const& A ,
                      VectorType& mtlX,
                      VectorType const& mtlB) override
    {
      mtl::HypreParVector x(mtlX);
      mtl::HypreParVector b(mtlB);
      config(solver);
      int error = 0;
      if(useTransposed)
        error = HYPRE_BoomerAMGSolveT(solver, matrix, b, x);
      else
        error = HYPRE_BoomerAMGSolve(solver, matrix, b, x);
      mtl::convert(x.getHypreVector(), mtlX);

      int num_iter = 0;
      HYPRE_BoomerAMGGetNumIterations(solver, &num_iter);
      oem.setIterations(num_iter);

      double rel_resid = 0.0;
      HYPRE_BoomerAMGGetFinalRelativeResidualNorm(solver, &rel_resid);
      oem.setRelativeResidual(rel_resid);

      oem.setErrorCode(error);
      return error;
    }

    /// Implementation of \ref RunnerInterface::exit()
    virtual void exit() override
    {
      if (solverCreated)
        HYPRE_BoomerAMGDestroy(solver);
      solverCreated = false;
    }

  private:
    void setTransposed(mtl::row_major)
    {
      useTransposed = false;
    }

    void setTransposed(mtl::col_major)
    {
      useTransposed = true;
    }

  protected:
    LinearSolverInterface& oem;

  private:
    HYPRE_Solver solver;
    HYPRE_ParCSRMatrix matrix;
    mtl::HypreMatrix hypreMatrix;

    mtl::AMGConfigurator config;

    bool useTransposed;
    bool solverCreated;
  };

  /**
   * \ingroup Solver
   * \class AMDiS::HypreSolver
   * \brief
   * Wrapper for the external HYPRE-AMG solver
   */
  using HypreSolver = LinearSolver<MTLTypes::MTLMatrix, MTLTypes::MTLVector, Hypre_Runner>;

} // end namespace AMDiS

#endif // MTL_HAS_HYPRE


// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2010-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, 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.
//
// Eigen 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 Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#include "common.h"
#include <Eigen/LU>

// computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges
EIGEN_LAPACK_FUNC(getrf,(int *m, int *n, RealScalar *pa, int *lda, int *ipiv, int *info))
{
  *info = 0;
        if(*m<0)                  *info = -1;
  else  if(*n<0)                  *info = -2;
  else  if(*lda<std::max(1,*m))   *info = -4;
  if(*info!=0)
  {
    int e = -*info;
    return xerbla_(SCALAR_SUFFIX_UP"GETRF", &e, 6);
  }

  if(*m==0 || *n==0)
    return 0;

  Scalar* a = reinterpret_cast<Scalar*>(pa);
  int nb_transpositions;
  int ret = Eigen::internal::partial_lu_impl<Scalar,ColMajor,int>
                 ::blocked_lu(*m, *n, a, *lda, ipiv, nb_transpositions);

  for(int i=0; i<std::min(*m,*n); ++i)
    ipiv[i]++;

  if(ret>=0)
    *info = ret+1;

  return 0;
}

//GETRS solves a system of linear equations
//    A * X = B  or  A' * X = B
//  with a general N-by-N matrix A using the LU factorization computed  by GETRF
EIGEN_LAPACK_FUNC(getrs,(char *trans, int *n, int *nrhs, RealScalar *pa, int *lda, int *ipiv, RealScalar *pb, int *ldb, int *info))
{
  *info = 0;
        if(OP(*trans)==INVALID)  *info = -1;
  else  if(*n<0)                 *info = -2;
  else  if(*nrhs<0)              *info = -3;
  else  if(*lda<std::max(1,*n))  *info = -5;
  else  if(*ldb<std::max(1,*n))  *info = -8;
  if(*info!=0)
  {
    int e = -*info;
    return xerbla_(SCALAR_SUFFIX_UP"GETRS", &e, 6);
  }

  Scalar* a = reinterpret_cast<Scalar*>(pa);
  Scalar* b = reinterpret_cast<Scalar*>(pb);
  MatrixType lu(a,*n,*n,*lda);
  MatrixType B(b,*n,*nrhs,*ldb);

  for(int i=0; i<*n; ++i)
    ipiv[i]--;
  if(OP(*trans)==NOTR)
  {
    B = PivotsType(ipiv,*n) * B;
    lu.triangularView<UnitLower>().solveInPlace(B);
    lu.triangularView<Upper>().solveInPlace(B);
  }
  else if(OP(*trans)==TR)
  {
    lu.triangularView<Upper>().transpose().solveInPlace(B);
    lu.triangularView<UnitLower>().transpose().solveInPlace(B);
    B = PivotsType(ipiv,*n).transpose() * B;
  }
  else if(OP(*trans)==ADJ)
  {
    lu.triangularView<Upper>().adjoint().solveInPlace(B);
    lu.triangularView<UnitLower>().adjoint().solveInPlace(B);
    B = PivotsType(ipiv,*n).transpose() * B;
  }
  for(int i=0; i<*n; ++i)
    ipiv[i]++;

  return 0;
}


#include "spdlog/spdlog.h"
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <limits>
#include <mimkl/data_structures.hpp>
#include <mimkl/definitions.hpp>
#include <mimkl/io.hpp>
#include <mimkl/kernels.hpp>
#include <mimkl/kernels_handler.hpp>
#include <mimkl/linear_algebra.hpp>
#include <mimkl/models/easy_mkl.hpp>
#include <mimkl/solvers/komd.hpp>
#include <mimkl/utilities.hpp>
#include <numeric>
#include <ostream>
#include <spdlog/fmt/ostr.h>
//#include <Eigen/Cholesky> // check if inducer is sdp llt() ...

using dlib::mat;
using mimkl::data_structures::DataFrame;
using mimkl::data_structures::indexing_from_vector;
using mimkl::data_structures::range;
using mimkl::definitions::Indexing;
using mimkl::kernels_handler::KernelsHandler;
using mimkl::models::EasyMKL;
using mimkl::utilities::check_invocable;
using mimkl::utilities::print_type;
#define SPDLOG_DEBUG_ON
#define SPDLOG_TRACE_ON

// Compile time log levels
// define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON
// SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}",
// 1, 3.23);  SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {}
// ,{}", 1, 3.23);

typedef std::function<MATRIX(double)(const MATRIX(double) &, const MATRIX(double) &)>
InducedFunction;
typedef std::vector<InducedFunction> Function_vec;
typedef std::vector<MATRIX(double)> Matrix_vec;

// snipped from http://en.cppreference.com/w/cpp/container/vector/vector
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v)
{
    s.put('[');
    char comma[3] = {'\0', ' ', '\0'};
    for (const auto &e : v)
    {
        s << comma << e;
        comma[0] = ',';
    }
    return s << ']';
}

MATRIX(double)
test_easy_mkl(const Function_vec function_vec,
              const Matrix_vec matrix_vec,
              const Matrix_vec matrix_vec_test,
              const bool precompute,
              const bool trace_normalization,
              std::shared_ptr<spdlog::logger> console,
              MATRIX(double) X,
              MATRIX(double) Y,
              std::vector<std::string> labels,
              std::vector<std::string> labels_y)
{
    console->info("\n\nRun with precompute = {} and trace_normalization = "
                  "{}\n\n",
                  precompute, trace_normalization);

    console->info("rows X,Y : {},{}", X.rows(), Y.rows());
    console->info("sizes matric_vec : {},{}, sizes matric_vec_test : {},{}",
                  matrix_vec[0].rows(), matrix_vec[0].cols(),
                  matrix_vec_test[0].rows(), matrix_vec_test[0].cols());

    console->info("\nMatricial construction\n");

    EasyMKL<double, InducedFunction> easy_mat =
    EasyMKL<double, InducedFunction>(matrix_vec, precompute, trace_normalization);
    console->info("constructor of easy_mat done");

    easy_mat.set_lambda(0.8);

    easy_mat.fit(matrix_vec, labels);
    MATRIX(double) beta_mat_1 = easy_mat.get_etas();
    MATRIX(double) gammas_mat_1 = easy_mat.get_gammas();
    MATRIX(double) biases_mat_1 = easy_mat.get_biases();
    std::vector<std::string> pred_mat_1 = easy_mat.predict(matrix_vec_test);
    try
    {
        easy_mat.fit(matrix_vec_test, labels);
        MATRIX(double) beta_mat_2 = easy_mat.get_etas();
        MATRIX(double) gammas_mat_2 = easy_mat.get_gammas();
        MATRIX(double) biases_mat_2 = easy_mat.get_biases();
        std::vector<std::string> pred_mat_2 = easy_mat.predict(matrix_vec_test);

        console->error("not caught error on matricial non-squared.");
    }
    catch (const std::exception &e)
    {
        console->warn("caught error on matricial non-squared. message: \n{}",
                      e.what());
    };
    try
    {
        easy_mat.fit(X, labels);
        MATRIX(double) beta_mat_fun = easy_mat.get_etas();
        MATRIX(double) gammas_mat_fun = easy_mat.get_gammas();
        MATRIX(double) biases_mat_fun = easy_mat.get_biases();
        std::vector<std::string> pred_mat_fun = easy_mat.predict(Y);
        console->error("not caught error on matricial set_lhs.");
    }
    catch (const std::exception &e)
    {
        console->warn("caught error on matricial set_lhs. message: \n{}",
                      e.what());
    };
    // retrain
    easy_mat.fit(matrix_vec, labels);

    console->info("\nFunctional construction\n");

    EasyMKL<double, InducedFunction> easy(function_vec, precompute,
                                          trace_normalization);
    console->info("constructor of easy_fun done");

    easy.set_lambda(0.8);

    easy.fit(matrix_vec, labels);
    MATRIX(double) beta_fun_mat = easy.get_etas();
    MATRIX(double) gammas_fun_mat = easy.get_gammas();
    MATRIX(double) biases_fun_mat = easy.get_biases();
    console->info("fitted");
    try
    {
        std::vector<std::string> pred_fun_mat_fun =
        easy.predict(Y); // lhs unavailable ...
        console->error("not caught error on functional missing set_lhs.");
    }
    catch (const std::exception &e)
    {
        console->warn(
        "caught error on functional missing set_lhs. message: \n{}", e.what());
    };
    std::vector<std::string> pred_fun_mat_mat = easy.predict(matrix_vec_test);

    //
    easy.fit(X, labels);
    MATRIX(double) beta_fun_1 = easy.get_etas();
    MATRIX(double) gammas_fun_1 = easy.get_gammas();
    MATRIX(double) biases_fun_1 = easy.get_biases();
    std::vector<std::string> pred_fun_1 = easy.predict(Y);
    std::vector<std::string> pred_fun_mat_1 = easy.predict(matrix_vec_test);

    console->info("retrain");

    // retrain

    easy.fit(Y, labels_y);
    MATRIX(double) beta_fun_2 = easy.get_etas();
    MATRIX(double) gammas_fun_2 = easy.get_gammas();
    MATRIX(double) biases_fun_2 = easy.get_biases();
    console->info("");

    std::vector<std::string> pred_fun_2 = easy.predict(Y);

    console->info("");
    try
    {
        std::vector<std::string> pred_fun_mat_2 =
        easy.predict(matrix_vec_test); // works only if trained on X
        console->error(
        "not caught error on functional prediction with mismatching "
        "kernel_size.");
    }
    catch (const std::exception &e)
    {
        console->warn("caught error on functional prediction with mismatching "
                      "kernel_size. message: \n{}",
                      e.what());
    };
    console->info("");

    try
    {
        easy_mat.fit(matrix_vec_test, labels);
        MATRIX(double) beta_fun_mat_2 = easy.get_etas();
        MATRIX(double) gammas_fun_mat_2 = easy.get_gammas();
        MATRIX(double) biases_fun_mat_2 = easy.get_biases();
        console->error(
        "not caught error on functional with non-squared matricial.");
    }
    catch (const std::exception &e)
    {
        console->warn("caught error on functional with non-squared matricial. "
                      "message: \n{}",
                      e.what());
    };

    assert(((beta_mat_1 - beta_fun_mat).norm() == 0) && "beta_mat_1");
    assert(((beta_fun_1 - beta_fun_mat).norm() == 0) && "beta_fun_1");
    assert(((gammas_mat_1 - gammas_fun_mat).norm() == 0) && "beta_mat_1");
    assert(((gammas_fun_1 - gammas_fun_mat).norm() == 0) && "beta_fun_1");
    assert(((biases_mat_1 - biases_fun_mat).norm() == 0) && "beta_mat_1");
    assert(((biases_fun_1 - biases_fun_mat).norm() == 0) && "beta_fun_1");

    std::cout << pred_mat_1 << std::endl;
    std::cout << pred_fun_mat_mat << std::endl;
    std::cout << pred_fun_1 << std::endl;
    ;
    std::cout << pred_fun_mat_1 << std::endl;

    assert((pred_mat_1 == pred_fun_mat_mat) && (pred_mat_1 == pred_fun_1) &&
           pred_mat_1 == pred_fun_mat_1);

    std::cout << pred_mat_1 << std::endl;
    std::cout << pred_fun_2 << std::endl;
    return beta_fun_mat;
}

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

    // Runtime log levels
    spdlog::set_level(spdlog::level::info); // Set global log level to info
    auto console = spdlog::stdout_color_mt("console");
    spdlog::get("EasyMKL")->set_level(spdlog::level::debug);
    spdlog::get("KernelsHandler")->set_level(spdlog::level::debug);

    const Index rows = 6; // 3 to reproduce single member in class  error
    const Index dims = 2;

    MATRIX(double) X(rows, 2);
    //	X << 1., 1., 3., 1., 1., 2.;
    X << 1., 1., 3., 1., 1., 4., 3., 2., 1., -1., 3., -1.;
    console->info("X\n{}", X);

    std::vector<std::string> labels;
    labels.reserve(rows);
    labels.push_back("a");
    labels.push_back("b");
    labels.push_back("a");
    labels.push_back("b");
    labels.push_back("c");
    labels.push_back("c");

    Eigen::SparseMatrix<double> L(2, 2);
    mimkl::linear_algebra::fill_sparse_diagonal(L, 1.0);

    Eigen::SparseMatrix<double> L1(dims, dims);
    typedef Eigen::Triplet<double> TripletDouble; // (row,col,coef)
    std::vector<TripletDouble> triplet_list;
    triplet_list.reserve(4);
    triplet_list.push_back(TripletDouble(0, 1, 1.));
    //  triplet_list.push_back(TripletDouble(1, 2, 1.));
    triplet_list.push_back(TripletDouble(1, 0, 1.));
    //  triplet_list.push_back(TripletDouble(2, 1, 1.));
    L1.setFromTriplets(triplet_list.begin(), triplet_list.end());

    // L1: (not SDP)
    // 0 1
    // 1 0

    std::vector<Eigen::SparseMatrix<double>> inducer_vec;
    inducer_vec.reserve(2);
    inducer_vec.push_back(L);
    inducer_vec.push_back(L1);

    typedef std::function<MATRIX(double)(const MATRIX(double) &, const MATRIX(double) &,
                                         const Eigen::SparseMatrix<double>)>
    InducerFunction;
    typedef std::function<MATRIX(double)(const MATRIX(double) &,
                                         const MATRIX(double) &)>
    InducedFunction;

    const double degree = 1.;
    const double offset = 0.;

    auto lambda_logger = spdlog::stdout_color_mt("lambda");
    InducerFunction k_poly =
    [degree, offset](const MATRIX(double) & lhs, const MATRIX(double) & rhs,
                     const Eigen::SparseMatrix<double> inducer) {
        spdlog::get("lambda")->trace(
        "about to call inducer function\n"
        "inducer \n{}\n"
        //        			 "lhs rows {}  cols {} \n"
        //        			 "rhs rows {}  cols {} \n"
        //        			 "kernel rows {}  cols {} \n"
        //        			 "inducer rows {}  cols {} \n"
        "type of function\n{}",
        inducer,
        //        			 lhs.rows(), lhs.cols(),
        //        			 rhs.rows(), rhs.cols(),
        //        			 kernel_matrix.rows(),
        //        kernel_matrix.cols(),
        //        			 inducer.rows(), inducer.cols(),
        print_type(mimkl::induction::induce_polynomial_kernel<
                   MATRIX(double), MATRIX(double), MATRIX(double),
                   Eigen::SparseMatrix<double>>));
        return mimkl::induction::induce_polynomial_kernel<MATRIX(double)>(
        lhs, rhs, inducer, degree, offset);
    };

    MATRIX(double) K(rows, rows);
    console->info("\e[1;32mtype: \e[0m \n{}",
                  print_type<decltype(k_poly(X, X, L))>());
    console->info("decltype argument evaluated?:\n{}", K);
    console->info("\e[1;32mtype: \e[0m \n{}", print_type(k_poly));
    console->info("check_invocable? {}", check_invocable(k_poly));

    std::vector<InducedFunction> function_vec =
    mimkl::induction::inducer_combination(k_poly, inducer_vec);
    //// 	==inducer_combination:
    //	  for (Eigen::SparseMatrix<double> inducer : inducer_vec) {
    //		  function_vec.push_back(
    //			[&,inducer](const MATRIX(double) &lhs,
    //					  const MATRIX(double)&rhs,
    //					  const MATRIX(double) &kernel_matrix) {
    //			  k_poly(lhs, rhs, kernel_matrix, inducer);
    //			});
    //
    //	  }

    MATRIX(double) K2 = function_vec[0](X, X);
    console->info("a kernel from function:\n{}", K2);

    MATRIX(double) K3 = function_vec[1](X, X);
    console->info("another kernel from function:\n{}", K3);

    std::vector<MATRIX(double)> matrix_vec(2);
    matrix_vec[0] = K2;
    matrix_vec[1] = K3;

    MATRIX(double) Y(7, 2);
    Y << 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.;
    console->info("Y\n{}", Y);
    std::vector<std::string> labels_y{"frogurt",
                                      "frogurt",
                                      "frogurt",
                                      "frogurt",
                                      "Pan-Galactic Gargle Blaster",
                                      "Pan-Galactic Gargle Blaster",
                                      "Pan-Galactic Gargle Blaster"};

    std::vector<MATRIX(double)> matrix_vec_test(2);

    matrix_vec_test[0] = function_vec[0](X, Y);
    matrix_vec_test[1] = function_vec[1](X, Y);

    MATRIX(double)
    ft = test_easy_mkl(function_vec, matrix_vec, matrix_vec_test, false, true,
                       console, X, Y, labels, labels_y);
    MATRIX(double)
    ff = test_easy_mkl(function_vec, matrix_vec, matrix_vec_test, false, false,
                       console, X, Y, labels, labels_y);
    MATRIX(double)
    tf = test_easy_mkl(function_vec, matrix_vec, matrix_vec_test, true, false,
                       console, X, Y, labels, labels_y);
    MATRIX(double)
    tt = test_easy_mkl(function_vec, matrix_vec, matrix_vec_test, true, true,
                       console, X, Y, labels, labels_y);

    assert(((tt - ft).norm() == 0) && "trace_normalization true");
    assert(((ff - tf).norm() == 0) && "trace_normalization false");

    console->info("trace_normalization true: \n{}", tt);
    console->info("trace_normalization false: \n{}", tf);

    // remember L1 is not SDP so weights can turn out negative
    return EXIT_SUCCESS;
}


#include <Rcpp.h>

//[[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>

//[[Rcpp::depends(BH)]]
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>

#include "experimentalSetup.hpp"
#include "encodingScheme.hpp"
#include "AuxiliaryFunctions.hpp"

InitialPopulationRandomVariates::InitialPopulationRandomVariates(const Eigen::VectorXd & numberOfAlleles, const std::size_t & seed) :
    rng(seed), M(numberOfAlleles.size())
{
    for (std::size_t m = 0; m < M; m++)
    {
        boost::random::uniform_int_distribution<> uniform_interval_m(0, numberOfAlleles[m] - 1);
        interval_uniform_generator uniform_interval_vector_m(rng, uniform_interval_m);
        generate_uniform_interval.push_back(uniform_interval_vector_m);
    }
}

RandomVariates::RandomVariates(const Eigen::VectorXd & numberOfAlleles, const std::size_t & numberOfUnknownContributors,
                               const std::size_t & seed) :
    rng(seed), M(numberOfAlleles.size()),
    uniform_real(0.0, 1.0), generate_uniform_real(rng, uniform_real),
    uniform_01(0, 1), generate_uniform_binary(rng, uniform_01),
    uniform_marker(0, M - 1), generate_uniform_marker(rng, uniform_marker),
    uniform_unknown_contributor(0, numberOfUnknownContributors - 1), generate_uniform_unknown_contributor(rng, uniform_unknown_contributor)
{
    for (std::size_t m = 0; m < M; m++)
    {
        boost::random::uniform_int_distribution<> uniform_mutation_m(1, numberOfAlleles[m] - 1);
        interval_uniform_generator uniform_mutation_vector_m(rng, uniform_mutation_m);
        generate_uniform_mutation.push_back(uniform_mutation_vector_m);
    }
}

//[[Rcpp::export(.testing_rv_struct)]]
Eigen::MatrixXd testing_rv_struct(const Eigen::VectorXd & numberOfAlleles, const std::size_t & numberOfUnknownContributors,
                                  const std::size_t & seed, const std::size_t & numberOfSimulations)
{
    RandomVariates RV(numberOfAlleles, numberOfUnknownContributors, seed);

    const std::size_t & N = numberOfSimulations;
    const std::size_t & M = numberOfAlleles.size();

    Eigen::MatrixXd res(N, M + 4);
    for (std::size_t n = 0; n < N; n++)
    {
        res(n, 0) = RV.generate_uniform_real();
        res(n, 1) = RV.generate_uniform_binary();
        res(n, 2) = RV.generate_uniform_marker();
        res(n, 3) = RV.generate_uniform_unknown_contributor();
        for (std::size_t m = 0; m < M; m++)
        {
            res(n, 4 + m) = RV.generate_uniform_mutation[m]();
        }
    }

    return res;
}

//
ExperimentalSetup::ExperimentalSetup(const std::size_t & numberOfMarkers, const Eigen::VectorXd & numberOfAlleles, const std::size_t & numberOfContributors,
                                     const std::size_t & numberOfKnownContributors, const Eigen::MatrixXd & knownProfiles, const Eigen::MatrixXd & allKnownProfiles,
                                     const Eigen::VectorXd & coverage, const std::vector< std::vector < Eigen::MatrixXd > > & potentialParents, const Eigen::VectorXd & markerImbalances,
                                     const double & convexMarkerImbalanceInterpolation, const Eigen::VectorXd & noiseParameters,
                                     const Eigen::VectorXd & tolerance, const double & theta, const Eigen::VectorXd & alleleFrequencies, const std::size_t & levelsOfStutterRecursion,
                                     const bool & dualEstimation)
{
    NumberOfMarkers = numberOfMarkers;
    NumberOfAlleles = numberOfAlleles;
    PartialSumAlleles = partialSumEigen(numberOfAlleles);

    NumberOfContributors = numberOfContributors;
    KnownProfiles = knownProfiles;
    NumberOfKnownContributors = numberOfKnownContributors;

    AllKnownProfiles = allKnownProfiles;

    Coverage = coverage;
    PotentialParents = potentialParents;
    MarkerImbalances = markerImbalances;
    NoiseParameters = noiseParameters;

    ConvexMarkerImbalanceInterpolation = convexMarkerImbalanceInterpolation;
    Tolerance = tolerance;
    LevelsOfStutterRecursion = levelsOfStutterRecursion;
    DualEstimation = dualEstimation;

    Theta = theta;
    AlleleFrequencies = alleleFrequencies;
}

Eigen::VectorXd ExperimentalSetup::GenerateUnknownGenotype(InitialPopulationRandomVariates & RV)
{
    const Eigen::VectorXd partialSumAlleles = partialSumEigen(NumberOfAlleles);
    std::size_t NumberOfUnknownContributors = (NumberOfContributors - NumberOfKnownContributors);

    Eigen::VectorXd individual(2 * NumberOfUnknownContributors * NumberOfMarkers);
    for (std::size_t i = 0; i < NumberOfMarkers; i++)
    {
        boost::random::uniform_int_distribution<> uniform(0, NumberOfAlleles[i] - 1);
        for (std::size_t j = 0; j < 2 * NumberOfUnknownContributors; j++)
        {
            std::size_t k = 2 * NumberOfUnknownContributors * i + j;

            double d = RV.generate_uniform_interval[i]();

            individual(k) = d;
        }
    }

    return individual;
}


// Copyright (c) 2019 Graphcore Ltd. All rights reserved.
#define BOOST_TEST_MODULE PipelineTrainingTest0

#include <../random_util.hpp>
#include <boost/test/unit_test.hpp>
#include <filereader.hpp>
#include <popart/builder.hpp>
#include <popart/dataflow.hpp>
#include <popart/devicemanager.hpp>
#include <popart/inputshapeinfo.hpp>
#include <popart/ndarraywrapper.hpp>
#include <popart/op/identity.hpp>
#include <popart/op/l1.hpp>
#include <popart/session.hpp>
#include <popart/sgd.hpp>
#include <popart/tensorinfo.hpp>
#include <popart/tensornames.hpp>
#include <popart/testdevice.hpp>

#include <algorithm>
#include <map>
#include <tuple>
#include <vector>

// Dropout is usually performed on an activation Tensor. This test checks that
// it works on a Var Tensor too.

BOOST_AUTO_TEST_CASE(AutoVirtualGraphReluOnWeightTest0) {

  enum class TestType {
    PipelineHardware,
    PipelineHardwareRecomputation,
    SingleBatchSimulator
  };

  auto test = [](TestType tt) {
    using namespace popart;

    // weights will be initiliased randomly
    int seed = 1011;
    DefaultRandomEngine eng(seed);
    UniformRealDistribution<float> fdis(0.f, 1.f);

    // --------- Defining Tensor sizes ----------
    // in this example, accumulationFactor = replicationFactor = 1,
    // so batchSize = microBatchSize
    // number of samples in a batch ( = samples per weight update)
    int64_t batchSize = 11;
    // the number of weight update steps is batchesPerStep,
    int64_t batchesPerStep = (tt != TestType::SingleBatchSimulator ? 13 : 1);
    // samples in a step (samples processed with each SingleBatchSimulator to
    // run(...))
    int64_t samplesPerStep = batchesPerStep * batchSize;
    // an input data sample will be a rank-1 tensor of size,
    int64_t seqLen = 7;
    std::vector<int64_t> sampleShape{seqLen};
    // in this test, weights are of the same shape as data samples,
    std::vector<int64_t> weightShape = sampleShape;
    // shape of a batch
    std::vector<int64_t> batchShape{batchSize, seqLen};
    std::vector<int64_t> stepDataShape{batchesPerStep, batchSize, seqLen};
    std::vector<int64_t> stepWeightShape{batchesPerStep, seqLen};
    TensorInfo sampleInfo{"FLOAT", sampleShape};
    TensorInfo weightInfo = sampleInfo;
    TensorInfo batchInfo{"FLOAT", batchShape};
    TensorInfo stepDataInfo{"FLOAT", stepDataShape};
    int64_t weightsElms    = seqLen;
    int64_t batchElms      = seqLen * batchSize;
    int64_t stepDataElms   = batchElms * batchesPerStep;
    int64_t stepWeightElms = batchesPerStep * weightsElms;

    // ------- Building the Model -----------------
    // A layer of the network:
    //
    // activation
    //   input
    //    |
    //    |    weight
    //    |     /
    //    |  dropout
    //    |   /
    //    |  /
    //    add
    //     |
    //     |
    //     |
    // activation
    //    out
    //

    auto builder     = Builder::create();
    auto aiOnnx      = builder->aiOnnxOpset9();
    auto aiGraphcore = builder->aiGraphcoreOpset1();
    auto input1      = builder->addInputTensor(batchInfo, "1tupni");
    // weights by layer
    std::map<int, std::vector<float>> wVals;
    // name of weights tensor by layer:
    std::map<int, TensorId> wNames;

    // name of mask tensor by layer:
    std::map<int, TensorId> mNames;

    float pDrop = 1. / 3.0f;

    // initial weight magnitude (sign will be chosen randomly)
    float wMag = 1000.;
    std::vector<TensorId> maskIds;
    auto addLayer = [&aiOnnx,
                     pDrop,
                     &maskIds,
                     &builder,
                     sampleInfo,
                     &wVals,
                     &eng,
                     &fdis,
                     seqLen,
                     &wNames,
                     &mNames,
                     wMag](TensorId actIn, int layerNumber) {
      std::vector<float> wVec(seqLen);
      for (int j = 0; j < seqLen; ++j) {
        wVec[j] = fdis(eng) > 0.5 ? -wMag : wMag;
      }
      wVals.insert({layerNumber, std::move(wVec)});
      ConstVoidData wData{wVals.at(layerNumber).data(), sampleInfo};
      auto w = builder->addInitializedInputTensor(wData);
      wNames.insert({layerNumber, w});
      auto w0_drop_pair = aiOnnx.dropout({w}, 2, pDrop);
      auto w_pos        = w0_drop_pair[0];
      maskIds.push_back(w0_drop_pair[1]);
      mNames.insert({layerNumber, w0_drop_pair[1]});
      auto actOut = aiOnnx.add({w_pos, actIn}, "act0");
      return actOut;
    };

    auto actOut = addLayer(input1, 0);
    for (int i = 1; i < 7; ++i) {
      actOut = addLayer(actOut, i);
    }
    float lambda = 1;
    actOut       = builder->aiGraphcoreOpset1().l1loss(
        {actOut}, lambda, ReductionType::Sum);
    auto proto = builder->getModelProto();

    // -------------- Losses, anchors, etc ----------------------
    AnchorReturnTypeMap anchorMap;
    for (auto &maskId : maskIds) {
      anchorMap.insert({maskId, AnchorReturnType("All")});
    }
    auto dataFlow   = DataFlow(batchesPerStep, anchorMap);
    float learnRate = 1;
    auto optimizer  = ConstSGD(learnRate);

    auto device = createTestDevice(TEST_TARGET, 2);

    SessionOptions userOptions;
    userOptions.virtualGraphMode = VirtualGraphMode::Auto;
    userOptions.enablePipelining =
        (tt == TestType::PipelineHardware ? true : false);
    userOptions.autoRecomputation =
        (tt == TestType::PipelineHardwareRecomputation
             ? RecomputationType::Standard
             : RecomputationType::None);

    auto session = popart::TrainingSession::createFromOnnxModel(
        proto,
        dataFlow,
        actOut,
        optimizer,
        device,
        InputShapeInfo(),
        userOptions,
        popart::Patterns(PatternsLevel::Default));

    session->prepareDevice();

    // The samples, all with LARGE POSITIVE value
    std::vector<float> v_input_0(stepDataElms, wMag * 100.);

    using BoolType = uint8_t;
    std::map<TensorId, std::vector<BoolType>> maskAnchorReturns;
    std::map<TensorId, popart::NDArrayWrapper<BoolType>> ancWrappers;
    std::map<TensorId, popart::IArray &> anchors;
    for (TensorId maskId : maskIds) {
      maskAnchorReturns.emplace(maskId, std::vector<BoolType>(stepWeightElms));

      NDArrayWrapper<BoolType> ancWrapper(maskAnchorReturns[maskId].data(),
                                          stepWeightShape);

      ancWrappers.emplace(
          maskId,
          popart::NDArrayWrapper<BoolType>{maskAnchorReturns[maskId].data(),
                                           stepWeightShape});
    }

    for (TensorId maskId : maskIds) {
      anchors.insert({maskId, ancWrappers.at(maskId)});
    }

    WeightsIO weightsRead;
    std::map<int, std::vector<float>> weightsOut;
    for (auto &x : wVals) {
      int layer = x.first;
      weightsOut.insert({layer, std::vector<float>(x.second.size(), -99)});
      weightsRead.insert(wNames.at(layer),
                         {weightsOut.at(layer).data(), weightInfo});
    }

    // write initial weights to host
    session->weightsFromHost();

    int nSteps    = 5;
    auto nUpdates = samplesPerStep * nSteps;
    for (int i = 0; i < nSteps; ++i) {
      std::cout << "Iteration (call to run(...)) # " << i << std::endl;

      popart::NDArrayWrapper<float> input1_wrapper(v_input_0.data(),
                                                   stepDataInfo);

      std::map<popart::TensorId, popart::IArray &> inputs = {
          {input1, input1_wrapper}};

      popart::StepIO stepio(inputs, anchors);

      // We fix the seed, so we expect the set of masks to be the same step vs
      // step. However, inside a step, batch vs batch, you will get different
      // masks.
      session->setRandomSeed(31415);

      // process the 400 samples (100 batches), streaming back mask anchors each
      // batch
      session->run(stepio);
    }

    // read final weights back
    session->weightsToHost();
    session->readWeights(weightsRead);

    // Check that the dropout masks are different on hardware.
    auto maskZero = maskAnchorReturns.at(mNames.at(0));
    for (auto &x : weightsOut) {
      auto layer = x.first;
      auto wName = wNames.at(layer);
      auto mName = mNames.at(layer);

      auto &returnedWeights = x.second;
      auto mask             = maskAnchorReturns.at(mName);
      auto &initialWeights  = wVals.at(layer);

      std::vector<uint32_t> maskSum(seqLen, 0);

      // Combine all masks into one. mask is of size stepWeightElms, but is
      // effectively a concatenation of of all masks for this layer for all
      // batches. On hardware these will be different, as expected.
      for (int i = 0; i < mask.size(); i++) {
        maskSum[i % seqLen] += static_cast<uint32_t>(mask.at(i));
      }
      for (int i = 0; i < returnedWeights.size(); ++i) {
        auto mval = static_cast<uint32_t>(maskSum.at(i));
        // Dividing by 1 - pDrop causes too many floating point rounding
        // errors, so just multiply by 1 / (1 - 1/3) = 1.5
        float delta = mval * learnRate * lambda * batchSize * nSteps * 1.5f;
        // This is a percentage difference test, i.e. values within 1e-8
        // There is a small difference due to FP rounding errors.
        BOOST_CHECK_CLOSE(static_cast<double>(returnedWeights.at(i) + delta),
                          static_cast<double>(initialWeights.at(i)),
                          1e-6);
      }
    }
  };

  test(TestType::PipelineHardware);

  test(TestType::PipelineHardwareRecomputation);

  test(TestType::SingleBatchSimulator);
}


//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <boost/test/unit_test.hpp>
#include "ParserFlatbuffersSerializeFixture.hpp"
#include "../Deserializer.hpp"

#include <string>

BOOST_AUTO_TEST_SUITE(Deserializer)

struct PermuteFixture : public ParserFlatbuffersSerializeFixture
{
    explicit PermuteFixture(const std::string &inputShape,
                            const std::string &dimMappings,
                            const std::string &outputShape,
                            const std::string &dataType)
    {
        m_JsonString = R"(
            {
                inputIds: [0],
                outputIds: [2],
                layers: [
                    {
                        layer_type: "InputLayer",
                        layer: {
                            base: {
                                layerBindingId: 0,
                                base: {
                                    index: 0,
                                    layerName: "InputLayer",
                                    layerType: "Input",
                                    inputSlots: [{
                                        index: 0,
                                        connection: {sourceLayerIndex:0, outputSlotIndex:0 },
                                    }],
                                    outputSlots: [{
                                        index: 0,
                                        tensorInfo: {
                                            dimensions: )" + inputShape + R"(,
                                            dataType: )" + dataType + R"(
                                        }
                                    }]
                                }
                            }
                        }
                    },
                    {
                        layer_type: "PermuteLayer",
                        layer: {
                            base: {
                                index: 1,
                                layerName: "PermuteLayer",
                                layerType: "Permute",
                                inputSlots: [{
                                    index: 0,
                                    connection: {sourceLayerIndex:0, outputSlotIndex:0 },
                                }],
                                outputSlots: [{
                                    index: 0,
                                    tensorInfo: {
                                        dimensions: )" + outputShape + R"(,
                                        dataType: )" + dataType + R"(
                                    }
                                }]
                            },
                            descriptor: {
                                dimMappings: )" + dimMappings + R"(,
                            }
                        }
                    },
                    {
                        layer_type: "OutputLayer",
                        layer: {
                            base:{
                                layerBindingId: 2,
                                base: {
                                    index: 2,
                                    layerName: "OutputLayer",
                                    layerType: "Output",
                                    inputSlots: [{
                                        index: 0,
                                        connection: {sourceLayerIndex:1, outputSlotIndex:0 },
                                    }],
                                    outputSlots: [{
                                        index: 0,
                                        tensorInfo: {
                                            dimensions: )" + outputShape + R"(,
                                            dataType: )" + dataType + R"(
                                        },
                                    }],
                                }
                            }
                        },
                    }
                ]
            }
        )";
        SetupSingleInputSingleOutput("InputLayer", "OutputLayer");
    }
};

struct SimplePermute2DFixture : PermuteFixture
{
    SimplePermute2DFixture() : PermuteFixture("[ 2, 3 ]",
                                              "[ 1, 0 ]",
                                              "[ 3, 2 ]",
                                              "QuantisedAsymm8") {}
};

BOOST_FIXTURE_TEST_CASE(SimplePermute2DQuantisedAsymm8, SimplePermute2DFixture)
{
    RunTest<2, armnn::DataType::QuantisedAsymm8>(0,
                                                 { 1, 2, 3, 4, 5, 6 },
                                                 { 1, 4, 2, 5, 3, 6 });
}

struct SimplePermute4DFixture : PermuteFixture
{
    SimplePermute4DFixture() : PermuteFixture("[ 1, 2, 3, 4 ]",
                                              "[ 3, 2, 1, 0 ]",
                                              "[ 4, 3, 2, 1 ]",
                                              "QuantisedAsymm8") {}
};

BOOST_FIXTURE_TEST_CASE(SimplePermute4DQuantisedAsymm8, SimplePermute4DFixture)
{
    RunTest<4, armnn::DataType::QuantisedAsymm8>(0,
                                                 {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
                                                   13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 },
                                                 {  1, 13,  5, 17,  9, 21,  2, 14,  6, 18, 10, 22,
                                                    3, 15,  7, 19, 11, 23,  4, 16,  8, 20, 12, 24 });
}

BOOST_AUTO_TEST_SUITE_END()