-
Notifications
You must be signed in to change notification settings - Fork 2
/
gslWrappers.cpp
executable file
·713 lines (602 loc) · 16.4 KB
/
gslWrappers.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*--------------------------------
** USU FAST Group
** Eric Addison
** gslWrappers.cpp
** implementation of gslWrappers classes
*--------------------------------*/
#include "gslWrappers.h"
namespace gslWrappers
{
//************************************************************
// gslRootFinder
//************************************************************
/// Root finder class using Brent's method
gslRootFinder::gslRootFinder(double xmin, double xmax, double (*f)(double,void*), double Niter, double reltol, double abstol)
/// constructor
// inputs: xmin, xmax = interval endpoints
// *f = pointer to function to find root of
// Niter = max number of iteratons -- default = 1000;
// reltol = relative error tolerance
// abstol = absolute error tolerance
{
x_lo = xmin;
x_hi = xmax;
F.function = f;
F.params = NULL; // params needs to be explicitly set if needed
max_iter = Niter;
epsrel = reltol;
epsabs = abstol;
rootFinderInit();
}
gslRootFinder::~gslRootFinder()
/// destructor
{
gsl_root_fsolver_free (s);
}
void gslRootFinder::rootFinderInit()
/// initialize root finder stuff
{
T = gsl_root_fsolver_brent;
s = gsl_root_fsolver_alloc (T);
}
double gslRootFinder::findRoot()
{
gsl_root_fsolver_set (s, &F, x_lo, x_hi);
int status, iter=0;
double r=0;
do {
iter++;
status = gsl_root_fsolver_iterate (s);
r = gsl_root_fsolver_root (s);
x_lo = gsl_root_fsolver_x_lower (s);
x_hi = gsl_root_fsolver_x_upper (s);
status = gsl_root_test_interval (x_lo, x_hi,epsabs,epsrel);
} while (status == GSL_CONTINUE && iter < max_iter);
root = r;
return r;
}
//************************************************************
// gslIntegrator
//************************************************************
gslIntegrator::gslIntegrator()
/// default constructor
{
w = gsl_integration_cquad_workspace_alloc(10000); // set up workspace
initd = false; // integrand F has NOT been set
got_pars = false;
nevals = result = error = 0;
epsabs = 0;
epsrel = 1e-7; // initialized error tolerances
}
gslIntegrator::gslIntegrator(gsl_function G)
/// constructor with gsl_function argument
{
w = gsl_integration_cquad_workspace_alloc(10000); // set up workspace
F = G;
initd = true; // integrand F has been set
got_pars = true;
nevals = result = error = 0;
epsabs = 0;
epsrel = 1e-7; // initialized error tolerances
}
gslIntegrator::gslIntegrator(double (*f)(double x, void * params))
/// constructor with function pointer input
{
w = gsl_integration_cquad_workspace_alloc(10000); // set up workspace
F.function = f;
initd = true; // integrand F has been set
got_pars = false;
nevals = result = error = 0;
epsabs = 0;
epsrel = 1e-7; // initialized error tolerances
}
gslIntegrator::gslIntegrator(double (*f)(double x, void * params), void * params)
/// constructor with function pointer input and params
{
w = gsl_integration_cquad_workspace_alloc(10000); // set up workspace
F.function = f;
F.params = params;
initd = true; // integrand F has been set
got_pars = true;
result = nevals = error = 0;
epsabs = 0;
epsrel = 1e-7; // initialized error tolerances
}
gslIntegrator::~gslIntegrator()
/// destructor
{
gsl_integration_cquad_workspace_free (w);
}
void gslIntegrator::set_integrand(gsl_function G)
/// change integrand function
{ F = G; }
void gslIntegrator::set_integrand(double (*f)(double x, void * params))
/// another change integrand function
{ F.function = f; initd = true; got_pars = false; }
void gslIntegrator::set_params(void * p)
/// set params pointer
{
F.params = p;
got_pars = true;
}
bool gslIntegrator::init_checker(const char * fun)
/// check if initialization is complete
{
using std::cerr;
if( !initd )
{
cerr << "\nError: gslWrappers::gslIntegrator::" << fun << " -- integrand not initialized";
return false;
}
else if( !got_pars )
{
cerr << "\nError: gslWrappers::gslIntegrator::" << fun << " -- params pointer not initialized";
return false;
}
return true;
}
double gslIntegrator::get_result()
/// return result
{
if( !init_checker("get_result()") )
return 0;
return result;
}
double gslIntegrator::get_error()
/// return error
{
if( !init_checker("get_error()") )
return 0;
return error;
}
double gslIntegrator::get_nevals()
/// return error
{
if( !init_checker("get_nevals()") )
return 0;
return nevals;
}
void gslIntegrator::set_epsabs(double e)
/// set absolute error tolerance
{ epsabs = e; }
void gslIntegrator::set_epsrel(double e)
/// set relative error tolerance
{ epsrel = e; }
double gslIntegrator::get_epsabs()
/// set absolute error tolerance
{ return epsabs; }
double gslIntegrator::get_epsrel()
/// set relative error tolerance
{ return epsrel; }
double gslIntegrator::integrate(double xmin, double xmax)
/// do the integration
{
if ( !init_checker("integrate()") )
return 0;
gsl_integration_cquad(&F,xmin,xmax,epsabs,epsrel,w,&result,&error,&nevals);
return result;
}
double gslIntegrator::integrate(double xmin, double xmax, void * p)
/// do the integration and provide params
{
F.params = p;
got_pars = true;
if ( !init_checker("integrate()") )
return 0;
gsl_integration_cquad(&F,xmin,xmax,epsabs,epsrel,w,&result,&error,&nevals);
return result;
}
double gslIntegrator::operator()(double xmin, double xmax, void * p)
{ return integrate(xmin,xmax,p); }
double gslIntegrator::operator()(double xmin, double xmax)
{ return integrate(xmin,xmax); }
//************************************************************
// gslMatrix
//************************************************************
gslMatrix::gslMatrix()
/// default constructor
{
m = gsl_matrix_alloc (1, 1); // allocate space for a 1x1
gsl_matrix_set (m, 1, 1, 0.0); // set value to 0
rows = cols = 1;
loc[0] = loc[1] = 0;
temp = 0.0;
}
gslMatrix::gslMatrix(int M, int N)
/// construct empty MxN matrix
{
m = gsl_matrix_alloc(M,N); // allocate space for MxN
for(int i=0; i<M; i++) { // set all values to zero
for(int j=0; j<N; j++)
gsl_matrix_set(m,i,j,0.0);
}
rows = M;
cols = N;
loc[0] = loc[1] = 0;
temp = 0.0;
}
gslMatrix::gslMatrix(const gslMatrix & n)
/// copy constructor
{
rows = n.rows;
cols = n.cols; // copy rows and cols
m = gsl_matrix_alloc(rows,cols);// allocate new space
for(int i=0; i<rows; i++) { // deep copy
for(int j=0; j<cols; j++)
gsl_matrix_set(m,i,j,gsl_matrix_get(n.m,i,j));
}
loc[0] = loc[1] = 0;
temp = gsl_matrix_get(n.m,0,0);
}
gslMatrix::gslMatrix(const gsl_matrix * n, int i,int j)
{
rows = i;
cols = j; // copy rows and cols
m = gsl_matrix_alloc(rows,cols);// allocate new space
for(int i=0; i<rows; i++) { // deep copy
for(int j=0; j<cols; j++)
gsl_matrix_set(m,i,j,gsl_matrix_get(n,i,j));
}
loc[0] = loc[1] = 0;
temp = gsl_matrix_get(m,0,0);
}
gslMatrix::gslMatrix(double *X, int i, int j)
/// constructor for double ** (inside of MatInit)
{
rows = i;
cols = j; // copy rows and cols
fflush(stdout);
m = gsl_matrix_alloc(rows,cols);// allocate new space
for(int i=0; i<rows; i++) { // deep copy
for(int j=0; j<cols; j++)
gsl_matrix_set(m,i,j,X[i*cols+j]);
}
loc[0] = loc[1] = 0;
temp = gsl_matrix_get(m,0,0);
}
gslMatrix::~gslMatrix()
/// destructor
{ gsl_matrix_free (m); }
void gslMatrix::reassign()
/// reassign value to loc position
{
gsl_matrix_set(m,loc[0],loc[1],temp);
}
double & gslMatrix::operator()(const int i, const int j)
/// overload the () operator
{
if ( i<0 || i>=rows || j<0 || j>=cols )
{
std::cerr << "\nError: gslWrappers::gslMatrix::operator() -- Matrix indices out of bounds";
temp = GSL_WRAP_ERR;
return temp;
}
reassign();
temp = gsl_matrix_get(m,i,j);
loc[0] = i; loc[1] = j;
return temp;
}
void gslMatrix::show(std::ostream & os)
/// print out matrix with default ostream as cout
{
reassign();
using std::endl;
using std::setw;
os << endl;
for (int i=0; i<rows; i++)
{
os << "[";
for (int j=0; j<cols; j++)
os << setw(4) << gsl_matrix_get(m,i,j) << ", ";
os << "\b\b ]" << endl;
}
}
gslMatrix & gslMatrix::operator=(const gslMatrix & n)
/// assignment operator
{
reassign();
gsl_matrix_free (m); // free old memory
rows = n.rows;
cols = n.cols; // copy rows and cols
m = gsl_matrix_alloc(rows,cols);// allocate new space
for(int i=0; i<rows; i++) { // deep copy
for(int j=0; j<cols; j++)
gsl_matrix_set(m,i,j,gsl_matrix_get(n.m,i,j));
}
loc[0] = loc[1] = 0;
temp = gsl_matrix_get(n.m,0,0);
return *this;
}
gslMatrix gslMatrix::operator+(const gslMatrix & n)
/// addition operator
{
reassign();
gslMatrix sum(*this); // new sum matrix
gsl_matrix_add(sum.m,n.m); // gsl addition routine
return sum;
}
gslMatrix gslMatrix::operator-()
/// negation operator
{
reassign();
gslMatrix neg(*this); // new sum matrix
for(int i=0; i<rows; i++) { // deep copy
for(int j=0; j<cols; j++)
gsl_matrix_set(neg.m,i,j,-gsl_matrix_get(neg.m,i,j));
}
return neg;
}
gslMatrix gslMatrix::operator-(const gslMatrix & n)
/// subtraction operator
{
reassign();
gslMatrix diff(*this); // new sum matrix
gsl_matrix_sub(diff.m,n.m); // gsl addition routine
return diff;
}
gslMatrix gslMatrix::operator*(const double k)
/// scalar multiplication
{
reassign();
gslMatrix prod(*this);
gsl_matrix_scale(prod.m,k);
return prod;
}
gslMatrix gslMatrix::operator+(const double k)
/// add a constant value to the whole matrix
{
reassign();
gslMatrix sum(*this);
gsl_matrix_add_constant(sum.m,k);
return sum;
}
gslMatrix gslMatrix::operator&(const gslMatrix & n)
/// elementwise multiplication -- like matlab .*
{
reassign();
gslMatrix prod(*this);
gsl_matrix_mul_elements(prod.m,n.m);
return prod;
}
gslMatrix gslMatrix::operator|(const gslMatrix & n)
/// elementwise division -- like matlab ./
{
reassign();
gslMatrix quot(*this);
gsl_matrix_div_elements(quot.m,n.m);
return quot;
}
gslMatrix gslMatrix::operator*(const gslMatrix & n)
/// matrix-matrix multiplication
{
reassign();
if (cols != n.rows)
{
std::cerr << "\nError: gslWrappers::gslMatrix::operator* -- inner matrix dimensions must agree";
gslMatrix err(1,1);
err(1,1) = GSL_WRAP_ERR;
return err;
}
gslMatrix prod(rows,n.cols);
// call to the gsl interface to BLAS functions:
// http://www.gnu.org/software/gsl/manual/html_node/Level-3-GSL-BLAS-Interface.html
gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,m,n.m,0.0,prod.m);
return prod;
}
gslMatrix gslMatrix::operator~()
/// inversion operator
{
reassign();
if(rows != cols)
{
std::cerr << "\nError: gslWrappers::gslMatrix::operator~ -- Matrix must be square";
gslMatrix inv(rows,cols);
return inv;
}
// the following code is weird, but it works.
// my original attempt didn't work, so this is what I will use
// similar to gsl example: http://www.gnu.org/software/gsl/manual/html_node/Linear-Algebra-Examples.html
gsl_matrix *M = gsl_matrix_alloc( rows, cols);
gsl_matrix_memcpy( M,m );
gsl_matrix *inv1 = gsl_matrix_alloc(rows,cols);
int s;
gsl_permutation * p = gsl_permutation_alloc (rows);
gsl_linalg_LU_decomp (M, p, &s);
gsl_linalg_LU_invert (M, p, inv1);
gslMatrix inv(inv1,rows,cols); // make new gslMatrix using inv1
gsl_matrix_free(M);
gsl_matrix_free(inv1);
gsl_permutation_free(p);
return inv; // return inv
}
gslMatrix gslMatrix::inv()
/// another inversion call
{ reassign(); return ~(*this); }
gslMatrix gslMatrix::operator*=( const gslMatrix & n)
{
reassign();
(*this) = (*this)*n;
return *this;
}
gslMatrix gslMatrix::operator+=( const gslMatrix & n)
{
reassign();
(*this) = (*this)+n;
return *this;
}
gslMatrix gslMatrix::operator^(int k)
/// raise a matrix to a power
{
reassign();
if(rows != cols)
{
std::cerr << "\nError: gslWrappers::gslMatrix::operator^ -- Matrix must be square";
gslMatrix inv(1,1);
return inv;
}
if (k == 0)
return eye();
else if(k > 0)
{
gslMatrix prod(rows,cols);
prod = (*this);
for(int i=0;i<k-1;i++)
prod *= (*this);
return prod;
}
else
{
gslMatrix prod(rows,cols);
gslMatrix inv(rows,cols);
inv = ~(*this);
prod = inv;
for(int i=0;i<abs(k)-1;i++)
prod *= inv;
return prod;
}
}
gslMatrix gslMatrix::eye(int n)
/// return an identity matrix of size n
{
reassign();
if (n==0)
n=rows;
else
n = ( n>0 ? n : 1);
gslMatrix I(n,n);
for(int i=0;i<n;i++)
I(i,i) = 1;
return I;
}
gslMatrix operator*(const double k, gslMatrix & M )
{ return M*k; }
gslMatrix operator+(const double k, gslMatrix & M )
{ return M+k; }
//************************************************************
// gslCSpline
//************************************************************
gslCSpline::gslCSpline(double * x, double * y, int N)
/// constructor
{
// define spline working variables
acc = gsl_interp_accel_alloc ();
spline = gsl_spline_alloc (gsl_interp_cspline,N); // allocate spline
// initialize spline
gsl_spline_init(spline,x,y,N);
initd = 1;
}
gslCSpline::~gslCSpline()
/// destructor
{
gsl_spline_free (spline);
gsl_interp_accel_free(acc);
}
double gslCSpline::operator()(double xi)
/// overload () for interpolation calls
{
if (initd)
return gsl_spline_eval(spline,xi,acc);
else
{
std::cerr << "\nError: gslWrappers::gslCSpline::operator() -- spline not initialized\n";
return GSL_WRAP_ERR;
}
}
void gslCSpline::init_spline(double * x, double * y, int N)
{
// define spline working variables
acc = gsl_interp_accel_alloc ();
spline = gsl_spline_alloc (gsl_interp_cspline,N); // allocate spline
// initialize spline
gsl_spline_init(spline,x,y,N);
initd = 1;
}
//************************************************************
// Test Functions
//************************************************************
void MatrixTest()
{
using std::cout;
using std::endl;
cout << "\nCreating empty matrix...";
gslMatrix M(3,3);
cout << "success\n";
cout << "Testing (i,j) opertor:\n";
cout << "\tM(2,2) = " << M(2,2) << endl;
cout << "\tM(1,1) = " << M(1,1) << endl;
cout << "\nTesting (i,j) on assignment on M(1,1)...";
fflush(stdout);
M(1,1) = 12.0;
cout << "Success\n\tM(1,1) = " << M(1,1) << endl;
cout << "\nTesting matrix show() routine:\n";
M.show();
cout << "\nCreating two new matrices for multiplication test...";
double f[2][3] = {{1.0, 2.0, 3.0},{3.0,4.0,3.0}};
double g[3][4] = {{1,2,3,4}, {2,3,4,5}, {4,5,6,7}};
gslMatrix M1(f[0],2,3), M2(g[0],3,4);
M1.show();
M2.show();
cout << "Product M1*M2 = ";
(M1*M2).show();
cout << "\nTesting identity .eye() function...";
M1.eye().show();
M2.eye(4).show();
cout << "\nTesting inversion...";
double h[3][3] = { {1,2,3},{4,5,6},{1,3,7} };
//double h[3][3] = { {1,0,0},{0,1,0},{0,0,1} };
gslMatrix M3(h[0],3,3);
M3.show();
cout << "inv(M3):\n";
M3.inv().show();
cout << "\nM3 * inv(M3):";
( M3 * ~M3).show();
cout << "\nTesting matrix power M^3:\n";
double ff[3][3] = {{ 2,0,0},{0,2,0},{0,0,2}};
gslMatrix M4(ff[0],3,3);
(M4^-3).show();
cout <<"\nTesting += and M+k:\n";
M4+=(2*M4);
M4.show();
}
double func(double x, void * params)
{
double a = *( (double *) params);
return a*x;
}
double gunc(double x, void * params)
{
(void)params; // params not used
return x*cos(x)+exp(-x*x);
}
void IntTest()
{
using std::cout;
using std::endl;
cout << "Creating a gslIntegrator object with function func...";
gslIntegrator inter(&func);
cout << "Success\n";
cout << "\nAttemtping integration of f(x)=x from x=[0..10]...";
double a = 1;
cout << "\n\tresult = " << inter.integrate(0,10,(void*)&a) << "\n";
cout << "\nAttemtping integration of g(x)=x*cos(x)+exp(-x^2) from x=[0..10]...";
inter.set_integrand(&gunc);
inter.integrate(0,1);
inter.set_params(NULL);
cout << "\n\tresult = " << inter(0,10) << "\n";
}
void rootTest()
{
using namespace std;
cout << "Creating a gslRootFinder object with function func2 = x^2-3...";
gslRootFinder finder(0,5,&func2);
cout << "Success!\n\n";
cout << "Finding root...";
finder.findRoot();
cout << "Success! root = " << finder.get_root() << endl;
}
double func2(double x, void * params)
{
(void)params;
return x*x - 3;
}
} // END NAMESPACE GSLWRAPPERS