OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
GSLCompat.h
Go to the documentation of this file.
1//
2// GSL compatibility stubs
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef OPAL_GSL_COMPAT_HH
19#define OPAL_GSL_COMPAT_HH
20
21#include <cmath>
22#include <limits>
23
27typedef void (*gsl_error_handler_t)(const char* reason, const char* file, int line, int gsl_errno);
28
33 // Store and return previous handler (simplified - no actual storage)
34 static gsl_error_handler_t prev_handler = nullptr;
35 gsl_error_handler_t old = prev_handler;
36 prev_handler = handler;
37 return old;
38}
39
43 // Return a null handler
44 return nullptr;
45}
46
50inline double gsl_sf_erf(double x) { return std::erf(x); }
51
57inline double gsl_sf_pow_int(double x, int n) { return std::pow(x, n); }
58
63inline double gsl_sf_gamma(double x) { return std::tgamma(x); }
64
69inline double gsl_sf_fact(unsigned int n) {
70 if (n > 170) {
71 // Factorial of numbers > 170 overflows double
72 // Use gamma function for large numbers
73 return std::tgamma(static_cast<double>(n + 1));
74 }
75 double result = 1.0;
76 for (unsigned int i = 2; i <= n; ++i) {
77 result *= static_cast<double>(i);
78 }
79 return result;
80}
81
87inline double gsl_sf_choose(unsigned int n, unsigned int m) {
88 if (m > n) return 0.0;
89 if (m == 0 || m == n) return 1.0;
90
91 // Use symmetry: choose(n, m) = choose(n, n-m)
92 if (m > n / 2) m = n - m;
93
94 // Compute using iterative method to avoid overflow
95 double result = 1.0;
96 for (unsigned int i = 0; i < m; ++i) {
97 result = result * (n - i) / (i + 1);
98 }
99 return result;
100}
101
103namespace gsl {
104 constexpr double GSL_POSINF = std::numeric_limits<double>::infinity();
105 constexpr double GSL_NEGINF = -std::numeric_limits<double>::infinity();
106 constexpr double GSL_NAN = std::numeric_limits<double>::quiet_NaN();
107} // namespace gsl
108
114inline double gsl_hypot(double x, double y) { return std::hypot(x, y); }
115
116// GSL_REAL and GSL_IMAG macros for complex numbers
117#define GSL_REAL(z) ((z).dat[0])
118#define GSL_IMAG(z) ((z).dat[1])
119
120#endif // OPAL_GSL_COMPAT_HH
gsl_error_handler_t gsl_set_error_handler(gsl_error_handler_t handler)
Set a global error handler and return the previous handler.
Definition GSLCompat.h:32
double gsl_sf_gamma(double x)
Gamma function .
Definition GSLCompat.h:63
double gsl_sf_fact(unsigned int n)
Factorial (computed via for large ).
Definition GSLCompat.h:69
double gsl_sf_erf(double x)
Error function .
Definition GSLCompat.h:50
double gsl_sf_pow_int(double x, int n)
Integer power .
Definition GSLCompat.h:57
void(* gsl_error_handler_t)(const char *reason, const char *file, int line, int gsl_errno)
Signature of a GSL-style error handler callback.
Definition GSLCompat.h:27
double gsl_sf_choose(unsigned int n, unsigned int m)
Binomial coefficient .
Definition GSLCompat.h:87
double gsl_hypot(double x, double y)
Hypotenuse with overflow-safe std::hypot.
Definition GSLCompat.h:114
gsl_error_handler_t gsl_set_error_handler_off()
Disable error handling and return a null handler.
Definition GSLCompat.h:42
GSL-compatible math constants.
Definition GSLCompat.h:103
constexpr double GSL_NEGINF
Definition GSLCompat.h:105
constexpr double GSL_NAN
Definition GSLCompat.h:106
constexpr double GSL_POSINF
Definition GSLCompat.h:104