ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
histogram.hpp
Go to the documentation of this file.
1/*
2 * $Id: histogram.hpp 293 2012-12-17 20:27:36Z jdl3 $
3 * Copyright (C) 2012, 2020 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_HISTOGRAM_HPP
21#define CCGSL_HISTOGRAM_HPP
22
23#include<new>
24#include<gsl/gsl_histogram.h>
25#include"exception.hpp"
26
27namespace gsl {
31 class histogram {
32 public:
37 ccgsl_pointer = 0;
38 count = 0; // initially nullptr will do
39 }
40 // Refines random access container
41 // Refines assignable
46 explicit histogram( size_t const n ){
47 ccgsl_pointer = gsl_histogram_alloc( n );
48 // just plausibly we could allocate histogram but not count
49 try { count = new size_t; } catch( std::bad_alloc& e ){
50 // try to tidy up before rethrowing
51 gsl_histogram_free( ccgsl_pointer );
52 throw e;
53 }
54 *count = 1; // initially there is just one reference to ccgsl_pointer
55 }
62 explicit histogram( gsl_histogram* v ){
63 ccgsl_pointer = v;
64 // just plausibly we could fail to allocate count: no further action needed.
65 count = new size_t;
66 *count = 1; // initially there is just one reference to ccgsl_pointer
67 }
68 // copy constructor
74 count = v.count; if( count != 0 ) ++*count; }
75 // assignment operator
81 // first, possibly delete anything pointed to by this
82 if( count == 0 or --*count == 0 ){
83 if( ccgsl_pointer != 0 ) gsl_histogram_free( ccgsl_pointer );
84 delete count;
85 } // Then copy
86 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
87 }
88 // destructor
93 if( count == 0 or --*count == 0 ){
94 // could have allocated null pointer
95 if( ccgsl_pointer != 0 ) gsl_histogram_free( ccgsl_pointer );
96 delete count;
97 }
98 }
99#ifdef __GXX_EXPERIMENTAL_CXX0X__
105 std::swap( count, v.count );
106 v.ccgsl_pointer = nullptr;
107 }
114 histogram( std::move( v ) ).swap( *this );
115 return *this;
116 }
117#endif
118 // Refines equality comparable
119 // == operator
126 bool operator==( histogram const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
127 // != operator
134 bool operator!=( histogram const& v ) const { return not operator==( v ); }
135 // Refines forward container
136 // Refines less than comparable
137 // operator<
146 bool operator<( histogram const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
147 // operator>
156 bool operator>( histogram const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
157 // operator<=
166 bool operator<=( histogram const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
167 // operator>=
176 bool operator>=( histogram const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
181 bool empty() const { return ccgsl_pointer == 0; }
182 // swap() --- should work even if sizes don't match
188 void swap( histogram& v ){
189 std::swap( ccgsl_pointer, v.ccgsl_pointer );
190 std::swap( count, v.count );
191 }
192 private:
196 gsl_histogram* ccgsl_pointer;
200 size_t* count;
201 public:
202 // shared reference functions
207 gsl_histogram* get() const { return ccgsl_pointer; }
213 bool unique() const { return count != 0 and *count == 1; }
218 size_t use_count() const { return count == 0 ? 0 : *count; }
224#ifdef __GXX_EXPERIMENTAL_CXX0X__
225 explicit
226#endif
227 operator bool() const { return ccgsl_pointer != 0; }
228
229#ifndef DOXYGEN_SKIP
235 inline static histogram calloc( size_t n ){ return histogram( gsl_histogram_calloc( n ) ); }
236
244 inline static histogram calloc_uniform( size_t const n, double const xmin, double const xmax ){
245 return histogram( gsl_histogram_calloc_uniform( n, xmin, xmax ) ); }
246#endif /* DOXYGEN_SKIP */
247
253 int increment( double x ){ return gsl_histogram_increment( get(), x ); }
254
261 int accumulate( double x, double weight ){
262 return gsl_histogram_accumulate( get(), x, weight ); }
263
264#ifndef DOXYGEN_SKIP
271 int find( double const x, size_t* i ) const {
272 return gsl_histogram_find( get(), x, i ); }
273#endif /* DOXYGEN_SKIP */
280 int find( double const x, size_t& i ) const {
281 return gsl_histogram_find( get(), x, &i ); }
282
288 double get( size_t i ) const { return gsl_histogram_get( get(), i ); }
289
295 double operator[]( size_t i ) const { return gsl_histogram_get( get(), i ); }
296
304 int get_range( size_t i, double* lower, double* upper ) const {
305 return gsl_histogram_get_range( get(), i, lower, upper ); }
313 int get_range( size_t i, double& lower, double& upper ) const {
314 return gsl_histogram_get_range( get(), i, &lower, &upper ); }
315
320 double max() const { return gsl_histogram_max( get() ); }
321
326 double min() const { return gsl_histogram_min( get() ); }
327
332 size_t bins() const { return gsl_histogram_bins( get() ); }
333
337 void reset(){ gsl_histogram_reset( get() ); }
338
339#ifndef DOXYGEN_SKIP
346 inline static histogram calloc_range( size_t n, double* range ){
347 return histogram( gsl_histogram_calloc_range( n, range ) ); }
354 template<typename RANGE>
355 inline static histogram calloc_range( RANGE& range ){
356 return histogram( gsl_histogram_calloc_range( range.size(), range.data() ) ); }
357#endif /* DOXYGEN_SKIP */
358
365 template<typename RANGE>
366 int set_ranges( RANGE const& range ){
367 return gsl_histogram_set_ranges( get(), range.data(), range.size() ); }
368
375 int set_ranges_uniform( double xmin, double xmax ){
376 return gsl_histogram_set_ranges_uniform( get(), xmin, xmax ); }
377
383 int memcpy( histogram const& source ){ return gsl_histogram_memcpy( get(), source.get() ); }
384
389 histogram clone() const { return histogram( gsl_histogram_clone( get() ) ); }
390
395 double max_val() const { return gsl_histogram_max_val( get() ); }
396
401 size_t max_bin() const { return gsl_histogram_max_bin( get() ); }
402
407 double min_val() const { return gsl_histogram_min_val( get() ); }
408
413 size_t min_bin() const { return gsl_histogram_min_bin( get() ); }
414
421 bool equal_bins_p( histogram const& h2 ) const {
422 return gsl_histogram_equal_bins_p( get(), h2.get() ) != 0; }
423
429 int add( histogram const& h2 ){ return gsl_histogram_add( get(), h2.get() ); }
430
436 int sub( histogram const& h2 ){ return gsl_histogram_sub( get(), h2.get() ); }
437
443 int mul( histogram const& h2 ){ return gsl_histogram_mul( get(), h2.get() ); }
444
450 int div( histogram const& h2 ){ return gsl_histogram_div( get(), h2.get() ); }
451
457 int scale( double const scale ){ return gsl_histogram_scale( get(), scale ); }
458
464 int shift( double const shift ){ return gsl_histogram_shift( get(), shift ); }
465
470 double sigma() const { return gsl_histogram_sigma( get() ); }
471
476 double mean() const { return gsl_histogram_mean( get() ); }
477
482 double sum() const { return gsl_histogram_sum( get() ); }
483
489 int fwrite( FILE* stream ) const { return gsl_histogram_fwrite( stream, get() ); }
490
496 int fread( FILE* stream ){ return gsl_histogram_fread( stream, get() ); }
497
505 int fprintf( FILE* stream, char const* range_format, char const* bin_format ) const {
506 return gsl_histogram_fprintf( stream, get(), range_format, bin_format ); }
507
513 int fscanf( FILE* stream ){ return gsl_histogram_fscanf( stream, get() ); }
514
515 //PDF functions
519 class pdf {
520 public:
525 ccgsl_pointer = 0;
526 count = 0; // initially nullptr will do
527 }
528 // Refines random access container
529 // Refines assignable
534 explicit pdf( size_t const n ){
535 ccgsl_pointer = gsl_histogram_pdf_alloc( n );
536 // just plausibly we could allocate pdf but not count
537 try { count = new size_t; } catch( std::bad_alloc& e ){
538 // try to tidy up before rethrowing
539 gsl_histogram_pdf_free( ccgsl_pointer );
540 throw e;
541 }
542 *count = 1; // initially there is just one reference to ccgsl_pointer
543 }
550 explicit pdf( gsl_histogram_pdf* v ){
551 ccgsl_pointer = v;
552 // just plausibly we could fail to allocate count: no further action needed.
553 count = new size_t;
554 *count = 1; // initially there is just one reference to ccgsl_pointer
555 }
556 // copy constructor
562 count = v.count; if( count != 0 ) ++*count; }
563 // assignment operator
568 pdf& operator=( pdf const& v ){
569 // first, possibly delete anything pointed to by this
570 if( count == 0 or --*count == 0 ){
571 if( ccgsl_pointer != 0 ) gsl_histogram_pdf_free( ccgsl_pointer );
572 delete count;
573 } // Then copy
575 if( count != 0 ){ ++*count; }
576 return *this;
577 }
578 // destructor
583 if( count == 0 or --*count == 0 ){
584 // could have allocated null pointer
585 if( ccgsl_pointer != 0 ) gsl_histogram_pdf_free( ccgsl_pointer );
586 delete count;
587 }
588 }
589#ifdef __GXX_EXPERIMENTAL_CXX0X__
594 pdf( pdf&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
595 std::swap( count, v.count );
596 v.ccgsl_pointer = nullptr;
597 }
604 pdf( std::move( v ) ).swap( *this );
605 return *this;
606 }
607#endif
608 // Refines equality comparable
609 // == operator
616 bool operator==( pdf const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
617 // != operator
624 bool operator!=( pdf const& v ) const { return not operator==( v ); }
625 // Refines forward container
626 // Refines less than comparable
627 // operator<
636 bool operator<( pdf const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
637 // operator>
646 bool operator>( pdf const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
647 // operator<=
656 bool operator<=( pdf const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
657 // operator>=
666 bool operator>=( pdf const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
671 bool empty() const { return ccgsl_pointer == 0; }
672 // swap() --- should work even if sizes don't match
678 void swap( pdf& v ){
679 std::swap( ccgsl_pointer, v.ccgsl_pointer );
680 std::swap( count, v.count );
681 }
682 private:
686 gsl_histogram_pdf* ccgsl_pointer;
690 size_t* count;
691 public:
692 // shared reference functions
697 gsl_histogram_pdf* get() const { return ccgsl_pointer; }
703 bool unique() const { return count != 0 and *count == 1; }
708 size_t use_count() const { return count == 0 ? 0 : *count; }
714#ifdef __GXX_EXPERIMENTAL_CXX0X__
715 explicit
716#endif
717 operator bool() const { return ccgsl_pointer != 0; }
718
724 int init( histogram const& h ){ return gsl_histogram_pdf_init( get(), h.get() ); }
725
731 double sample( double r ){ return gsl_histogram_pdf_sample( get(), r ); }
732 };
733 };
734}
735#endif
736
Histogram probability (empirical) density functions.
Definition: histogram.hpp:519
bool empty() const
Find if the pdf is empty.
Definition: histogram.hpp:671
bool operator<(pdf const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:636
void swap(pdf &v)
Swap two pdf objects.
Definition: histogram.hpp:678
bool operator==(pdf const &v) const
Two pdf are identically equal if their elements are identical.
Definition: histogram.hpp:616
int init(histogram const &h)
C++ version of gsl_histogram_pdf_init().
Definition: histogram.hpp:724
size_t use_count() const
Find how many pdf objects share this pointer.
Definition: histogram.hpp:708
pdf(pdf const &v)
The copy constructor.
Definition: histogram.hpp:561
pdf & operator=(pdf const &v)
The assignment operator.
Definition: histogram.hpp:568
bool operator>=(pdf const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:666
size_t * count
The shared reference count.
Definition: histogram.hpp:690
pdf()
The default constructor is only really useful for assigning to.
Definition: histogram.hpp:524
pdf(gsl_histogram_pdf *v)
Could construct from a gsl_histogram_pdf.
Definition: histogram.hpp:550
gsl_histogram_pdf * ccgsl_pointer
The shared pointer.
Definition: histogram.hpp:686
bool operator!=(pdf const &v) const
Two pdf are different if their elements are not identical.
Definition: histogram.hpp:624
pdf(pdf &&v)
Move constructor.
Definition: histogram.hpp:594
bool operator<=(pdf const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:656
double sample(double r)
C++ version of gsl_histogram_pdf_sample().
Definition: histogram.hpp:731
gsl_histogram_pdf * get() const
Get the gsl_histogram_pdf.
Definition: histogram.hpp:697
pdf(size_t const n)
The default constructor creates a new pdf with n elements.
Definition: histogram.hpp:534
bool unique() const
Find if this is the only object sharing the gsl_histogram_pdf.
Definition: histogram.hpp:703
bool operator>(pdf const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:646
~pdf()
The destructor only deletes the pointers if count reaches zero.
Definition: histogram.hpp:582
pdf & operator=(pdf &&v)
Move operator.
Definition: histogram.hpp:603
Histograms.
Definition: histogram.hpp:31
void swap(histogram &v)
Swap two histogram objects.
Definition: histogram.hpp:188
int get_range(size_t i, double &lower, double &upper) const
C++ version of gsl_histogram_get_range().
Definition: histogram.hpp:313
double operator[](size_t i) const
C++ version of gsl_histogram_get().
Definition: histogram.hpp:295
int fread(FILE *stream)
C++ version of gsl_histogram_fread().
Definition: histogram.hpp:496
double min_val() const
C++ version of gsl_histogram_min_val().
Definition: histogram.hpp:407
double mean() const
C++ version of gsl_histogram_mean().
Definition: histogram.hpp:476
double get(size_t i) const
C++ version of gsl_histogram_get().
Definition: histogram.hpp:288
bool empty() const
Find if the histogram is empty.
Definition: histogram.hpp:181
int fscanf(FILE *stream)
C++ version of gsl_histogram_fscanf().
Definition: histogram.hpp:513
int mul(histogram const &h2)
C++ version of gsl_histogram_mul().
Definition: histogram.hpp:443
bool operator==(histogram const &v) const
Two histogram are identically equal if their elements are identical.
Definition: histogram.hpp:126
size_t min_bin() const
C++ version of gsl_histogram_min_bin().
Definition: histogram.hpp:413
histogram clone() const
C++ version of gsl_histogram_clone().
Definition: histogram.hpp:389
int sub(histogram const &h2)
C++ version of gsl_histogram_sub().
Definition: histogram.hpp:436
bool operator<=(histogram const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:166
int memcpy(histogram const &source)
C++ version of gsl_histogram_memcpy().
Definition: histogram.hpp:383
bool operator>(histogram const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:156
int fprintf(FILE *stream, char const *range_format, char const *bin_format) const
C++ version of gsl_histogram_fprintf().
Definition: histogram.hpp:505
int find(double const x, size_t &i) const
C++ version of gsl_histogram_find().
Definition: histogram.hpp:280
bool unique() const
Find if this is the only object sharing the gsl_histogram.
Definition: histogram.hpp:213
double sigma() const
C++ version of gsl_histogram_sigma().
Definition: histogram.hpp:470
double min() const
C++ version of gsl_histogram_min().
Definition: histogram.hpp:326
void reset()
C++ version of gsl_histogram_reset().
Definition: histogram.hpp:337
histogram()
The default constructor is only really useful for assigning to.
Definition: histogram.hpp:36
size_t bins() const
C++ version of gsl_histogram_bins().
Definition: histogram.hpp:332
int increment(double x)
C++ version of gsl_histogram_increment().
Definition: histogram.hpp:253
histogram(histogram const &v)
The copy constructor.
Definition: histogram.hpp:73
bool operator>=(histogram const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:176
~histogram()
The destructor only deletes the pointers if count reaches zero.
Definition: histogram.hpp:92
int add(histogram const &h2)
C++ version of gsl_histogram_add().
Definition: histogram.hpp:429
int scale(double const scale)
C++ version of gsl_histogram_scale().
Definition: histogram.hpp:457
size_t * count
The shared reference count.
Definition: histogram.hpp:200
gsl_histogram * get() const
Get the gsl_histogram.
Definition: histogram.hpp:207
int set_ranges(RANGE const &range)
C++ version of gsl_histogram_set_ranges().
Definition: histogram.hpp:366
int get_range(size_t i, double *lower, double *upper) const
C++ version of gsl_histogram_get_range().
Definition: histogram.hpp:304
histogram(gsl_histogram *v)
Could construct from a gsl_histogram.
Definition: histogram.hpp:62
bool operator<(histogram const &v) const
A container needs to define an ordering for sorting.
Definition: histogram.hpp:146
gsl_histogram * ccgsl_pointer
The shared pointer.
Definition: histogram.hpp:196
histogram(histogram &&v)
Move constructor.
Definition: histogram.hpp:104
size_t max_bin() const
C++ version of gsl_histogram_max_bin().
Definition: histogram.hpp:401
size_t use_count() const
Find how many histogram objects share this pointer.
Definition: histogram.hpp:218
bool equal_bins_p(histogram const &h2) const
C++ version of gsl_histogram_equal_bins_p().
Definition: histogram.hpp:421
histogram(size_t const n)
The default constructor creates a new histogram with n elements.
Definition: histogram.hpp:46
int fwrite(FILE *stream) const
C++ version of gsl_histogram_fwrite().
Definition: histogram.hpp:489
histogram & operator=(histogram const &v)
The assignment operator.
Definition: histogram.hpp:80
int div(histogram const &h2)
C++ version of gsl_histogram_div().
Definition: histogram.hpp:450
int set_ranges_uniform(double xmin, double xmax)
C++ version of gsl_histogram_set_ranges_uniform().
Definition: histogram.hpp:375
int accumulate(double x, double weight)
C++ version of gsl_histogram_accumulate().
Definition: histogram.hpp:261
histogram & operator=(histogram &&v)
Move operator.
Definition: histogram.hpp:113
bool operator!=(histogram const &v) const
Two histogram are different if their elements are not identical.
Definition: histogram.hpp:134
int shift(double const shift)
C++ version of gsl_histogram_shift().
Definition: histogram.hpp:464
double max_val() const
C++ version of gsl_histogram_max_val().
Definition: histogram.hpp:395
double sum() const
C++ version of gsl_histogram_sum().
Definition: histogram.hpp:482
double max() const
C++ version of gsl_histogram_max().
Definition: histogram.hpp:320
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34