Björn Höhrmann


Algorithm::LineSegments - Piecewise linear function approximation


  use Algorithm::LineSegments;
  my @points = line_segment_points(
    points => \@numbers,
    continue => sub {
      my ($segment_count, $cost_factor) = @_;
      return 0 if $segment_count <= 10;
      return 1;


This module takes discrete data points like time series data and computes a piecewise linear function, line segments, approximating them. It does this by merging groups of adjacent points into lines, always picking the pair that produces the smallest error, until it is told to stop.



Returns a list of [[$x0, $y0], [$x1, $y1]] pairs describing line segments. Options are


Array of numbers, the input data.


A callback function that is called with the number of remaining segments and the cost of the current merge with the expectation the callback returns a true value if it should perform the merge and continue, and a false value if it should stop merging and return. The default is to merge until only three line segments are left.


A callback function that is called with two list references of points and it should return a number indicating how costly it is, how much of an error it introduces, if all points are made into a single line segment. The default projects all data points to the unit range 0 .. 1 based on the maximum and minimum value and computes the euclidean distance between the points and the corresponding points on a line that would cover them all.


The function line_segment_points by default.


  Copyright (c) 2014 Bjoern Hoehrmann <>.
  This module is licensed under the same terms as Perl itself.