ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
histogram2d.hpp
Go to the documentation of this file.
1/*
2 * $Id: histogram2d.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_HISTOGRAM2D_HPP
21#define CCGSL_HISTOGRAM2D_HPP
22
23#include<new>
24#include<gsl/gsl_histogram2d.h>
25#include"exception.hpp"
26
27namespace gsl {
32 public:
37 ccgsl_pointer = 0;
38 count = 0; // initially nullptr will do
39 }
40 // Refines random access container
41 // Refines assignable
47 explicit histogram2d( size_t const nx, size_t const ny ){
48 ccgsl_pointer = gsl_histogram2d_alloc( nx, ny );
49 // just plausibly we could allocate histogram2d but not count
50 try { count = new size_t; } catch( std::bad_alloc& e ){
51 // try to tidy up before rethrowing
52 gsl_histogram2d_free( ccgsl_pointer );
53 throw e;
54 }
55 *count = 1; // initially there is just one reference to ccgsl_pointer
56 }
63 explicit histogram2d( gsl_histogram2d* v ){
64 ccgsl_pointer = v;
65 // just plausibly we could fail to allocate count: no further action needed.
66 count = new size_t;
67 *count = 1; // initially there is just one reference to ccgsl_pointer
68 }
69 // copy constructor
75 count = v.count; if( count != 0 ) ++*count; }
76 // assignment operator
82 // first, possibly delete anything pointed to by this
83 if( count == 0 or --*count == 0 ){
84 if( ccgsl_pointer != 0 ) gsl_histogram2d_free( ccgsl_pointer );
85 delete count;
86 } // Then copy
87 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
88 }
89 // destructor
94 if( count == 0 or --*count == 0 ){
95 // could have allocated null pointer
96 if( ccgsl_pointer != 0 ) gsl_histogram2d_free( ccgsl_pointer );
97 delete count;
98 }
99 }
100#ifdef __GXX_EXPERIMENTAL_CXX0X__
106 std::swap( count, v.count );
107 v.ccgsl_pointer = nullptr;
108 }
115 histogram2d( std::move( v ) ).swap( *this );
116 return *this;
117 }
118#endif
119 // Refines equality comparable
120 // == operator
127 bool operator==( histogram2d const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
128 // != operator
135 bool operator!=( histogram2d const& v ) const { return not operator==( v ); }
136 // Refines forward container
137 // Refines less than comparable
138 // operator<
147 bool operator<( histogram2d const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
148 // operator>
157 bool operator>( histogram2d const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
158 // operator<=
167 bool operator<=( histogram2d const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
168 // operator>=
177 bool operator>=( histogram2d const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
182 bool empty() const { return ccgsl_pointer == 0; }
183 // swap() --- should work even if sizes don't match
189 void swap( histogram2d& v ){
190 std::swap( ccgsl_pointer, v.ccgsl_pointer );
191 std::swap( count, v.count );
192 }
193 private:
197 gsl_histogram2d* ccgsl_pointer;
201 size_t* count;
202 public:
203 // shared reference functions
208 gsl_histogram2d* get() const { return ccgsl_pointer; }
214 bool unique() const { return count != 0 and *count == 1; }
219 size_t use_count() const { return count == 0 ? 0 : *count; }
225#ifdef __GXX_EXPERIMENTAL_CXX0X__
226 explicit
227#endif
228 operator bool() const { return ccgsl_pointer != 0; }
229
236 inline static histogram2d calloc( size_t const nx, size_t const ny ){
237 return histogram2d( gsl_histogram2d_calloc( nx, ny ) ); }
238
239#ifndef DOXYGEN_SKIP
250 inline static histogram2d calloc_uniform( size_t const nx, size_t const ny,
251 double const xmin, double const xmax,
252 double const ymin, double const ymax ){
253 return histogram2d( gsl_histogram2d_calloc_uniform( nx, ny, xmin, xmax, ymin, ymax ) ); }
254#endif /* DOXYGEN_SKIP */
255
262 int increment( double x, double y ){ return gsl_histogram2d_increment( get(), x, y ); }
263
271 int accumulate( double x, double y, double weight ){
272 return gsl_histogram2d_accumulate( get(), x, y, weight ); }
273
274#ifndef DOXYGEN_SKIP
283 int find( double const x, double const y, size_t* i, size_t* j ) const {
284 return gsl_histogram2d_find( get(), x, y, i, j ); }
285#endif /* DOXYGEN_SKIP */
294 int find( double const x, double const y, size_t& i, size_t& j ) const {
295 return gsl_histogram2d_find( get(), x, y, &i, &j ); }
296
303 double get( size_t const i, size_t const j ) const {
304 return gsl_histogram2d_get( get(), i, j ); }
305
306#ifndef DOXYGEN_SKIP
314 int get_xrange( size_t const i, double* xlower, double* xupper ) const {
315 return gsl_histogram2d_get_xrange( get(), i, xlower, xupper ); }
316#endif /* DOXYGEN_SKIP */
324 int get_xrange( size_t const i, double& xlower, double& xupper ) const {
325 return gsl_histogram2d_get_xrange( get(), i, &xlower, &xupper ); }
326
327#ifndef DOXYGEN_SKIP
335 int get_yrange( size_t const i, double* ylower, double* yupper ) const {
336 return gsl_histogram2d_get_yrange( get(), i, ylower, yupper ); }
337#endif /* DOXYGEN_SKIP */
345 int get_yrange( size_t const j, double& ylower, double& yupper ) const {
346 return gsl_histogram2d_get_yrange( get(), j, &ylower, &yupper ); }
347
352 double xmax() const { return gsl_histogram2d_xmax( get() ); }
353
358 double xmin() const { return gsl_histogram2d_xmin( get() ); }
359
364 size_t nx() const { return gsl_histogram2d_nx( get() ); }
365
370 double ymax() const { return gsl_histogram2d_ymax( get() ); }
371
376 double ymin() const { return gsl_histogram2d_ymin( get() ); }
377
382 size_t ny() const { return gsl_histogram2d_ny( get() ); }
383
387 void reset(){ gsl_histogram2d_reset( get() ); }
388
389#ifndef DOXYGEN_SKIP
398 inline static histogram2d calloc_range( size_t nx, size_t ny, double* xrange,
399 double* yrange ){
400 return histogram2d( gsl_histogram2d_calloc_range( nx, ny, xrange, yrange ) ); }
408 template<typename XRANGE,typename YRANGE>
409 inline static histogram2d calloc_range( XRANGE& xrange, YRANGE& yrange ){
410 return histogram2d( gsl_histogram2d_calloc_range( xrange.size(), yrange.size(),
411 xrange.data(), yrange.data() ) ); }
412#endif /* DOXYGEN_SKIP */
413
422 int set_ranges_uniform( double xmin, double xmax, double ymin, double ymax ){
423 return gsl_histogram2d_set_ranges_uniform( get(), xmin, xmax, ymin, ymax ); }
424
433 int set_ranges( double const xrange[], size_t xsize, double const yrange[], size_t ysize ){
434 return gsl_histogram2d_set_ranges( get(), xrange, xsize, yrange, ysize ); }
442 template<typename XRANGE,typename YRANGE>
443 int set_ranges( XRANGE const& xrange, YRANGE const& yrange ){
444 return gsl_histogram2d_set_ranges( get(), xrange.data(), xrange.size(),
445 yrange.data(), yrange.size() ); }
446
452 int memcpy( histogram2d const& source ){
453 return gsl_histogram2d_memcpy( get(), source.get() ); }
454
459 histogram2d clone() const { return histogram2d( gsl_histogram2d_clone( get() ) ); }
460
465 double max_val() const { return gsl_histogram2d_max_val( get() ); }
466
467#ifndef DOXYGEN_SKIP
473 void max_bin( size_t* i, size_t* j ) const { gsl_histogram2d_max_bin( get(), i, j ); }
474#endif /* DOXYGEN_SKIP */
480 void max_bin( size_t& i, size_t& j ) const { gsl_histogram2d_max_bin( get(), &i, &j ); }
481
486 double min_val() const { return gsl_histogram2d_min_val( get() ); }
487
488#ifndef DOXYGEN_SKIP
494 void min_bin( size_t* i, size_t* j ) const { gsl_histogram2d_min_bin( get(), i, j ); }
495#endif /* DOXYGEN_SKIP */
501 void min_bin( size_t& i, size_t& j ) const { gsl_histogram2d_min_bin( get(), &i, &j ); }
502
507 double xmean() const { return gsl_histogram2d_xmean( get() ); }
508
513 double ymean() const { return gsl_histogram2d_ymean( get() ); }
514
519 double xsigma() const { return gsl_histogram2d_xsigma( get() ); }
520
525 double ysigma() const { return gsl_histogram2d_ysigma( get() ); }
526
531 double cov() const { return gsl_histogram2d_cov( get() ); }
532
537 double sum() const { return gsl_histogram2d_sum( get() ); }
538
545 bool equal_bins_p( histogram2d const& h2 ) const {
546 return gsl_histogram2d_equal_bins_p( get(), h2.get() ) != 0; }
547
553 int add( histogram2d const& h2 ){ return gsl_histogram2d_add( get(), h2.get() ); }
554
560 int sub( histogram2d const& h2 ){ return gsl_histogram2d_sub( get(), h2.get() ); }
561
567 int mul( histogram2d const& h2 ){ return gsl_histogram2d_mul( get(), h2.get() ); }
568
574 int div( histogram2d const& h2 ){ return gsl_histogram2d_div( get(), h2.get() ); }
575
581 int scale( double scale ){ return gsl_histogram2d_scale( get(), scale ); }
582
588 int shift( double shift ){ return gsl_histogram2d_shift( get(), shift ); }
589
595 int fwrite( FILE* stream ) const { return gsl_histogram2d_fwrite( stream, get() ); }
596
602 int fread( FILE* stream ){ return gsl_histogram2d_fread( stream, get() ); }
603
611 int fprintf( FILE* stream, char const* range_format, char const* bin_format ) const {
612 return gsl_histogram2d_fprintf( stream, get(), range_format, bin_format ); }
613
619 int fscanf( FILE* stream ){ return gsl_histogram2d_fscanf( stream, get() ); }
620
621 //PDF functions
622
626 class pdf {
627 public:
632 ccgsl_pointer = 0;
633 count = 0; // initially nullptr will do
634 }
635 // Refines random access container
636 // Refines assignable
642 explicit pdf( size_t const nx, size_t const ny ){
643 ccgsl_pointer = gsl_histogram2d_pdf_alloc( nx, ny );
644 // just plausibly we could allocate pdf but not count
645 try { count = new size_t; } catch( std::bad_alloc& e ){
646 // try to tidy up before rethrowing
647 gsl_histogram2d_pdf_free( ccgsl_pointer );
648 throw e;
649 }
650 *count = 1; // initially there is just one reference to ccgsl_pointer
651 }
658 explicit pdf( gsl_histogram2d_pdf* v ){
659 ccgsl_pointer = v;
660 // just plausibly we could fail to allocate count: no further action needed.
661 count = new size_t;
662 *count = 1; // initially there is just one reference to ccgsl_pointer
663 }
664 // copy constructor
670 count = v.count; if( count != 0 ) ++*count; }
671 // assignment operator
676 pdf& operator=( pdf const& v ){
677 // first, possibly delete anything pointed to by this
678 if( count == 0 or --*count == 0 ){
679 if( ccgsl_pointer != 0 ) gsl_histogram2d_pdf_free( ccgsl_pointer );
680 delete count;
681 } // Then copy
682 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
683 }
684 // destructor
689 if( count == 0 or --*count == 0 ){
690 // could have allocated null pointer
691 if( ccgsl_pointer != 0 ) gsl_histogram2d_pdf_free( ccgsl_pointer );
692 delete count;
693 }
694 }
695#ifdef __GXX_EXPERIMENTAL_CXX0X__
700 pdf( pdf&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
701 std::swap( count, v.count );
702 v.ccgsl_pointer = nullptr;
703 }
710 pdf( std::move( v ) ).swap( *this );
711 return *this;
712 }
713#endif
714 // Refines equality comparable
715 // == operator
722 bool operator==( pdf const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
723 // != operator
730 bool operator!=( pdf const& v ) const { return not operator==( v ); }
731 // Refines forward container
732 // Refines less than comparable
733 // operator<
742 bool operator<( pdf const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
743 // operator>
752 bool operator>( pdf const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
753 // operator<=
762 bool operator<=( pdf const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
763 // operator>=
772 bool operator>=( pdf const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
777 bool empty() const { return ccgsl_pointer == 0; }
778 // swap() --- should work even if sizes don't match
784 void swap( pdf& v ){
785 std::swap( ccgsl_pointer, v.ccgsl_pointer );
786 std::swap( count, v.count );
787 }
788 private:
792 gsl_histogram2d_pdf* ccgsl_pointer;
796 size_t* count;
797 public:
798 // shared reference functions
803 gsl_histogram2d_pdf* get() const { return ccgsl_pointer; }
809 bool unique() const { return count != 0 and *count == 1; }
814 size_t use_count() const { return count == 0 ? 0 : *count; }
820#ifdef __GXX_EXPERIMENTAL_CXX0X__
821 explicit
822#endif
823 operator bool() const { return ccgsl_pointer != 0; }
824
830 int init( histogram2d const& h ) const {
831 return gsl_histogram2d_pdf_init( get(), h.get() ); }
832
833#ifndef DOXYGEN_SKIP
842 int sample( double r1, double r2, double* x, double* y ){
843 return gsl_histogram2d_pdf_sample( get(), r1, r2, x, y ); }
844#endif /* DOXYGEN_SKIP */
853 int sample( double r1, double r2, double& x, double& y ){
854 return gsl_histogram2d_pdf_sample( get(), r1, r2, &x, &y ); }
855
856 };
857 };
858}
859#endif
Empirical probability density functions.
size_t * count
The shared reference count.
bool operator<=(pdf const &v) const
A container needs to define an ordering for sorting.
bool operator==(pdf const &v) const
Two pdf are identically equal if their elements are identical.
pdf & operator=(pdf &&v)
Move operator.
bool operator>=(pdf const &v) const
A container needs to define an ordering for sorting.
gsl_histogram2d_pdf * get() const
Get the gsl_histogram2d_pdf.
size_t use_count() const
Find how many pdf objects share this pointer.
bool operator<(pdf const &v) const
A container needs to define an ordering for sorting.
pdf()
The default constructor is only really useful for assigning to.
pdf(pdf &&v)
Move constructor.
bool unique() const
Find if this is the only object sharing the gsl_histogram2d_pdf.
pdf(gsl_histogram2d_pdf *v)
Could construct from a gsl_histogram2d_pdf.
int init(histogram2d const &h) const
C++ version of gsl_histogram2d_pdf_init().
pdf(size_t const nx, size_t const ny)
The default constructor creates a new pdf with n elements.
pdf & operator=(pdf const &v)
The assignment operator.
int sample(double r1, double r2, double &x, double &y)
C++ version of gsl_histogram2d_pdf_sample().
bool empty() const
Find if the pdf is empty.
pdf(pdf const &v)
The copy constructor.
bool operator>(pdf const &v) const
A container needs to define an ordering for sorting.
void swap(pdf &v)
Swap two pdf objects.
bool operator!=(pdf const &v) const
Two pdf are different if their elements are not identical.
~pdf()
The destructor only deletes the pointers if count reaches zero.
gsl_histogram2d_pdf * ccgsl_pointer
The shared pointer.
2D histograms.
Definition: histogram2d.hpp:31
bool operator>(histogram2d const &v) const
A container needs to define an ordering for sorting.
double ymean() const
C++ version of gsl_histogram2d_ymean().
double xmean() const
C++ version of gsl_histogram2d_xmean().
int find(double const x, double const y, size_t &i, size_t &j) const
C++ version of gsl_histogram2d_find().
bool operator<(histogram2d const &v) const
A container needs to define an ordering for sorting.
bool empty() const
Find if the histogram2d is empty.
size_t nx() const
C++ version of gsl_histogram2d_nx().
double get(size_t const i, size_t const j) const
C++ version of gsl_histogram2d_get().
double ysigma() const
C++ version of gsl_histogram2d_ysigma().
int sub(histogram2d const &h2)
C++ version of gsl_histogram2d_sub().
bool operator==(histogram2d const &v) const
Two histogram2d are identically equal if their elements are identical.
double max_val() const
C++ version of gsl_histogram2d_max_val().
~histogram2d()
The destructor only deletes the pointers if count reaches zero.
Definition: histogram2d.hpp:93
int shift(double shift)
C++ version of gsl_histogram2d_shift().
static histogram2d calloc(size_t const nx, size_t const ny)
C++ version of gsl_histogram2d_calloc().
double sum() const
C++ version of gsl_histogram2d_sum().
int memcpy(histogram2d const &source)
C++ version of gsl_histogram2d_memcpy().
int scale(double scale)
C++ version of gsl_histogram2d_scale().
bool operator>=(histogram2d const &v) const
A container needs to define an ordering for sorting.
bool operator!=(histogram2d const &v) const
Two histogram2d are different if their elements are not identical.
void max_bin(size_t &i, size_t &j) const
C++ version of gsl_histogram2d_max_bin().
gsl_histogram2d * get() const
Get the gsl_histogram2d.
int get_xrange(size_t const i, double &xlower, double &xupper) const
C++ version of gsl_histogram2d_get_xrange().
int set_ranges(XRANGE const &xrange, YRANGE const &yrange)
C++ version of gsl_histogram2d_set_ranges().
gsl_histogram2d * ccgsl_pointer
The shared pointer.
int add(histogram2d const &h2)
C++ version of gsl_histogram2d_add().
double xsigma() const
C++ version of gsl_histogram2d_xsigma().
void swap(histogram2d &v)
Swap two histogram2d objects.
void min_bin(size_t &i, size_t &j) const
C++ version of gsl_histogram2d_min_bin().
histogram2d & operator=(histogram2d const &v)
The assignment operator.
Definition: histogram2d.hpp:81
void reset()
C++ version of gsl_histogram2d_reset().
int fprintf(FILE *stream, char const *range_format, char const *bin_format) const
C++ version of gsl_histogram2d_fprintf().
double xmin() const
C++ version of gsl_histogram2d_xmin().
int accumulate(double x, double y, double weight)
C++ version of gsl_histogram2d_accumulate().
histogram2d(histogram2d const &v)
The copy constructor.
Definition: histogram2d.hpp:74
int mul(histogram2d const &h2)
C++ version of gsl_histogram2d_mul().
int increment(double x, double y)
C++ version of gsl_histogram2d_increment().
size_t * count
The shared reference count.
histogram2d()
The default constructor is only really useful for assigning to.
Definition: histogram2d.hpp:36
histogram2d(size_t const nx, size_t const ny)
The default constructor creates a new histogram2d with n elements.
Definition: histogram2d.hpp:47
histogram2d & operator=(histogram2d &&v)
Move operator.
int set_ranges(double const xrange[], size_t xsize, double const yrange[], size_t ysize)
C++ version of gsl_histogram2d_set_ranges().
size_t use_count() const
Find how many histogram2d objects share this pointer.
bool unique() const
Find if this is the only object sharing the gsl_histogram2d.
size_t ny() const
C++ version of gsl_histogram2d_ny().
double xmax() const
C++ version of gsl_histogram2d_xmax().
int fwrite(FILE *stream) const
C++ version of gsl_histogram2d_fwrite().
double cov() const
C++ version of gsl_histogram2d_cov().
double ymax() const
C++ version of gsl_histogram2d_ymax().
int fread(FILE *stream)
C++ version of gsl_histogram2d_fread().
histogram2d clone() const
C++ version of gsl_histogram2d_clone().
int div(histogram2d const &h2)
C++ version of gsl_histogram2d_div().
bool operator<=(histogram2d const &v) const
A container needs to define an ordering for sorting.
histogram2d(gsl_histogram2d *v)
Could construct from a gsl_histogram2d.
Definition: histogram2d.hpp:63
int get_yrange(size_t const j, double &ylower, double &yupper) const
C++ version of gsl_histogram2d_get_yrange().
int set_ranges_uniform(double xmin, double xmax, double ymin, double ymax)
C++ version of gsl_histogram2d_set_ranges_uniform().
double ymin() const
C++ version of gsl_histogram2d_ymin().
double min_val() const
C++ version of gsl_histogram2d_min_val().
histogram2d(histogram2d &&v)
Move constructor.
int fscanf(FILE *stream)
C++ version of gsl_histogram2d_fscanf().
bool equal_bins_p(histogram2d const &h2) const
C++ version of gsl_histogram2d_equal_bins_p().
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34