ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
multilarge_nlinear.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_MULTILARGE
21#define CCGSL_MULTILARGE
22
23#include<cmath>
24#include<gsl/gsl_multilarge_nlinear.h>
25#include"matrix.hpp"
26#include"vector.hpp"
28
29namespace gsl {
30 namespace multilarge {
34 namespace nlinear {
38 typedef gsl_multilarge_nlinear_type type;
39 namespace {
43 type const* trust = gsl_multilarge_nlinear_trust;
44 }
48 typedef gsl_multilarge_nlinear_parameters parameters;
52 typedef gsl_multilarge_nlinear_trs trs;
56 typedef gsl_multilarge_nlinear_scale scale;
60 typedef gsl_multilarge_nlinear_solver solver;
61 namespace {
65 trs const* lm = gsl_multilarge_nlinear_trs_lm;
69 trs const* lmaccel = gsl_multilarge_nlinear_trs_lmaccel;
73 trs const* dogleg = gsl_multilarge_nlinear_trs_dogleg;
77 trs const* ddogleg = gsl_multilarge_nlinear_trs_ddogleg;
81 trs const* subspace2D = gsl_multilarge_nlinear_trs_subspace2D;
85 trs const* cgst = gsl_multilarge_nlinear_trs_cgst;
89 scale const* levenberg = gsl_multilarge_nlinear_scale_levenberg;
93 scale const* marquardt = gsl_multilarge_nlinear_scale_marquardt;
97 scale const* more = gsl_multilarge_nlinear_scale_more;
101 solver const* cholesky = gsl_multilarge_nlinear_solver_cholesky;
105 //solver const* mcholesky = gsl_multilarge_nlinear_solver_mcholesky;
109 solver const* none = gsl_multilarge_nlinear_solver_none;
110 }
114 typedef gsl_multilarge_nlinear_fdf fdf;
118 typedef gsl_multilarge_nlinear_fdtype fdtype;
122 class workspace {
123 public:
128 ccgsl_pointer = 0;
129 count = 0; // initially nullptr will do
130 }
138 explicit workspace( type const* T, parameters const* params, size_t const n,
139 size_t const p ){
140 ccgsl_pointer = gsl_multilarge_nlinear_alloc( T, params, n, p );
141 // just plausibly we could allocate workspace but not count
142 try { count = new size_t; } catch( std::bad_alloc& e ){
143 // try to tidy up before rethrowing
144 gsl_multilarge_nlinear_free( ccgsl_pointer );
145 throw e;
146 }
147 *count = 1; // initially there is just one reference to ccgsl_pointer
148 }
155 explicit workspace( gsl_multilarge_nlinear_workspace* v ){
156 ccgsl_pointer = v;
157 // just plausibly we could fail to allocate count: no further action needed.
158 count = new size_t;
159 *count = 1; // initially there is just one reference to ccgsl_pointer
160 }
161 // copy constructor
167 count = v.count; if( count != 0 ) ++*count; }
168 // assignment operator
174 // first, possibly delete anything pointed to by this
175 if( count == 0 or --*count == 0 ){
176 if( ccgsl_pointer != 0 ) gsl_multilarge_nlinear_free( ccgsl_pointer );
177 delete count;
178 } // Then copy
179 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count;
180 return *this;
181 }
182 // destructor
187 if( count == 0 or --*count == 0 ){
188 // could have allocated null pointer
189 if( ccgsl_pointer != 0 ) gsl_multilarge_nlinear_free( ccgsl_pointer );
190 delete count;
191 }
192 }
193#ifdef __GXX_EXPERIMENTAL_CXX0X__
199 std::swap( count, v.count );
200 v.ccgsl_pointer = nullptr;
201 }
208 workspace( std::move( v ) ).swap( *this );
209 return *this;
210 }
211#endif
212 // Allow possibility of assigning from gsl_multilarge_nlinear_workspace without sharing
223 ( gsl_multilarge_nlinear_workspace* w ){
224 if( count != 0 and --*count == 0 ){
225 // could have allocated null pointer
226 if( ccgsl_pointer != 0 ) gsl_multilarge_nlinear_free( ccgsl_pointer );
227 }
228 ccgsl_pointer = w;
229 if(0 == count) count = new size_t;
230 *count = 2; // should never be able to delete ccgsl_pointer
231 }
232 // Refines equality comparable
233 // == operator
240 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
241 // != operator
248 bool operator!=( workspace const& v ) const { return not operator==( v ); }
249 // Refines forward container
250 // Refines less than comparable
251 // operator<
260 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
261 // operator>
270 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
271 // operator<=
280 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
281 // operator>=
290 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
295 bool empty() const { return ccgsl_pointer == 0; }
296 // swap() --- should work even if sizes don't match
302 void swap( workspace& v ){
303 std::swap( ccgsl_pointer, v.ccgsl_pointer );
304 std::swap( count, v.count );
305 }
306 private:
310 gsl_multilarge_nlinear_workspace* ccgsl_pointer;
314 size_t* count;
315 public:
316 // shared reference functions
321 gsl_multilarge_nlinear_workspace* get() const { return ccgsl_pointer; }
327 bool unique() const { return count != 0 and *count == 1; }
332 size_t use_count() const { return count == 0 ? 0 : *count; }
338#ifdef __GXX_EXPERIMENTAL_CXX0X__
339 explicit
340#endif
341 operator bool() const { return ccgsl_pointer != 0; }
349 return gsl_multilarge_nlinear_init( x.get(), &fdf, get() ); }
357 int winit( vector const& x, vector const& wts,
359 return gsl_multilarge_nlinear_winit( x.get(), wts.get(), &fdf, get() ); }
364 int iterate(){ return gsl_multilarge_nlinear_iterate( get() ); }
369 double avratio() const { return gsl_multilarge_nlinear_avratio( get() ); }
375 int rcond( double& rcond ){ return gsl_multilarge_nlinear_rcond( &rcond, get() ); }
381 int covar( matrix& covar ){ return gsl_multilarge_nlinear_covar( covar.get(), get() ); }
382 };
395 public:
401 virtual void function( size_t const iter, workspace const& w ) = 0;
402 };
403#ifndef DOXYGEN_SKIP
407 class CallbackImplement {
408 public:
412 typedef void (*function_t)(size_t const,void*,gsl_multilarge_nlinear_workspace const*);
416 class base_F {
417 public:
421 virtual ~base_F(){}
426 virtual function_t getFunction() = 0;
427 };
431 template<typename T> class F : public base_F {
432 public:
436 F(){}
443 static void function( size_t const i, void* params,
444 gsl_multilarge_nlinear_workspace const* w ){
445 T* t = reinterpret_cast<T*>( params );
446 workspace W;
447 W.wrap_gsl_multilarge_nlinear_workspace_without_ownership
448 ( const_cast<gsl_multilarge_nlinear_workspace*>( w ) );
449 return t->function( i, W );
450 }
455 function_t getFunction(){ return &F<T>::function; }
456 };
461 template<typename T> CallbackImplement( T& t ){
462 f = new F<T>;
463 // just plausibly we could fail to allocate count: no further action needed.
464 count = new size_t;
465 *count = 1; // initially there is just one reference to f
466 }
467 // Copy and move constructors
468 // copy constructor
473 CallbackImplement( CallbackImplement const& v ) : f( v.f ), count( v.count ){
474 if( count != 0 ) ++*count; // CallbackImplementation is now shared.
475 }
476 // assignment operator
481 CallbackImplement& operator=( CallbackImplement const& v ){
482 // first, possibly delete anything pointed to by this
483 if( count == 0 or --*count == 0 ){
484 delete f;
485 delete count;
486 }
487 // Then copy
488 f = v.f;
489 count = v.count;
490 if( count != 0 ) ++*count; // CallbackImplement object is now shared.
491 return *this;
492 }
493#ifdef __GXX_EXPERIMENTAL_CXX0X__
498 CallbackImplement( CallbackImplement&& v ) : f( v.f ), count( nullptr ){
499 std::swap( count, v.count );
500 v.f = nullptr;
501 }
507 CallbackImplement& operator=( CallbackImplement&& v ){
508 std::swap( f, v.f );
509 std::swap( count, v.count );
510 return *this;
511 }
512#endif
516 ~CallbackImplement(){
517 // first, possibly delete anything pointed to by f
518 if( count == 0 or --*count == 0 ){
519 delete f;
520 delete count;
521 }
522 }
527 function_t getFunction(){ return f->getFunction(); }
528 private:
532 base_F* f;
536 size_t* count;
537 };
538#endif
539 // Functions
545 return gsl_multilarge_nlinear_default_parameters(); }
554 workspace& w ){
555 return gsl_multilarge_nlinear_init( x.get(), &fdf, w.get() ); }
564 inline int winit( vector const& x, vector const& wts,
566 return gsl_multilarge_nlinear_winit( x.get(), wts.get(), &fdf, w.get() ); }
572 inline int iterate( workspace& w ){ return gsl_multilarge_nlinear_iterate( w.get() ); }
578 inline double avratio( workspace const& w ){
579 return gsl_multilarge_nlinear_avratio( w.get() ); }
586 inline int rcond( double& rcond, workspace const& w ){
587 return gsl_multilarge_nlinear_rcond( &rcond, w.get() ); }
594 inline int covar( matrix& covar, workspace& w ){
595 return gsl_multilarge_nlinear_covar( covar.get(), w.get() ); }
608 inline int
609 driver( size_t const maxiter,
610 double const xtol,
611 double const gtol,
612 double const ftol,
613 CallbackBase& callback,
614 int& info, workspace& w ){
615 void* callback_params = reinterpret_cast<void*>( &callback );
616 CallbackImplement callbackImplement( callback );
617 void (*function)(size_t const,void*,gsl_multilarge_nlinear_workspace const*)
618 = callbackImplement.getFunction();
619 return gsl_multilarge_nlinear_driver( maxiter, xtol, gtol, ftol, function,
620 callback_params, &info, w.get() ); }
624 typedef void (*driver_callback)(const size_t iter, void* params,
625 gsl_multilarge_nlinear_workspace const* w);
638 inline int driver( size_t const maxiter, double const xtol, double const gtol,
639 double const ftol,driver_callback callback,
640 void* callback_params, int& info, workspace& w ){
641 return gsl_multilarge_nlinear_driver( maxiter, xtol, gtol, ftol, callback,
642 callback_params, &info, w.get() ); }
648 inline char const* name( workspace const& w ){
649 return gsl_multilarge_nlinear_name( w.get() ); }
656 return vector( gsl_multilarge_nlinear_position( w.get() ) ); }
662 inline vector residual( workspace const& w ){
663 return vector( gsl_multilarge_nlinear_residual( w.get() ) ); }
669 inline vector step( workspace const& w ){
670 return vector( gsl_multilarge_nlinear_step( w.get() ) ); }
676 inline size_t niter( workspace const& w ){
677 return gsl_multilarge_nlinear_niter( w.get() ); }
683 inline char const* trs_name( workspace const& w ){
684 return gsl_multilarge_nlinear_trs_name( w.get() ); }
694 vector const& swts, vector& y ){
695 return gsl_multilarge_nlinear_eval_f( &fdf, x.get(), swts.get(), y.get() ); }
711 inline int eval_df( CBLAS_TRANSPOSE_t const TransJ, vector const& x, vector const& f,
712 vector const& u, vector const& swts, double const h,
714 vector& v, matrix& JTJ, vector& work ){
715 return gsl_multilarge_nlinear_eval_df( TransJ, x.get(), f.get(), u.get(),
716 swts.get(), h, fdtype, &fdf, v.get(),
717 JTJ.get(), work.get() ); }
730 inline int eval_fvv( double const h, vector const& x, vector const& v, vector const& f,
732 vector& yvv, vector& work ){
733 return gsl_multilarge_nlinear_eval_fvv( h, x.get(), v.get(), f.get(), swts.get(),
734 &fdf, yvv.get(), work.get() ); }
744 inline int test( double const xtol, double const gtol, double const ftol, int& info,
745 workspace const& w ){
746 return gsl_multilarge_nlinear_test( xtol, gtol, ftol, &info, w.get() ); }
759 inline int df( double const h, fdtype const fdtype, vector const& x, vector const& wts,
761 vector& work ){
762 return gsl_multilarge_nlinear_df( h, fdtype, x.get(), wts.get(), &fdf, f.get(), J.get(),
763 work.get() ); }
777 inline int fdfvv( double const h, vector const& x, vector const& v, vector const& f,
778 matrix const& J, vector const& swts,
780 vector& work ){
781 return gsl_multilarge_nlinear_fdfvv( h, x.get(), v.get(), f.get(), J.get(), swts.get(),
782 &fdf, fvv.get(), work.get() ); }
783 }
784 }
785}
786
787#endif
This class handles matrix objects as shared handles.
Definition: matrix.hpp:72
gsl_matrix * get()
Get the gsl_matrix.
Definition: matrix.hpp:1207
You can create callbacks as a subclass of this class.
virtual void function(size_t const iter, workspace const &w)=0
This is the callback function.
Class that extends gsl_multilarge_nlinear_fdf so that it can be constructed from arbitrary function o...
Workspace for solving systems.
bool unique() const
Find if this is the only object sharing the gsl_multilarge_nlinear_workspace.
workspace()
The default constructor is only really useful for assigning to.
double avratio() const
C++ version of gsl_multilarge_nlinear_avratio().
gsl_multilarge_nlinear_workspace * ccgsl_pointer
The shared pointer.
workspace(workspace &&v)
Move constructor.
bool operator==(workspace const &v) const
Two workspace are identically equal if their elements are identical.
bool empty() const
Find if the workspace is empty.
workspace(workspace const &v)
The copy constructor.
int winit(vector const &x, vector const &wts, gsl::multilarge::nlinear::function_fdf &fdf)
C++ version of gsl_multilarge_nlinear_winit().
int covar(matrix &covar)
C++ version of gsl_multilarge_nlinear_covar().
bool operator>=(workspace const &v) const
A container needs to define an ordering for sorting.
bool operator>(workspace const &v) const
A container needs to define an ordering for sorting.
bool operator<(workspace const &v) const
A container needs to define an ordering for sorting.
void swap(workspace &v)
Swap two workspace objects.
int rcond(double &rcond)
C++ version of gsl_multilarge_nlinear_rcond().
~workspace()
The destructor only deletes the pointers if count reaches zero.
gsl_multilarge_nlinear_workspace * get() const
Get the gsl_multilarge_nlinear_workspace.
bool operator<=(workspace const &v) const
A container needs to define an ordering for sorting.
int init(vector const &x, gsl::multilarge::nlinear::function_fdf &fdf)
C++ version of gsl_multilarge_nlinear_init().
workspace(type const *T, parameters const *params, size_t const n, size_t const p)
The default constructor creates a new workspace with window length K.
void wrap_gsl_multilarge_nlinear_workspace_without_ownership(gsl_multilarge_nlinear_workspace *w)
This function is intended mainly for internal use.
bool operator!=(workspace const &v) const
Two workspace are different if their elements are not identical.
workspace(gsl_multilarge_nlinear_workspace *v)
Could construct from a gsl_multilarge_nlinear_workspace*.
size_t use_count() const
Find how many workspace objects share this pointer.
int iterate()
C++ version of gsl_multilarge_nlinear_iterate().
size_t * count
The shared reference count.
workspace & operator=(workspace &&v)
Move operator.
workspace & operator=(workspace const &v)
The assignment operator.
This class handles vector objects as shared handles.
Definition: vector.hpp:74
gsl_vector * get()
Get the gsl_vector.
Definition: vector.hpp:1320
gsl_multilarge_nlinear_parameters parameters
Typedef for shorthand.
size_t niter(workspace const &w)
C++ version of gsl_multilarge_nlinear_niter().
void(* driver_callback)(const size_t iter, void *params, gsl_multilarge_nlinear_workspace const *w)
Convenient typedef.
double avratio(workspace const &w)
C++ version of gsl_multilarge_nlinear_avratio().
gsl_multilarge_nlinear_type type
Typedef for shorthand.
vector residual(workspace const &w)
C++ version of gsl_multilarge_nlinear_residual().
int rcond(double &rcond, workspace const &w)
C++ version of gsl_multilarge_nlinear_rcond().
int covar(matrix &covar, workspace &w)
C++ version of gsl_multilarge_nlinear_covar().
gsl_multilarge_nlinear_fdf fdf
Typedef for shorthand.
int iterate(workspace &w)
C++ version of gsl_multilarge_nlinear_iterate().
int init(vector const &x, gsl::multilarge::nlinear::function_fdf &fdf, workspace &w)
C++ version of gsl_multilarge_nlinear_init().
vector step(workspace const &w)
C++ version of gsl_multilarge_nlinear_step().
gsl_multilarge_nlinear_trs trs
Typedef for shorthand.
int eval_f(gsl::multilarge::nlinear::function_fdf &fdf, vector const &x, vector const &swts, vector &y)
C++ version of gsl_multilarge_nlinear_eval_f().
int eval_fvv(double const h, vector const &x, vector const &v, vector const &f, vector const &swts, gsl::multilarge::nlinear::function_fdf &fdf, vector &yvv, vector &work)
C++ version of gsl_multilarge_nlinear_eval_fvv().
int winit(vector const &x, vector const &wts, gsl::multilarge::nlinear::function_fdf &fdf, workspace &w)
C++ version of gsl_multilarge_nlinear_winit().
gsl_multilarge_nlinear_fdtype fdtype
Typedef for shorthand.
int eval_df(CBLAS_TRANSPOSE_t const TransJ, vector const &x, vector const &f, vector const &u, vector const &swts, double const h, fdtype const fdtype, gsl::multilarge::nlinear::function_fdf &fdf, vector &v, matrix &JTJ, vector &work)
C++ version of gsl_multilarge_nlinear_eval_df().
vector position(workspace &w)
C++ version of gsl_multilarge_nlinear_position().
char const * trs_name(workspace const &w)
C++ version of gsl_multilarge_nlinear_trs_name().
gsl_multilarge_nlinear_scale scale
Typedef for shorthand.
int test(double const xtol, double const gtol, double const ftol, int &info, workspace const &w)
C++ version of gsl_multilarge_nlinear_test().
int fdfvv(double const h, vector const &x, vector const &v, vector const &f, matrix const &J, vector const &swts, gsl::multilarge::nlinear::function_fdf &fdf, vector &fvv, vector &work)
C++ version of gsl_multilarge_nlinear_fdfvv().
int driver(size_t const maxiter, double const xtol, double const gtol, double const ftol, CallbackBase &callback, int &info, workspace &w)
C++ version of gsl_multilarge_nlinear_driver().
gsl_multilarge_nlinear_solver solver
Typedef for shorthand.
int df(double const h, fdtype const fdtype, vector const &x, vector const &wts, gsl::multilarge::nlinear::function_fdf &fdf, vector const &f, matrix &J, vector &work)
C++ version of gsl_multilarge_nlinear_df().
char const * name(workspace const &w)
C++ version of gsl_multilarge_nlinear_name().
parameters default_parameters()
C++ version of gsl_multilarge_nlinear_default_parameters().
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
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