ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
min.hpp
Go to the documentation of this file.
1/*
2 * $Id: min.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_MIN_HPP
21#define CCGSL_MIN_HPP
22
23#include<new>
24#include<gsl/gsl_min.h>
25#include"function_scl.hpp"
26
27namespace gsl {
31 namespace min {
36 class fminimizer {
37 public:
41 typedef gsl_min_fminimizer_type type;
46 ccgsl_pointer = 0;
47 count = 0; // initially nullptr will do
48 }
49 // Refines random access container
50 // Refines assignable
55 explicit fminimizer( type const* T ){
56 ccgsl_pointer = gsl_min_fminimizer_alloc( T );
57 // just plausibly we could allocate fminimizer but not count
58 try { count = new size_t; } catch( std::bad_alloc& e ){
59 // try to tidy up before rethrowing
60 gsl_min_fminimizer_free( ccgsl_pointer );
61 throw e;
62 }
63 *count = 1; // initially there is just one reference to ccgsl_pointer
64 }
71 explicit fminimizer( gsl_min_fminimizer* v ){
72 ccgsl_pointer = v;
73 // just plausibly we could fail to allocate count: no further action needed.
74 count = new size_t;
75 *count = 1; // initially there is just one reference to ccgsl_pointer
76 }
77 // copy constructor
83 count = v.count; if( count != 0 ) ++*count; }
84 // assignment operator
90 // first, possibly delete anything pointed to by this
91 if( count == 0 or --*count == 0 ){
92 if( ccgsl_pointer != 0 ) gsl_min_fminimizer_free( ccgsl_pointer );
93 delete count;
94 } // Then copy
95 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
96 }
97 // destructor
102 if( count == 0 or --*count == 0 ){
103 // could have allocated null pointer
104 if( ccgsl_pointer != 0 ) gsl_min_fminimizer_free( ccgsl_pointer );
105 delete count;
106 }
107 }
108#ifdef __GXX_EXPERIMENTAL_CXX0X__
114 std::swap( count, v.count );
115 v.ccgsl_pointer = nullptr;
116 }
123 fminimizer( std::move( v ) ).swap( *this );
124 return *this;
125 }
126#endif
127 // Refines equality comparable
128 // == operator
135 bool operator==( fminimizer const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
136 // != operator
143 bool operator!=( fminimizer const& v ) const { return not operator==( v ); }
144 // Refines forward container
145 // Refines less than comparable
146 // operator<
155 bool operator<( fminimizer const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
156 // operator>
165 bool operator>( fminimizer const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
166 // operator<=
175 bool operator<=( fminimizer const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
176 // operator>=
185 bool operator>=( fminimizer const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
190 bool empty() const { return ccgsl_pointer == 0; }
191 // swap() --- should work even if sizes don't match
197 void swap( fminimizer& v ){
198 std::swap( ccgsl_pointer, v.ccgsl_pointer );
199 std::swap( count, v.count );
200 }
201 private:
205 gsl_min_fminimizer* ccgsl_pointer;
209 size_t* count;
210 public:
211 // shared reference functions
216 gsl_min_fminimizer* get() const { return ccgsl_pointer; }
222 bool unique() const { return count != 0 and *count == 1; }
227 size_t use_count() const { return count == 0 ? 0 : *count; }
233#ifdef __GXX_EXPERIMENTAL_CXX0X__
234 explicit
235#endif
236 operator bool() const { return ccgsl_pointer != 0; }
237
247 inline static int set( fminimizer& s, function_scl& f, double x_minimum,
248 double x_lower, double x_upper ){
249 return gsl_min_fminimizer_set( s.get(), &f, x_minimum, x_lower, x_upper ); }
250#ifndef DOXYGEN_SKIP
260 inline static int set( fminimizer& s, gsl_function* f, double x_minimum,
261 double x_lower, double x_upper ){
262 return gsl_min_fminimizer_set( s.get(), f, x_minimum, x_lower, x_upper ); }
263#endif // DOXYGEN_SKIP
264
277 inline static int set_with_values( fminimizer& s, function_scl& f,
278 double x_minimum, double f_minimum,
279 double x_lower, double f_lower,
280 double x_upper, double f_upper ){
281 return gsl_min_fminimizer_set_with_values( s.get(), &f, x_minimum, f_minimum,
283#ifndef DOXYGEN_SKIP
296 inline static int set_with_values( fminimizer& s, gsl_function* f,
297 double x_minimum, double f_minimum,
298 double x_lower, double f_lower,
299 double x_upper, double f_upper ){
300 return gsl_min_fminimizer_set_with_values( s.get(), f, x_minimum, f_minimum,
302#endif // DOXYGEN_SKIP
303
309 inline static int iterate( fminimizer& s ){ return gsl_min_fminimizer_iterate( s.get() ); }
310
316 inline static std::string name( fminimizer const& s ){
317 return gsl_min_fminimizer_name( s.get() ); }
318
324 inline static double x_minimum( fminimizer const& s ){
325 return gsl_min_fminimizer_x_minimum( s.get() ); }
326
332 inline static double x_lower( fminimizer const& s ){
333 return gsl_min_fminimizer_x_lower( s.get() ); }
334
340 inline static double x_upper( fminimizer const& s ){
341 return gsl_min_fminimizer_x_upper( s.get() ); }
342
348 inline static double f_minimum( fminimizer const& s ){
349 return gsl_min_fminimizer_f_minimum( s.get() ); }
350
356 inline static double f_lower( fminimizer const& s ){
357 return gsl_min_fminimizer_f_lower( s.get() ); }
358
364 inline static double f_upper( fminimizer const& s ){
365 return gsl_min_fminimizer_f_upper( s.get() ); }
366
375 inline int set( function_scl& f, double x_minimum, double x_lower, double x_upper ){
376 return gsl_min_fminimizer_set( get(), &f, x_minimum, x_lower, x_upper ); }
377#ifndef DOXYGEN_SKIP
386 inline int set( gsl_function* f, double x_minimum, double x_lower, double x_upper ){
387 return gsl_min_fminimizer_set( get(), f, x_minimum, x_lower, x_upper ); }
388#endif // DOXYGEN_SKIP
389
401 inline int set_with_values( function_scl& f, double x_minimum, double f_minimum,
402 double x_lower, double f_lower, double x_upper, double f_upper ){
403 return gsl_min_fminimizer_set_with_values( get(), &f, x_minimum, f_minimum,
405#ifndef DOXYGEN_SKIP
417 inline int set_with_values( gsl_function* f, double x_minimum, double f_minimum,
418 double x_lower, double f_lower, double x_upper, double f_upper ){
419 return gsl_min_fminimizer_set_with_values( get(), f, x_minimum, f_minimum,
421#endif // DOXYGEN_SKIP
422
423
428 int iterate(){ return gsl_min_fminimizer_iterate( get() ); }
429
434 char const* name() const { return gsl_min_fminimizer_name( get() ); }
435
440 double x_minimum() const { return gsl_min_fminimizer_x_minimum( get() ); }
441
446 double x_lower(){ return gsl_min_fminimizer_x_lower( get() ); }
447
452 double x_upper() const { return gsl_min_fminimizer_x_upper( get() ); }
453
458 double f_minimum() const { return gsl_min_fminimizer_f_minimum( get() ); }
459
464 double f_lower() const { return gsl_min_fminimizer_f_lower( get() ); }
465
470 double f_upper() const { return gsl_min_fminimizer_f_upper( get() ); }
471
476 inline static type const* goldensection(){ return gsl_min_fminimizer_goldensection; }
477
482 inline static type const* brent(){ return gsl_min_fminimizer_brent; }
483
488 inline static type const* quad_golden(){ return gsl_min_fminimizer_quad_golden; }
489
490 };
491
500 inline int test_interval( double x_lower, double x_upper, double epsabs, double epsrel ){
501 return gsl_min_test_interval( x_lower, x_upper, epsabs, epsrel ); }
502
515 inline int find_bracket( function_scl& f, double& x_minimum, double& f_minimum,
516 double& x_lower, double& f_lower, double& x_upper,
517 double& f_upper, size_t eval_max ){
518 return gsl_min_find_bracket( &f, &x_minimum, &f_minimum, &x_lower, &f_lower, &x_upper,
519 &f_upper, eval_max ); }
520#ifndef DOXYGEN_SKIP
533 inline int find_bracket( gsl_function* f, double* x_minimum, double* f_minimum,
534 double* x_lower, double* f_lower, double* x_upper,
535 double* f_upper, size_t eval_max ){
536 return gsl_min_find_bracket( f, x_minimum, f_minimum, x_lower, f_lower, x_upper,
537 f_upper, eval_max ); }
538#endif
539 }
540}
541#endif
Class that extends gsl_function so that it can be constructed from arbitrary function objects.
Workspace for one-dimensional minimisation.
Definition: min.hpp:36
bool empty() const
Find if the fminimizer is empty.
Definition: min.hpp:190
fminimizer(fminimizer const &v)
The copy constructor.
Definition: min.hpp:82
int set_with_values(function_scl &f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
C++ version of gsl_min_fminimizer_set_with_values().
Definition: min.hpp:401
gsl_min_fminimizer * ccgsl_pointer
The shared pointer.
Definition: min.hpp:205
double f_lower() const
C++ version of gsl_min_fminimizer_f_lower().
Definition: min.hpp:464
~fminimizer()
The destructor only deletes the pointers if count reaches zero.
Definition: min.hpp:101
fminimizer & operator=(fminimizer const &v)
The assignment operator.
Definition: min.hpp:89
bool operator==(fminimizer const &v) const
Two fminimizer are identically equal if their elements are identical.
Definition: min.hpp:135
void swap(fminimizer &v)
Swap two fminimizer objects.
Definition: min.hpp:197
static int iterate(fminimizer &s)
C++ version of gsl_min_fminimizer_iterate().
Definition: min.hpp:309
static int set(fminimizer &s, function_scl &f, double x_minimum, double x_lower, double x_upper)
C++ version of gsl_min_fminimizer_set().
Definition: min.hpp:247
double f_upper() const
C++ version of gsl_min_fminimizer_f_upper().
Definition: min.hpp:470
static double f_minimum(fminimizer const &s)
C++ version of gsl_min_fminimizer_f_minimum().
Definition: min.hpp:348
int set(function_scl &f, double x_minimum, double x_lower, double x_upper)
C++ version of gsl_min_fminimizer_set().
Definition: min.hpp:375
fminimizer(fminimizer &&v)
Move constructor.
Definition: min.hpp:113
bool operator>(fminimizer const &v) const
A container needs to define an ordering for sorting.
Definition: min.hpp:165
bool operator<(fminimizer const &v) const
A container needs to define an ordering for sorting.
Definition: min.hpp:155
static std::string name(fminimizer const &s)
C++ version of gsl_min_fminimizer_name().
Definition: min.hpp:316
fminimizer()
The default constructor is only really useful for assigning to.
Definition: min.hpp:45
double f_minimum() const
C++ version of gsl_min_fminimizer_f_minimum().
Definition: min.hpp:458
fminimizer(gsl_min_fminimizer *v)
Could construct from a gsl_min_fminimizer.
Definition: min.hpp:71
bool unique() const
Find if this is the only object sharing the gsl_min_fminimizer.
Definition: min.hpp:222
size_t use_count() const
Find how many fminimizer objects share this pointer.
Definition: min.hpp:227
gsl_min_fminimizer * get() const
Get the gsl_min_fminimizer.
Definition: min.hpp:216
static double f_upper(fminimizer const &s)
C++ version of gsl_min_fminimizer_f_upper().
Definition: min.hpp:364
size_t * count
The shared reference count.
Definition: min.hpp:209
static type const * goldensection()
Static type.
Definition: min.hpp:476
double x_upper() const
C++ version of gsl_min_fminimizer_x_upper().
Definition: min.hpp:452
static double x_upper(fminimizer const &s)
C++ version of gsl_min_fminimizer_x_upper().
Definition: min.hpp:340
fminimizer(type const *T)
The standard constructor creates a new minimizer of type fminimizer::type.
Definition: min.hpp:55
bool operator<=(fminimizer const &v) const
A container needs to define an ordering for sorting.
Definition: min.hpp:175
fminimizer & operator=(fminimizer &&v)
Move operator.
Definition: min.hpp:122
char const * name() const
C++ version of gsl_min_fminimizer_name().
Definition: min.hpp:434
int iterate()
C++ version of gsl_min_fminimizer_iterate().
Definition: min.hpp:428
static type const * brent()
Static type.
Definition: min.hpp:482
static double f_lower(fminimizer const &s)
C++ version of gsl_min_fminimizer_f_lower().
Definition: min.hpp:356
static double x_minimum(fminimizer const &s)
C++ version of gsl_min_fminimizer_x_minimum().
Definition: min.hpp:324
static int set_with_values(fminimizer &s, function_scl &f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
C++ version of gsl_min_fminimizer_set_with_values().
Definition: min.hpp:277
static double x_lower(fminimizer const &s)
C++ version of gsl_min_fminimizer_x_lower().
Definition: min.hpp:332
bool operator>=(fminimizer const &v) const
A container needs to define an ordering for sorting.
Definition: min.hpp:185
double x_minimum() const
C++ version of gsl_min_fminimizer_x_minimum().
Definition: min.hpp:440
static type const * quad_golden()
Static type.
Definition: min.hpp:488
bool operator!=(fminimizer const &v) const
Two fminimizer are different if their elements are not identical.
Definition: min.hpp:143
double x_lower()
C++ version of gsl_min_fminimizer_x_lower().
Definition: min.hpp:446
gsl_min_fminimizer_type type
Typedef.
Definition: min.hpp:41
int test_interval(double x_lower, double x_upper, double epsabs, double epsrel)
C++ version of gsl_min_test_interval().
Definition: min.hpp:500
int find_bracket(function_scl &f, double &x_minimum, double &f_minimum, double &x_lower, double &f_lower, double &x_upper, double &f_upper, size_t eval_max)
C++ version of gsl_min_find_bracket().
Definition: min.hpp:515
int min(movstat::end_t const endtype, vector const &x, vector &y, workspace &w)
C++ version of gsl_movstat_min().
Definition: movstat.hpp:581
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34