ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
movstat.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_MOVSTAT
21#define CCGSL_MOVSTAT
22
23#include<cmath>
24#include<gsl/gsl_movstat.h>
25#include"vector.hpp"
27
28namespace gsl {
32 namespace movstat {
36 typedef gsl_movstat_end_t end_t;
40 class workspace {
41 public:
46 ccgsl_pointer = 0;
47 count = 0; // initially nullptr will do
48 }
53 explicit workspace( size_t const K ){
54 ccgsl_pointer = gsl_movstat_alloc( K );
55 // just plausibly we could allocate workspace but not count
56 try { count = new size_t; } catch( std::bad_alloc& e ){
57 // try to tidy up before rethrowing
58 gsl_movstat_free( ccgsl_pointer );
59 throw e;
60 }
61 *count = 1; // initially there is just one reference to ccgsl_pointer
62 }
68 workspace( size_t const H, size_t const J ){
69 ccgsl_pointer = gsl_movstat_alloc2( H, J );
70 // just plausibly we could allocate workspace but not count
71 try { count = new size_t; } catch( std::bad_alloc& e ){
72 // try to tidy up before rethrowing
73 gsl_movstat_free( ccgsl_pointer );
74 throw e;
75 }
76 *count = 1; // initially there is just one reference to ccgsl_pointer
77 }
78#ifndef DOXYGEN_SKIP
85 workspace( size_t const accum_state_size,
86 size_t const H,
87 size_t const J ){
88 ccgsl_pointer = gsl_movstat_alloc_with_size( accum_state_size, H, J );
89 // just plausibly we could allocate workspace but not count
90 try { count = new size_t; } catch( std::bad_alloc& e ){
91 // try to tidy up before rethrowing
92 gsl_movstat_free( ccgsl_pointer );
93 throw e;
94 }
95 *count = 1; // initially there is just one reference to ccgsl_pointer
96 }
97#endif
104 explicit workspace( gsl_movstat_workspace* v ){
105 ccgsl_pointer = v;
106 // just plausibly we could fail to allocate count: no further action needed.
107 count = new size_t;
108 *count = 1; // initially there is just one reference to ccgsl_pointer
109 }
110 // copy constructor
116 count = v.count; if( count != 0 ) ++*count; }
117 // assignment operator
123 // first, possibly delete anything pointed to by this
124 if( count == 0 or --*count == 0 ){
125 if( ccgsl_pointer != 0 ) gsl_movstat_free( ccgsl_pointer );
126 delete count;
127 } // Then copy
128 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
129 return *this;
130 }
131 // destructor
136 if( count == 0 or --*count == 0 ){
137 // could have allocated null pointer
138 if( ccgsl_pointer != 0 ) gsl_movstat_free( ccgsl_pointer );
139 delete count;
140 }
141 }
142#ifdef __GXX_EXPERIMENTAL_CXX0X__
148 std::swap( count, v.count );
149 v.ccgsl_pointer = nullptr;
150 }
157 workspace( std::move( v ) ).swap( *this );
158 return *this;
159 }
160#endif
161 // Refines equality comparable
162 // == operator
169 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
170 // != operator
177 bool operator!=( workspace const& v ) const { return not operator==( v ); }
178 // Refines forward container
179 // Refines less than comparable
180 // operator<
189 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
190 // operator>
199 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
200 // operator<=
209 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
210 // operator>=
219 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
224 bool empty() const { return ccgsl_pointer == 0; }
225 // swap() --- should work even if sizes don't match
231 void swap( workspace& v ){
232 std::swap( ccgsl_pointer, v.ccgsl_pointer );
233 std::swap( count, v.count );
234 }
235 private:
239 gsl_movstat_workspace* ccgsl_pointer;
243 size_t* count;
244 public:
245 // shared reference functions
250 gsl_movstat_workspace* get() const { return ccgsl_pointer; }
256 bool unique() const { return count != 0 and *count == 1; }
261 size_t use_count() const { return count == 0 ? 0 : *count; }
267#ifdef __GXX_EXPERIMENTAL_CXX0X__
268 explicit
269#endif
270 operator bool() const { return ccgsl_pointer != 0; }
271 public:
272 // member functions
273#ifndef DOXYGEN_SKIP
284 int apply_accum( end_t const endtype, vector const& x,
285 gsl_movstat_accum const* accum, void* accum_params,
286 vector& y, vector& z ){
287 return gsl_movstat_apply_accum( endtype, x.get(), accum, accum_params,
288 y.get(), z.get(), get() ); }
289#endif
290#ifndef DOXYGEN_SKIP
299 int apply( end_t const endtype, gsl_movstat_function const* F,
300 vector const& x, vector& y ){
301 return gsl_movstat_apply( endtype, F, x.get(), y.get(), get() ); }
302#endif
311 int apply( end_t const endtype, movstat::function const& F,
312 vector const& x, vector& y ){
313 return gsl_movstat_apply( endtype, &F, x.get(), y.get(), get() ); }
321 int mean( end_t const endtype, vector const& x, vector& y ){
322 return gsl_movstat_mean( endtype, x.get(), y.get(), get() ); }
330 int variance( end_t const endtype, vector const& x, vector& y ){
331 return gsl_movstat_variance( endtype, x.get(), y.get(), get() ); }
339 int sd( end_t const endtype, vector const& x, vector& y ){
340 return gsl_movstat_sd( endtype, x.get(), y.get(), get() ); }
348 int median( end_t const endtype, vector const& x, vector& y ){
349 return gsl_movstat_median( endtype, x.get(), y.get(), get() ); }
357 int min( end_t const endtype, vector const& x, vector& y ){
358 return gsl_movstat_min( endtype, x.get(), y.get(), get() ); }
366 int max( end_t const endtype, vector const& x, vector& y ){
367 return gsl_movstat_max( endtype, x.get(), y.get(), get() ); }
376 int minmax( end_t const endtype, vector const& x, vector& y_min,
377 vector& y_max ){
378 return gsl_movstat_minmax( endtype, x.get(), y_min.get(), y_max.get(), get() ); }
387 int mad0( end_t const endtype, vector const& x, vector& xmedian,
388 vector& xmad ){
389 return gsl_movstat_mad0( endtype, x.get(), xmedian.get(), xmad.get(), get() ); }
398 int mad( end_t const endtype, vector const& x, vector& xmedian,
399 vector& xmad ){
400 return gsl_movstat_mad( endtype, x.get(), xmedian.get(), xmad.get(), get() ); }
409 int qqr( end_t const endtype, vector const& x, double const q,
410 vector& xqqr ){
411 return gsl_movstat_qqr( endtype, x.get(), q, xqqr.get(), get() ); }
419 int Sn( end_t const endtype, vector const& x, vector& xscale ){
420 return gsl_movstat_Sn( endtype, x.get(), xscale.get(), get() ); }
428 int Qn( end_t const endtype, vector const& x, vector& xscale ){
429 return gsl_movstat_Qn( endtype, x.get(), xscale.get(), get() ); }
437 int sum( end_t const endtype, vector const& x, vector& y ){
438 return gsl_movstat_sum( endtype, x.get(), y.get(), get() ); }
439 };
440 enum {
444 PADZERO = GSL_MOVSTAT_END_PADZERO,
448 PADVALUE = GSL_MOVSTAT_END_PADVALUE,
452 TRUNCATE = GSL_MOVSTAT_END_TRUNCATE
453 };
454#ifndef DOXYGEN_SKIP
466 inline int apply_accum( movstat::end_t const endtype, vector const& x,
467 gsl_movstat_accum const* accum, void* accum_params,
468 vector& y, vector& z, workspace& w ){
469 return gsl_movstat_apply_accum( endtype, x.get(), accum, accum_params,
470 y.get(), z.get(), w.get() ); }
480 inline int apply( movstat::end_t const endtype, gsl_movstat_function const* F,
481 vector const& x, vector& y, workspace& w ){
482 return gsl_movstat_apply( endtype, F, x.get(), y.get(), w.get() ); }
483#endif
493 inline int apply( movstat::end_t const endtype, movstat::function const& F,
494 vector const& x, vector& y, workspace& w ){
495 return gsl_movstat_apply( endtype, &F, x.get(), y.get(), w.get() ); }
496
497#ifndef DOXYGEN_SKIP
509 inline size_t fill( movstat::end_t const endtype, vector const& x,
510 size_t const idx, size_t const H, size_t const J, double* window ){
511 return gsl_movstat_fill( endtype, x.get(), idx, H, J, window ); }
512#endif
524 inline size_t fill( movstat::end_t const endtype, vector const& x,
525 size_t const idx, size_t const H, size_t const J, gsl::vector& window ){
526 if( window.size() != H + J + 1 )
527 gsl_error( "window is not correct size", __FILE__, __LINE__, exception::GSL_EBADLEN );
528 return gsl_movstat_fill( endtype, x.get(), idx, H, J, window.get()->data ); }
537 inline int mean( movstat::end_t const endtype, vector const& x, vector& y,
538 workspace& w ){
539 return gsl_movstat_mean( endtype, x.get(), y.get(), w.get() ); }
548 inline int variance( movstat::end_t const endtype, vector const& x, vector& y,
549 workspace& w ){
550 return gsl_movstat_variance( endtype, x.get(), y.get(), w.get() ); }
559 inline int sd( movstat::end_t const endtype, vector const& x, vector& y,
560 workspace& w ){
561 return gsl_movstat_sd( endtype, x.get(), y.get(), w.get() ); }
570 inline int median( movstat::end_t const endtype, vector const& x, vector& y,
571 workspace& w ){
572 return gsl_movstat_median( endtype, x.get(), y.get(), w.get() ); }
581 inline int min( movstat::end_t const endtype, vector const& x, vector& y,
582 workspace& w ){
583 return gsl_movstat_min( endtype, x.get(), y.get(), w.get() ); }
592 inline int max( movstat::end_t const endtype, vector const& x, vector& y,
593 workspace& w ){
594 return gsl_movstat_max( endtype, x.get(), y.get(), w.get() ); }
604 inline int minmax( movstat::end_t const endtype, vector const& x, vector& y_min,
605 vector& y_max, workspace& w ){
606 return gsl_movstat_minmax( endtype, x.get(), y_min.get(), y_max.get(), w.get() ); }
616 inline int mad0( movstat::end_t const endtype, vector const& x, vector& xmedian,
617 vector& xmad, workspace& w ){
618 return gsl_movstat_mad0( endtype, x.get(), xmedian.get(), xmad.get(), w.get() ); }
628 inline int mad( movstat::end_t const endtype, vector const& x, vector& xmedian,
629 vector& xmad, workspace& w ){
630 return gsl_movstat_mad( endtype, x.get(), xmedian.get(), xmad.get(), w.get() ); }
640 inline int qqr( movstat::end_t const endtype, vector const& x, double const q,
641 vector& xqqr, workspace& w ){
642 return gsl_movstat_qqr( endtype, x.get(), q, xqqr.get(), w.get() ); }
651 inline int Sn( movstat::end_t const endtype, vector const& x, vector& xscale,
652 workspace& w ){
653 return gsl_movstat_Sn( endtype, x.get(), xscale.get(), w.get() ); }
662 inline int Qn( movstat::end_t const endtype, vector const& x, vector& xscale,
663 workspace& w ){
664 return gsl_movstat_Qn( endtype, x.get(), xscale.get(), w.get() ); }
673 inline int sum( movstat::end_t const endtype, vector const& x, vector& y,
674 workspace& w ){
675 return gsl_movstat_sum( endtype, x.get(), y.get(), w.get() ); }
676
680 namespace accum {
684 inline gsl_movstat_accum const* mad(){ return gsl_movstat_accum_mad; }
688 inline gsl_movstat_accum const* max(){ return gsl_movstat_accum_max; }
692 inline gsl_movstat_accum const* mean(){ return gsl_movstat_accum_mean; }
696 inline gsl_movstat_accum const* median(){ return gsl_movstat_accum_median; }
700 inline gsl_movstat_accum const* min(){ return gsl_movstat_accum_min; }
704 inline gsl_movstat_accum const* minmax(){ return gsl_movstat_accum_minmax; }
708 inline gsl_movstat_accum const* sd(){ return gsl_movstat_accum_sd; }
712 inline gsl_movstat_accum const* Sn(){ return gsl_movstat_accum_Sn; }
716 inline gsl_movstat_accum const* sum(){ return gsl_movstat_accum_sum; }
720 inline gsl_movstat_accum const* Qn(){ return gsl_movstat_accum_Qn; }
724 inline gsl_movstat_accum const* qqr(){ return gsl_movstat_accum_qqr; }
725#ifndef DOXYGEN_SKIP
726 inline gsl_movstat_accum const* userfunc(){ return gsl_movstat_accum_userfunc; }
727#endif
731 inline gsl_movstat_accum const* variance(){ return gsl_movstat_accum_variance; }
732 }
733 }
734}
735
736#endif
@ GSL_EBADLEN
matrix, vector lengths are not conformant
Definition: exception.hpp:490
Class that extends gsl_movstat_function so that it can be constructed from arbitrary function objects...
Workspace for moving window statistics.
Definition: movstat.hpp:40
workspace(workspace &&v)
Move constructor.
Definition: movstat.hpp:147
size_t * count
The shared reference count.
Definition: movstat.hpp:243
workspace(workspace const &v)
The copy constructor.
Definition: movstat.hpp:115
int max(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_max().
Definition: movstat.hpp:366
int apply(end_t const endtype, movstat::function const &F, vector const &x, vector &y)
C++ version of gsl_movstat_apply().
Definition: movstat.hpp:311
int mean(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_mean().
Definition: movstat.hpp:321
int Qn(end_t const endtype, vector const &x, vector &xscale)
C++ version of gsl_movstat_Qn().
Definition: movstat.hpp:428
int minmax(end_t const endtype, vector const &x, vector &y_min, vector &y_max)
C++ version of gsl_movstat_minmax().
Definition: movstat.hpp:376
bool empty() const
Find if the workspace is empty.
Definition: movstat.hpp:224
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: movstat.hpp:219
workspace & operator=(workspace const &v)
The assignment operator.
Definition: movstat.hpp:122
workspace(gsl_movstat_workspace *v)
Could construct from a gsl_movstat_workspace*.
Definition: movstat.hpp:104
gsl_movstat_workspace * ccgsl_pointer
The shared pointer.
Definition: movstat.hpp:239
int mad0(end_t const endtype, vector const &x, vector &xmedian, vector &xmad)
C++ version of gsl_movstat_mad0().
Definition: movstat.hpp:387
int qqr(end_t const endtype, vector const &x, double const q, vector &xqqr)
C++ version of gsl_movstat_qqr().
Definition: movstat.hpp:409
int median(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_median().
Definition: movstat.hpp:348
int variance(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_variance().
Definition: movstat.hpp:330
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: movstat.hpp:135
int min(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_min().
Definition: movstat.hpp:357
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: movstat.hpp:169
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: movstat.hpp:261
int sd(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_sd().
Definition: movstat.hpp:339
workspace(size_t const K)
The default constructor creates a new workspace with window length K.
Definition: movstat.hpp:53
int mad(end_t const endtype, vector const &x, vector &xmedian, vector &xmad)
C++ version of gsl_movstat_mad().
Definition: movstat.hpp:398
gsl_movstat_workspace * get() const
Get the gsl_movstat_workspace.
Definition: movstat.hpp:250
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
Definition: movstat.hpp:177
int Sn(end_t const endtype, vector const &x, vector &xscale)
C++ version of gsl_movstat_Sn().
Definition: movstat.hpp:419
void swap(workspace &v)
Swap two workspace objects.
Definition: movstat.hpp:231
int sum(end_t const endtype, vector const &x, vector &y)
C++ version of gsl_movstat_sum().
Definition: movstat.hpp:437
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: movstat.hpp:199
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: movstat.hpp:189
bool unique() const
Find if this is the only object sharing the gsl_movstat_workspace.
Definition: movstat.hpp:256
workspace & operator=(workspace &&v)
Move operator.
Definition: movstat.hpp:156
workspace(size_t const H, size_t const J)
Creates a new workspace with H samples before current and J after current sample.
Definition: movstat.hpp:68
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: movstat.hpp:209
workspace()
The default constructor is only really useful for assigning to.
Definition: movstat.hpp:45
This class handles vector objects as shared handles.
Definition: vector.hpp:74
gsl_vector * get()
Get the gsl_vector.
Definition: vector.hpp:1320
size_type size() const
The size (number of elements) of the vector.
Definition: vector.hpp:1169
gsl_movstat_accum const * minmax()
Moving window minmax.
Definition: movstat.hpp:704
gsl_movstat_accum const * sd()
Moving window standard deviation.
Definition: movstat.hpp:708
gsl_movstat_accum const * Qn()
Moving window .
Definition: movstat.hpp:720
gsl_movstat_accum const * Sn()
Moving window .
Definition: movstat.hpp:712
gsl_movstat_accum const * max()
Moving window maximum.
Definition: movstat.hpp:688
gsl_movstat_accum const * mad()
Moving window mean absolute deviation?
Definition: movstat.hpp:684
gsl_movstat_accum const * median()
Moving window median.
Definition: movstat.hpp:696
gsl_movstat_accum const * qqr()
Moving window q-quantile range.
Definition: movstat.hpp:724
gsl_movstat_accum const * variance()
Moving window variance.
Definition: movstat.hpp:731
gsl_movstat_accum const * sum()
Moving window maximum.
Definition: movstat.hpp:716
gsl_movstat_accum const * min()
Moving window minimum.
Definition: movstat.hpp:700
gsl_movstat_accum const * mean()
Moving window mean.
Definition: movstat.hpp:692
int qqr(movstat::end_t const endtype, vector const &x, double const q, vector &xqqr, workspace &w)
C++ version of gsl_movstat_qqr().
Definition: movstat.hpp:640
int max(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_max().
Definition: movstat.hpp:592
int Sn(movstat::end_t const endtype, vector const &x, vector &xscale, workspace &w)
C++ version of gsl_movstat_Sn().
Definition: movstat.hpp:651
int mad0(movstat::end_t const endtype, vector const &x, vector &xmedian, vector &xmad, workspace &w)
C++ version of gsl_movstat_mad0().
Definition: movstat.hpp:616
int minmax(movstat::end_t const endtype, vector const &x, vector &y_min, vector &y_max, workspace &w)
C++ version of gsl_movstat_minmax().
Definition: movstat.hpp:604
size_t fill(movstat::end_t const endtype, vector const &x, size_t const idx, size_t const H, size_t const J, gsl::vector &window)
C++ version of gsl_movstat_fill().
Definition: movstat.hpp:524
int min(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_min().
Definition: movstat.hpp:581
int Qn(movstat::end_t const endtype, vector const &x, vector &xscale, workspace &w)
C++ version of gsl_movstat_Qn().
Definition: movstat.hpp:662
int variance(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_variance().
Definition: movstat.hpp:548
@ TRUNCATE
Defined for handling end points.
Definition: movstat.hpp:452
@ PADVALUE
Defined for handling end points.
Definition: movstat.hpp:448
@ PADZERO
Defined for handling end points.
Definition: movstat.hpp:444
int mad(movstat::end_t const endtype, vector const &x, vector &xmedian, vector &xmad, workspace &w)
C++ version of gsl_movstat_mad().
Definition: movstat.hpp:628
int median(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_median().
Definition: movstat.hpp:570
int sd(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_sd().
Definition: movstat.hpp:559
int mean(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_mean().
Definition: movstat.hpp:537
int apply(movstat::end_t const endtype, movstat::function const &F, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_apply().
Definition: movstat.hpp:493
int sum(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_sum().
Definition: movstat.hpp:673
gsl_movstat_end_t end_t
Convenient typedef.
Definition: movstat.hpp:36
double F(double phi, double k, mode_t mode)
C++ version of gsl_sf_ellint_F().
Definition: sf_ellint.hpp:138
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34