⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 histogram.texi

📁 开放gsl矩阵运算
💻 TEXI
📖 第 1 页 / 共 4 页
字号:
@cindex histograms@cindex binning dataThis chapter describes functions for creating histograms.  Histogramsprovide a convenient way of summarizing the distribution of a set ofdata. A histogram consists of a set of @dfn{bins} which count the numberof events falling into a given range of a continuous variable @math{x}.In GSL the bins of a histogram contain floating-point numbers, so theycan be used to record both integer and non-integer distributions.  Thebins can use arbitrary sets of ranges (uniformly spaced bins are thedefault).  Both one and two-dimensional histograms are supported.Once a histogram has been created it can also be converted into aprobability distribution function.  The library provides efficientroutines for selecting random samples from probabilitydistributions.  This can be useful for generating simulations based realdata.The functions are declared in the header files @file{gsl_histogram.h}and @file{gsl_histogram2d.h}.@menu* The histogram struct::        * Histogram allocation::        * Copying Histograms::          * Updating and accessing histogram elements::  * Searching histogram ranges::  * Histogram Statistics::        * Histogram Operations::        * Reading and writing histograms::  * Resampling from histograms::  * The histogram probability distribution struct::  * Example programs for histograms::  * Two dimensional histograms::  * The 2D histogram struct::     * 2D Histogram allocation::     * Copying 2D Histograms::       * Updating and accessing 2D histogram elements::  * Searching 2D histogram ranges::  * 2D Histogram Statistics::     * 2D Histogram Operations::     * Reading and writing 2D histograms::  * Resampling from 2D histograms::  * Example programs for 2D histograms::  @end menu@node The histogram struct@section The histogram structA histogram is defined by the following struct,@deftp {Data Type} {gsl_histogram}@table @code@item size_t nThis is the number of histogram bins@item double * rangeThe ranges of the bins are stored in an array of @var{n+1} elementspointed to by @var{range}.@item double * binThe counts for each bin are stored in an array of @var{n} elementspointed to by @var{bin}.  The bins are floating-point numbers, so you canincrement them by non-integer values if necessary.@end table@end deftp@comment @noindentThe range for @var{bin}[i] is given by @var{range}[i] to@var{range}[i+1].  For @math{n} bins there are @math{n+1} entries in thearray @var{range}.  Each bin is inclusive at the lower end and exclusiveat the upper end.  Mathematically this means that the bins are defined bythe following inequality,@tex\beforedisplay$$\hbox{bin[i] corresponds to range[i]} \le x < \hbox{range[i+1]}$$\afterdisplay@end tex@ifinfo@displaybin[i] corresponds to range[i] <= x < range[i+1]@end display@end ifinfo@noindentHere is a diagram of the correspondence between ranges and bins on thenumber-line for @math{x},@smallexample     [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[5] )  ---|---------|---------|---------|---------|---------|---  x   r[0]      r[1]      r[2]      r[3]      r[4]      r[5]@end smallexample@noindentIn this picture the values of the @var{range} array are denoted by@math{r}.  On the left-hand side of each bin the square bracket"@code{[}" denotes an inclusive lower bound (@c{$r \le x$}@math{r <= x}), and the round parentheses "@code{)}" on the right-handside denote an exclusive upper bound (@math{x < r}).  Thus any sampleswhich fall on the upper end of the histogram are excluded.  If you wantto include this value for the last bin you will need to add an extra binto your histogram.The @code{gsl_histogram} struct and its associated functions are definedin the header file @file{gsl_histogram.h}.@node Histogram allocation@section Histogram allocationThe functions for allocating memory to a histogram follow the style of@code{malloc} and @code{free}.  In addition they also perform their ownerror checking.  If there is insufficient memory available to allocate ahistogram then the functions call the error handler (with an errornumber of @code{GSL_ENOMEM}) in addition to returning a null pointer.Thus if you use the library error handler to abort your program then itisn't necessary to check every histogram @code{alloc}.@deftypefun {gsl_histogram *} gsl_histogram_alloc (size_t @var{n})This function allocates memory for a histogram with @var{n} bins, andreturns a pointer to a newly created @code{gsl_histogram} struct.  Ifinsufficient memory is available a null pointer is returned and theerror handler is invoked with an error code of @code{GSL_ENOMEM}. Thebins and ranges are not initialized, and should be prepared using one ofthe range-setting functions below in order to make the histogram readyfor use.@end deftypefun@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc (size_t @var{n})@comment This function allocates memory for a histogram with @var{n} bins, and@comment returns a pointer to its newly initialized @code{gsl_histogram} struct.@comment The bins are uniformly spaced with a total range of @comment @c{$0 \le  x < n$}@comment @math{0 <=  x < n},@comment as shown in the table below.@comment @tex@comment \beforedisplay@comment $$@comment \matrix{@comment \hbox{bin[0]}&\hbox{corresponds to}& 0 \le x < 1\cr@comment \hbox{bin[1]}&\hbox{corresponds to}& 1 \le x < 2\cr@comment \dots&\dots&\dots\cr@comment \hbox{bin[n-1]}&\hbox{corresponds to}&n-1 \le x < n}@comment $$@comment \afterdisplay@comment @end tex@comment @ifinfo@comment @display@comment bin[0] corresponds to 0 <= x < 1@comment bin[1] corresponds to 1 <= x < 2@comment @dots{}@comment bin[n-1] corresponds to n-1 <= x < n@comment @end display@comment @end ifinfo@comment @noindent@comment The bins are initialized to zero so the histogram is ready for use.@comment If insufficient memory is available a null pointer is returned and the@comment error handler is invoked with an error code of @code{GSL_ENOMEM}.@comment @end deftypefun@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc_uniform (size_t @var{n}, double @var{xmin}, double @var{xmax})@comment This function allocates memory for a histogram with @var{n} uniformly@comment spaced bins from @var{xmin} to @var{xmax}, and returns a pointer to the@comment newly initialized @code{gsl_histogram} struct. @comment If insufficient memory is available a null pointer is returned and the@comment error handler is invoked with an error code of @code{GSL_ENOMEM}.@comment @end deftypefun@comment @deftypefun {gsl_histogram *} gsl_histogram_calloc_range (size_t @var{n}, double * @var{range})@comment This function allocates a histogram of size @var{n} using the @math{n+1}@comment bin ranges specified by the array @var{range}.@comment @end deftypefun@deftypefun int gsl_histogram_set_ranges (gsl_histogram * @var{h}, const double @var{range}[], size_t @var{size})This function sets the ranges of the existing histogram @var{h} usingthe array @var{range} of size @var{size}.  The values of the histogrambins are reset to zero.  The @code{range} array should contain thedesired bin limits.  The ranges can be arbitrary, subject to therestriction that they are monotonically increasing.The following example shows how to create a histogram with logarithmicbins with ranges [1,10), [10,100) and [100,1000).@examplegsl_histogram * 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 */double range[4] = @{ 1.0, 10.0, 100.0, 1000.0 @};gsl_histogram_set_ranges (h, range, 4);@end example@noindentNote that the size of the @var{range} array should be defined to be oneelement bigger than the number of bins.  The additional element isrequired for the upper value of the final bin.@end deftypefun@deftypefun int gsl_histogram_set_ranges_uniform (gsl_histogram * @var{h}, double @var{xmin}, double @var{xmax})This function sets the ranges of the existing histogram @var{h} to coverthe range @var{xmin} to @var{xmax} uniformly.  The values of thehistogram bins are reset to zero.  The bin ranges are shown in the tablebelow,@tex\beforedisplay$$\matrix{\hbox{bin[0]}&\hbox{corresponds to}& xmin \le  x < xmin + d\cr\hbox{bin[1]} &\hbox{corresponds to}& xmin + d \le  x < xmin + 2 d\cr\dots&\dots&\dots\cr\hbox{bin[n-1]} & \hbox{corresponds to}& xmin + (n-1)d \le  x < xmax}$$\afterdisplay@end tex@ifinfo@displaybin[0] corresponds to xmin <= x < xmin + dbin[1] corresponds to xmin + d <= x < xmin + 2 d......bin[n-1] corresponds to xmin + (n-1)d <= x < xmax@end display@end ifinfo@noindentwhere @math{d} is the bin spacing, @math{d = (xmax-xmin)/n}.@end deftypefun@deftypefun void gsl_histogram_free (gsl_histogram * h)This function frees the histogram @var{h} and all of the memoryassociated with it.@end deftypefun@node Copying Histograms@section Copying Histograms@deftypefun int gsl_histogram_memcpy (gsl_histogram * @var{dest}, const gsl_histogram * @var{src})This function copies the histogram @var{src} into the pre-existinghistogram @var{dest}, making @var{dest} into an exact copy of @var{src}.The two histograms must be of the same size.@end deftypefun@deftypefun {gsl_histogram *} gsl_histogram_clone (const gsl_histogram * @var{src})This function returns a pointer to a newly created histogram which is anexact copy of the histogram @var{src}.@end deftypefun@node Updating and accessing histogram elements@section Updating and accessing histogram elementsThere are two ways to access histogram bins, either by specifying an@math{x} coordinate or by using the bin-index directly.  The functionsfor accessing the histogram through @math{x} coordinates use a binarysearch to identify the bin which covers the appropriate range.@deftypefun int gsl_histogram_increment (gsl_histogram * h, double x)This function updates the histogram @var{h} by adding one (1.0) to thebin whose range contains the coordinate @var{x}. If @var{x} lies in the valid range of the histogram then the functionreturns zero to indicate success.  If @var{x} is less than the lowerlimit of the histogram then the function returns @code{GSL_EDOM}, andnone of bins are modified.  Similarly, if the value of @var{x} is greaterthan or equal to the upper limit of the histogram then the functionreturns @code{GSL_EDOM}, and none of the bins are modified.  The errorhandler is not called, however, since it is often necessary to computehistogram for a small range of a larger dataset, ignoring the valuesoutside the range of interest.@end deftypefun@deftypefun int gsl_histogram_accumulate (gsl_histogram * @var{h}, double @var{x}, double @var{weight})This function is similar to @code{gsl_histogram_increment} but increasesthe value of the appropriate bin in the histogram @var{h} by thefloating-point number @var{weight}.@end deftypefun@deftypefun double gsl_histogram_get (const gsl_histogram * @var{h}, size_t @var{i})This function returns the contents of the @var{i}th bin of the histogram@var{h}.  If @var{i} lies outside the valid range of indices for thehistogram then the error handler is called with an error code of@code{GSL_EDOM} and the function returns 0.@end deftypefun@deftypefun int gsl_histogram_get_range (const gsl_histogram * @var{h}, size_t @var{i}, double * @var{lower}, double * @var{upper})This function finds the upper and lower range limits of the @var{i}thbin of the histogram @var{h}.  If the index @var{i} is valid then thecorresponding range limits are stored in @var{lower} and @var{upper}.The lower limit is inclusive (i.e. events with this coordinate areincluded in the bin) and the upper limit is exclusive (i.e. events withthe coordinate of the upper limit are excluded and fall in theneighboring higher bin, if it exists).  The function returns 0 toindicate success.  If @var{i} lies outside the valid range of indices forthe histogram then the error handler is called and the function returnsan error code of @code{GSL_EDOM}.@end deftypefun@deftypefun double gsl_histogram_max (const gsl_histogram * @var{h})@deftypefunx double gsl_histogram_min (const gsl_histogram * @var{h})@deftypefunx size_t gsl_histogram_bins (const gsl_histogram * @var{h})These functions return the maximum upper and minimum lower range limitsand the number of bins of the histogram @var{h}.  They provide a way of

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -