ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
interp.hpp
Go to the documentation of this file.
1/*
2 * $Id: interp.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_INTERP_HPP
21#define CCGSL_INTERP_HPP
22
23#include<new>
24#include<stdexcept>
25#include<gsl/gsl_interp.h>
26
27namespace gsl {
31 class interp {
32 public:
36 typedef gsl_interp_type type;
41 ccgsl_pointer = 0;
42 count = 0; // initially nullptr will do
43 }
44 // Refines random access container
45 // Refines assignable
51 explicit interp( type const* T, size_t const n ){
52 ccgsl_pointer = gsl_interp_alloc( T, n );
53 // just plausibly we could allocate interp but not count
54 try { count = new size_t; } catch( std::bad_alloc& e ){
55 // try to tidy up before rethrowing
56 gsl_interp_free( ccgsl_pointer );
57 throw e;
58 }
59 *count = 1; // initially there is just one reference to ccgsl_pointer
60 }
67 explicit interp( gsl_interp* v ){
68 ccgsl_pointer = v;
69 // just plausibly we could fail to allocate count: no further action needed.
70 count = new size_t;
71 *count = 1; // initially there is just one reference to ccgsl_pointer
72 }
73 // copy constructor
79 count = v.count; if( count != 0 ) ++*count; }
80 // assignment operator
85 interp& operator=( interp const& v ){
86 // first, possibly delete anything pointed to by this
87 if( count == 0 or --*count == 0 ){
88 if( ccgsl_pointer != 0 ) gsl_interp_free( ccgsl_pointer );
89 delete count;
90 } // Then copy
91 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
92 }
93 // destructor
98 if( count == 0 or --*count == 0 ){
99 // could have allocated null pointer
100 if( ccgsl_pointer != 0 ) gsl_interp_free( ccgsl_pointer );
101 delete count;
102 }
103 }
104#ifdef __GXX_EXPERIMENTAL_CXX0X__
109 interp( interp&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
110 std::swap( count, v.count );
111 v.ccgsl_pointer = nullptr;
112 }
119 interp( std::move( v ) ).swap( *this );
120 return *this;
121 }
122#endif
123 // Refines equality comparable
124 // == operator
131 bool operator==( interp const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
132 // != operator
139 bool operator!=( interp const& v ) const { return not operator==( v ); }
140 // Refines forward container
141 // Refines less than comparable
142 // operator<
151 bool operator<( interp const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
152 // operator>
161 bool operator>( interp const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
162 // operator<=
171 bool operator<=( interp const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
172 // operator>=
181 bool operator>=( interp const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
186 bool empty() const { return ccgsl_pointer == 0; }
187 // swap() --- should work even if sizes don't match
193 void swap( interp& v ){
194 std::swap( ccgsl_pointer, v.ccgsl_pointer );
195 std::swap( count, v.count );
196 }
197 private:
201 gsl_interp* ccgsl_pointer;
205 size_t* count;
206 public:
207 // shared reference functions
212 gsl_interp* get() const { return ccgsl_pointer; }
218 bool unique() const { return count != 0 and *count == 1; }
223 size_t use_count() const { return count == 0 ? 0 : *count; }
229#ifdef __GXX_EXPERIMENTAL_CXX0X__
230 explicit
231#endif
232 operator bool() const { return ccgsl_pointer != 0; }
233
237 class accel {
238 public:
239 // Refines random access container
240 // Refines assignable
245 ccgsl_pointer = gsl_interp_accel_alloc();
246 // just plausibly we could allocate accel but not count
247 try { count = new size_t; } catch( std::bad_alloc& e ){
248 // try to tidy up before rethrowing
249 gsl_interp_accel_free( ccgsl_pointer );
250 throw e;
251 }
252 *count = 1; // initially there is just one reference to ccgsl_pointer
253 }
260 explicit accel( gsl_interp_accel* v ){
261 ccgsl_pointer = v;
262 // just plausibly we could fail to allocate count: no further action needed.
263 count = new size_t;
264 *count = 1; // initially there is just one reference to ccgsl_pointer
265 }
266 // copy constructor
272 count = v.count; if( count != 0 ) ++*count; }
273 // assignment operator
278 accel& operator=( accel const& v ){
279 // first, possibly delete anything pointed to by this
280 if( count == 0 or --*count == 0 ){
281 if( ccgsl_pointer != 0 ) gsl_interp_accel_free( ccgsl_pointer );
282 delete count;
283 } // Then copy
284 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
285 }
286 // destructor
291 if( count == 0 or --*count == 0 ){
292 // could have allocated null pointer
293 if( ccgsl_pointer != 0 ) gsl_interp_accel_free( ccgsl_pointer );
294 delete count;
295 }
296 }
297#ifdef __GXX_EXPERIMENTAL_CXX0X__
302 accel( accel&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
303 std::swap( count, v.count );
304 v.ccgsl_pointer = nullptr;
305 }
312 accel( std::move( v ) ).swap( *this );
313 return *this;
314 }
315#endif
316 // Refines equality comparable
317 // == operator
324 bool operator==( accel const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
325 // != operator
332 bool operator!=( accel const& v ) const { return not operator==( v ); }
333 // Refines forward container
334 // Refines less than comparable
335 // operator<
344 bool operator<( accel const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
345 // operator>
354 bool operator>( accel const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
355 // operator<=
364 bool operator<=( accel const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
365 // operator>=
374 bool operator>=( accel const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
379 bool empty() const { return ccgsl_pointer == 0; }
380 // swap() --- should work even if sizes don't match
386 void swap( accel& v ){
387 std::swap( ccgsl_pointer, v.ccgsl_pointer );
388 std::swap( count, v.count );
389 }
390 private:
394 gsl_interp_accel* ccgsl_pointer;
398 size_t* count;
399 public:
400 // shared reference functions
405 gsl_interp_accel* get() const { return ccgsl_pointer; }
411 bool unique() const { return count != 0 and *count == 1; }
416 size_t use_count() const { return count == 0 ? 0 : *count; }
422#ifdef __GXX_EXPERIMENTAL_CXX0X__
423 explicit
424#endif
425 operator bool() const { return ccgsl_pointer != 0; }
426
431 int reset(){ return gsl_interp_accel_reset( get() ); }
439 size_t find( double const xa[], size_t len, double x ){
440 return gsl_interp_accel_find( get(), xa, len, x ); }
448 template<typename XA>
449 size_t find( XA const& xa, double x ){
450 return gsl_interp_accel_find( get(), xa.data(), xa.size(), x ); }
451 };
452
460 int init( double const xa[], double const ya[], size_t const size ){
461 return gsl_interp_init( get(), xa, ya, size ); }
462
470 template<typename XA,typename YA>
471 int init( XA const& xa, YA const& ya ){
472 if( ya.size() != xa.size() )
473 throw std::length_error("gsl::interp::init(): xa and ya must have the same size.");
474 return gsl_interp_init( get(), xa.data(), ya.data(), xa.size() ); }
475
480 char const* name() const { return gsl_interp_name( get() ); }
481
486 unsigned int min_size() const { return gsl_interp_min_size( get() ); }
487
493 unsigned int type_min_size( gsl_interp_type const* T ){
494 return gsl_interp_type_min_size( T ); }
495#ifndef DOXYGEN_SKIP
505 int eval_e( double const xa[], double const ya[], double x, accel& a, double* y ) const {
506 return gsl_interp_eval_e( get(), xa, ya, x, a.get(), y ); }
507#endif /* DOXYGEN_SKIP */
517 int eval_e( double const xa[], double const ya[], double x, accel& a, double& y ) const {
518 return gsl_interp_eval_e( get(), xa, ya, x, a.get(), &y ); }
519
528 double eval( double const xa[], double const ya[], double x, accel& a ) const {
529 return gsl_interp_eval( get(), xa, ya, x, a.get() ); }
530
531#ifndef DOXYGEN_SKIP
541 int eval_deriv_e( double const xa[], double const ya[], double x, accel& a, double* d )
542 const { return gsl_interp_eval_deriv_e( get(), xa, ya, x, a.get(), d ); }
543#endif /* DOXYGEN_SKIP */
553 int eval_deriv_e( double const xa[], double const ya[], double x, accel& a, double& d )
554 const { return gsl_interp_eval_deriv_e( get(), xa, ya, x, a.get(), &d ); }
555
564 double eval_deriv( double const xa[], double const ya[], double x, accel& a ) const {
565 return gsl_interp_eval_deriv( get(), xa, ya, x, a.get() ); }
566
567#ifndef DOXYGEN_SKIP
577 int eval_deriv2_e( double const xa[], double const ya[], double x, accel& a, double* d2 )
578 const { return gsl_interp_eval_deriv2_e( get(), xa, ya, x, a.get(), d2 ); }
579#endif /* DOXYGEN_SKIP */
589 int eval_deriv2_e( double const xa[], double const ya[], double x, accel& a, double& d2 )
590 const { return gsl_interp_eval_deriv2_e( get(), xa, ya, x, a.get(), &d2 ); }
591
600 double eval_deriv2( double const xa[], double const ya[], double x, accel& a ) const {
601 return gsl_interp_eval_deriv2( get(), xa, ya, x, a.get() ); }
602
603#ifndef DOXYGEN_SKIP
614 int eval_integ_e( double const xa[], double const ya[], double a, double b,
615 accel& acc, double* result ) const {
616 return gsl_interp_eval_integ_e( get(), xa, ya, a, b, acc.get(), result ); }
617#endif /* DOXYGEN_SKIP */
628 int eval_integ_e( double const xa[], double const ya[], double a, double b,
629 accel& acc, double& result ) const {
630 return gsl_interp_eval_integ_e( get(), xa, ya, a, b, acc.get(), &result ); }
631
641 double eval_integ( double const xa[], double const ya[], double a, double b, accel& acc )
642 const { return gsl_interp_eval_integ( get(), xa, ya, a, b, acc.get() ); }
643
652 size_t bsearch( double const x_array[], double x, size_t index_lo, size_t index_hi ){
653 return gsl_interp_bsearch( x_array, x, index_lo, index_hi ); }
654
655 // Template versions
656#ifndef DOXYGEN_SKIP
667 template<typename XA,typename YA>
668 int eval_e( XA const& xa, YA const& ya, double x, accel& a, double* y ) const {
669 return gsl_interp_eval_e( get(), xa.data(), ya.data(), x, a.get(), y ); }
670#endif /* DOXYGEN_SKIP */
681 template<typename XA,typename YA>
682 int eval_e( XA const& xa, YA const& ya, double x, accel& a, double& y ) const {
683 return gsl_interp_eval_e( get(), xa.data(), ya.data(), x, a.get(), &y ); }
684
694 template<typename XA,typename YA>
695 double eval( XA const& xa, YA const& ya, double x, accel& a ) const {
696 return gsl_interp_eval( get(), xa.data(), ya.data(), x, a.get() ); }
697
698#ifndef DOXYGEN_SKIP
709 template<typename XA,typename YA>
710 int eval_deriv_e( XA const& xa, YA const& ya, double x, accel& a, double* d ) const {
711 return gsl_interp_eval_deriv_e( get(), xa.data(), ya.data(), x, a.get(), d ); }
712#endif /* DOXYGEN_SKIP */
723 template<typename XA,typename YA>
724 int eval_deriv_e( XA const& xa, YA const& ya, double x, accel& a, double& d ) const {
725 return gsl_interp_eval_deriv_e( get(), xa.data(), ya.data(), x, a.get(), &d ); }
726
736 template<typename XA,typename YA>
737 double eval_deriv( XA const& xa, YA const& ya, double x, accel& a ) const {
738 return gsl_interp_eval_deriv( get(), xa.data(), ya.data(), x, a.get() ); }
739
740#ifndef DOXYGEN_SKIP
751 template<typename XA,typename YA>
752 int eval_deriv2_e( XA const& xa, YA const& ya, double x, accel& a, double* d2 ) const {
753 return gsl_interp_eval_deriv2_e( get(), xa.data(), ya.data(), x, a.get(), d2 ); }
754#endif /* DOXYGEN_SKIP */
765 template<typename XA,typename YA>
766 int eval_deriv2_e( XA const& xa, YA const& ya, double x, accel& a, double& d2 ) const {
767 return gsl_interp_eval_deriv2_e( get(), xa.data(), ya.data(), x, a.get(), &d2 ); }
768
778 template<typename XA,typename YA>
779 double eval_deriv2( XA const& xa, YA const& ya, double x, accel& a ) const {
780 return gsl_interp_eval_deriv2( get(), xa.data(), ya.data(), x, a.get() ); }
781
782#ifndef DOXYGEN_SKIP
794 template<typename XA,typename YA>
795 int eval_integ_e( XA const& xa, YA const& ya, double a, double b,
796 accel& acc, double* result ) const {
797 return gsl_interp_eval_integ_e( get(), xa.data(), ya.data(), a, b, acc.get(), result ); }
798#endif /* DOXYGEN_SKIP */
810 template<typename XA,typename YA>
811 int eval_integ_e( XA const& xa, YA const& ya, double a, double b,
812 accel& acc, double& result ) const {
813 return gsl_interp_eval_integ_e( get(), xa.data(), ya.data(), a, b, acc.get(), &result ); }
814
825 template<typename XA,typename YA>
826 double eval_integ( XA const& xa, YA const& ya, double a, double b, accel& acc ) const {
827 return gsl_interp_eval_integ( get(), xa.data(), ya.data(), a, b, acc.get() ); }
828
838 template<typename X>
839 static size_t bsearch( X const& x_array, double x, size_t index_lo, size_t index_hi ){
840 return gsl_interp_bsearch( x_array.data(), x, index_lo, index_hi ); }
841
842 // Types
847 inline static type const* cspline(){ return gsl_interp_cspline; }
852 inline static type const* cspline_periodic(){ return gsl_interp_cspline_periodic; }
857 inline static type const* akima(){ return gsl_interp_akima; }
862 inline static type const* akima_periodic(){ return gsl_interp_akima_periodic; }
867 inline static type const* steffen(){ return gsl_interp_steffen; }
868 };
869}
870#endif
Workspace for acceleration.
Definition: interp.hpp:237
~accel()
The destructor only deletes the pointers if count reaches zero.
Definition: interp.hpp:290
bool unique() const
Find if this is the only object sharing the gsl_interp_accel.
Definition: interp.hpp:411
bool operator>=(accel const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:374
accel(accel const &v)
The copy constructor.
Definition: interp.hpp:271
bool operator<(accel const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:344
void swap(accel &v)
Swap two accel objects.
Definition: interp.hpp:386
bool operator<=(accel const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:364
size_t find(XA const &xa, double x)
C++ version of gsl_interp_accel_find().
Definition: interp.hpp:449
bool operator!=(accel const &v) const
Two accel are different if their elements are not identical.
Definition: interp.hpp:332
accel & operator=(accel const &v)
The assignment operator.
Definition: interp.hpp:278
size_t use_count() const
Find how many accel objects share this pointer.
Definition: interp.hpp:416
bool operator==(accel const &v) const
Two accel are identically equal if their elements are identical.
Definition: interp.hpp:324
accel & operator=(accel &&v)
Move operator.
Definition: interp.hpp:311
int reset()
C++ version of gsl_interp_accel_reset().
Definition: interp.hpp:431
size_t find(double const xa[], size_t len, double x)
C++ version of gsl_interp_accel_find().
Definition: interp.hpp:439
bool operator>(accel const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:354
size_t * count
The shared reference count.
Definition: interp.hpp:398
accel()
The default constructor creates a new accel.
Definition: interp.hpp:244
accel(accel &&v)
Move constructor.
Definition: interp.hpp:302
accel(gsl_interp_accel *v)
Could construct from a gsl_interp_accel.
Definition: interp.hpp:260
gsl_interp_accel * ccgsl_pointer
The shared pointer.
Definition: interp.hpp:394
gsl_interp_accel * get() const
Get the gsl_interp_accel.
Definition: interp.hpp:405
bool empty() const
Find if the accel is empty.
Definition: interp.hpp:379
Interpolation functions and objects.
Definition: interp.hpp:31
interp(interp const &v)
The copy constructor.
Definition: interp.hpp:78
double eval_deriv2(XA const &xa, YA const &ya, double x, accel &a) const
C++ version of gsl_interp_eval_deriv2().
Definition: interp.hpp:779
interp & operator=(interp const &v)
The assignment operator.
Definition: interp.hpp:85
interp()
The default constructor is only really useful for assigning to.
Definition: interp.hpp:40
static type const * steffen()
Static type.
Definition: interp.hpp:867
static type const * cspline()
Static type.
Definition: interp.hpp:847
double eval_integ(double const xa[], double const ya[], double a, double b, accel &acc) const
C++ version of gsl_interp_eval_integ().
Definition: interp.hpp:641
gsl_interp * get() const
Get the gsl_interp.
Definition: interp.hpp:212
char const * name() const
C++ version of gsl_interp_name().
Definition: interp.hpp:480
int eval_integ_e(double const xa[], double const ya[], double a, double b, accel &acc, double &result) const
C++ version of gsl_interp_eval_integ_e().
Definition: interp.hpp:628
double eval(double const xa[], double const ya[], double x, accel &a) const
C++ version of gsl_interp_eval().
Definition: interp.hpp:528
interp & operator=(interp &&v)
Move operator.
Definition: interp.hpp:118
unsigned int min_size() const
C++ version of gsl_interp_min_size().
Definition: interp.hpp:486
double eval(XA const &xa, YA const &ya, double x, accel &a) const
C++ version of gsl_interp_eval().
Definition: interp.hpp:695
int eval_deriv_e(XA const &xa, YA const &ya, double x, accel &a, double &d) const
C++ version of gsl_interp_eval_deriv_e().
Definition: interp.hpp:724
interp(interp &&v)
Move constructor.
Definition: interp.hpp:109
bool operator<(interp const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:151
double eval_deriv(double const xa[], double const ya[], double x, accel &a) const
C++ version of gsl_interp_eval_deriv().
Definition: interp.hpp:564
int eval_deriv2_e(double const xa[], double const ya[], double x, accel &a, double &d2) const
C++ version of gsl_interp_eval_deriv2_e().
Definition: interp.hpp:589
int init(double const xa[], double const ya[], size_t const size)
C++ version of gsl_interp_init().
Definition: interp.hpp:460
interp(gsl_interp *v)
Could construct from a gsl_interp.
Definition: interp.hpp:67
interp(type const *T, size_t const n)
The default constructor creates a new interp with n elements.
Definition: interp.hpp:51
gsl_interp_type type
Convenient typedef.
Definition: interp.hpp:36
size_t bsearch(double const x_array[], double x, size_t index_lo, size_t index_hi)
C++ version of gsl_interp_bsearch().
Definition: interp.hpp:652
bool operator!=(interp const &v) const
Two interp are different if their elements are not identical.
Definition: interp.hpp:139
int eval_integ_e(XA const &xa, YA const &ya, double a, double b, accel &acc, double &result) const
C++ version of gsl_interp_eval_integ_e().
Definition: interp.hpp:811
gsl_interp * ccgsl_pointer
The shared pointer.
Definition: interp.hpp:201
static type const * akima()
Static type.
Definition: interp.hpp:857
int eval_e(XA const &xa, YA const &ya, double x, accel &a, double &y) const
C++ version of gsl_interp_eval_e().
Definition: interp.hpp:682
bool operator==(interp const &v) const
Two interp are identically equal if their elements are identical.
Definition: interp.hpp:131
void swap(interp &v)
Swap two interp objects.
Definition: interp.hpp:193
static size_t bsearch(X const &x_array, double x, size_t index_lo, size_t index_hi)
C++ version of gsl_interp_bsearch().
Definition: interp.hpp:839
double eval_deriv2(double const xa[], double const ya[], double x, accel &a) const
C++ version of gsl_interp_eval_deriv2().
Definition: interp.hpp:600
static type const * akima_periodic()
Static type.
Definition: interp.hpp:862
double eval_integ(XA const &xa, YA const &ya, double a, double b, accel &acc) const
C++ version of gsl_interp_eval_integ().
Definition: interp.hpp:826
bool unique() const
Find if this is the only object sharing the gsl_interp.
Definition: interp.hpp:218
double eval_deriv(XA const &xa, YA const &ya, double x, accel &a) const
C++ version of gsl_interp_eval_deriv().
Definition: interp.hpp:737
size_t use_count() const
Find how many interp objects share this pointer.
Definition: interp.hpp:223
int eval_e(double const xa[], double const ya[], double x, accel &a, double &y) const
C++ version of gsl_interp_eval_e().
Definition: interp.hpp:517
size_t * count
The shared reference count.
Definition: interp.hpp:205
bool operator>=(interp const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:181
int eval_deriv2_e(XA const &xa, YA const &ya, double x, accel &a, double &d2) const
C++ version of gsl_interp_eval_deriv2_e().
Definition: interp.hpp:766
int eval_deriv_e(double const xa[], double const ya[], double x, accel &a, double &d) const
C++ version of gsl_interp_eval_deriv_e().
Definition: interp.hpp:553
~interp()
The destructor only deletes the pointers if count reaches zero.
Definition: interp.hpp:97
bool operator>(interp const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:161
unsigned int type_min_size(gsl_interp_type const *T)
C++ version of gsl_interp_type_min_size().
Definition: interp.hpp:493
bool empty() const
Find if the interp is empty.
Definition: interp.hpp:186
static type const * cspline_periodic()
Static type.
Definition: interp.hpp:852
bool operator<=(interp const &v) const
A container needs to define an ordering for sorting.
Definition: interp.hpp:171
int init(XA const &xa, YA const &ya)
C++ version of gsl_interp_init().
Definition: interp.hpp:471
size_t size(series const &cs)
C++ version of gsl_cheb_size().
Definition: chebyshev.hpp:287
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
double b(int order, double qq)
C++ version of gsl_sf_mathieu_b().
Definition: sf_mathieu.hpp:298
double a(int order, double qq)
C++ version of gsl_sf_mathieu_a().
Definition: sf_mathieu.hpp:272
gsl_sf_result result
Typedef for gsl_sf_result.
Definition: sf_result.hpp:30
int accel(ARRAY const &array, workspace &w, double &sum_accel, double &abserr)
C++ version of gsl_sum_levin_u_accel().
Definition: sum.hpp:281
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34