TMB Documentation  v1.9.11
bessel.hpp
1 #ifndef TINY_AD_BESSEL_H
2 #define TINY_AD_BESSEL_H
3 
4 #include "undefs.h" // Rcpp
5 /* Standalone ? */
6 #ifndef R_RCONFIG_H
7 #include <cmath>
8 #include <iostream>
9 #include <float.h> // INFINITY etc
10 #include <stdlib.h> // calloc (bessel)
11 #undef R_PosInf
12 #undef R_NegInf
13 #undef F77_NAME
14 #undef FALSE
15 #undef TRUE
16 #undef R_NaN
17 #undef R_FINITE
18 #define R_PosInf INFINITY
19 #define R_NegInf -INFINITY
20 #define F77_NAME(x) x
21 #define FALSE false
22 #define TRUE true
23 #define R_NaN NAN
24 #define R_FINITE(x) R_finite(x)
25 #endif
26 
27 #include "../gamma/gamma.hpp"
28 
29 namespace bessel_utils {
30 using gamma_utils::Rf_gamma_cody;
31 
32 /* Selected functions may be called ignoring derivatives */
33 template<class T> int R_finite(T x) { return std::isfinite(asDouble(x)); }
34 template<class T> int isnan(T x) { return std::isnan(asDouble(x)); }
35 
36 /* Common defines for Rmath routines */
37 #undef ML_ERROR
38 #undef MATHLIB_ERROR
39 #undef MATHLIB_WARNING
40 #undef MATHLIB_WARNING2
41 #undef MATHLIB_WARNING3
42 #undef MATHLIB_WARNING4
43 #undef MATHLIB_WARNING5
44 #undef ML_POSINF
45 #undef ML_NEGINF
46 #undef ML_NAN
47 #undef M_SQRT_2dPI
48 #undef ISNAN
49 # define ML_ERROR(x, s) /* nothing */
50 # define MATHLIB_ERROR(fmt,x) /* nothing */
51 # define MATHLIB_WARNING(fmt,x) /* nothing */
52 # define MATHLIB_WARNING2(fmt,x,x2) /* nothing */
53 # define MATHLIB_WARNING3(fmt,x,x2,x3) /* nothing */
54 # define MATHLIB_WARNING4(fmt,x,x2,x3,x4) /* nothing */
55 # define MATHLIB_WARNING5(fmt,x,x2,x3,x4,x5) /* nothing */
56 #define ML_POSINF R_PosInf
57 #define ML_NEGINF R_NegInf
58 #define ML_NAN R_NaN
59 #define M_SQRT_2dPI 0.797884560802865355879892119869 /* sqrt(2/pi) */
60 #define ISNAN(x) (isnan(x)!=0)
61 
62 /* I got crashes with Eigen if not setting this: */
63 #define MATHLIB_STANDALONE 1
64 #include "bessel_k.cpp"
65 #undef min0
66 #undef max0
67 
68 template <class Float> Float sinpi(Float x) { return sin(x * M_PI); }
69 template <class Float> Float cospi(Float x) { return cos(x * M_PI); }
70 template<class S, class T>
71 T fmax2(S x, T y) {return (x < y) ? y : x;}
72 template<class Float> Float bessel_y(Float x, Float alpha);
73 #include "bessel_j.cpp"
74 #include "bessel_y.cpp"
75 
76 /* FIXME: */
77 template<class Float>
78 Float R_pow(Float x, Float n) {return pow(x, n);}
79 template<class Float>
80 Float R_pow_di(Float x, int n)
81 {
82  Float pow = 1.0;
83 
84  if (ISNAN(x)) return x;
85  if (n != 0) {
86  if (!R_FINITE(x)) return R_pow(x, (Float)n);
87  if (n < 0) { n = -n; x = 1/x; }
88  for(;;) {
89  if(n & 01) pow *= x;
90  if(n >>= 1) x *= x; else break;
91  }
92  }
93  return pow;
94 }
95 template<class Float>
96 Float ldexp (Float x, int expo) {
97  return exp( log(x) + expo * log(2.0) );
98 }
99 
100 #include "bessel_i.cpp"
101 #undef MATHLIB_STANDALONE
102 #include "undefs.h"
103 
104 } // End namespace bessel_utils
105 
106 // using bessel_utils::bessel_k;
107 // using bessel_utils::bessel_j;
108 // using bessel_utils::bessel_y;
109 // using bessel_utils::bessel_i;
110 
111 #endif
License: GPL v2