ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
roots.hpp
Go to the documentation of this file.
1/*
2 * $Id: roots.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_ROOTS_HPP
21#define CCGSL_ROOTS_HPP
22
23#include<new>
24#include<gsl/gsl_roots.h>
25#include"function_scl.hpp"
26#include"function_fdf.hpp"
27
28namespace gsl {
32 namespace root {
38 class fsolver {
39 public:
43 typedef gsl_root_fsolver_type type;
48 ccgsl_pointer = 0;
49 count = 0; // initially nullptr will do
50 }
51 // Refines random access container
52 // Refines assignable
57 explicit fsolver( type const* T ){
58 ccgsl_pointer = gsl_root_fsolver_alloc( T );
59 // just plausibly we could allocate fsolver but not count
60 try { count = new size_t; } catch( std::bad_alloc& e ){
61 // try to tidy up before rethrowing
62 gsl_root_fsolver_free( ccgsl_pointer );
63 throw e;
64 }
65 *count = 1; // initially there is just one reference to ccgsl_pointer
66 }
73 explicit fsolver( gsl_root_fsolver* v ){
74 ccgsl_pointer = v;
75 // just plausibly we could fail to allocate count: no further action needed.
76 count = new size_t;
77 *count = 1; // initially there is just one reference to ccgsl_pointer
78 }
79 // copy constructor
85 count = v.count; if( count != 0 ) ++*count; }
86 // assignment operator
92 // first, possibly delete anything pointed to by this
93 if( count == 0 or --*count == 0 ){
94 if( ccgsl_pointer != 0 ) gsl_root_fsolver_free( ccgsl_pointer );
95 delete count;
96 } // Then copy
97 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
98 }
99 // destructor
104 if( count == 0 or --*count == 0 ){
105 // could have allocated null pointer
106 if( ccgsl_pointer != 0 ) gsl_root_fsolver_free( ccgsl_pointer );
107 delete count;
108 }
109 }
110#ifdef __GXX_EXPERIMENTAL_CXX0X__
116 std::swap( count, v.count );
117 v.ccgsl_pointer = nullptr;
118 }
125 fsolver( std::move( v ) ).swap( *this );
126 return *this;
127 }
128#endif
129 // Refines equality comparable
130 // == operator
137 bool operator==( fsolver const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
138 // != operator
145 bool operator!=( fsolver const& v ) const { return not operator==( v ); }
146 // Refines forward container
147 // Refines less than comparable
148 // operator<
157 bool operator<( fsolver const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
158 // operator>
167 bool operator>( fsolver const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
168 // operator<=
177 bool operator<=( fsolver const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
178 // operator>=
187 bool operator>=( fsolver const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
192 bool empty() const { return ccgsl_pointer == 0; }
193 // swap() --- should work even if sizes don't match
199 void swap( fsolver& v ){
200 std::swap( ccgsl_pointer, v.ccgsl_pointer );
201 std::swap( count, v.count );
202 }
203 private:
207 gsl_root_fsolver* ccgsl_pointer;
211 size_t* count;
212 public:
213 // shared reference functions
218 gsl_root_fsolver* get() const { return ccgsl_pointer; }
224 bool unique() const { return count != 0 and *count == 1; }
229 size_t use_count() const { return count == 0 ? 0 : *count; }
235#ifdef __GXX_EXPERIMENTAL_CXX0X__
236 explicit
237#endif
238 operator bool() const { return ccgsl_pointer != 0; }
239
248 inline static int set( fsolver& s, function_scl& f, double x_lower, double x_upper ){
249 return gsl_root_fsolver_set( s.get(), &f, x_lower, x_upper ); }
250#ifndef DOXYGEN_SKIP
259 inline static int set( fsolver& s, gsl_function* f, double x_lower, double x_upper ){
260 return gsl_root_fsolver_set( s.get(), f, x_lower, x_upper ); }
261#endif // DOXYGEN_SKIP
262
268 inline static int iterate( fsolver& s ){
269 return gsl_root_fsolver_iterate( s.get() ); }
270
276 inline static std::string name( fsolver const& s ){
277 return std::string( gsl_root_fsolver_name( s.get() ) ); }
278
284 inline static double root( fsolver const& s ){
285 return gsl_root_fsolver_root( s.get() ); }
286
292 inline static double x_lower( fsolver const& s ){
293 return gsl_root_fsolver_x_lower( s.get() ); }
294
300 inline static double x_upper( fsolver const& s ){
301 return gsl_root_fsolver_x_upper( s.get() ); }
302
310 int set( function_scl& f, double x_lower, double x_upper ){
311 return gsl_root_fsolver_set( get(), &f, x_lower, x_upper ); }
312#ifndef DOXYGEN_SKIP
320 int set( gsl_function* f, double x_lower, double x_upper ){
321 return gsl_root_fsolver_set( get(), f, x_lower, x_upper ); }
322#endif // DOXYGEN_SKIP
323
328 int iterate(){ return gsl_root_fsolver_iterate( get() ); }
329
334 char const* name() const { return gsl_root_fsolver_name( get() ); }
335
340 double root() const { return gsl_root_fsolver_root( get() ); }
341
346 double x_lower() const { return gsl_root_fsolver_x_lower( get() ); }
347
352 double x_upper() const { return gsl_root_fsolver_x_upper( get() ); }
353
358 inline static type const* bisection(){ return gsl_root_fsolver_bisection; }
363 inline static type const* brent(){ return gsl_root_fsolver_brent; }
368 inline static type const* falsepos(){ return gsl_root_fsolver_falsepos; }
369 };
370
376 class fdfsolver {
377 public:
381 typedef gsl_root_fdfsolver_type type;
386 ccgsl_pointer = 0;
387 count = 0; // initially nullptr will do
388 }
389 // Refines random access container
390 // Refines assignable
395 explicit fdfsolver( type const* T ){
396 ccgsl_pointer = gsl_root_fdfsolver_alloc( T );
397 // just plausibly we could allocate fdfsolver but not count
398 try { count = new size_t; } catch( std::bad_alloc& e ){
399 // try to tidy up before rethrowing
400 gsl_root_fdfsolver_free( ccgsl_pointer );
401 throw e;
402 }
403 *count = 1; // initially there is just one reference to ccgsl_pointer
404 }
411 explicit fdfsolver( gsl_root_fdfsolver* v ){
412 ccgsl_pointer = v;
413 // just plausibly we could fail to allocate count: no further action needed.
414 count = new size_t;
415 *count = 1; // initially there is just one reference to ccgsl_pointer
416 }
417 // copy constructor
423 count = v.count; if( count != 0 ) ++*count; }
424 // assignment operator
430 // first, possibly delete anything pointed to by this
431 if( count == 0 or --*count == 0 ){
432 if( ccgsl_pointer != 0 ) gsl_root_fdfsolver_free( ccgsl_pointer );
433 delete count;
434 } // Then copy
435 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
436 }
437 // destructor
442 if( count == 0 or --*count == 0 ){
443 // could have allocated null pointer
444 if( ccgsl_pointer != 0 ) gsl_root_fdfsolver_free( ccgsl_pointer );
445 delete count;
446 }
447 }
448#ifdef __GXX_EXPERIMENTAL_CXX0X__
454 std::swap( count, v.count );
455 v.ccgsl_pointer = nullptr;
456 }
463 fdfsolver( std::move( v ) ).swap( *this );
464 return *this;
465 }
466#endif
467 // Refines equality comparable
468 // == operator
475 bool operator==( fdfsolver const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
476 // != operator
483 bool operator!=( fdfsolver const& v ) const { return not operator==( v ); }
484 // Refines forward container
485 // Refines less than comparable
486 // operator<
495 bool operator<( fdfsolver const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
496 // operator>
505 bool operator>( fdfsolver const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
506 // operator<=
515 bool operator<=( fdfsolver const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
516 // operator>=
525 bool operator>=( fdfsolver const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
530 bool empty() const { return ccgsl_pointer == 0; }
531 // swap() --- should work even if sizes don't match
537 void swap( fdfsolver& v ){
538 std::swap( ccgsl_pointer, v.ccgsl_pointer );
539 std::swap( count, v.count );
540 }
541 private:
545 gsl_root_fdfsolver* ccgsl_pointer;
549 size_t* count;
550 public:
551 // shared reference functions
556 gsl_root_fdfsolver* get() const { return ccgsl_pointer; }
562 bool unique() const { return count != 0 and *count == 1; }
567 size_t use_count() const { return count == 0 ? 0 : *count; }
573#ifdef __GXX_EXPERIMENTAL_CXX0X__
574 explicit
575#endif
576 operator bool() const { return ccgsl_pointer != 0; }
577
585 inline static int set( fdfsolver& s, function_fdf& fdf, double root ){
586 return gsl_root_fdfsolver_set( s.get(), &fdf, root ); }
587#ifndef DOXYGEN_SKIP
595 inline static int set( fdfsolver& s, gsl_function_fdf* fdf, double root ){
596 return gsl_root_fdfsolver_set( s.get(), fdf, root ); }
597#endif // DOXYGEN_SKIP
598
604 inline static int iterate( fdfsolver& s ){ return gsl_root_fdfsolver_iterate( s.get() ); }
605
611 inline static std::string name( fdfsolver const& s ){
612 return std::string( gsl_root_fdfsolver_name( s.get() ) ); }
613
619 inline static double root( fdfsolver const& s ){ return gsl_root_fdfsolver_root( s.get() ); }
620
627 int set( function_fdf& fdf, double root ){
628 return gsl_root_fdfsolver_set( get(), &fdf, root ); }
629#ifndef DOXYGEN_SKIP
636 int set( gsl_function_fdf* fdf, double root ){
637 return gsl_root_fdfsolver_set( get(), fdf, root ); }
638#endif // DOXYGEN_SKIP
639
644 int iterate(){ return gsl_root_fdfsolver_iterate( get() ); }
645
650 char const* name(){ return gsl_root_fdfsolver_name( get() ); }
651
656 double root(){ return gsl_root_fdfsolver_root( get() ); }
657
662 inline static type const* newton(){ return gsl_root_fdfsolver_newton; }
667 inline static type const* secant(){ return gsl_root_fdfsolver_secant; }
672 inline static type const* steffenson(){ return gsl_root_fdfsolver_steffenson; }
673 };
674
678 namespace test {
687 inline int interval( double x_lower, double x_upper, double epsabs, double epsrel ){
688 return gsl_root_test_interval( x_lower, x_upper, epsabs, epsrel ); }
689
696 inline int residual( double f, double epsabs ){
697 return gsl_root_test_residual( f, epsabs ); }
698
707 inline int delta( double x1, double x0, double epsabs, double epsrel ){
708 return gsl_root_test_delta( x1, x0, epsabs, epsrel ); }
709 }
710 }
711}
712#endif
Class that extends gsl_function_fdf so that it can be constructed from arbitrary function objects.
Class that extends gsl_function so that it can be constructed from arbitrary function objects.
Workspace for root finding with a derivative.
Definition: roots.hpp:376
static std::string name(fdfsolver const &s)
C++ version of gsl_root_fdfsolver_name().
Definition: roots.hpp:611
int iterate()
C++ version of gsl_root_fdfsolver_iterate().
Definition: roots.hpp:644
double root()
C++ version of gsl_root_fdfsolver_root().
Definition: roots.hpp:656
static double root(fdfsolver const &s)
C++ version of gsl_root_fdfsolver_root().
Definition: roots.hpp:619
size_t use_count() const
Find how many fdfsolver objects share this pointer.
Definition: roots.hpp:567
size_t * count
The shared reference count.
Definition: roots.hpp:549
static int iterate(fdfsolver &s)
C++ version of gsl_root_fdfsolver_iterate().
Definition: roots.hpp:604
fdfsolver()
The default constructor is only really useful for assigning to.
Definition: roots.hpp:385
static int set(fdfsolver &s, function_fdf &fdf, double root)
C++ version of gsl_root_fdfsolver_set().
Definition: roots.hpp:585
fdfsolver(fdfsolver &&v)
Move constructor.
Definition: roots.hpp:453
static type const * newton()
Static type.
Definition: roots.hpp:662
bool operator<(fdfsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:495
fdfsolver & operator=(fdfsolver const &v)
The assignment operator.
Definition: roots.hpp:429
int set(function_fdf &fdf, double root)
C++ version of gsl_root_fdfsolver_set().
Definition: roots.hpp:627
bool operator>=(fdfsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:525
fdfsolver(gsl_root_fdfsolver *v)
Could construct from a gsl_root_fdfsolver.
Definition: roots.hpp:411
fdfsolver(type const *T)
The default constructor creates a new fdfsolver of one of the type fdfsolver::type.
Definition: roots.hpp:395
gsl_root_fdfsolver_type type
Typedef.
Definition: roots.hpp:381
bool empty() const
Find if the fdfsolver is empty.
Definition: roots.hpp:530
fdfsolver & operator=(fdfsolver &&v)
Move operator.
Definition: roots.hpp:462
gsl_root_fdfsolver * ccgsl_pointer
The shared pointer.
Definition: roots.hpp:545
bool operator!=(fdfsolver const &v) const
Two fdfsolver are different if their elements are not identical.
Definition: roots.hpp:483
static type const * secant()
Static type.
Definition: roots.hpp:667
gsl_root_fdfsolver * get() const
Get the gsl_root_fdfsolver.
Definition: roots.hpp:556
bool unique() const
Find if this is the only object sharing the gsl_root_fdfsolver.
Definition: roots.hpp:562
static type const * steffenson()
Static type.
Definition: roots.hpp:672
bool operator<=(fdfsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:515
void swap(fdfsolver &v)
Swap two fdfsolver.
Definition: roots.hpp:537
fdfsolver(fdfsolver const &v)
The copy constructor.
Definition: roots.hpp:422
bool operator>(fdfsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:505
~fdfsolver()
The destructor only deletes the pointers if count reaches zero.
Definition: roots.hpp:441
char const * name()
C++ version of gsl_root_fdfsolver_name().
Definition: roots.hpp:650
bool operator==(fdfsolver const &v) const
Two fdfsolver are identically equal if their elements are identical.
Definition: roots.hpp:475
Workspace for root finding without a derivative.
Definition: roots.hpp:38
static type const * bisection()
Static type.
Definition: roots.hpp:358
double x_upper() const
C++ version of gsl_root_fsolver_x_upper().
Definition: roots.hpp:352
bool operator>(fsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:167
gsl_root_fsolver * get() const
Get the gsl_root_fsolver.
Definition: roots.hpp:218
void swap(fsolver &v)
Swap two fsolver.
Definition: roots.hpp:199
gsl_root_fsolver_type type
Typedef.
Definition: roots.hpp:43
bool operator==(fsolver const &v) const
Two fsolver are identically equal if their elements are identical.
Definition: roots.hpp:137
static int iterate(fsolver &s)
C++ version of gsl_root_fsolver_iterate().
Definition: roots.hpp:268
int iterate()
C++ version of gsl_root_fsolver_iterate().
Definition: roots.hpp:328
~fsolver()
The destructor only deletes the pointers if count reaches zero.
Definition: roots.hpp:103
static std::string name(fsolver const &s)
C++ version of gsl_root_fsolver_name().
Definition: roots.hpp:276
fsolver(fsolver &&v)
Move constructor.
Definition: roots.hpp:115
size_t * count
The shared reference count.
Definition: roots.hpp:211
bool operator!=(fsolver const &v) const
Two fsolver are different if their elements are not identical.
Definition: roots.hpp:145
bool empty() const
Find if the fsolver is empty.
Definition: roots.hpp:192
static type const * falsepos()
Static type.
Definition: roots.hpp:368
fsolver(gsl_root_fsolver *v)
Could construct from a gsl_root_fsolver.
Definition: roots.hpp:73
bool operator>=(fsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:187
bool operator<(fsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:157
gsl_root_fsolver * ccgsl_pointer
The shared pointer.
Definition: roots.hpp:207
bool operator<=(fsolver const &v) const
A container needs to define an ordering for sorting.
Definition: roots.hpp:177
char const * name() const
C++ version of gsl_root_fsolver_name().
Definition: roots.hpp:334
int set(function_scl &f, double x_lower, double x_upper)
C++ version of gsl_root_fsolver_set().
Definition: roots.hpp:310
static double root(fsolver const &s)
C++ version of gsl_root_fsolver_root().
Definition: roots.hpp:284
fsolver & operator=(fsolver &&v)
Move operator.
Definition: roots.hpp:124
double x_lower() const
C++ version of gsl_root_fsolver_x_lower().
Definition: roots.hpp:346
double root() const
C++ version of gsl_root_fsolver_root().
Definition: roots.hpp:340
static int set(fsolver &s, function_scl &f, double x_lower, double x_upper)
C++ version of gsl_root_fsolver_set().
Definition: roots.hpp:248
fsolver()
The default constructor is only really useful for assigning to.
Definition: roots.hpp:47
fsolver & operator=(fsolver const &v)
The assignment operator.
Definition: roots.hpp:91
fsolver(type const *T)
The default constructor creates a new fsolver of type fsolver::type.
Definition: roots.hpp:57
static double x_upper(fsolver const &s)
C++ version of gsl_root_fsolver_x_upper().
Definition: roots.hpp:300
bool unique() const
Find if this is the only object sharing the gsl_root_fsolver.
Definition: roots.hpp:224
fsolver(fsolver const &v)
The copy constructor.
Definition: roots.hpp:84
static type const * brent()
Static type.
Definition: roots.hpp:363
size_t use_count() const
Find how many fsolver objects share this pointer.
Definition: roots.hpp:229
static double x_lower(fsolver const &s)
C++ version of gsl_root_fsolver_x_lower().
Definition: roots.hpp:292
int test(double const xtol, double const gtol, double const ftol, int *info, workspace const &w)
C++ version of gsl_multifit_nlinear_test().
gsl_multilarge_nlinear_fdf fdf
Typedef for shorthand.
int residual(double f, double epsabs)
C++ version of gsl_root_test_residual().
Definition: roots.hpp:696
int delta(double x1, double x0, double epsabs, double epsrel)
C++ version of gsl_root_test_delta().
Definition: roots.hpp:707
int interval(double x_lower, double x_upper, double epsabs, double epsrel)
C++ version of gsl_root_test_interval().
Definition: roots.hpp:687
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34