Created
October 22, 2013 16:20
-
-
Save floswald/7103639 to your computer and use it in GitHub Desktop.
cpp file to http://stackoverflow.com/questions/19450198/calling-gsl-function-inside-a-class-in-a-shared-library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "MyClass.h" | |
| // Define Class members | |
| // ===================== | |
| double MyClass::GetSolution( ) { | |
| // point the pt_MyClass element to this class | |
| p->pt_MyClass = this; | |
| // instantiate a gsl_function ... | |
| gsl_function F; | |
| F.params = p; | |
| // ... and point the address of our | |
| // wrapper to it | |
| F.function = &gslClassWrapper; | |
| std::cout << "at x=2, objective function value is " << gslClassWrapper(2.0,p) << std::endl; | |
| std::cout << "at x=1, objective function value is " << gslClassWrapper(1.0,p) << std::endl; | |
| // set up GSL root solver | |
| const gsl_root_fsolver_type *T; | |
| gsl_root_fsolver *sroot; | |
| T = gsl_root_fsolver_brent; | |
| sroot = gsl_root_fsolver_alloc (T); | |
| double low = -5; | |
| double hi = 5; | |
| double root; | |
| root = find_root(sroot,&F, low , hi ); | |
| return root; | |
| } | |
| double MyClass::getC( void ) { return( p->c); } ; | |
| // Define Objective Function | |
| // ========================= | |
| double MyClass::obj(double x, void * par) { | |
| gsl_f_pars *p = (gsl_f_pars *)par; | |
| //return x; | |
| //return (p->a * x + p->b) * x + p->c; | |
| return pow(x - p->a , 3); | |
| } | |
| // main routine | |
| // ============ | |
| int main(){ | |
| MyClass C; | |
| // allocate the param structure | |
| gsl_f_pars myp; | |
| myp.a=1.0; | |
| myp.b= 0; | |
| myp.c=-5; | |
| // point to myp from inside the class | |
| C.setPars( &myp ); | |
| std::cout << "checking value of C: " << C.getC() << std::endl; | |
| std::cout << "computing result : " << C.GetSolution() << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment