ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
rstat.hpp
Go to the documentation of this file.
1/*
2 * $Id$
3 * Copyright (C) 2010, 2018, 2019, 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_RSTAT
21#define CCGSL_RSTAT
22
23#include<cmath>
24#include<gsl/gsl_rstat.h>
25#include"vector.hpp"
26
27namespace gsl {
31 namespace rstat {
35 class workspace {
36 public:
40 explicit workspace(){
41 ccgsl_pointer = gsl_rstat_alloc();
42 // just plausibly we could allocate workspace but not count
43 try { count = new size_t; } catch( std::bad_alloc& e ){
44 // try to tidy up before rethrowing
45 gsl_rstat_free( ccgsl_pointer );
46 throw e;
47 }
48 *count = 1; // initially there is just one reference to ccgsl_pointer
49 }
56 explicit workspace( gsl_rstat_workspace* v ){
57 ccgsl_pointer = v;
58 // just plausibly we could fail to allocate count: no further action needed.
59 count = new size_t;
60 *count = 1; // initially there is just one reference to ccgsl_pointer
61 }
62 // copy constructor
68 count = v.count; if( count != 0 ) ++*count; }
69 // assignment operator
75 // first, possibly delete anything pointed to by this
76 if( count == 0 or --*count == 0 ){
77 if( ccgsl_pointer != 0 ) gsl_rstat_free( ccgsl_pointer );
78 delete count;
79 } // Then copy
80 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
81 return *this;
82 }
83 // destructor
88 if( count == 0 or --*count == 0 ){
89 // could have allocated null pointer
90 if( ccgsl_pointer != 0 ) gsl_rstat_free( ccgsl_pointer );
91 delete count;
92 }
93 }
94#ifdef __GXX_EXPERIMENTAL_CXX0X__
100 std::swap( count, v.count );
101 v.ccgsl_pointer = nullptr;
102 }
109 workspace( std::move( v ) ).swap( *this );
110 return *this;
111 }
112#endif
113 // Refines equality comparable
114 // == operator
121 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
122 // != operator
129 bool operator!=( workspace const& v ) const { return not operator==( v ); }
130 // Refines forward container
131 // Refines less than comparable
132 // operator<
141 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
142 // operator>
151 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
152 // operator<=
161 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
162 // operator>=
171 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
176 bool empty() const { return ccgsl_pointer == 0; }
177 // swap() --- should work even if sizes don't match
183 void swap( workspace& v ){
184 std::swap( ccgsl_pointer, v.ccgsl_pointer );
185 std::swap( count, v.count );
186 }
187 private:
191 gsl_rstat_workspace* ccgsl_pointer;
195 size_t* count;
196 public:
197 // shared reference functions
202 gsl_rstat_workspace* get() const { return ccgsl_pointer; }
208 bool unique() const { return count != 0 and *count == 1; }
213 size_t use_count() const { return count == 0 ? 0 : *count; }
219#ifdef __GXX_EXPERIMENTAL_CXX0X__
220 explicit
221#endif
222 operator bool() const { return ccgsl_pointer != 0; }
223 public:
224 // member functions
229 size_t n() const { return gsl_rstat_n( get() ); }
235 int add( double const x ){ return gsl_rstat_add( x, get() ); }
240 double min() const { return gsl_rstat_min( get() ); }
245 double
246 max() const { return gsl_rstat_max( get() ); }
251 double mean() const { return gsl_rstat_mean( get() ); }
256 double variance() const { return gsl_rstat_variance( get() ); }
261 double sd() const { return gsl_rstat_sd( get() ); }
266 double rms() const { return gsl_rstat_rms( get() ); }
271 double sd_mean() const { return gsl_rstat_sd_mean( get() ); }
276 double median() const { return gsl_rstat_median( get() ); }
281 double skew() const { return gsl_rstat_skew( get() ); }
286 double kurtosis() const { return gsl_rstat_kurtosis( get() ); }
291 int reset(){ return gsl_rstat_reset( get() ); }
292 };
293 // namespace functions
299 inline size_t n( workspace const& w ){ return gsl_rstat_n( w.get() ); }
306 inline int add( double const x, workspace& w ){ return gsl_rstat_add( x, w.get() ); }
312 inline double min( workspace const& w ){ return gsl_rstat_min( w.get() ); }
318 inline double
319 max( workspace const& w ){ return gsl_rstat_max( w.get() ); }
325 inline double mean( workspace const& w ){ return gsl_rstat_mean( w.get() ); }
331 inline double variance( workspace const& w ){ return gsl_rstat_variance( w.get() ); }
337 inline double sd( workspace const& w ){ return gsl_rstat_sd( w.get() ); }
343 inline double rms( workspace const& w ){ return gsl_rstat_rms( w.get() ); }
349 inline double sd_mean( workspace const& w ){ return gsl_rstat_sd_mean( w.get() ); }
355 inline double median( workspace& w ){ return gsl_rstat_median( w.get() ); }
361 inline double skew( workspace const& w ){ return gsl_rstat_skew( w.get() ); }
367 inline double kurtosis( workspace const& w ){ return gsl_rstat_kurtosis( w.get() ); }
373 inline int reset( workspace& w ){ return gsl_rstat_reset( w.get() ); }
378 public:
383 ccgsl_pointer = 0;
384 count = 0; // initially nullptr will do
385 }
390 explicit quantile_workspace( double const p ){
391 ccgsl_pointer = gsl_rstat_quantile_alloc( p );
392 // just plausibly we could allocate quantile_workspace but not count
393 try { count = new size_t; } catch( std::bad_alloc& e ){
394 // try to tidy up before rethrowing
395 gsl_rstat_quantile_free( ccgsl_pointer );
396 throw e;
397 }
398 *count = 1; // initially there is just one reference to ccgsl_pointer
399 }
406 explicit quantile_workspace( gsl_rstat_quantile_workspace* v ){
407 ccgsl_pointer = v;
408 // just plausibly we could fail to allocate count: no further action needed.
409 count = new size_t;
410 *count = 1; // initially there is just one reference to ccgsl_pointer
411 }
412 // copy constructor
418 count = v.count; if( count != 0 ) ++*count; }
419 // assignment operator
425 // first, possibly delete anything pointed to by this
426 if( count == 0 or --*count == 0 ){
427 if( ccgsl_pointer != 0 ) gsl_rstat_quantile_free( ccgsl_pointer );
428 delete count;
429 } // Then copy
430 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
431 return *this;
432 }
433 // destructor
438 if( count == 0 or --*count == 0 ){
439 // could have allocated null pointer
440 if( ccgsl_pointer != 0 ) gsl_rstat_quantile_free( ccgsl_pointer );
441 delete count;
442 }
443 }
444#ifdef __GXX_EXPERIMENTAL_CXX0X__
450 : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
451 std::swap( count, v.count );
452 v.ccgsl_pointer = nullptr;
453 }
460 quantile_workspace( std::move( v ) ).swap( *this );
461 return *this;
462 }
463#endif
464 // Refines equality comparable
465 // == operator
472 bool operator==( quantile_workspace const& v ) const
473 { return ccgsl_pointer == v.ccgsl_pointer; }
474 // != operator
481 bool operator!=( quantile_workspace const& v ) const { return not operator==( v ); }
482 // Refines forward container
483 // Refines less than comparable
484 // operator<
493 bool operator<( quantile_workspace const& v ) const
494 { return ccgsl_pointer < v.ccgsl_pointer; }
495 // operator>
504 bool operator>( quantile_workspace const& v ) const
505 { return ccgsl_pointer > v.ccgsl_pointer; }
506 // operator<=
515 bool operator<=( quantile_workspace const& v ) const
516 { return ccgsl_pointer <= v.ccgsl_pointer; }
517 // operator>=
526 bool operator>=( quantile_workspace const& v ) const
527 { return ccgsl_pointer >= v.ccgsl_pointer; }
532 bool empty() const { return ccgsl_pointer == 0; }
533 // swap() --- should work even if sizes don't match
541 std::swap( ccgsl_pointer, v.ccgsl_pointer );
542 std::swap( count, v.count );
543 }
544 private:
548 gsl_rstat_quantile_workspace* ccgsl_pointer;
552 size_t* count;
553 public:
554 // shared reference functions
559 gsl_rstat_quantile_workspace* get() const { return ccgsl_pointer; }
565 bool unique() const { return count != 0 and *count == 1; }
570 size_t use_count() const { return count == 0 ? 0 : *count; }
576#ifdef __GXX_EXPERIMENTAL_CXX0X__
577 explicit
578#endif
579 operator bool() const { return ccgsl_pointer != 0; }
580 public:
581 // member functions
586 int reset(){
587 return gsl_rstat_quantile_reset( get() ); }
593 int add( double const x ){
594 return gsl_rstat_quantile_add( x, get() ); }
599 double get_value(){
600 return gsl_rstat_quantile_get( get() ); }
601 };
605 namespace quantile {
611 inline int reset( quantile_workspace& w ){
612 return gsl_rstat_quantile_reset( w.get() ); }
619 inline int add( double const x, quantile_workspace& w ){
620 return gsl_rstat_quantile_add( x, w.get() ); }
626 inline double get( quantile_workspace& w ){
627 return gsl_rstat_quantile_get( w.get() ); }
628 }
629 }
630}
631
632#endif
Workspace for quantile running statistics.
Definition: rstat.hpp:377
int add(double const x)
C++ version of gsl_rstat_quantile_add().
Definition: rstat.hpp:593
quantile_workspace()
The default constructor is only really useful for assigning to.
Definition: rstat.hpp:382
int reset()
C++ version of gsl_rstat_quantile_reset().
Definition: rstat.hpp:586
quantile_workspace(quantile_workspace &&v)
Move constructor.
Definition: rstat.hpp:449
gsl_rstat_quantile_workspace * ccgsl_pointer
The shared pointer.
Definition: rstat.hpp:548
bool operator<(quantile_workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:493
gsl_rstat_quantile_workspace * get() const
Get the gsl_rstat_quantile_workspace.
Definition: rstat.hpp:559
quantile_workspace(double const p)
The default constructor creates a new quantile_workspace.
Definition: rstat.hpp:390
size_t * count
The shared reference count.
Definition: rstat.hpp:552
~quantile_workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: rstat.hpp:437
void swap(quantile_workspace &v)
Swap two quantile_workspace objects.
Definition: rstat.hpp:540
bool unique() const
Find if this is the only object sharing the gsl_rstat_quantile_workspace.
Definition: rstat.hpp:565
bool operator<=(quantile_workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:515
size_t use_count() const
Find how many quantile_workspace objects share this pointer.
Definition: rstat.hpp:570
bool operator==(quantile_workspace const &v) const
Two quantile_workspace are identically equal if their elements are identical.
Definition: rstat.hpp:472
bool empty() const
Find if the quantile_workspace is empty.
Definition: rstat.hpp:532
quantile_workspace & operator=(quantile_workspace const &v)
The assignment operator.
Definition: rstat.hpp:424
quantile_workspace & operator=(quantile_workspace &&v)
Move operator.
Definition: rstat.hpp:459
bool operator>(quantile_workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:504
quantile_workspace(gsl_rstat_quantile_workspace *v)
Could construct from a gsl_rstat_quantile_workspace*.
Definition: rstat.hpp:406
double get_value()
C++ version of gsl_rstat_quantile_get().
Definition: rstat.hpp:599
bool operator!=(quantile_workspace const &v) const
Two quantile_workspace are different if their elements are not identical.
Definition: rstat.hpp:481
bool operator>=(quantile_workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:526
quantile_workspace(quantile_workspace const &v)
The copy constructor.
Definition: rstat.hpp:417
Workspace for running statistics.
Definition: rstat.hpp:35
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:141
gsl_rstat_workspace * ccgsl_pointer
The shared pointer.
Definition: rstat.hpp:191
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
Definition: rstat.hpp:129
workspace & operator=(workspace const &v)
The assignment operator.
Definition: rstat.hpp:74
int add(double const x)
C++ version of gsl_rstat_add().
Definition: rstat.hpp:235
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: rstat.hpp:121
workspace(workspace &&v)
Move constructor.
Definition: rstat.hpp:99
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:151
bool empty() const
Find if the workspace is empty.
Definition: rstat.hpp:176
bool unique() const
Find if this is the only object sharing the gsl_rstat_workspace.
Definition: rstat.hpp:208
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:171
workspace(workspace const &v)
The copy constructor.
Definition: rstat.hpp:67
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: rstat.hpp:161
workspace()
The default constructor creates a new workspace.
Definition: rstat.hpp:40
double sd() const
C++ version of gsl_rstat_sd().
Definition: rstat.hpp:261
double max() const
C++ version of gsl_rstat_max().
Definition: rstat.hpp:246
int reset()
C++ version of gsl_rstat_reset().
Definition: rstat.hpp:291
double variance() const
C++ version of gsl_rstat_variance().
Definition: rstat.hpp:256
double mean() const
C++ version of gsl_rstat_mean().
Definition: rstat.hpp:251
workspace & operator=(workspace &&v)
Move operator.
Definition: rstat.hpp:108
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: rstat.hpp:87
double rms() const
C++ version of gsl_rstat_rms().
Definition: rstat.hpp:266
workspace(gsl_rstat_workspace *v)
Could construct from a gsl_rstat_workspace*.
Definition: rstat.hpp:56
double kurtosis() const
C++ version of gsl_rstat_kurtosis().
Definition: rstat.hpp:286
double median() const
C++ version of gsl_rstat_median().
Definition: rstat.hpp:276
size_t * count
The shared reference count.
Definition: rstat.hpp:195
double skew() const
C++ version of gsl_rstat_skew().
Definition: rstat.hpp:281
gsl_rstat_workspace * get() const
Get the gsl_rstat_workspace.
Definition: rstat.hpp:202
size_t n() const
C++ version of gsl_rstat_n().
Definition: rstat.hpp:229
void swap(workspace &v)
Swap two workspace objects.
Definition: rstat.hpp:183
double sd_mean() const
C++ version of gsl_rstat_sd_mean().
Definition: rstat.hpp:271
double min() const
C++ version of gsl_rstat_min().
Definition: rstat.hpp:240
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: rstat.hpp:213
int add(double const x, quantile_workspace &w)
C++ version of gsl_rstat_quantile_add().
Definition: rstat.hpp:619
double get(quantile_workspace &w)
C++ version of gsl_rstat_quantile_get().
Definition: rstat.hpp:626
int reset(quantile_workspace &w)
C++ version of gsl_rstat_quantile_reset().
Definition: rstat.hpp:611
double sd_mean(workspace const &w)
C++ version of gsl_rstat_sd_mean().
Definition: rstat.hpp:349
double max(workspace const &w)
C++ version of gsl_rstat_max().
Definition: rstat.hpp:319
double variance(workspace const &w)
C++ version of gsl_rstat_variance().
Definition: rstat.hpp:331
double kurtosis(workspace const &w)
C++ version of gsl_rstat_kurtosis().
Definition: rstat.hpp:367
double min(workspace const &w)
C++ version of gsl_rstat_min().
Definition: rstat.hpp:312
double sd(workspace const &w)
C++ version of gsl_rstat_sd().
Definition: rstat.hpp:337
double mean(workspace const &w)
C++ version of gsl_rstat_mean().
Definition: rstat.hpp:325
int reset(workspace &w)
C++ version of gsl_rstat_reset().
Definition: rstat.hpp:373
int add(double const x, workspace &w)
C++ version of gsl_rstat_add().
Definition: rstat.hpp:306
double rms(workspace const &w)
C++ version of gsl_rstat_rms().
Definition: rstat.hpp:343
double skew(workspace const &w)
C++ version of gsl_rstat_skew().
Definition: rstat.hpp:361
double median(workspace &w)
C++ version of gsl_rstat_median().
Definition: rstat.hpp:355
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