Math::...togram(3pm)
π NAME π
Math::GSL::Histogram β Create and manipulate histograms of data
okay bestie, welcome to Math::...togram(3pm) from the User Contributed Perl Documentation, and it is lowkey iconic π
β¨ SYNOPSIS β¨
use Math::GSL::Histogram qw/:all/;
my $H = gsl_histogram_alloc(100);
gsl_histogram_set_ranges_uniform($H,0,101);
gsl_histogram_increment($H, -50 ); # ignored
gsl_histogram_increment($H, 70 );
gsl_histogram_increment($H, 85.2 );
my $G = gsl_histogram_clone($H);
my $value = gsl_histogram_get($G, 70);
my ($max,$min) = (gsl_histogram_min_val($H), gsl_histogram_max_val($H) );
my $sum = gsl_histogram_sum($H);
πΈ DESCRIPTION πΈ
okay so here's the whole roster of functions this module hands you, no gatekeeping:
gsl_histogram_alloc($n)β this one grabs memory for a histogram with$nbins and hands back a pointer to a freshly-madegsl_histogramstruct π«Ά but heads up: the bins and the ranges are not initialized yet. you gotta set them up with one of the range-setting functions down below before this histogram is actually ready to serve. no vibes until you do that.gsl_histogram_callocgsl_histogram_calloc_uniformgsl_histogram_free($h)β frees the histogram$hplus every bit of memory attached to it. say bye πgsl_histogram_increment($h, $x)β updates histogram$hby adding one (1.0) to the bin whose range contains the coordinate$x. if$xis inside the valid range of the histogram, the function returns zero, i.e. success, slay. if$xis less than the lower limit of the histogram, it returns$GSL_EDOMand none of the bins get touched. same deal if$xis greater than or equal to the upper limit:$GSL_EDOM, and none of the bins are modified. but here's the tea β the error handler is not called, because it's super common to compute histograms over just a small range of a bigger dataset and simply ignore everything outside your range of interest πgsl_histogram_accumulate($h, $x, $weight)β basicallygsl_histogram_increment's sibling, except instead of +1 it bumps the right bin in histogram$hup by the floating-point number$weightβ¨gsl_histogram_find($h, $x)β finds the bin number that covers the coordinate$xin histogram$h. it locates the bin with a binary search, and there's an optimization baked in for histograms with a uniform range, so in that case it returns the correct bin immediately, no cap. if$xis found in the range of the histogram, the function returns the bin number and returns$GSL_SUCCESS. if$xis outside the valid range of the histogram, it returns$GSL_EDOMand the error handler gets invoked πgsl_histogram_get($h, $i)β returns the contents of the$i-th bin of histogram$h. if$iis outside the valid range of indices for that histogram, the error handler is called with an error code ofGSL_EDOMand the function returns0πgsl_histogram_get_range($h, $i)β finds the upper and lower range limits of the$i-th bin of histogram$h. if the index$iis valid, the corresponding range limits come back after the0, in this order: lower first, then upper. the lower limit is inclusive (events with this coordinate are included in the bin) and the upper limit is exclusive (events with the coordinate of the upper limit are excluded and land in the neighboring higher bin, if one exists). the function returns0to say success. if$ilies outside the valid range of indices for the histogram, the error handler gets called and the function returns an error code of$GSL_EDOM.gsl_histogram_max($h)β returns the maximum upper limit of histogram$h. it's a way of getting that value without poking at thegsl_histogramstruct directly πgsl_histogram_min($h)β returns the minimum lower range limit of histogram$h. again: no need to touch thegsl_histogramstruct directly, we're being polite.gsl_histogram_bins($h)β returns the number of bins of the histogram$hlimit. it's a way of determining this value without accessing thegsl_histogramstruct directly.gsl_histogram_reset($h)β resets every bin in histogram$hback to zero. factory reset but make it math π§gsl_histogram_calloc_rangegsl_histogram_set_ranges($h, $range, $size)β sets the ranges of the existing histogram$husing the array$rangeof size$size. the values of the histogram bins get reset to zero. your$rangearray should hold the desired bin limits, and those ranges can be arbitrary β with one rule: they must be monotonically increasing. also note that the size of the$rangearray should be defined as one element bigger than the number of bins, because that extra element is what supplies the upper value of the final bin π₯gsl_histogram_set_ranges_uniform($h, $xmin, $xmax)β sets the ranges of the existing histogram$hso they cover the range$xminto$xmaxuniformly. the values of the histogram bins get reset to zero. the bin ranges look like this:
bin[0] corresponds to xmin <= x < xmin + d
bin[1] corresponds to xmin + d <= x < xmin + 2 d
......
bin[n-1] corresponds to xmin + (n-1)d <= x < xmax
where d is the bin spacing, d = (xmax-xmin)/n.
gsl_histogram_memcpy($dest, $src)β copies histogram$srcinto the pre-existing histogram$dest, turning$destinto an exact copy of$src. the two histograms must be the same size, that's non-negotiable bestie.gsl_histogram_clone($src)β returns a pointer to a newly created histogram that's an exact copy of histogram$src. twin energy π¦gsl_histogram_max_val($h)β returns the maximum value contained in the histogram bins.gsl_histogram_max_bin($h)β returns the index of the bin containing the maximum value. if several bins are out here holding that same maximum value, the smallest index is the one returned.gsl_histogram_min_val($h)β returns the minimum value contained in the histogram bins.gsl_histogram_min_bin($h)β returns the index of the bin containing the minimum value. if several bins contain the same maximum value, the smallest index is returned.gsl_histogram_equal_bins_p($h1, $h2)β returns1if all of the individual bin ranges of the two histograms are identical, and0otherwise.gsl_histogram_add($h1, $h2)β adds the contents of the bins in histogram$h2to the corresponding bins of histogram$h1, i.e.h'_1(i) = h_1(i) + h_2(i). the two histograms must have identical bin ranges.gsl_histogram_sub($h1, $h2)β subtracts the contents of the bins in histogram$h2from the corresponding bins of histogram$h1, i.e.h'_1(i) = h_1(i) - h_2(i). the two histograms must have identical bin ranges.gsl_histogram_mul($h1, $h2)β multiplies the contents of the bins of histogram$h1by the contents of the corresponding bins in histogram$h2, i.e.h'_1(i) = h_1(i) * h_2(i). the two histograms must have identical bin ranges.gsl_histogram_div($h1, $h2)β divides the contents of the bins of histogram$h1by the contents of the corresponding bins in histogram$h2, i.e.h'_1(i) = h_1(i) / h_2(i). the two histograms must have identical bin ranges.gsl_histogram_scale($h, $scale)β multiplies the contents of the bins of histogram$hby the constant$scale, i.e.h'_1(i) = h_1(i) * scale.gsl_histogram_shift($h, $offset)β shifts the contents of the bins of histogram$hby the constant$offset, i.e.h'_1(i) = h_1(i) + offset.gsl_histogram_sigma($h)β returns the standard deviation of the histogrammed variable, treating the histogram as a probability distribution. negative bin values are ignored for the purposes of this calculation, and the accuracy of the result is limited by the bin width π«Άgsl_histogram_mean($h)β returns the mean of the histogrammed variable, again treating the histogram as a probability distribution. negative bin values are ignored for the purposes of this calculation too, and yep, accuracy is limited by the bin width.gsl_histogram_sum($h)β returns the sum of all bin values. negative bin values are included in this sum, no filtering.gsl_histogram_fwrite($stream, $h)β writes the ranges and bins of histogram$hto the stream$streamin binary format.$streammust have been opened by thegsl_fopenfunction from theMath::GSLmodule. the return value is0for success and$GSL_EFAILEDif something went wrong writing to the file. β οΈ and because the data goes out in the native binary format, it may not be portable between different architectures.gsl_histogram_fread($stream, $h)β reads into histogram$hfrom the open stream$streamin binary format, and$streamagain must have been opened by thegsl_fopenfunction from theMath::GSLmodule.$hmust be preallocated with the correct size, because the function uses the number of bins in$hto figure out how many bytes to read. return value is0for success and$GSL_EFAILEDif there was a problem reading from the file. and the data is assumed to have been written in the native binary format on the same architecture πgsl_histogram_fprintf($stream, $h, $range_format, $bin_format)β writes the ranges and bins of histogram$hline-by-line to the stream$stream(from thegsl_fopenfunction in theMath::GSLmodule) using the format specifiers$range_formatand$bin_format. those should be one of the%g,%eor%fformats for floating point numbers. the function returns0for success and$GSL_EFAILEDif there was a problem writing to the file. the histogram output is formatted in three columns, separated by spaces, like this:
range[0] range[1] bin[0]
range[1] range[2] bin[1]
range[2] range[3] bin[2]
....
range[n-1] range[n] bin[n-1]
the values of the ranges get formatted using range_format and the values of the bins get formatted using bin_format. each line contains the lower and upper limit of the range of the bin plus the value of the bin itself. and yeah, since the upper limit of one bin is the lower limit of the next, those values get duplicated between lines β but that's on purpose, because it lets you manipulate the histogram with line-oriented tools π
π and that's part one bestie β more parts are still coming, so don't go anywhere!
okay bestie we're BACK and we're picking up right where we left off π no re-intros, no recap, we're just diving straight into the histogram functions. buckle up.
gsl_histogram_fscanf($stream, $h)β this one reads formatted data from the stream$stream(which you opened with thegsl_fopenfunction from theMath::GSLmodule, iykyk) straight into the histogram$h. the data has to be in the three-column format thatgsl_histogram_fprintfspits out, so they're like a matching set fr. and listen β$hmust be preallocated with the correct length, because the function literally uses the size of$hto figure out how many numbers to read. it's giving "you must know what you want before you ask." returns0for success and$GSL_EFAILEDif something went wrong reading from the file. πgsl_histogram_pdf_alloc($n)β allocates memory for a probability distribution with$nbins and hands you back a pointer to a freshly initializedgsl_histogram_pdfstruct. β¨ if there's not enough memory? null pointer, and the error handler gets invoked with error code$GSL_ENOMEM. so like, it will tell you. πgsl_histogram_pdf_init($p, $h)β initializes the probability distribution$pusing the contents of the histogram$h. but bestie. if any of the bins of$hare negative, the error handler gets invoked with error code$GSL_EDOM, because a probability distribution cannot contain negative values. that's not a vibe, that's a law of the universe. π«gsl_histogram_pdf_free($p)β frees the probability distribution function$pand every last bit of memory associated with it. be a good friend and call this. π«Άgsl_histogram_pdf_sample($p, $r)β this one takes$r, a uniform random number between zero and one, and computes a single random sample from the probability distribution$p. the algorithm used to compute the samplesis exactly this formula:
s = range[i] + delta * (range[i+1] - range[i])
where i is the index that satisfies sum[i] <= r < sum[i+1], and delta is:
(r - sum[i])/(sum[i+1] - sum[i])
not me doing math at you but look at her go, she's beautiful π
β¨ EXAMPLES β¨
here's how you'd make a histogram with logarithmic bins covering the ranges [1,10), [10,100) and [100,1000). chef's kiss:
$h = gsl_histogram_alloc (3);
# bin[0] covers the range 1 <= x < 10
# bin[1] covers the range 10 <= x < 100
# bin[2] covers the range 100 <= x < 1000
$range = [ 1.0, 10.0, 100.0, 1000.0 ];
gsl_histogram_set_ranges($h, $range, 4);
four range points, three bins, math checks out, we ate π
π AUTHORS π
Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
πΈ COPYRIGHT AND LICENSE πΈ
Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie!! π¦ this fine piece of documentation was served to you by perl v5.42.3, dated 2026-08-20, under the name Math::...togram(3pm) β yes the name is doing a little bit of hiding, we love an enigmatic main character π now go write some code and free your memory when you're done, mwah πβ¨
π¦ where this page comes from
- ubuntu stonking: libmath-gsl-perl 0.49-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms