-
Notifications
You must be signed in to change notification settings - Fork 2
/
gslWrappers.h
executable file
·184 lines (167 loc) · 6.11 KB
/
gslWrappers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//****************************
// Eric Addison
// USU
// gslWrappers.h
// header file for wrapper classes to make
// gsl easier to use in C++
//****************************
#include <iostream>
#include <iomanip>
#include <stdio.h>
// gsl header files
#ifndef _GSLINC_
#define _GSLINC_
#include <gsl/gsl_rng.h> // random number generation header
#include <gsl/gsl_randist.h> // probability distribution functions
#include <gsl/gsl_integration.h> // for numerical integration
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#endif
#ifndef _GSLWRAP_
#define _GSLWRAP_
// Class Declarations
namespace gslWrappers
{
const double GSL_WRAP_ERR = -98765.4321;
// gslRootFinder: wrapper class for gsl Brent method 1D root finder
class gslRootFinder
{
private:
const gsl_root_fsolver_type *T;
gsl_root_fsolver *s;
double x_lo, x_hi; // interval end points
double epsrel, epsabs; // error tolerances
double root; // the root we will find
int max_iter; // max iterations
bool initd;
public:
gsl_function F; // gsl function for root finding
gslRootFinder(double xmin, double xmax, double (*f)(double,void*) = NULL, double Niter = 1000, double reltol = 1e-6, double abstol = 0.0);
~gslRootFinder();
double findRoot(); // do the root finding
void rootFinderInit(); // initialize root finder stuff
// set methods
void set_max_iter(double Niter) { max_iter = Niter; }
void set_x_hi(double x) { x_hi = x; }
void set_x_lo(double x) { x_lo = x; }
void set_F(double (*f)(double,void*)) { F.function = f; }
void set_params(void * p) {F.params = p;}
// get methods
double get_max_iter() { return max_iter; }
double get_x_hi() {return x_hi;}
double get_x_lo() {return x_lo;}
double get_root() {return root;}
};
// gslIntegrator: wrapper class for cquad gsl integration
class gslIntegrator
{
private:
double result; // holds integration result
double error; // integration error
gsl_function F; // integrand function
size_t nevals; // number of evaluations used
gsl_integration_cquad_workspace *w; // integration workspace
bool initd; // flag to see if F has been initialized
bool got_pars; // flag to see if params has been provided
double epsrel; // relative error tolerance
double epsabs; // absolute error tolerance
bool init_checker(const char *); // initializaton checker
public:
// constructors and destructor
gslIntegrator();
gslIntegrator(gsl_function G);
~gslIntegrator();
gslIntegrator(double (*f)(double x, void * params), void * params);
gslIntegrator(double (*f)(double x, void * params));
// set and get functions
void set_integrand(gsl_function G); // change integrand function
void set_integrand(double (*f)(double x, void * params)); // change integrand function
void set_params(void * p); // set params
void set_epsabs(double e); // set the absolute error tolerance
void set_epsrel(double e); // set the relative error tolerance
double get_result(); // get the last integration result
double get_error(); // get the last integration error
double get_nevals(); // get the last value of nevals
double get_epsabs(); // get the absolute error
double get_epsrel(); // get the relative error
// integration call
double integrate(double, double); // do the integration
double integrate(double, double, void *); // integrate and provide params
// operator overload
double operator()(double,double,void*); // overload () to use for integration
double operator()(double,double);
};
// gslMatrix: wrapper class for the gsl_matrix
class gslMatrix
{
private:
gsl_matrix * m; // the actual matrix
int rows; // number of rows
int cols; // number of columns
// variables to work with M(i,j) = blah
double temp; // reassign value
int loc[2]; // location of reassigned value
void reassign(); // reassign a variable if needed
public:
// constructors and destructors
gslMatrix(); // default constructor
gslMatrix(int, int); // consructor for empty MxN matrix
gslMatrix(const gslMatrix &);// copy constructor
gslMatrix(const gsl_matrix *, int,int); // another constructor
gslMatrix(double*,int,int); // explicit value struct constructor
~gslMatrix();
// set and get functions
int get_rows() { return rows; } // get the number of rows
int get_cols() { return cols; } // get the number of cols
void show(std::ostream & os = std::cout);// print out the matrix
// other functions
gslMatrix eye(int n=0); // get an identity matrix
gslMatrix inv(); // invert matrix
// operator overloads
gslMatrix & operator=(const gslMatrix &); // assignment operator
gslMatrix operator+(const gslMatrix &); // addition
gslMatrix operator-(const gslMatrix &); // subtraction
gslMatrix operator*(const double); // scalar mult.
gslMatrix operator-(); // negation
gslMatrix operator+(const double); // adda constant
gslMatrix operator&(const gslMatrix & ); // elementwise mult
gslMatrix operator|(const gslMatrix & ); // elementwise div
gslMatrix operator*(const gslMatrix & ); // mat-mat mult
double & operator()(const int, const int); // call values
gslMatrix operator~(); // invert matrix
gslMatrix operator*=(const gslMatrix &); // mult. assignment
gslMatrix operator+=(const gslMatrix &); // mult. addition
gslMatrix operator^(int k); // matrix power
// friends
friend gslMatrix operator*(const double, gslMatrix &);
friend gslMatrix operator+(const double, gslMatrix &);
};
// gslCSpline: wrapper class for gsl cubic spline interpolation
class gslCSpline
{
private:
// GSL Interpolation data
gsl_interp_accel *acc;
gsl_spline *spline;
bool initd; // initialized flag
public:
gslCSpline() {initd = 0;}
gslCSpline(double *, double *, int);
~gslCSpline();
double operator()(double); // overload () to use for interpolation
void init_spline(double *, double *, int);
};
// Test functions
void MatrixTest();
double func(double, void*);
void IntTest();
double gunc(double, void*);
void rootTest();
double func2(double, void*);
} // END NAMESPACE GSLWRAPPERS
#endif