ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
sum.hpp
Go to the documentation of this file.
1/*
2 * $Id: sum.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_SUM_HPP
21#define CCGSL_SUM_HPP
22
23#include<new>
24#include<gsl/gsl_sum.h>
25
26namespace gsl {
30 namespace sum {
34 namespace levin_u {
38 class workspace {
39 public:
44 ccgsl_pointer = 0;
45 count = 0; // initially nullptr will do
46 }
47 // Refines random access container
48 // Refines assignable
53 explicit workspace( size_t const n ){
54 ccgsl_pointer = gsl_sum_levin_u_alloc( n );
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_sum_levin_u_free( ccgsl_pointer );
59 throw e;
60 }
61 *count = 1; // initially there is just one reference to ccgsl_pointer
62 }
69 explicit workspace( gsl_sum_levin_u_workspace* v ){
70 ccgsl_pointer = v;
71 // just plausibly we could fail to allocate count: no further action needed.
72 count = new size_t;
73 *count = 1; // initially there is just one reference to ccgsl_pointer
74 }
75 // copy constructor
81 count = v.count; if( count != 0 ) ++*count; }
82 // assignment operator
88 // first, possibly delete anything pointed to by this
89 if( count == 0 or --*count == 0 ){
90 if( ccgsl_pointer != 0 ) gsl_sum_levin_u_free( ccgsl_pointer );
91 delete count;
92 } // Then copy
93 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
94 }
95 // destructor
100 if( count == 0 or --*count == 0 ){
101 // could have allocated null pointer
102 if( ccgsl_pointer != 0 ) gsl_sum_levin_u_free( ccgsl_pointer );
103 delete count;
104 }
105 }
106#ifdef __GXX_EXPERIMENTAL_CXX0X__
112 std::swap( count, v.count );
113 v.ccgsl_pointer = nullptr;
114 }
121 workspace( std::move( v ) ).swap( *this );
122 return *this;
123 }
124#endif
125 // Refines equality comparable
126 // == operator
133 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
134 // != operator
141 bool operator!=( workspace const& v ) const { return not operator==( v ); }
142 // Refines forward container
143 // Refines less than comparable
144 // operator<
153 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
154 // operator>
163 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
164 // operator<=
173 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
174 // operator>=
183 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
188 bool empty() const { return ccgsl_pointer == 0; }
189 // swap() --- should work even if sizes don't match
195 void swap( workspace& v ){
196 std::swap( ccgsl_pointer, v.ccgsl_pointer );
197 std::swap( count, v.count );
198 }
199 private:
203 gsl_sum_levin_u_workspace* ccgsl_pointer;
207 size_t* count;
208 public:
209 // shared reference functions
214 gsl_sum_levin_u_workspace* get() const { return ccgsl_pointer; }
220 bool unique() const { return count != 0 and *count == 1; }
225 size_t use_count() const { return count == 0 ? 0 : *count; }
231#ifdef __GXX_EXPERIMENTAL_CXX0X__
232 explicit
233#endif
234 operator bool() const { return ccgsl_pointer != 0; }
239 size_t size() const { return ccgsl_pointer->size; }
244 size_t i() const { return ccgsl_pointer->i; }
249 size_t terms_used() const { return ccgsl_pointer->terms_used; }
254 double sum_plain() const { return ccgsl_pointer->sum_plain; }
255 };
256#ifndef DOXYGEN_SKIP
266 inline int accel( const double* array, size_t array_size, workspace& w, double* sum_accel,
267 double* abserr ){
268 return gsl_sum_levin_u_accel( array, array_size, w.get(), sum_accel, abserr );
269 }
270#endif // DOXYGEN_SKIP
280 template<typename ARRAY>
281 inline int accel( ARRAY const& array, workspace& w, double& sum_accel,
282 double& abserr ){
283 return gsl_sum_levin_u_accel( array.data(), array.size(), w.get(), &sum_accel, &abserr );
284 }
285 }
289 namespace levin_utrunc {
293 class workspace {
294 public:
299 ccgsl_pointer = 0;
300 count = 0; // initially nullptr will do
301 }
302 // Refines random access container
303 // Refines assignable
308 explicit workspace( size_t const n ){
309 ccgsl_pointer = gsl_sum_levin_utrunc_alloc( n );
310 // just plausibly we could allocate workspace but not count
311 try { count = new size_t; } catch( std::bad_alloc& e ){
312 // try to tidy up before rethrowing
313 gsl_sum_levin_utrunc_free( ccgsl_pointer );
314 throw e;
315 }
316 *count = 1; // initially there is just one reference to ccgsl_pointer
317 }
324 explicit workspace( gsl_sum_levin_utrunc_workspace* v ){
325 ccgsl_pointer = v;
326 // just plausibly we could fail to allocate count: no further action needed.
327 count = new size_t;
328 *count = 1; // initially there is just one reference to ccgsl_pointer
329 }
330 // copy constructor
336 count = v.count; if( count != 0 ) ++*count; }
337 // assignment operator
343 // first, possibly delete anything pointed to by this
344 if( count == 0 or --*count == 0 ){
345 if( ccgsl_pointer != 0 ) gsl_sum_levin_utrunc_free( ccgsl_pointer );
346 delete count;
347 } // Then copy
348 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
349 }
350 // destructor
355 if( count == 0 or --*count == 0 ){
356 // could have allocated null pointer
357 if( ccgsl_pointer != 0 ) gsl_sum_levin_utrunc_free( ccgsl_pointer );
358 delete count;
359 }
360 }
361#ifdef __GXX_EXPERIMENTAL_CXX0X__
367 std::swap( count, v.count );
368 v.ccgsl_pointer = nullptr;
369 }
376 workspace( std::move( v ) ).swap( *this );
377 return *this;
378 }
379#endif
380 // Refines equality comparable
381 // == operator
388 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
389 // != operator
396 bool operator!=( workspace const& v ) const { return not operator==( v ); }
397 // Refines forward container
398 // Refines less than comparable
399 // operator<
408 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
409 // operator>
418 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
419 // operator<=
428 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
429 // operator>=
438 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
443 bool empty() const { return ccgsl_pointer == 0; }
444 // swap() --- should work even if sizes don't match
450 void swap( workspace& v ){
451 std::swap( ccgsl_pointer, v.ccgsl_pointer );
452 std::swap( count, v.count );
453 }
454 private:
458 gsl_sum_levin_utrunc_workspace* ccgsl_pointer;
462 size_t* count;
463 public:
464 // shared reference functions
469 gsl_sum_levin_utrunc_workspace* get() const { return ccgsl_pointer; }
475 bool unique() const { return count != 0 and *count == 1; }
480 size_t use_count() const { return count == 0 ? 0 : *count; }
486#ifdef __GXX_EXPERIMENTAL_CXX0X__
487 explicit
488#endif
489 operator bool() const { return ccgsl_pointer != 0; }
494 size_t size() const { return ccgsl_pointer->size; }
499 size_t i() const { return ccgsl_pointer->i; }
504 size_t terms_used() const { return ccgsl_pointer->terms_used; }
509 double sum_plain() const { return ccgsl_pointer->sum_plain; }
510 };
511#ifndef DOXYGEN_SKIP
521 inline int accel( const double* array, size_t array_size, workspace& w, double* sum_accel,
522 double* abserr_trunc ){
523 return gsl_sum_levin_utrunc_accel( array, array_size, w.get(), sum_accel, abserr_trunc );
524 }
525#endif // DOXYGEN_SKIP
535 template<typename ARRAY>
536 inline int accel( ARRAY const& array, workspace& w, double* sum_accel, double* abserr_trunc ){
537 return gsl_sum_levin_utrunc_accel( array.data(), array.size(), w.get(), sum_accel, abserr_trunc );
538 }
539 }
540 }
541}
542#endif
Workspace for the Levin u transform.
Definition: sum.hpp:38
size_t i() const
Give access to underlying struct value.
Definition: sum.hpp:244
gsl_sum_levin_u_workspace * get() const
Get the gsl_sum_levin_u_workspace.
Definition: sum.hpp:214
double sum_plain() const
Give access to underlying struct value.
Definition: sum.hpp:254
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:183
workspace(gsl_sum_levin_u_workspace *v)
Could construct from a gsl_sum_levin_u_workspace.
Definition: sum.hpp:69
workspace()
The default constructor is only really useful for assigning to.
Definition: sum.hpp:43
size_t * count
The shared reference count.
Definition: sum.hpp:207
bool empty() const
Find if the workspace is empty.
Definition: sum.hpp:188
workspace(size_t const n)
The default constructor creates a new workspace with n elements.
Definition: sum.hpp:53
void swap(workspace &v)
Swap two workspace objects.
Definition: sum.hpp:195
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: sum.hpp:225
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
Definition: sum.hpp:141
workspace & operator=(workspace &&v)
Move operator.
Definition: sum.hpp:120
gsl_sum_levin_u_workspace * ccgsl_pointer
The shared pointer.
Definition: sum.hpp:203
size_t terms_used() const
Give access to underlying struct value.
Definition: sum.hpp:249
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: sum.hpp:99
workspace(workspace &&v)
Move constructor.
Definition: sum.hpp:111
workspace(workspace const &v)
The copy constructor.
Definition: sum.hpp:80
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:173
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:153
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:163
size_t size() const
Give access to underlying struct value.
Definition: sum.hpp:239
workspace & operator=(workspace const &v)
The assignment operator.
Definition: sum.hpp:87
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: sum.hpp:133
bool unique() const
Find if this is the only object sharing the gsl_sum_levin_u_workspace.
Definition: sum.hpp:220
Workspace for the Levin u transform without error estimation.
Definition: sum.hpp:293
workspace(size_t const n)
The default constructor creates a new workspace with n elements.
Definition: sum.hpp:308
void swap(workspace &v)
Swap two workspace objects.
Definition: sum.hpp:450
size_t size() const
Give access to underlying struct value.
Definition: sum.hpp:494
double sum_plain() const
Give access to underlying struct value.
Definition: sum.hpp:509
workspace()
The default constructor is only really useful for assigning to.
Definition: sum.hpp:298
size_t use_count() const
Find how many workspace objects share this pointer.
Definition: sum.hpp:480
workspace(workspace const &v)
The copy constructor.
Definition: sum.hpp:335
workspace(workspace &&v)
Move constructor.
Definition: sum.hpp:366
size_t * count
The shared reference count.
Definition: sum.hpp:462
size_t terms_used() const
Give access to underlying struct value.
Definition: sum.hpp:504
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:408
gsl_sum_levin_utrunc_workspace * get() const
Get the gsl_sum_levin_utrunc_workspace.
Definition: sum.hpp:469
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
Definition: sum.hpp:396
bool empty() const
Find if the workspace is empty.
Definition: sum.hpp:443
gsl_sum_levin_utrunc_workspace * ccgsl_pointer
The shared pointer.
Definition: sum.hpp:458
size_t i() const
Give access to underlying struct value.
Definition: sum.hpp:499
bool unique() const
Find if this is the only object sharing the gsl_sum_levin_utrunc_workspace.
Definition: sum.hpp:475
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:428
workspace & operator=(workspace &&v)
Move operator.
Definition: sum.hpp:375
workspace & operator=(workspace const &v)
The assignment operator.
Definition: sum.hpp:342
~workspace()
The destructor only deletes the pointers if count reaches zero.
Definition: sum.hpp:354
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:418
workspace(gsl_sum_levin_utrunc_workspace *v)
Could construct from a gsl_sum_levin_utrunc_workspace.
Definition: sum.hpp:324
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
Definition: sum.hpp:388
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
Definition: sum.hpp:438
int sum(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_sum().
Definition: movstat.hpp:673
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
int array(int const nmax, double const x, DATA &result_array)
C++ version of gsl_sf_hermite_array().
Definition: sf_hermite.hpp:324
int array_size(int const lmax, int const m)
C++ version of gsl_sf_legendre_array_size().
int accel(ARRAY const &array, workspace &w, double &sum_accel, double &abserr)
C++ version of gsl_sum_levin_u_accel().
Definition: sum.hpp:281
int accel(ARRAY const &array, workspace &w, double *sum_accel, double *abserr_trunc)
C++ version of gsl_sum_levin_utrunc_accel().
Definition: sum.hpp:536
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34