CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
GSLwrapper.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful,*
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  *******************************************************************/
20 
35 #include "GSLwrapper.h"
36 
37 using namespace std;
38 
39 using namespace cbl;
40 
41 
42 // ============================================================================
43 
44 
45 void cbl::wrapper::gsl::check_GSL_fail (const int status, const bool exit, const std::string CBLfunction, const std::string GSLroutine)
46 {
47  if (exit) {
48  if (status!=0) {
49  ErrorCBL((GSLroutine != par::defaultString) ? "the error has been raised by the gsl routine "+GSLroutine+" used in "+CBLfunction+": "+string(gsl_strerror(status)): "Error in "+CBLfunction+": "+string(gsl_strerror(status)), "check_GSL_fail", "GSLwrapper.cpp");
50  }
51  }
52  else
53  WarningMsgCBL("The gsl routine "+GSLroutine+" used in "+CBLfunction+" exited with status "+string(gsl_strerror(status)), "check_GSL_fail", "GSLwrapper.cpp");
54 }
55 
56 
57 // ============================================================================
58 
59 
60 double cbl::wrapper::gsl::generic_function (const double xx, void *params)
61 {
62  gsl::STR_generic_func_GSL *pp = (gsl::STR_generic_func_GSL *) params;
63  return pp->f(xx);
64 }
65 
66 
67 // ============================================================================
68 
69 
70 double cbl::wrapper::gsl::generic_roots (double xx, void *params)
71 {
72  gsl::STR_generic_func_GSL *pp = (gsl::STR_generic_func_GSL *) params;
73  return pp->f(xx)-pp->xx0;
74 }
75 
76 
77 // ============================================================================
78 
79 
80 double cbl::wrapper::gsl::generic_minimizer (const gsl_vector * xx, void * params)
81 {
82  vector<double> _xx;
83  for (size_t i=0; i<xx->size; i++)
84  _xx.push_back(gsl_vector_get(xx, i));
85 
86  gsl::STR_generic_func_GSL *pp = (gsl::STR_generic_func_GSL *) params;
87  pp->parameters_return = _xx;
88 
89  return pp->fmin(_xx);
90 }
91 
92 
93 // ============================================================================
94 
95 
96 double cbl::wrapper::gsl::generic_minimizer_return (const gsl_vector * xx, void * params)
97 {
98  vector<double> _xx;
99  for (size_t i=0; i<xx->size; i++)
100  _xx.push_back(gsl_vector_get(xx, i));
101 
102  gsl::STR_generic_func_GSL *pp = (gsl::STR_generic_func_GSL *) params;
103 
104  double val = pp->fmin_return(_xx);
105  pp->parameters_return = _xx;
106 
107  return val;
108 }
109 
110 
111 // ============================================================================
112 
113 
114 double cbl::wrapper::gsl::GSL_derivative (gsl_function Func, const double xx, const double hh, const double prec)
115 {
116  gsl_set_error_handler_off();
117 
118  double Deriv, error;
119 
120  int status = gsl_deriv_central(&Func, xx, hh, &Deriv, &error);
121  check_GSL_fail(status, true, "GSL_derivative", "gsl_deriv_central");
122 
123  return (Deriv!=0 && error/Deriv<prec) ? Deriv : ErrorCBL("error/Deriv = "+conv(error/Deriv, par::fDP6)+" > prec = "+conv(prec, par::fDP3), "GSL_derivative", "GSLwrapper.cpp");
124 }
125 
126 
127 // ============================================================================
128 
129 
130 double cbl::wrapper::gsl::GSL_integrate_romberg (gsl_function Func, const double a, const double b, const int npoints, const double eps_rel, const double eps_abs)
131 {
132 #if GSL_VERSION_OK==1
133  gsl_set_error_handler_off();
134 
135  gsl_integration_romberg_workspace * ws = gsl_integration_romberg_alloc(npoints);
136 
137  double Int;
138  size_t nevals;
139 
140  int status = gsl_integration_romberg(&Func, a, b, eps_abs, eps_rel, &Int, &nevals, ws);
141 
142  gsl_integration_romberg_free(ws);
143 
144  check_GSL_fail(status, true, "GSL_integrate_romberg", "gsl_integration_romberg");
145 
146  return Int;
147 #else
148 
149  (void)Func; (void)a; (void)b; (void)npoints; (void)eps_rel; (void)eps_abs;
150  ErrorCBL("Romberg integration is available with GSL version >=2.5", "GSL_integrate_romberg", "GSLwrapper.cpp");
151 
152  return 0;
153 #endif
154 }
155 
156 // ============================================================================
157 
158 
159 double cbl::wrapper::gsl::GSL_integrate_cquad (gsl_function Func, const double a, const double b, const double rel_err, const double abs_err, const int nevals)
160 {
161  gsl_set_error_handler_off();
162  double Int, error;
163  size_t nn = nevals;
164 
165  gsl_integration_cquad_workspace *ww = gsl_integration_cquad_workspace_alloc(nevals);
166 
167  int status = gsl_integration_cquad(&Func, a, b, abs_err, rel_err, ww, &Int, &error, &nn);
168 
169  gsl_integration_cquad_workspace_free(ww);
170 
171  check_GSL_fail(status, true, "GSL_integrate_cquad", "gsl_integration_cquad");
172 
173  return Int;
174 }
175 
176 
177 // ============================================================================
178 
179 
180 double cbl::wrapper::gsl::GSL_integrate_qag (gsl_function Func, const double a, const double b, const double rel_err, const double abs_err, const int limit_size, const int rule)
181 {
182  gsl_set_error_handler_off();
183 
184  double Int, error;
185  gsl_integration_workspace *ww = gsl_integration_workspace_alloc(limit_size);
186 
187  int status = gsl_integration_qag(&Func, a, b, abs_err, rel_err, limit_size, rule, ww, &Int, &error);
188 
189  check_GSL_fail(status, true, "GSL_integrate_qag", "gsl_integrate_qag");
190 
191  gsl_integration_workspace_free(ww);
192 
193  return Int;
194 }
195 
196 
197 // ============================================================================
198 
199 
200 double cbl::wrapper::gsl::GSL_integrate_qags (gsl_function Func, const double a, const double b, const double rel_err, const double abs_err, const int limit_size)
201 {
202  gsl_set_error_handler_off();
203 
204  double Int, error;
205  gsl_integration_workspace *ww = gsl_integration_workspace_alloc(limit_size);
206 
207  int status = gsl_integration_qags(&Func, a, b, abs_err, rel_err, limit_size, ww, &Int, &error);
208 
209  check_GSL_fail(status, true, "GSL_integrate_qags", "gsl_integrate_qags");
210 
211  gsl_integration_workspace_free(ww);
212 
213  return Int;
214 }
215 
216 
217 // ============================================================================
218 
219 
220 double cbl::wrapper::gsl::GSL_integrate_qagiu (gsl_function Func, const double a, const double rel_err, const double abs_err, const int limit_size)
221 {
222  gsl_set_error_handler_off();
223 
224  double Int, error;
225  gsl_integration_workspace *ww = gsl_integration_workspace_alloc(limit_size);
226 
227  int status = gsl_integration_qagiu(&Func, a, abs_err, rel_err, limit_size, ww, &Int, &error);
228 
229  check_GSL_fail(status, true, "GSL_integrate_qagiu", "gsl_integrate_qagiu");
230 
231  gsl_integration_workspace_free(ww);
232 
233  return Int;
234 }
235 
236 
237 // ============================================================================
238 
239 
240 double cbl::wrapper::gsl::GSL_integrate_qaws (gsl_function Func, const double a, const double b, const double alpha, const double beta, const int mu, const int nu, const double rel_err, const double abs_err, const int limit_size)
241 {
242  gsl_set_error_handler_off();
243 
244  gsl_integration_qaws_table *T = gsl_integration_qaws_table_alloc(alpha, beta, mu, nu);
245 
246  double Int, error;
247  gsl_integration_workspace *ww = gsl_integration_workspace_alloc(limit_size);
248 
249  int status = gsl_integration_qaws (&Func, a, b, T, abs_err, rel_err, limit_size, ww, &Int, &error);
250 
251  check_GSL_fail(status, true, "GSL_integrate_qaws", "gsl_integrate_qaws");
252 
253  gsl_integration_workspace_free(ww);
254  gsl_integration_qaws_table_free(T);
255 
256  return Int;
257 }
258 
259 
260 // ============================================================================
261 
262 
263 double cbl::wrapper::gsl::GSL_derivative (FunctionDoubleDouble func, const double xx, const double hh, const double prec)
264 {
265  STR_generic_func_GSL params;
266  params.f = func;
267 
268  gsl_function Func;
269  Func.function = generic_function;
270  Func.params = &params;
271 
272  return GSL_derivative(Func, xx, hh, prec);
273 }
274 
275 
276 // ============================================================================
277 
278 
279 double cbl::wrapper::gsl::GSL_integrate_romberg (FunctionDoubleDouble func, const double a, const double b, const int npoints, const double rel_err, const double abs_err)
280 {
281  STR_generic_func_GSL params;
282  params.f = func;
283 
284  gsl_function Func;
285  Func.function = generic_function;
286  Func.params = &params;
287 
288  return GSL_integrate_romberg(Func, a, b, npoints, rel_err, abs_err);
289 }
290 
291 
292 // ============================================================================
293 
294 
295 double cbl::wrapper::gsl::GSL_integrate_cquad (FunctionDoubleDouble func, const double a, const double b, const double rel_err, const double abs_err, const int nevals)
296 {
297  STR_generic_func_GSL params;
298  params.f = func;
299 
300  gsl_function Func;
301  Func.function = generic_function;
302  Func.params = &params;
303 
304  return GSL_integrate_cquad(Func, a, b, rel_err, abs_err, nevals);
305 }
306 
307 
308 // ============================================================================
309 
310 
311 double cbl::wrapper::gsl::GSL_integrate_qag (FunctionDoubleDouble func, const double a, const double b, const double rel_err, const double abs_err, const int limit_size, const int rule)
312 {
313  STR_generic_func_GSL params;
314  params.f = func;
315 
316  gsl_function Func;
317  Func.function = generic_function;
318  Func.params = &params;
319 
320  return GSL_integrate_qag(Func, a, b, rel_err, abs_err, limit_size, rule);
321 }
322 
323 
324 // ============================================================================
325 
326 
327 double cbl::wrapper::gsl::GSL_integrate_qags (FunctionDoubleDouble func, const double a, const double b, const double rel_err, const double abs_err, const int limit_size)
328 {
329  STR_generic_func_GSL params;
330  params.f = func;
331 
332  gsl_function Func;
333  Func.function = generic_function;
334  Func.params = &params;
335 
336  return GSL_integrate_qags(Func, a, b, rel_err, abs_err, limit_size);
337 }
338 
339 
340 // ============================================================================
341 
342 
343 double cbl::wrapper::gsl::GSL_integrate_qagiu (FunctionDoubleDouble func, const double a, const double rel_err, const double abs_err, const int limit_size)
344 {
345  STR_generic_func_GSL params;
346  params.f = func;
347 
348  gsl_function Func;
349  Func.function = generic_function;
350  Func.params = &params;
351 
352  return GSL_integrate_qagiu(Func, a, rel_err, abs_err, limit_size);
353 }
354 
355 
356 // ============================================================================
357 
358 
359 double cbl::wrapper::gsl::GSL_integrate_qaws (FunctionDoubleDouble func, const double a, const double b, const double alpha, const double beta, const int mu, const int nu, const double rel_err, const double abs_err, const int limit_size)
360 {
361  STR_generic_func_GSL params;
362  params.f = func;
363 
364  gsl_function Func;
365  Func.function = generic_function;
366  Func.params = &params;
367 
368  return GSL_integrate_qaws(Func, a, b, alpha, beta, mu, nu, abs_err, rel_err, limit_size);
369 }
370 
371 
372 // ============================================================================
373 
374 
375 double cbl::wrapper::gsl::GSL_integrate_cquad (FunctionDoubleDoublePtrVectorRef func, const std::shared_ptr<void> pp, const std::vector<double> par, const double a, const double b, const double rel_err, const double abs_err, const int nevals)
376 {
377  function<double(double)> func_bind = bind(func, placeholders::_1, pp, par);
378 
379  return GSL_integrate_cquad(func_bind, a, b, abs_err, rel_err, nevals);
380 }
381 
382 
383 // ============================================================================
384 
385 
386 double cbl::wrapper::gsl::GSL_integrate_qag (FunctionDoubleDoublePtrVectorRef func, const std::shared_ptr<void> pp, const std::vector<double> par, const double a, const double b, const double rel_err, const double abs_err, const int limit_size, const int rule)
387 {
388  function<double(double)> func_bind = bind(func, placeholders::_1, pp, par);
389 
390  return GSL_integrate_qag(func_bind, a, b, abs_err, rel_err, limit_size, rule);
391 }
392 
393 
394 // ============================================================================
395 
396 
397 double cbl::wrapper::gsl::GSL_integrate_qags (FunctionDoubleDoublePtrVectorRef func, const std::shared_ptr<void> pp, const std::vector<double> par, const double a, const double b, const double rel_err, const double abs_err, const int limit_size)
398 {
399  function<double(double)> func_bind = bind(func, placeholders::_1, pp, par);
400 
401  return GSL_integrate_qags(func_bind, a, b, abs_err, rel_err, limit_size);
402 }
403 
404 
405 // ============================================================================
406 
407 
408 double cbl::wrapper::gsl::GSL_integrate_qagiu (FunctionDoubleDoublePtrVectorRef func, const std::shared_ptr<void> pp, const std::vector<double> par, const double a, const double rel_err, const double abs_err, const int limit_size)
409 {
410  function<double(double)> func_bind = bind(func, placeholders::_1, pp, par);
411 
412  return GSL_integrate_qagiu(func_bind, a, rel_err, abs_err, limit_size);
413 }
414 
415 
416 // ============================================================================
417 
418 
419 double cbl::wrapper::gsl::GSL_integrate_qaws (FunctionDoubleDoublePtrVectorRef func, const std::shared_ptr<void> pp, const std::vector<double> par, const double a, const double b, const double alpha, const double beta, const int mu, const int nu, const double rel_err, const double abs_err, const int limit_size)
420 {
421  function<double(double)> func_bind = bind(func, placeholders::_1, pp, par);
422 
423  return GSL_integrate_qaws(func_bind, a, b, alpha, beta, mu, nu, rel_err, abs_err, limit_size);
424 }
425 
426 
427 // ============================================================================
428 
429 
430 double cbl::wrapper::gsl::GSL_root_brent (gsl_function Func, const double low_guess, const double up_guess, const double rel_err, const double abs_err)
431 {
432  gsl_set_error_handler_off();
433 
434  int status = 0, iter = 0, max_iter = 10000;
435  const gsl_root_fsolver_type *T;
436  double r;
437 
438  gsl_root_fsolver *s;
439  T = gsl_root_fsolver_brent;
440  s = gsl_root_fsolver_alloc(T);
441 
442  double x_lo = low_guess;
443  double x_hi = up_guess;
444 
445  gsl_root_fsolver_set(s, &Func, x_lo, x_hi);
446 
447  do
448  {
449  iter ++;
450  status = gsl_root_fsolver_iterate(s);
451 
452  if ((status!=GSL_SUCCESS) && (status!=GSL_CONTINUE))
453  check_GSL_fail(status, true, "GSL_root_brent", "gsl_root_fsolver_iterate");
454 
455  r = gsl_root_fsolver_root(s);
456  x_lo = gsl_root_fsolver_x_lower(s);
457  x_hi = gsl_root_fsolver_x_upper(s);
458 
459  status = gsl_root_test_interval(x_lo, x_hi, abs_err, rel_err);
460 
461  if ((status!=GSL_SUCCESS) && (status!=GSL_CONTINUE))
462  check_GSL_fail(status, true, "GSL_root_brent", "gsl_root_test_interval");
463  }
464 
465  while (status==GSL_CONTINUE && iter<max_iter);
466 
467  gsl_root_fsolver_free(s);
468 
469  check_GSL_fail(status, true, "GSL_minimize_nD", par::defaultString);
470 
471  return r;
472 }
473 
474 
475 // ============================================================================
476 
477 
478 double cbl::wrapper::gsl::GSL_root_brent (FunctionDoubleDouble func, const double xx0, const double low_guess, const double up_guess, const double rel_err, const double abs_err)
479 {
480  gsl_set_error_handler_off();
481 
482  STR_generic_func_GSL params;
483  params.f = func;
484  params.xx0 = xx0;
485 
486  gsl_function Func;
487  Func.function = &generic_roots;
488  Func.params = &params;
489 
490  return GSL_root_brent(Func, low_guess, up_guess, rel_err, abs_err);
491 }
492 
493 
494 // ============================================================================
495 
496 
497 vector<double> cbl::wrapper::gsl::GSL_minimize_nD (FunctionDoubleVector func, const std::vector<double> start, const std::vector<std::vector<double>> ranges, const unsigned int max_iter, const double tol, const double epsilon)
498 {
499  if (ranges.size() != start.size() && ranges.size() != 0)
500  ErrorCBL("vector of ranges must have the same size of start vector!", "GSL_minimize_nD", "GSLwrapper.cpp");
501  gsl_set_error_handler_off();
502 
503  size_t npar = start.size();
504 
505  STR_generic_func_GSL params;
506  params.fmin_return = (ranges.size() == start.size()) ? [&] (vector<double> par) {return (inRange(par, ranges)) ? func(par) : par::defaultDouble; } : func;
507 
508  const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex2;
509  gsl_multimin_fminimizer *s = NULL;
510  gsl_vector *ss, *x;
511  gsl_multimin_function minex_func;
512 
513  size_t iter = 0;
514  int status = 0;
515  double size;
516 
517  // Starting point
518 
519  x = gsl_vector_alloc(npar);
520  ss = gsl_vector_alloc(npar);
521 
522  for (size_t i=0; i<npar; i++) {
523  gsl_vector_set(x, i, start[i]);
524  double val = ((ranges.size()>0) && (start[i]+epsilon>ranges[i][1])) ? ranges[i][1]*0.999-start[i] : epsilon;
525  gsl_vector_set(ss, i, val);
526  }
527 
528  // Initialize the method and iterate
529 
530  minex_func.n = npar;
531  minex_func.f = &generic_minimizer_return;
532  minex_func.params = &params;
533 
534  s = gsl_multimin_fminimizer_alloc (T, npar);
535  gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
536 
537  do
538  {
539  iter ++;
540  status = gsl_multimin_fminimizer_iterate(s);
541 
542  if ((status!=GSL_SUCCESS) && (status!=GSL_CONTINUE))
543  check_GSL_fail(status, true, "GSL_minimize_nD", "gsl_multimin_fminimizer_iterate");
544 
545  size = gsl_multimin_fminimizer_size(s);
546 
547  status = gsl_multimin_test_size(size, tol);
548 
549  if ((status!=GSL_SUCCESS) && (status!=GSL_CONTINUE))
550  check_GSL_fail(status, true, "GSL_minimize_nD", "gsl_multimin_fminimizer_iterate");
551 
552  coutCBL << std::fixed << "\r" << "Iteration " << iter << std::scientific << std::setprecision(2) << " " << "Size= " << size << ", Tol = "<< tol << "\r"<<std::fixed; cout.flush();
553  }
554 
555  while (status==GSL_CONTINUE && iter<max_iter);
556 
557  gsl_vector_free(x);
558  gsl_vector_free(ss);
559  gsl_multimin_fminimizer_free(s);
560 
561  if (status==GSL_SUCCESS)
562  coutCBL << "Converged to a minimum at iteration " << iter << " (<" << max_iter << ")" << endl;
563  else if (status==GSL_CONTINUE)
564  ErrorCBL("Minimization has not converged. Try with lower tolerance or larger number of iterations", "GSL_minimize_nD", "GSLwrapper.cpp");
565  else
566  check_GSL_fail(status, true, "GSL_minimize_nD", par::defaultString);
567 
568  return params.parameters_return;
569 }
570 
571 
572 // ============================================================================
573 
574 
575 vector<double> cbl::wrapper::gsl::GSL_minimize_nD (FunctionDoubleVectorRef func, const std::vector<double> start, const std::vector<std::vector<double>> ranges, const unsigned int max_iter, const double tol, const double epsilon)
576 {
577  if (ranges.size()!=start.size() && ranges.size()!=0)
578  ErrorCBL("vector of ranges must have the same size of start vector!", "GSL_minimize_nD", "GSLwrapper.cpp");
579  gsl_set_error_handler_off();
580 
581  size_t npar = start.size();
582 
583  STR_generic_func_GSL params;
584  params.fmin_return = (ranges.size() == start.size()) ? [&] (vector<double> &par) { return (inRange(par, ranges)) ? func(par) : -par::defaultDouble; } : func;
585 
586  const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
587  gsl_multimin_fminimizer *s = NULL;
588  gsl_vector *ss, *x;
589  gsl_multimin_function minex_func;
590 
591  size_t iter = 0;
592  int status = 0;
593  double size;
594 
595 
596  // starting point
597 
598  x = gsl_vector_alloc(npar);
599  ss = gsl_vector_alloc(npar);
600 
601  for (size_t i=0; i<npar; i++) {
602  gsl_vector_set(x, i, start[i]);
603  double val = ((ranges.size()>0) && (start[i]+epsilon>ranges[i][1])) ? ranges[i][1]*0.999-start[i] : epsilon;
604  gsl_vector_set(ss, i, val);
605  }
606 
607 
608  // initialize the method and iterate
609 
610  minex_func.n = npar;
611  minex_func.f = &generic_minimizer_return;
612  minex_func.params = &params;
613 
614  s = gsl_multimin_fminimizer_alloc(T, npar);
615  gsl_multimin_fminimizer_set(s, &minex_func, x, ss);
616 
617  // check if current simplex size is smaller than requested tolerance
618  if (gsl_multimin_fminimizer_size(s)<tol)
619  ErrorCBL("The simplex size is smaller than requested tolerance, "+conv(gsl_multimin_fminimizer_size(s), par::fDP6)+"<"+conv(tol, par::fDP6)+", check your inputs!", "GSL_minimize_nD", "GSLwrapper.cpp");
620 
621  do {
622  iter ++;
623 
624  if ((status != GSL_SUCCESS) && (status!=GSL_CONTINUE))
625  check_GSL_fail(status, true, "GSL_minimize_nD", "gsl_multimin_test_size");
626 
627  status = gsl_multimin_fminimizer_iterate(s);
628 
629  size = gsl_multimin_fminimizer_size(s);
630 
631  status = gsl_multimin_test_size(size, tol);
632 
633  if ((status!=GSL_SUCCESS) && (status!=GSL_CONTINUE))
634  check_GSL_fail(status, true, "GSL_minimize_nD", "gsl_multimin_test_size");
635 
636  coutCBL << std::fixed << "\r" << "Iteration " << iter << std::scientific << std::setprecision(2) << " " << "Size= " << size << ", Tol = "<< tol << "\r"<<std::fixed; cout.flush();
637  }
638  while (status==GSL_CONTINUE && iter<max_iter);
639 
640  gsl_vector_free(x);
641  gsl_vector_free(ss);
642  gsl_multimin_fminimizer_free(s);
643 
644  if (status==GSL_SUCCESS)
645  coutCBL << "Converged to a minimum at iteration " << iter << " (<" << max_iter << ")" << endl;
646  else if (status==GSL_CONTINUE)
647  ErrorCBL("Minimization has not converged. Try with lower tolerance or larger number of iterations", "GSL_minimize_nD", "GSLwrapper.cpp");
648  else
649  check_GSL_fail(status, true, "GSL_minimize_nD", par::defaultString);
650 
651  return params.parameters_return;
652 }
653 
654 
655 // ============================================================================
656 
657 
658 double cbl::wrapper::gsl::GSL_minimize_1D (FunctionDoubleDouble func, const double start, double min, double max, const int max_iter, const bool verbose)
659 {
660  (void)verbose;
661  gsl_set_error_handler_off();
662 
663  const gsl_min_fminimizer_type *T = gsl_min_fminimizer_brent;
664  gsl_min_fminimizer *s = gsl_min_fminimizer_alloc(T);
665 
666  int status = 0;
667  int iter = 0;
668  double m = start;
669 
670  STR_generic_func_GSL params;
671  params.f = func;
672 
673  gsl_function F;
674  F.function = generic_function;
675  F.params = &func;
676 
677  gsl_min_fminimizer_set(s, &F, m, min, max);
678 
679  do
680  {
681  iter ++;
682  status = gsl_min_fminimizer_iterate(s);
683 
684  if ((status != GSL_SUCCESS) && (status != GSL_CONTINUE))
685  check_GSL_fail(status, true, "GSL_minimize_1D", "gsl_min_fminimizer_iterate");
686 
687  m = gsl_min_fminimizer_x_minimum(s);
688  min = gsl_min_fminimizer_x_lower(s);
689  max = gsl_min_fminimizer_x_upper(s);
690 
691  status = gsl_min_test_interval(min, max, 0.001, 0.0);
692 
693  if ((status != GSL_SUCCESS) && (status != GSL_CONTINUE))
694  check_GSL_fail(status, true, "GSL_minimize_1D", "gsl_min_test_interval");
695  }
696 
697  while (status==GSL_CONTINUE && iter<max_iter);
698 
699  gsl_min_fminimizer_free(s);
700 
701  if (status==GSL_SUCCESS)
702  coutCBL << "Converged to a minimum at iteration " << iter << " (<" << max_iter << ")" << endl;
703  else if (status==GSL_CONTINUE)
704  ErrorCBL("Minimization has not converged. Try with lower tolerance or larger number of iterations", "GSL_minimize_1D", "GSLwrapper.cpp");
705  else
706  check_GSL_fail(status, true, "GSL_minimize_1D", par::defaultString);
707 
708  return m;
709 }
710 
711 
712 // ============================================================================
713 
714 
715 double cbl::wrapper::gsl::GSL_polynomial_eval (const double x, const std::shared_ptr<void> fixed_parameters, const std::vector<double> coeff)
716 {
717  (void)fixed_parameters;
718  return gsl_poly_eval(coeff.data(), coeff.size(), x);
719 }
720 
721 
722 // ============================================================================
723 
724 
725 void cbl::wrapper::gsl::GSL_polynomial_root (const std::vector<double> coeff, std::vector<std::vector<double>> &root)
726 {
727  gsl_set_error_handler_off();
728 
729  size_t size = coeff.size();
730  size_t root_size = 2*(size-1);
731  double *z = new double[root_size];
732 
733  gsl_poly_complex_workspace *w = gsl_poly_complex_workspace_alloc(size);
734 
735  int status = gsl_poly_complex_solve(coeff.data(), size, w, z);
736 
737  check_GSL_fail(status, true, "GSL_polynomial", "gsl_poly_complex_solve");
738 
739  gsl_poly_complex_workspace_free(w);
740  root.resize(size-1,vector<double>(2,0));
741 
742  for (size_t i=0; i<size-1; i++) {
743  root[i][0] = z[2*i];
744  root[i][1] = z[2*i+1];
745  }
746 
747  delete[] z;
748 }
functions that wrap GSL routines for integration, root finding and minimization
#define coutCBL
CBL print message.
Definition: Kernel.h:734
static const char fDP6[]
conversion symbol for: double -> std::string
Definition: Constants.h:145
static const char fDP3[]
conversion symbol for: double -> std::string
Definition: Constants.h:136
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const double defaultDouble
default double value
Definition: Constants.h:348
static const double alpha
: the fine-structure constant
Definition: Constants.h:233
double GSL_minimize_1D(FunctionDoubleDouble func, const double start, double min=par::defaultDouble, double max=-par::defaultDouble, const int max_iter=1000, const bool verbose=false)
minimize the provided function using GSL procedure
Definition: GSLwrapper.cpp:658
double generic_function(const double xx, void *params)
function used to integrate interpolated function
Definition: GSLwrapper.cpp:60
double generic_roots(double xx, void *params)
generic roots
Definition: GSLwrapper.cpp:70
std::vector< double > GSL_minimize_nD(FunctionDoubleVector func, const std::vector< double > start, const std::vector< std::vector< double >> ranges, const unsigned int max_iter=1000, const double tol=1.e-6, const double epsilon=0.1)
minimize the provided function using GSL procedure
Definition: GSLwrapper.cpp:497
double generic_minimizer(const gsl_vector *xx, void *params)
generic roots
Definition: GSLwrapper.cpp:80
double GSL_derivative(gsl_function Func, const double xx, const double hh, const double prec=1.e-2)
the derivative of a function
Definition: GSLwrapper.cpp:114
double GSL_integrate_qagiu(gsl_function Func, const double a, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000)
integral, computed using the GSL qagiu method
Definition: GSLwrapper.cpp:220
void GSL_polynomial_root(const std::vector< double > coeff, std::vector< std::vector< double >> &root)
find polynomial roots
Definition: GSLwrapper.cpp:725
double GSL_integrate_qags(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000)
integral, computed using the GSL qags method
Definition: GSLwrapper.cpp:200
double GSL_integrate_romberg(gsl_function Func, const double a, const double b, const int npoints, const double eps_rel=1.e-4, const double eps_abs=1.e-12)
integral, using the gsl romberg method
Definition: GSLwrapper.cpp:130
double GSL_integrate_cquad(gsl_function func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int nevals=100)
integral, using the gsl cquad method
Definition: GSLwrapper.cpp:159
void check_GSL_fail(const int status, const bool exit, const std::string CBLfunction, const std::string GSLroutine)
Function used to check output of the wrapped GSL routines.
Definition: GSLwrapper.cpp:45
double generic_minimizer_return(const gsl_vector *xx, void *params)
generic roots
Definition: GSLwrapper.cpp:96
double GSL_integrate_qaws(gsl_function Func, const double a, const double b, const double alpha=0, const double beta=0, const int mu=1, const int nu=0, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000)
integral, computed using the GSL qaws method
Definition: GSLwrapper.cpp:240
double GSL_polynomial_eval(const double x, const std::shared_ptr< void > fixed_parameters, const std::vector< double > coeff)
evaluate a polynomial
Definition: GSLwrapper.cpp:715
double GSL_root_brent(gsl_function Func, const double low_guess, const double up_guess, const double rel_err=1.e-3, const double abs_err=0)
function to find roots using GSL qag method
Definition: GSLwrapper.cpp:430
double GSL_integrate_qag(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000, const int rule=6)
integral, computed using the GSL qag method
Definition: GSLwrapper.cpp:180
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
std::function< double(std::vector< double >)> FunctionDoubleVector
typedef of a function returning a double with a vector in input
Definition: Kernel.h:690
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
bool inRange(T value, T min, T max, bool include_limits=true)
return false value outside the range; true value inside the range
Definition: Kernel.h:1759
std::function< double(double)> FunctionDoubleDouble
typedef of a function returning a double with a double in input
Definition: Kernel.h:687
std::function< double(double, std::shared_ptr< void >, std::vector< double > &)> FunctionDoubleDoublePtrVectorRef
typedef of a function returning a double with a double, a pointer and a vector reference in input
Definition: Kernel.h:696
std::function< double(std::vector< double > &)> FunctionDoubleVectorRef
typedef of a function returning a double with a vector reference in input
Definition: Kernel.h:693
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747