<commit_before>#include <NumericalIntegration.h>

#include "gtest/gtest.h"
#include <iostream>
class IntegratorTest : public ::testing::Test
{
protected:
  IntegratorTest() : integrator(200)
  {
  }

  /**
   * This is the closed form of the integral of integrandPeak. The bounds are 0 to 1.
   */
  static double integralPeak(const double alpha);

  /**
   * This is the closed form of the integral of intregrandOscillates. The bounds are 0 to pi.
   */
  static double integralOscillates(const int alpha);

  /**
   * This is the closed form of the integral of integrandInfinite. The bounds are 0 to
   * 40*2^alphaInfinite.
   */
  static float integralInfinite(const float alpha);

  Eigen::Integrator<double> integrator;
};

/**
 * This integrand has a peak of height 4^alphaPeak at x = pi/4.
 */
class IntegrandPeakFunctor
{
public:
  double operator()(const double param) const
  {
    using std::pow;
    return pow(4., -m_alpha) / (pow(param - M_PI / 4., 2.) + pow(16., -m_alpha));
  }

  /**
   * @param alpha A parameter for varying the peak.
   */
  void setAlpha(const double alpha) {m_alpha = alpha;}

private:
  double m_alpha;
};

/**
 * This integrand oscillates more strongly for increasing alphaOscillates.
 */
class IntegrandOscillatesFunctor
{
public:
  double operator()(const double param) const
  {
    using std::sin;
    using std::cos;
    using std::pow;
    return cos(pow(2., static_cast<double>(m_alpha)) * sin(param));
  }

  /**
   * A parameter for varying the oscillation strength.
   */
  void setAlpha(const double alpha) {m_alpha = alpha;}

private:
  double m_alpha;
};

/**
 * This integrand has an infinite interval. It is well-behaved and tends to 0 quickly.
 */
class IntegrandInfiniteFunctor
{
public:
  float operator()(const float param) const
  {
    using std::pow;
    return pow(param, 2.) * exp(-param * pow(2, -m_alpha));
  }

  /**
   * A paramater for varying the upper bound.
   */
  void setAlpha(const float alpha) {m_alpha = alpha;}

private:
  float m_alpha;
};

double IntegratorTest::integralPeak(const double alpha)
{
  using std::pow;
  double factor = pow(4., alpha - 1.);

  return atan((4 - M_PI) * factor) + atan(M_PI * factor);
}

/*
 * The closed form of this integral has the 0th order Bessel function of the first kind as a
 * factor. Literals are used in the absence of a routine for computing this Bessel function.
 */
double IntegratorTest::integralOscillates(const int alpha)
{
  double integrals[11] =
    {
       2.4039394306344129,
       0.7033736269566008,
      -1.2476829250428461,
       0.5392569146860977,
      -0.5494616459466271,
       0.4337880026347335,
       0.2908801021737259,
       0.0046251228506773,
      -0.1151503602390470,
      -0.0718346295951386,
       0.0458999248689193
    };

  return integrals[alpha];
}

float IntegratorTest::integralInfinite(const float alpha)
{
  float e40 = exp(40.);
  using std::pow;
  return (e40 - 841.) * pow(2., 3. * alpha + 1.) / e40;
}

template <typename Scalar_>
Scalar_ desiredRelativeError()
{
  return std::numeric_limits<Scalar_>::epsilon() * 50.;
}

template <typename Scalar_>
typename Eigen::Integrator<Scalar_>::QuadratureRule quadratureRules(const size_t i)
{
  static const typename Eigen::Integrator<Scalar_>::QuadratureRule quadratureRules[6] =
    {
      Eigen::Integrator<Scalar_>::GaussKronrod15,
      Eigen::Integrator<Scalar_>::GaussKronrod21,
      Eigen::Integrator<Scalar_>::GaussKronrod31,
      Eigen::Integrator<Scalar_>::GaussKronrod41,
      Eigen::Integrator<Scalar_>::GaussKronrod51,
      Eigen::Integrator<Scalar_>::GaussKronrod61
    };

  return quadratureRules[i];
}

TEST_F(IntegratorTest, qagPeak)
{
  const size_t numKeys = 6;

  IntegrandPeakFunctor integrandPeakFunctor;
<commit_msg>One more file to update pass by reference within.<commit_after>#include <NumericalIntegration.h>

#include "gtest/gtest.h"
#include <iostream>
class IntegratorTest : public ::testing::Test
{
protected:
  IntegratorTest() : integrator(200)
  {
  }

  /**
   * This is the closed form of the integral of integrandPeak. The bounds are 0 to 1.
   */
  static double integralPeak(const double& alpha);

  /**
   * This is the closed form of the integral of intregrandOscillates. The bounds are 0 to pi.
   */
  static double integralOscillates(const int& alpha);

  /**
   * This is the closed form of the integral of integrandInfinite. The bounds are 0 to
   * 40*2^alphaInfinite.
   */
  static float integralInfinite(const float& alpha);

  Eigen::Integrator<double> integrator;
};

/**
 * This integrand has a peak of height 4^alphaPeak at x = pi/4.
 */
class IntegrandPeakFunctor
{
public:
  double operator()(const double& param) const
  {
    using std::pow;
    return pow(4., -m_alpha) / (pow(param - M_PI / 4., 2.) + pow(16., -m_alpha));
  }

  /**
   * \param alpha A parameter for varying the peak.
   */
  void setAlpha(const double& alpha) {m_alpha = alpha;}

private:
  double m_alpha;
};

/**
 * This integrand oscillates more strongly for increasing alphaOscillates.
 */
class IntegrandOscillatesFunctor
{
public:
  double operator()(const double& param) const
  {
    using std::sin;
    using std::cos;
    using std::pow;
    return cos(pow(2., static_cast<double>(m_alpha)) * sin(param));
  }

  /**
   * A parameter for varying the oscillation strength.
   */
  void setAlpha(const double& alpha) {m_alpha = alpha;}

private:
  double m_alpha;
};

/**
 * This integrand has an infinite interval. It is well-behaved and tends to 0 quickly.
 */
class IntegrandInfiniteFunctor
{
public:
  float operator()(const float& param) const
  {
    using std::pow;
    return pow(param, 2.) * exp(-param * pow(2, -m_alpha));
  }

  /**
   * A paramater for varying the upper bound.
   */
  void setAlpha(const float& alpha) {m_alpha = alpha;}

private:
  float m_alpha;
};

double IntegratorTest::integralPeak(const double& alpha)
{
  using std::pow;
  double factor = pow(4., alpha - 1.);

  return atan((4 - M_PI) * factor) + atan(M_PI * factor);
}

/**
 * QuadratureRule are the Gauss-Kronrod abscissae and weights of the pre-calculated rules.
 */
template <typename Scalar_>
typename Eigen::Integrator<Scalar_>::QuadratureRule quadratureRules(const size_t& i)
{
  static const typename Eigen::Integrator<Scalar_>::QuadratureRule quadratureRules[12] =
    {
      Eigen::Integrator<Scalar_>::GaussKronrod15,
      Eigen::Integrator<Scalar_>::GaussKronrod21,
      Eigen::Integrator<Scalar_>::GaussKronrod31,
      Eigen::Integrator<Scalar_>::GaussKronrod41,
      Eigen::Integrator<Scalar_>::GaussKronrod51,
      Eigen::Integrator<Scalar_>::GaussKronrod61,
      Eigen::Integrator<Scalar_>::GaussKronrod71,
      Eigen::Integrator<Scalar_>::GaussKronrod81,
      Eigen::Integrator<Scalar_>::GaussKronrod91,
      Eigen::Integrator<Scalar_>::GaussKronrod101,
      Eigen::Integrator<Scalar_>::GaussKronrod121,
      Eigen::Integrator<Scalar_>::GaussKronrod201
    };

  return quadratureRules[i];
}

/**
 * Relative machine precision.
 */
template <typename Scalar_>
Scalar_ desiredRelativeError()
{
  return std::numeric_limits<Scalar_>::epsilon() * 50.;
}

/**
 * The closed form of this integral has the 0th order Bessel function of the first kind as a
 * factor. Literals are used in the absence of a routine for computing this Bessel function.
 */
double IntegratorTest::integralOscillates(const int& alpha)
{
  double integrals[11] =
    {
       2.4039394306344129,
       0.7033736269566008,
      -1.2476829250428461,
       0.5392569146860977,
      -0.5494616459466271,
       0.4337880026347335,
       0.2908801021737259,
       0.0046251228506773,
      -0.1151503602390470,
      -0.0718346295951386,
       0.0458999248689193
    };

  return integrals[alpha];
}

float IntegratorTest::integralInfinite(const float& alpha)
{
  float e40 = exp(40.);
  using std::pow;
  return (e40 - 841.) * pow(2., 3. * alpha + 1.) / e40;
}

TEST_F(IntegratorTest, qagPeak)
{
  const size_t numKeys = 6;

  IntegrandPeakFunctor integrandPeakFunctor;

{'fraction_non_alphanumeric': 0.1177716849451645, 'fraction_numerical': 0.06281156530408774, 'mean_word_length': 3.6575739988392337, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6241158', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2692, 'n_words': 700}