Alien-libhisto
view release on metacpan or search on metacpan
bundled/include/histo/histo.h view on Meta::CPAN
/* ========================================================================= */
/* Lifecycle & Memory Management */
/* ========================================================================= */
/**
* @brief Creates a new uniform-bin histogram over the range [min, max].
*
* @param[in] nbins Number of equidistant bins (1 <= nbins <= HISTO_MAX_NBINS).
* @param[in] min Lower boundary coordinate (finite, min < max).
* @param[in] max Upper boundary coordinate (finite, max > min).
* @param[in] flags Bitwise OR of feature flags (e.g. HISTO_FLAG_TRACK_SUMW2).
* @return Pointer to initialized histogram handle, or NULL on error.
*/
histo_t* histo_create_uniform(uint32_t nbins, double min, double max, uint32_t flags);
/**
* @brief Creates a new variable-bin histogram from an array of monotonic edges.
*
* @param[in] nbins Number of bins (1 <= nbins <= HISTO_MAX_NBINS).
* @param[in] edges Array of (nbins + 1) strictly increasing edge values.
* @param[in] flags Bitwise OR of feature flags.
* @return Pointer to initialized histogram handle, or NULL on error.
*/
histo_t* histo_create_variable(uint32_t nbins, const double *edges, uint32_t flags);
/**
* @brief Destroys a histogram and releases all allocated memory.
*
* Safe to call with NULL.
*
* @param[in,out] h Histogram handle to destroy.
*/
void histo_destroy(histo_t *h);
/**
* @brief Creates an exact clone or empty schema copy of a histogram.
*
* @param[in] src Source histogram to clone.
* @param[in] empty If true, allocate same binning structure but clear all counts.
* @return Pointer to new cloned histogram handle, or NULL on error.
*/
histo_t* histo_clone(const histo_t *src, bool empty);
/**
* @brief Resets all bin contents, weights, moments, and out-of-range counters to zero.
*
* @param[in,out] h Histogram handle to reset.
* @return HISTO_OK on success, or HISTO_ERR_INVALID_ARG if h is NULL.
*/
histo_status_t histo_reset(histo_t *h);
/* ========================================================================= */
/* Ingestion / Filling */
/* ========================================================================= */
/**
* @brief Fills a single value into the histogram with unit weight (1.0).
*
* @param[in,out] h Histogram handle.
* @param[in] x Sample coordinate.
* @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x is NaN/Inf.
*/
histo_status_t histo_fill(histo_t *h, double x);
/**
* @brief Fills a single value into the histogram with a specified weight.
*
* @param[in,out] h Histogram handle.
* @param[in] x Sample coordinate.
* @param[in] weight Real-valued event weight.
* @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x/weight is NaN/Inf.
*/
histo_status_t histo_fill_w(histo_t *h, double x, double weight);
/**
* @brief Batch fills N contiguous values with optional weights.
*
* @param[in,out] h Histogram handle.
* @param[in] n Number of samples to ingest.
* @param[in] x Contiguous array of sample coordinates.
* @param[in] weights Optional contiguous array of weights (NULL for unit weights).
* @return HISTO_OK on success, or HISTO_WARN_NON_FINITE if non-finite samples were skipped.
*/
histo_status_t histo_fill_n(histo_t *h, size_t n, const double *x, const double *weights);
/**
* @brief Strided batch fill for Array-of-Structs (AoS) data layouts.
*
* @param[in,out] h Histogram handle.
* @param[in] n Number of samples to ingest.
* @param[in] x Pointer to first sample coordinate.
* @param[in] x_stride_bytes Byte stride between consecutive sample coordinates.
* @param[in] weights Optional pointer to first sample weight (NULL for unit).
* @param[in] w_stride_bytes Byte stride between consecutive weights.
* @return HISTO_OK on success, or HISTO_WARN_NON_FINITE if non-finite samples were skipped.
*/
histo_status_t histo_fill_strided(histo_t *h, size_t n,
const double *x, size_t x_stride_bytes,
const double *weights, size_t w_stride_bytes);
/**
* @brief Directly accumulates weight into a specific bin index.
*
* @param[in,out] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[in] weight Weight to add.
* @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if bin_index >= nbins.
*/
histo_status_t histo_fill_bin(histo_t *h, uint32_t bin_index, double weight);
/* ========================================================================= */
/* Geometry & Bin Query */
/* ========================================================================= */
/**
* @brief Returns the number of in-range active bins.
*
* @param[in] h Histogram handle.
* @return Number of bins, or 0 if h is NULL.
*/
uint32_t histo_nbins(const histo_t *h);
/**
* @brief Returns the binning model type.
*
* @param[in] h Histogram handle.
* @return HISTO_BIN_UNIFORM or HISTO_BIN_VARIABLE.
*/
histo_bin_type_t histo_bin_type(const histo_t *h);
/**
* @brief Retrieves the overall range limits [min, max].
*
* @param[in] h Histogram handle.
* @param[out] out_min Output pointer for lower range limit.
* @param[out] out_max Output pointer for upper range limit.
* @return HISTO_OK on success, or HISTO_ERR_INVALID_ARG if any pointer is NULL.
*/
histo_status_t histo_range(const histo_t *h, double *out_min, double *out_max);
/**
* @brief Locates the bin index for coordinate x using boundary-guarded lookup.
*
* @param[in] h Histogram handle.
* @param[in] x Coordinate to locate.
* @param[out] out_bin Output pointer: [0, nbins-1] on hit, -1 for underflow, nbins for overflow.
* @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x is NaN.
*/
histo_status_t histo_find_bin(const histo_t *h, double x, int64_t *out_bin);
/**
* @brief Retrieves the boundary interval [lower, upper] for a specific bin.
*
* @param[in] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[out] out_lower Output pointer for lower bin boundary.
* @param[out] out_upper Output pointer for upper bin boundary.
* @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if bin_index >= nbins.
*/
histo_status_t histo_bin_bounds(const histo_t *h, uint32_t bin_index, double *out_lower, double *out_upper);
/**
* @brief Computes the midpoint center of a specific bin.
*
* @param[in] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[out] out_center Output pointer for midpoint coordinate.
* @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if bin_index >= nbins.
*/
histo_status_t histo_bin_center(const histo_t *h, uint32_t bin_index, double *out_center);
/**
* @brief Retrieves the accumulated content (weight sum) of a bin.
*
* @param[in] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[out] out_content Output pointer for bin content.
* @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if bin_index >= nbins.
*/
histo_status_t histo_bin_content(const histo_t *h, uint32_t bin_index, double *out_content);
/**
* @brief Calculates the statistical uncertainty (error) of a bin.
*
* Returns sqrt(sum_w2) if tracked, or Poisson sqrt(N) for unweighted bins.
*
* @param[in] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[out] out_error Output pointer for calculated uncertainty.
* @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if bin_index >= nbins.
*/
histo_status_t histo_bin_error(const histo_t *h, uint32_t bin_index, double *out_error);
/**
* @brief Retrieves the sum of squared weights (sum_w2) for a bin.
*
* @param[in] h Histogram handle.
* @param[in] bin_index Target bin index [0, nbins - 1].
* @param[out] out_sum_w2 Output pointer for sum_w2.
* @return HISTO_OK on success, or HISTO_ERR_INVALID_ARG if sum_w2 is not tracked.
*/
histo_status_t histo_bin_sum_w2(const histo_t *h, uint32_t bin_index, double *out_sum_w2);
/**
* @brief Retrieves total accumulated weight across all bins (excluding underflow/overflow).
*
* @param[in] h Histogram handle.
* @return Total weight, or 0.0 if h is NULL.
*/
double histo_total_weight(const histo_t *h);
/**
* @brief Retrieves total number of fill operations performed.
*
* @param[in] h Histogram handle.
* @return Total fill count, or 0 if h is NULL.
*/
uint64_t histo_num_entries(const histo_t *h);
/**
* @brief Retrieves total accumulated underflow weight.
*
* @param[in] h Histogram handle.
* @return Underflow weight sum, or 0.0 if h is NULL.
*/
double histo_underflow(const histo_t *h);
/**
* @brief Retrieves total accumulated underflow squared weights (sum of w^2).
*
* @param[in] h Histogram handle.
* @return Underflow sum_w2, or 0.0 if h is NULL or sum_w2 tracking is disabled.
*/
double histo_underflow_sum_w2(const histo_t *h);
/**
* @brief Retrieves total accumulated overflow weight.
*
* @param[in] h Histogram handle.
* @return Overflow weight sum, or 0.0 if h is NULL.
*/
double histo_overflow(const histo_t *h);
/**
* @brief Retrieves total accumulated overflow squared weights (sum of w^2).
*
* @param[in] h Histogram handle.
* @return Overflow sum_w2, or 0.0 if h is NULL or sum_w2 tracking is disabled.
*/
double histo_overflow_sum_w2(const histo_t *h);
/**
* @brief Retrieves the feature configuration flags bitmask for this histogram.
*
* @param[in] h Histogram handle.
* @return Bitmask of HISTO_FLAG_* flags, or 0 if h is NULL.
*/
uint32_t histo_flags(const histo_t *h);
/**
* @brief Retrieves total number of non-finite (NaN / Inf) rejected samples.
*
* @param[in] h Histogram handle.
* @return Non-finite sample count, or 0 if h is NULL.
*/
uint64_t histo_nan_count(const histo_t *h);
/* ========================================================================= */
/* Statistical Moments & Quantiles */
/* ========================================================================= */
/**
* @brief Computes the distribution mean.
*
* @param[in] h Histogram handle.
* @param[out] out_mean Output pointer for mean.
* @return HISTO_OK on success, or HISTO_ERR_EMPTY if histogram has zero weight.
*/
histo_status_t histo_mean(const histo_t *h, double *out_mean);
/**
* @brief Computes the distribution variance.
*
* @param[in] h Histogram handle.
* @param[out] out_variance Output pointer for variance.
* @return HISTO_OK on success, or HISTO_ERR_EMPTY if histogram has zero weight.
*/
histo_status_t histo_variance(const histo_t *h, double *out_variance);
/**
* @brief Computes the distribution standard deviation.
*
* @param[in] h Histogram handle.
* @param[out] out_std_dev Output pointer for standard deviation.
* @return HISTO_OK on success, or HISTO_ERR_EMPTY if histogram has zero weight.
*/
histo_status_t histo_std_dev(const histo_t *h, double *out_std_dev);
/**
* @brief Computes the k-th central statistical moment M_k = sum(w_i * (x_i - mean)^k) / total_weight.
*
* @param[in] h Histogram handle.
* @param[in] k Order of the central moment (k >= 0).
* @param[out] out_moment Output pointer for calculated moment.
* @return HISTO_OK on success, or HISTO_ERR_EMPTY if histogram has zero weight.
*/
histo_status_t histo_central_moment(const histo_t *h, uint32_t k, double *out_moment);
/**
* @brief Computes the distribution skewness (gamma_1 = M_3 / M_2^(3/2)).
*
* @param[in] h Histogram handle.
* @param[out] out_skewness Output pointer for skewness.
* @return HISTO_OK on success, HISTO_ERR_DIV_BY_ZERO if variance is zero, or HISTO_ERR_EMPTY.
*/
histo_status_t histo_skewness(const histo_t *h, double *out_skewness);
/**
* @brief Computes the distribution kurtosis (beta_2 = M_4 / M_2^2).
*
bundled/include/histo/histo.h view on Meta::CPAN
* @param[out] out_divergence Output pointer for calculated KL divergence in nats.
* @return HISTO_OK on success, HISTO_ERR_INCOMPATIBLE if geometries mismatch, or HISTO_ERR_EMPTY.
*/
histo_status_t histo_cmp_kl_divergence(const histo_t *h1, const histo_t *h2, double *out_divergence);
/**
* @brief Computes the Bhattacharyya distance -ln(sum(sqrt(P_i * Q_i))) between two histograms.
*
* @param[in] h1 First histogram.
* @param[in] h2 Second histogram.
* @param[out] out_distance Output pointer for Bhattacharyya distance (>= 0.0).
* @return HISTO_OK on success, HISTO_ERR_INCOMPATIBLE if geometries mismatch, or HISTO_ERR_EMPTY.
*/
histo_status_t histo_cmp_bhattacharyya(const histo_t *h1, const histo_t *h2, double *out_distance);
/* ========================================================================= */
/* Arithmetic Operations & Transformations */
/* ========================================================================= */
/**
* @brief Performs element-wise addition: target += other.
*
* @param[in,out] target Destination histogram to accumulate into.
* @param[in] other Source histogram to add.
* @return HISTO_OK on success, or HISTO_ERR_INCOMPATIBLE if geometries mismatch.
*/
histo_status_t histo_add(histo_t *target, const histo_t *other);
/**
* @brief Performs element-wise subtraction: target -= other.
*
* @param[in,out] target Destination histogram.
* @param[in] other Source histogram to subtract.
* @return HISTO_OK on success, or HISTO_ERR_INCOMPATIBLE if geometries mismatch.
*/
histo_status_t histo_subtract(histo_t *target, const histo_t *other);
/**
* @brief Performs element-wise multiplication: target *= other.
*
* @param[in,out] target Destination histogram.
* @param[in] other Source histogram to multiply by.
* @return HISTO_OK on success, or HISTO_ERR_INCOMPATIBLE if geometries mismatch.
*/
histo_status_t histo_multiply(histo_t *target, const histo_t *other);
/**
* @brief Performs element-wise division: target /= other.
*
* @param[in,out] target Destination histogram.
* @param[in] other Source histogram to divide by.
* @return HISTO_OK on success, or HISTO_ERR_INCOMPATIBLE if geometries mismatch.
*/
histo_status_t histo_divide(histo_t *target, const histo_t *other);
/**
* @brief Scales all bin contents and weights by a scalar factor.
*
* @param[in,out] h Histogram handle to scale.
* @param[in] factor Scalar multiplier (finite).
* @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if factor is NaN/Inf.
*/
histo_status_t histo_scale(histo_t *h, double factor);
/**
* @brief Normalizes the histogram such that total in-range weight equals target_area.
*
* @param[in,out] h Histogram handle to normalize.
* @param[in] target_area Desired integrated area (finite, target_area > 0.0).
* @return HISTO_OK on success, HISTO_ERR_EMPTY if zero weight, or HISTO_ERR_INVALID_ARG.
*/
histo_status_t histo_normalize(histo_t *h, double target_area);
/**
* @brief Merges adjacent uniform bins by an integer rebinning factor.
*
* @param[in] src Source uniform histogram.
* @param[in] factor Positive integer rebinning factor (nbins must be divisible by factor).
* @return Pointer to newly allocated rebinned histogram, or NULL on error.
*/
histo_t* histo_rebin(const histo_t *src, uint32_t factor);
/**
* @brief Extracts a subset slice of bins [start_bin, end_bin].
*
* @param[in] src Source histogram.
* @param[in] start_bin First bin index to include.
* @param[in] end_bin Last bin index to include (inclusive).
* @param[in] empty If true, allocate new sliced structure with zero counts.
* @return Pointer to newly allocated sliced histogram, or NULL on error.
*/
histo_t* histo_slice(const histo_t *src, uint32_t start_bin, uint32_t end_bin, bool empty);
/**
* @brief Generates a Cumulative Distribution Function (CDF) histogram.
*
* @param[in] src Source histogram.
* @param[in] prenormalization Normalization target (e.g. 1.0 or 100.0).
* @return Pointer to newly allocated CDF histogram, or NULL on error.
*/
histo_t* histo_cdf(const histo_t *src, double prenormalization);
/* ========================================================================= */
/* Serialization & Deserialization */
/* ========================================================================= */
/**
* @brief Calculates exact byte capacity required for canonical binary serialization.
*
* @param[in] h Histogram handle.
* @return Total byte length required (256-byte header + Little-Endian payloads).
*/
size_t histo_serialize_binary_size(const histo_t *h);
/**
* @brief Serializes histogram into a caller-allocated binary buffer.
*
* @param[in] h Histogram handle to serialize.
* @param[out] buf Caller-allocated destination buffer.
* @param[in] capacity Byte capacity of buf.
* @return HISTO_OK on success, or HISTO_ERR_SERIALIZATION if capacity too small.
( run in 0.937 second using v1.01-cache-2.11-cpan-60884a8df76 )