<commit_before>#ifndef ALEPH_MATH_PIECEWISE_LINEAR_FUNCTION_HH__
#define ALEPH_MATH_PIECEWISE_LINEAR_FUNCTION_HH__

#include <aleph/math/KahanSummation.hh>

#include <functional>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <stdexcept>

#include <cmath>

namespace aleph
{

namespace math
{

namespace detail
{

/** Performs linear interpolation between two points */
template <class D, class I> I lerp( D x, D x0, I y0, D x1, I y1 )
{
  return y0 + (y1-y0) * (x-x0) / (x1-x0);
}

} // namespace detail

template <class D, class I = D> class PiecewiseLinearFunction
{
public:
  using Domain = D;
  using Image  = I;

  /** Creates an empty piecewise linear function */
  PiecewiseLinearFunction() = default;

  /**
    Creates a new piecewise linear function from a range of values. The values
    must consist of pairs that are convertible to double. Duplicates are not
    permitted and will result in an exception being thrown.

    @param begin Input iterator to begin of range
    @param end   Input iterator to end of range
  */

  template <class InputIterator> PiecewiseLinearFunction( InputIterator begin,
                                                          InputIterator end )
  {
    for( InputIterator it = begin; it != end; ++it )
    {
      auto x       = it->first;
      auto y       = it->second;<commit_msg>Documented piecewise linear function class<commit_after>#ifndef ALEPH_MATH_PIECEWISE_LINEAR_FUNCTION_HH__
#define ALEPH_MATH_PIECEWISE_LINEAR_FUNCTION_HH__

#include <aleph/math/KahanSummation.hh>

#include <functional>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <stdexcept>

#include <cmath>

namespace aleph
{

namespace math
{

namespace detail
{

/** Performs linear interpolation between two points */
template <class D, class I> I lerp( D x, D x0, I y0, D x1, I y1 )
{
  return y0 + (y1-y0) * (x-x0) / (x1-x0);
}

} // namespace detail

/**
  @class PiecewiseLinearFunction
  @brief Models a piecewise linear function

  A piecewise linear function is fully defined by a set of pairs of
  coordinates, specifying values in the *domain* and the *image* of
  the function. This class permits various arithmetical operations,
  such as addition and subtraction, with piecewise linear functions
  and provides a number of other common operations.

  @tparam D Type of the *domain* of the function
  @tparam I Type of the *image* of the function
*/

template <class D, class I = D> class PiecewiseLinearFunction
{
public:
  using Domain = D;
  using Image  = I;

  /** Creates an empty piecewise linear function */
  PiecewiseLinearFunction() = default;

  /**
    Creates a new piecewise linear function from a range of values. The values
    must consist of pairs that are convertible to double. Duplicates are not
    permitted and will result in an exception being thrown.

    @param begin Input iterator to begin of range
    @param end   Input iterator to end of range
  */

  template <class InputIterator> PiecewiseLinearFunction( InputIterator begin,
                                                          InputIterator end )
  {
    for( InputIterator it = begin; it != end; ++it )
    {
      auto x       = it->first;
      auto y       = it->second;
{'fraction_non_alphanumeric': 0.08024502297090352, 'fraction_numerical': 0.006125574272588055, 'mean_word_length': 3.1237373737373737, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4143788', 'n_tokens_mistral': 992, 'n_tokens_neox': 972, 'n_words': 377}