glucat 0.12.0
scalar.h
Go to the documentation of this file.
1#ifndef _GLUCAT_SCALAR_H
2#define _GLUCAT_SCALAR_H
3/***************************************************************************
4 GluCat : Generic library of universal Clifford algebra templates
5 scalar.h : Define functions for scalar_t
6 -------------------
7 begin : 2001-12-20
8 copyright : (C) 2001-2016 by Paul C. Leopardi
9 ***************************************************************************
10
11 This library is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published
13 by the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with this library. If not, see <http://www.gnu.org/licenses/>.
23
24 ***************************************************************************
25 This library is based on a prototype written by Arvind Raja and was
26 licensed under the LGPL with permission of the author. See Arvind Raja,
27 "Object-oriented implementations of Clifford algebras in C++: a prototype",
28 in Ablamowicz, Lounesto and Parra (eds.)
29 "Clifford algebras with numeric and symbolic computations, Birkhauser, 1996."
30 ***************************************************************************
31 See also Arvind Raja's original header comments and references in glucat.h
32 ***************************************************************************/
33
34#include "glucat/portability.h"
35#include "glucat/global.h"
36
37#include <boost/numeric/ublas/traits.hpp>
38
39#include <cmath>
40#include <limits>
41
42namespace glucat
43{
45 // Reference: [AA], 2.4, p. 30-31
46 template< typename Scalar_T >
48 {
49 private:
51 inline
52 static
53 auto
54 isInf(const Scalar_T& val, bool_to_type<false>) -> bool
55 { return false; }
56
58 inline
59 static
60 auto
61 isInf(const Scalar_T& val, bool_to_type<true>) -> bool
62 { return _GLUCAT_ISINF(val); }
63
65 inline
66 static
67 auto
68 isNaN(const Scalar_T& val, bool_to_type<false>) -> bool
69 { return false; }
70
72 inline
73 static
74 auto
75 isNaN(const Scalar_T& val, bool_to_type<true>) -> bool
76 { return _GLUCAT_ISNAN(val); }
77
78 public:
80 inline
81 static
82 auto
83 isInf(const Scalar_T& val) -> bool
84 {
85 return isInf(val,
86 bool_to_type< std::numeric_limits<Scalar_T>::has_infinity >() );
87 }
88
90 inline
91 static
92 auto
93 isNaN(const Scalar_T& val) -> bool
94 {
95 return isNaN(val,
96 bool_to_type< std::numeric_limits<Scalar_T>::has_quiet_NaN >() );
97 }
98
100 inline
101 static
102 auto
103 isNaN_or_isInf(const Scalar_T& val) -> bool
104 {
105 return isNaN(val,
106 bool_to_type< std::numeric_limits<Scalar_T>::has_quiet_NaN >() )
107 || isInf(val,
108 bool_to_type< std::numeric_limits<Scalar_T>::has_infinity >() );
109 }
110
112 inline
113 static
114 auto
115 NaN() -> Scalar_T
116 {
117 return std::numeric_limits<Scalar_T>::has_quiet_NaN
118 ? std::numeric_limits<Scalar_T>::quiet_NaN()
119 : Scalar_T(std::log(0.0));
120 }
121
123 inline
124 static
125 auto
126 to_int(const Scalar_T& val) -> int
127 { return static_cast<int>(val); }
128
130 inline
131 static
132 auto
133 to_double(const Scalar_T& val) -> double
134 { return static_cast<double>(val); }
135
137 template <typename Other_Scalar_T >
138 inline
139 static
140 auto
141 to_scalar_t(const Other_Scalar_T& val) -> Scalar_T
142 { return static_cast<Scalar_T>(val); }
143
145 struct promoted {using type = double;};
146
148 struct demoted {using type = float;};
149
151 inline
152 static
153 auto
154 fmod(const Scalar_T& lhs, const Scalar_T& rhs) -> Scalar_T
155 { return std::fmod(lhs, rhs); }
156
158 inline
159 static
160 auto
161 conj(const Scalar_T& val) -> Scalar_T
162 { return val; }
163
165 inline
166 static
167 auto
168 real(const Scalar_T& val) -> Scalar_T
169 { return val; }
170
172 inline
173 static
174 auto
175 imag(const Scalar_T& val) -> Scalar_T
176 { return Scalar_T(0); }
177
179 inline
180 static
181 auto
182 abs(const Scalar_T& val) -> Scalar_T
183 { return boost::numeric::ublas::type_traits<Scalar_T>::UBLAS_ABS(val); }
184
186 inline
187 static
188 auto
189 pi() -> Scalar_T
190 { return Scalar_T(3.14159265358979323); }
191
193 inline
194 static
195 auto
196 ln_2() -> Scalar_T
197 { return Scalar_T(0.693147180559945309); }
198
200 inline
201 static
202 auto
203 pow(const Scalar_T& val, int n) -> Scalar_T
204 { return std::pow(val, n); }
205
207 inline
208 static
209 auto
210 sqrt(const Scalar_T& val) -> Scalar_T
211 { return boost::numeric::ublas::type_traits<Scalar_T>::UBLAS_SQRT(val); }
212
214 inline
215 static
216 auto
217 exp(const Scalar_T& val) -> Scalar_T
218 { return std::exp(val); }
219
221 inline
222 static
223 auto
224 log(const Scalar_T& val) -> Scalar_T
225 { return std::log(val); }
226
228 inline
229 static
230 auto
231 log2(const Scalar_T& val) -> Scalar_T
232 { return log(val)/ln_2(); }
233
235 inline
236 static
237 auto
238 cos(const Scalar_T& val) -> Scalar_T
239 { return std::cos(val); }
240
242 inline
243 static
244 auto
245 acos(const Scalar_T& val) -> Scalar_T
246 { return std::acos(val); }
247
249 inline
250 static
251 auto
252 cosh(const Scalar_T& val) -> Scalar_T
253 { return std::cosh(val); }
254
256 inline
257 static
258 auto
259 sin(const Scalar_T& val) -> Scalar_T
260 { return std::sin(val); }
261
263 inline
264 static
265 auto
266 asin(const Scalar_T& val) -> Scalar_T
267 { return std::asin(val); }
268
270 inline
271 static
272 auto
273 sinh(const Scalar_T& val) -> Scalar_T
274 { return std::sinh(val); }
275
277 inline
278 static
279 auto
280 tan(const Scalar_T& val) -> Scalar_T
281 { return std::tan(val); }
282
284 inline
285 static
286 auto
287 atan(const Scalar_T& val) -> Scalar_T
288 { return std::atan(val); }
289
291 inline
292 static
293 auto
294 tanh(const Scalar_T& val) -> Scalar_T
295 { return std::tanh(val); }
296
297 };
298
300 template< typename Scalar_T >
301 inline
302 auto
303 log2(const Scalar_T& x) -> Scalar_T
304 { return numeric_traits<Scalar_T>::log2(x); }
305}
306
307#endif // _GLUCAT_SCALAR_H
Bool to type.
Definition global.h:70
Extra traits which extend numeric limits.
Definition scalar.h:48
static auto log2(const Scalar_T &val) -> Scalar_T
Log base 2.
Definition scalar.h:231
static auto isNaN(const Scalar_T &val, bool_to_type< false >) -> bool
Smart isnan specialised for Scalar_T without quiet NaN.
Definition scalar.h:68
static auto isInf(const Scalar_T &val, bool_to_type< true >) -> bool
Smart isinf specialised for Scalar_T with infinity.
Definition scalar.h:61
static auto sqrt(const Scalar_T &val) -> Scalar_T
Square root of scalar.
Definition scalar.h:210
static auto fmod(const Scalar_T &lhs, const Scalar_T &rhs) -> Scalar_T
Modulo function for scalar.
Definition scalar.h:154
static auto NaN() -> Scalar_T
Smart NaN.
Definition scalar.h:115
static auto real(const Scalar_T &val) -> Scalar_T
Real part of scalar.
Definition scalar.h:168
static auto exp(const Scalar_T &val) -> Scalar_T
Exponential.
Definition scalar.h:217
static auto conj(const Scalar_T &val) -> Scalar_T
Complex conjugate of scalar.
Definition scalar.h:161
static auto isNaN_or_isInf(const Scalar_T &val) -> bool
Smart isnan or isinf.
Definition scalar.h:103
static auto sin(const Scalar_T &val) -> Scalar_T
Sine of scalar.
Definition scalar.h:259
static auto abs(const Scalar_T &val) -> Scalar_T
Absolute value of scalar.
Definition scalar.h:182
static auto ln_2() -> Scalar_T
log(2)
Definition scalar.h:196
static auto isNaN(const Scalar_T &val, bool_to_type< true >) -> bool
Smart isnan specialised for Scalar_T with quiet NaN.
Definition scalar.h:75
static auto to_scalar_t(const Other_Scalar_T &val) -> Scalar_T
Cast to Scalar_T.
Definition scalar.h:141
static auto isInf(const Scalar_T &val) -> bool
Smart isinf.
Definition scalar.h:83
static auto cosh(const Scalar_T &val) -> Scalar_T
Hyperbolic cosine of scalar.
Definition scalar.h:252
static auto to_double(const Scalar_T &val) -> double
Cast to double.
Definition scalar.h:133
static auto cos(const Scalar_T &val) -> Scalar_T
Cosine of scalar.
Definition scalar.h:238
static auto asin(const Scalar_T &val) -> Scalar_T
Inverse sine of scalar.
Definition scalar.h:266
static auto log(const Scalar_T &val) -> Scalar_T
Logarithm of scalar.
Definition scalar.h:224
static auto isInf(const Scalar_T &val, bool_to_type< false >) -> bool
Smart isinf specialised for Scalar_T without infinity.
Definition scalar.h:54
static auto sinh(const Scalar_T &val) -> Scalar_T
Hyperbolic sine of scalar.
Definition scalar.h:273
static auto tanh(const Scalar_T &val) -> Scalar_T
Hyperbolic tangent of scalar.
Definition scalar.h:294
static auto imag(const Scalar_T &val) -> Scalar_T
Imaginary part of scalar.
Definition scalar.h:175
static auto pi() -> Scalar_T
Pi.
Definition scalar.h:189
static auto pow(const Scalar_T &val, int n) -> Scalar_T
Integer power.
Definition scalar.h:203
static auto isNaN(const Scalar_T &val) -> bool
Smart isnan.
Definition scalar.h:93
static auto acos(const Scalar_T &val) -> Scalar_T
Inverse cosine of scalar.
Definition scalar.h:245
static auto to_int(const Scalar_T &val) -> int
Cast to int.
Definition scalar.h:126
static auto tan(const Scalar_T &val) -> Scalar_T
Tangent of scalar.
Definition scalar.h:280
static auto atan(const Scalar_T &val) -> Scalar_T
Inverse tangent of scalar.
Definition scalar.h:287
auto log2(const Scalar_T &x) -> Scalar_T
Log base 2 of scalar.
Definition scalar.h:303
#define _GLUCAT_ISINF(x)
Definition portability.h:43
#define _GLUCAT_ISNAN(x)
Definition portability.h:42