comparison armadillo-2.4.4/include/armadillo_bits/cmath_wrap.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2010 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2010 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14
15 //! \addtogroup cmath_wrap
16 //! @{
17
18
19
20 //
21 // wrappers for isfinite
22 //
23
24
25
26 template<typename eT>
27 arma_inline
28 bool
29 arma_isfinite(eT val)
30 {
31 arma_ignore(val);
32
33 return true;
34 }
35
36
37
38 template<>
39 arma_inline
40 bool
41 arma_isfinite(float x)
42 {
43 #if defined(ARMA_HAVE_STD_ISFINITE)
44 {
45 return (std::isfinite(x) != 0);
46 }
47 #else
48 {
49 const bool x_is_inf = ( (x == x) && ((x - x) != float(0)) );
50 const bool x_is_nan = (x != x);
51
52 return ( (x_is_inf == false) && (x_is_nan == false) );
53 }
54 #endif
55 }
56
57
58
59 template<>
60 arma_inline
61 bool
62 arma_isfinite(double x)
63 {
64 #if defined(ARMA_HAVE_STD_ISFINITE)
65 {
66 return (std::isfinite(x) != 0);
67 }
68 #else
69 {
70 const bool x_is_inf = ( (x == x) && ((x - x) != double(0)) );
71 const bool x_is_nan = (x != x);
72
73 return ( (x_is_inf == false) && (x_is_nan == false) );
74 }
75 #endif
76 }
77
78
79
80 template<typename T>
81 arma_inline
82 bool
83 arma_isfinite(const std::complex<T>& x)
84 {
85 if( (arma_isfinite(x.real()) == false) || (arma_isfinite(x.imag()) == false) )
86 {
87 return false;
88 }
89 else
90 {
91 return true;
92 }
93 }
94
95
96
97 //
98 // wrappers for trigonometric functions
99 //
100
101
102
103 // Wherever possible, try to use TR1 versions of the functions below,
104 // otherwise fall back to Boost Math.
105 //
106 // complex acos
107 // complex asin
108 // complex atan
109 //
110 // real acosh
111 // real asinh
112 // real atanh
113 //
114 // complex acosh
115 // complex asinh
116 // complex atanh
117 //
118 //
119 // If TR1 not present and Boost math not present,
120 // we have our own rudimentary versions of:
121 //
122 // real acosh
123 // real asinh
124 // real atanh
125
126
127
128 #if defined(ARMA_USE_BOOST)
129 #define arma_boost_wrap(trig_fn, val) ( (boost::math::trig_fn)(val) )
130 #else
131 #define arma_boost_wrap(trig_fn, val) ( arma_stop( #trig_fn "(): need Boost libraries" ), val )
132 #endif
133
134
135 template<typename T>
136 arma_inline
137 std::complex<T>
138 arma_acos(const std::complex<T>& x)
139 {
140 #if defined(ARMA_HAVE_STD_TR1)
141 {
142 return std::tr1::acos(x);
143 }
144 #else
145 {
146 return arma_boost_wrap(acos, x);
147 }
148 #endif
149 }
150
151
152
153 template<typename T>
154 arma_inline
155 std::complex<T>
156 arma_asin(const std::complex<T>& x)
157 {
158 #if defined(ARMA_HAVE_STD_TR1)
159 {
160 return std::tr1::asin(x);
161 }
162 #else
163 {
164 return arma_boost_wrap(asin, x);
165 }
166 #endif
167 }
168
169
170
171 template<typename T>
172 arma_inline
173 std::complex<T>
174 arma_atan(const std::complex<T>& x)
175 {
176 #if defined(ARMA_HAVE_STD_TR1)
177 {
178 return std::tr1::atan(x);
179 }
180 #else
181 {
182 return arma_boost_wrap(atan, x);
183 }
184 #endif
185 }
186
187
188
189 template<typename eT>
190 arma_inline
191 eT
192 arma_acosh(const eT x)
193 {
194 #if defined(ARMA_HAVE_STD_TR1)
195 {
196 return std::tr1::acosh(x);
197 }
198 #elif defined(ARMA_USE_BOOST)
199 {
200 return boost::math::acosh(x);
201 }
202 #else
203 {
204 if(x >= eT(1))
205 {
206 // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/
207 return std::log( x + std::sqrt(x*x - eT(1)) );
208 }
209 else
210 {
211 if(std::numeric_limits<eT>::has_quiet_NaN == true)
212 {
213 return -(std::numeric_limits<eT>::quiet_NaN());
214 }
215 else
216 {
217 return eT(0);
218 }
219 }
220 }
221 #endif
222 }
223
224
225
226 template<typename eT>
227 arma_inline
228 eT
229 arma_asinh(const eT x)
230 {
231 #if defined(ARMA_HAVE_STD_TR1)
232 {
233 return std::tr1::asinh(x);
234 }
235 #elif defined(ARMA_USE_BOOST)
236 {
237 return boost::math::asinh(x);
238 }
239 #else
240 {
241 // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/02/
242 return std::log( x + std::sqrt(x*x + eT(1)) );
243 }
244 #endif
245 }
246
247
248
249 template<typename eT>
250 arma_inline
251 eT
252 arma_atanh(const eT x)
253 {
254 #if defined(ARMA_HAVE_STD_TR1)
255 {
256 return std::tr1::atanh(x);
257 }
258 #elif defined(ARMA_USE_BOOST)
259 {
260 return boost::math::atanh(x);
261 }
262 #else
263 {
264 if( (x >= eT(-1)) && (x <= eT(+1)) )
265 {
266 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/
267 return std::log( ( eT(1)+x ) / ( eT(1)-x ) ) / eT(2);
268 }
269 else
270 {
271 if(std::numeric_limits<eT>::has_quiet_NaN == true)
272 {
273 return -(std::numeric_limits<eT>::quiet_NaN());
274 }
275 else
276 {
277 return eT(0);
278 }
279 }
280 }
281 #endif
282 }
283
284
285
286 template<typename T>
287 arma_inline
288 std::complex<T>
289 arma_acosh(const std::complex<T>& x)
290 {
291 #if defined(ARMA_HAVE_STD_TR1)
292 {
293 return std::tr1::acosh(x);
294 }
295 #else
296 {
297 return arma_boost_wrap(acosh, x);
298 }
299 #endif
300 }
301
302
303
304 template<typename T>
305 arma_inline
306 std::complex<T>
307 arma_asinh(const std::complex<T>& x)
308 {
309 #if defined(ARMA_HAVE_STD_TR1)
310 {
311 return std::tr1::asinh(x);
312 }
313 #else
314 {
315 return arma_boost_wrap(asinh, x);
316 }
317 #endif
318 }
319
320
321
322 template<typename T>
323 arma_inline
324 std::complex<T>
325 arma_atanh(const std::complex<T>& x)
326 {
327 #if defined(ARMA_HAVE_STD_TR1)
328 {
329 return std::tr1::atanh(x);
330 }
331 #else
332 {
333 return arma_boost_wrap(atanh, x);
334 }
335 #endif
336 }
337
338
339
340 #undef arma_boost_wrap
341
342
343
344 //! @}