Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU Lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * Lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU Lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: NumericTraits.h,v 1.16 2007-06-23 15:58:58 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_NUMERIC_TRAITS_H
narshu 25:143b19c1fb05 25 #define TVMET_NUMERIC_TRAITS_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #if defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 28 # include <complex>
narshu 25:143b19c1fb05 29 #endif
narshu 25:143b19c1fb05 30 #include <cmath>
narshu 25:143b19c1fb05 31 #include <limits>
narshu 25:143b19c1fb05 32
narshu 25:143b19c1fb05 33 #include <tvmet/CompileTimeError.h>
narshu 25:143b19c1fb05 34
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 namespace tvmet {
narshu 25:143b19c1fb05 37
narshu 25:143b19c1fb05 38
narshu 25:143b19c1fb05 39 /**
narshu 25:143b19c1fb05 40 * \class NumericTraits NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 41 * \brief Traits for integral types for operations.
narshu 25:143b19c1fb05 42 *
narshu 25:143b19c1fb05 43 * For each type we have to specialize this traits.
narshu 25:143b19c1fb05 44 *
narshu 25:143b19c1fb05 45 * \note Keep in mind that the long types long long and long double doesn't
narshu 25:143b19c1fb05 46 * have traits. This is due to the sum_type. We can't give a guarantee
narshu 25:143b19c1fb05 47 * that there is a type of holding the sum. Therefore using this traits
narshu 25:143b19c1fb05 48 * is only safe if you have long long resp. long double types by
narshu 25:143b19c1fb05 49 * working on long ints and doubles. Otherwise you will get not expected
narshu 25:143b19c1fb05 50 * result for some circumstances. Anyway, you can use big integer/float
narshu 25:143b19c1fb05 51 * libraries and specialize the traits by your own.
narshu 25:143b19c1fb05 52 *
narshu 25:143b19c1fb05 53 * \todo The abs function of complex<non_float_type> can have an
narshu 25:143b19c1fb05 54 * overrun due to numeric computation. Solve it (someone
narshu 25:143b19c1fb05 55 * using value_type=long here?)
narshu 25:143b19c1fb05 56 */
narshu 25:143b19c1fb05 57 template<class T>
narshu 25:143b19c1fb05 58 struct NumericTraits {
narshu 25:143b19c1fb05 59 typedef T base_type;
narshu 25:143b19c1fb05 60 typedef T value_type;
narshu 25:143b19c1fb05 61 typedef value_type sum_type;
narshu 25:143b19c1fb05 62 typedef value_type diff_type;
narshu 25:143b19c1fb05 63 typedef value_type float_type;
narshu 25:143b19c1fb05 64 typedef value_type signed_type;
narshu 25:143b19c1fb05 65
narshu 25:143b19c1fb05 66 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 67 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 68
narshu 25:143b19c1fb05 69 static inline
narshu 25:143b19c1fb05 70 base_type real(argument_type x);
narshu 25:143b19c1fb05 71
narshu 25:143b19c1fb05 72 static inline
narshu 25:143b19c1fb05 73 base_type imag(argument_type x);
narshu 25:143b19c1fb05 74
narshu 25:143b19c1fb05 75 static inline
narshu 25:143b19c1fb05 76 value_type conj(argument_type x);
narshu 25:143b19c1fb05 77
narshu 25:143b19c1fb05 78 static inline
narshu 25:143b19c1fb05 79 base_type abs(argument_type x);
narshu 25:143b19c1fb05 80
narshu 25:143b19c1fb05 81 static inline
narshu 25:143b19c1fb05 82 value_type sqrt(argument_type x);
narshu 25:143b19c1fb05 83
narshu 25:143b19c1fb05 84 static inline
narshu 25:143b19c1fb05 85 base_type norm_1(argument_type x) {
narshu 25:143b19c1fb05 86 return NumericTraits<base_type>::abs(traits_type::real(x))
narshu 25:143b19c1fb05 87 + NumericTraits<base_type>::abs(traits_type::imag(x));
narshu 25:143b19c1fb05 88 }
narshu 25:143b19c1fb05 89
narshu 25:143b19c1fb05 90 static inline
narshu 25:143b19c1fb05 91 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 92
narshu 25:143b19c1fb05 93 static inline
narshu 25:143b19c1fb05 94 base_type norm_inf(argument_type x) {
narshu 25:143b19c1fb05 95 return std::max(NumericTraits<base_type>::abs(traits_type::real(x)),
narshu 25:143b19c1fb05 96 NumericTraits<base_type>::abs(traits_type::imag(x)));
narshu 25:143b19c1fb05 97 }
narshu 25:143b19c1fb05 98
narshu 25:143b19c1fb05 99 static inline
narshu 25:143b19c1fb05 100 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 101 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 102 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 103 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 104
narshu 25:143b19c1fb05 105 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 106 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 107 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 108 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 109 }
narshu 25:143b19c1fb05 110 };
narshu 25:143b19c1fb05 111
narshu 25:143b19c1fb05 112
narshu 25:143b19c1fb05 113 /*
narshu 25:143b19c1fb05 114 * numeric traits for standard types
narshu 25:143b19c1fb05 115 */
narshu 25:143b19c1fb05 116
narshu 25:143b19c1fb05 117
narshu 25:143b19c1fb05 118 /**
narshu 25:143b19c1fb05 119 * \class NumericTraits<char> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 120 * \brief Traits specialized for char.
narshu 25:143b19c1fb05 121 */
narshu 25:143b19c1fb05 122 template<>
narshu 25:143b19c1fb05 123 struct NumericTraits<char> {
narshu 25:143b19c1fb05 124 typedef char value_type;
narshu 25:143b19c1fb05 125 typedef value_type base_type;
narshu 25:143b19c1fb05 126 typedef long sum_type;
narshu 25:143b19c1fb05 127 typedef int diff_type;
narshu 25:143b19c1fb05 128 typedef float float_type;
narshu 25:143b19c1fb05 129 typedef char signed_type;
narshu 25:143b19c1fb05 130
narshu 25:143b19c1fb05 131 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 132 typedef value_type argument_type;
narshu 25:143b19c1fb05 133
narshu 25:143b19c1fb05 134 static inline
narshu 25:143b19c1fb05 135 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 136
narshu 25:143b19c1fb05 137 static inline
narshu 25:143b19c1fb05 138 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 139
narshu 25:143b19c1fb05 140 static inline
narshu 25:143b19c1fb05 141 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 142
narshu 25:143b19c1fb05 143 static inline
narshu 25:143b19c1fb05 144 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 145
narshu 25:143b19c1fb05 146 static inline
narshu 25:143b19c1fb05 147 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 148 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 149 }
narshu 25:143b19c1fb05 150
narshu 25:143b19c1fb05 151 static inline
narshu 25:143b19c1fb05 152 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 153
narshu 25:143b19c1fb05 154 static inline
narshu 25:143b19c1fb05 155 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 156
narshu 25:143b19c1fb05 157 static inline
narshu 25:143b19c1fb05 158 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 159
narshu 25:143b19c1fb05 160 static inline
narshu 25:143b19c1fb05 161 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 162
narshu 25:143b19c1fb05 163 enum { is_complex = false };
narshu 25:143b19c1fb05 164
narshu 25:143b19c1fb05 165 /** Complexity on operations. */
narshu 25:143b19c1fb05 166 enum {
narshu 25:143b19c1fb05 167 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 168 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 169 };
narshu 25:143b19c1fb05 170 };
narshu 25:143b19c1fb05 171
narshu 25:143b19c1fb05 172
narshu 25:143b19c1fb05 173 /**
narshu 25:143b19c1fb05 174 * \class NumericTraits<unsigned char> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 175 * \brief Traits specialized for unsigned char.
narshu 25:143b19c1fb05 176 *
narshu 25:143b19c1fb05 177 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 178 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 179 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 180 * this function is missing here.
narshu 25:143b19c1fb05 181 */
narshu 25:143b19c1fb05 182 template<>
narshu 25:143b19c1fb05 183 struct NumericTraits<unsigned char> {
narshu 25:143b19c1fb05 184 typedef unsigned char value_type;
narshu 25:143b19c1fb05 185 typedef value_type base_type;
narshu 25:143b19c1fb05 186 typedef unsigned long sum_type;
narshu 25:143b19c1fb05 187 typedef int diff_type;
narshu 25:143b19c1fb05 188 typedef float float_type;
narshu 25:143b19c1fb05 189 typedef int signed_type;
narshu 25:143b19c1fb05 190
narshu 25:143b19c1fb05 191 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 192 typedef value_type argument_type;
narshu 25:143b19c1fb05 193
narshu 25:143b19c1fb05 194 static inline
narshu 25:143b19c1fb05 195 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 196
narshu 25:143b19c1fb05 197 static inline
narshu 25:143b19c1fb05 198 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 199
narshu 25:143b19c1fb05 200 static inline
narshu 25:143b19c1fb05 201 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 202
narshu 25:143b19c1fb05 203 static inline
narshu 25:143b19c1fb05 204 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 205 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 206 }
narshu 25:143b19c1fb05 207
narshu 25:143b19c1fb05 208 static inline
narshu 25:143b19c1fb05 209 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 210
narshu 25:143b19c1fb05 211 static inline
narshu 25:143b19c1fb05 212 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 213
narshu 25:143b19c1fb05 214 static inline
narshu 25:143b19c1fb05 215 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 216
narshu 25:143b19c1fb05 217 static inline
narshu 25:143b19c1fb05 218 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 219
narshu 25:143b19c1fb05 220 enum { is_complex = false };
narshu 25:143b19c1fb05 221
narshu 25:143b19c1fb05 222 /** Complexity on operations. */
narshu 25:143b19c1fb05 223 enum {
narshu 25:143b19c1fb05 224 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 225 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 226 };
narshu 25:143b19c1fb05 227 };
narshu 25:143b19c1fb05 228
narshu 25:143b19c1fb05 229
narshu 25:143b19c1fb05 230 /**
narshu 25:143b19c1fb05 231 * \class NumericTraits<short int> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 232 * \brief Traits specialized for short int.
narshu 25:143b19c1fb05 233 */
narshu 25:143b19c1fb05 234 template<>
narshu 25:143b19c1fb05 235 struct NumericTraits<short int> {
narshu 25:143b19c1fb05 236 typedef short int value_type;
narshu 25:143b19c1fb05 237 typedef value_type base_type;
narshu 25:143b19c1fb05 238 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 239 typedef long long sum_type;
narshu 25:143b19c1fb05 240 #else
narshu 25:143b19c1fb05 241 typedef long sum_type;
narshu 25:143b19c1fb05 242 #endif
narshu 25:143b19c1fb05 243 typedef int diff_type;
narshu 25:143b19c1fb05 244 typedef float float_type;
narshu 25:143b19c1fb05 245 typedef short int signed_type;
narshu 25:143b19c1fb05 246
narshu 25:143b19c1fb05 247 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 248 typedef value_type argument_type;
narshu 25:143b19c1fb05 249
narshu 25:143b19c1fb05 250 static inline
narshu 25:143b19c1fb05 251 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 252
narshu 25:143b19c1fb05 253 static inline
narshu 25:143b19c1fb05 254 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 255
narshu 25:143b19c1fb05 256 static inline
narshu 25:143b19c1fb05 257 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 258
narshu 25:143b19c1fb05 259 static inline
narshu 25:143b19c1fb05 260 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 261
narshu 25:143b19c1fb05 262 static inline
narshu 25:143b19c1fb05 263 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 264 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 265 }
narshu 25:143b19c1fb05 266
narshu 25:143b19c1fb05 267 static inline
narshu 25:143b19c1fb05 268 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 269
narshu 25:143b19c1fb05 270 static inline
narshu 25:143b19c1fb05 271 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 272
narshu 25:143b19c1fb05 273 static inline
narshu 25:143b19c1fb05 274 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 275
narshu 25:143b19c1fb05 276 static inline
narshu 25:143b19c1fb05 277 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 278
narshu 25:143b19c1fb05 279 enum { is_complex = false };
narshu 25:143b19c1fb05 280
narshu 25:143b19c1fb05 281 /** Complexity on operations. */
narshu 25:143b19c1fb05 282 enum {
narshu 25:143b19c1fb05 283 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 284 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 285 };
narshu 25:143b19c1fb05 286 };
narshu 25:143b19c1fb05 287
narshu 25:143b19c1fb05 288
narshu 25:143b19c1fb05 289 /**
narshu 25:143b19c1fb05 290 * \class NumericTraits<short unsigned int> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 291 * \brief Traits specialized for short unsigned int.
narshu 25:143b19c1fb05 292 *
narshu 25:143b19c1fb05 293 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 294 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 295 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 296 * this function is missing here.
narshu 25:143b19c1fb05 297 */
narshu 25:143b19c1fb05 298 template<>
narshu 25:143b19c1fb05 299 struct NumericTraits<short unsigned int> {
narshu 25:143b19c1fb05 300 typedef short unsigned int value_type;
narshu 25:143b19c1fb05 301 typedef value_type base_type;
narshu 25:143b19c1fb05 302 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 303 typedef unsigned long long sum_type;
narshu 25:143b19c1fb05 304 #else
narshu 25:143b19c1fb05 305 typedef unsigned long sum_type;
narshu 25:143b19c1fb05 306 #endif
narshu 25:143b19c1fb05 307 typedef int diff_type;
narshu 25:143b19c1fb05 308 typedef float float_type;
narshu 25:143b19c1fb05 309 typedef int signed_type;
narshu 25:143b19c1fb05 310
narshu 25:143b19c1fb05 311 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 312 typedef value_type argument_type;
narshu 25:143b19c1fb05 313
narshu 25:143b19c1fb05 314 static inline
narshu 25:143b19c1fb05 315 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 316
narshu 25:143b19c1fb05 317 static inline
narshu 25:143b19c1fb05 318 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 319
narshu 25:143b19c1fb05 320 static inline
narshu 25:143b19c1fb05 321 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 322
narshu 25:143b19c1fb05 323 static inline
narshu 25:143b19c1fb05 324 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 325 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 326 }
narshu 25:143b19c1fb05 327
narshu 25:143b19c1fb05 328 static inline
narshu 25:143b19c1fb05 329 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 330
narshu 25:143b19c1fb05 331 static inline
narshu 25:143b19c1fb05 332 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 333
narshu 25:143b19c1fb05 334 static inline
narshu 25:143b19c1fb05 335 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 336
narshu 25:143b19c1fb05 337 static inline
narshu 25:143b19c1fb05 338 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 339
narshu 25:143b19c1fb05 340 enum { is_complex = false };
narshu 25:143b19c1fb05 341
narshu 25:143b19c1fb05 342 /** Complexity on operations. */
narshu 25:143b19c1fb05 343 enum {
narshu 25:143b19c1fb05 344 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 345 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 346 };
narshu 25:143b19c1fb05 347 };
narshu 25:143b19c1fb05 348
narshu 25:143b19c1fb05 349
narshu 25:143b19c1fb05 350 /**
narshu 25:143b19c1fb05 351 * \class NumericTraits<int> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 352 * \brief Traits specialized for int.
narshu 25:143b19c1fb05 353 */
narshu 25:143b19c1fb05 354 template<>
narshu 25:143b19c1fb05 355 struct NumericTraits<int> {
narshu 25:143b19c1fb05 356 typedef int value_type;
narshu 25:143b19c1fb05 357 typedef value_type base_type;
narshu 25:143b19c1fb05 358 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 359 typedef long long sum_type;
narshu 25:143b19c1fb05 360 #else
narshu 25:143b19c1fb05 361 typedef long sum_type;
narshu 25:143b19c1fb05 362 #endif
narshu 25:143b19c1fb05 363 typedef int diff_type;
narshu 25:143b19c1fb05 364 typedef double float_type;
narshu 25:143b19c1fb05 365 typedef int signed_type;
narshu 25:143b19c1fb05 366
narshu 25:143b19c1fb05 367 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 368 typedef value_type argument_type;
narshu 25:143b19c1fb05 369
narshu 25:143b19c1fb05 370 static inline
narshu 25:143b19c1fb05 371 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 372
narshu 25:143b19c1fb05 373 static inline
narshu 25:143b19c1fb05 374 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 375
narshu 25:143b19c1fb05 376 static inline
narshu 25:143b19c1fb05 377 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 378
narshu 25:143b19c1fb05 379 static inline
narshu 25:143b19c1fb05 380 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 381
narshu 25:143b19c1fb05 382 static inline
narshu 25:143b19c1fb05 383 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 384 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 385 }
narshu 25:143b19c1fb05 386
narshu 25:143b19c1fb05 387 static inline
narshu 25:143b19c1fb05 388 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 389
narshu 25:143b19c1fb05 390 static inline
narshu 25:143b19c1fb05 391 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 392
narshu 25:143b19c1fb05 393 static inline
narshu 25:143b19c1fb05 394 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 395
narshu 25:143b19c1fb05 396 static inline
narshu 25:143b19c1fb05 397 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 398
narshu 25:143b19c1fb05 399 enum { is_complex = false };
narshu 25:143b19c1fb05 400
narshu 25:143b19c1fb05 401 /** Complexity on operations. */
narshu 25:143b19c1fb05 402 enum {
narshu 25:143b19c1fb05 403 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 404 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 405 };
narshu 25:143b19c1fb05 406 };
narshu 25:143b19c1fb05 407
narshu 25:143b19c1fb05 408
narshu 25:143b19c1fb05 409 /**
narshu 25:143b19c1fb05 410 * \class NumericTraits<unsigned int> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 411 * \brief Traits specialized for unsigned int.
narshu 25:143b19c1fb05 412 *
narshu 25:143b19c1fb05 413 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 414 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 415 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 416 * this function is missing here.
narshu 25:143b19c1fb05 417 */
narshu 25:143b19c1fb05 418 template<>
narshu 25:143b19c1fb05 419 struct NumericTraits<unsigned int> {
narshu 25:143b19c1fb05 420 typedef unsigned int value_type;
narshu 25:143b19c1fb05 421 typedef value_type base_type;
narshu 25:143b19c1fb05 422 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 423 typedef unsigned long long sum_type;
narshu 25:143b19c1fb05 424 #else
narshu 25:143b19c1fb05 425 typedef unsigned long sum_type;
narshu 25:143b19c1fb05 426 #endif
narshu 25:143b19c1fb05 427 typedef int diff_type;
narshu 25:143b19c1fb05 428 typedef double float_type;
narshu 25:143b19c1fb05 429 typedef long signed_type;
narshu 25:143b19c1fb05 430
narshu 25:143b19c1fb05 431 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 432 typedef value_type argument_type;
narshu 25:143b19c1fb05 433
narshu 25:143b19c1fb05 434 static inline
narshu 25:143b19c1fb05 435 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 436
narshu 25:143b19c1fb05 437 static inline
narshu 25:143b19c1fb05 438 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 439
narshu 25:143b19c1fb05 440 static inline
narshu 25:143b19c1fb05 441 base_type abs(argument_type x) { return x; }
narshu 25:143b19c1fb05 442
narshu 25:143b19c1fb05 443 static inline
narshu 25:143b19c1fb05 444 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 445 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 446 }
narshu 25:143b19c1fb05 447
narshu 25:143b19c1fb05 448 static inline
narshu 25:143b19c1fb05 449 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 450
narshu 25:143b19c1fb05 451 static inline
narshu 25:143b19c1fb05 452 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 453
narshu 25:143b19c1fb05 454 static inline
narshu 25:143b19c1fb05 455 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 456
narshu 25:143b19c1fb05 457 static inline
narshu 25:143b19c1fb05 458 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 459
narshu 25:143b19c1fb05 460 enum { is_complex = false };
narshu 25:143b19c1fb05 461
narshu 25:143b19c1fb05 462 /** Complexity on operations. */
narshu 25:143b19c1fb05 463 enum {
narshu 25:143b19c1fb05 464 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 465 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 466 };
narshu 25:143b19c1fb05 467 };
narshu 25:143b19c1fb05 468
narshu 25:143b19c1fb05 469
narshu 25:143b19c1fb05 470 /**
narshu 25:143b19c1fb05 471 * \class NumericTraits<long> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 472 * \brief Traits specialized for long.
narshu 25:143b19c1fb05 473 */
narshu 25:143b19c1fb05 474 template<>
narshu 25:143b19c1fb05 475 struct NumericTraits<long> {
narshu 25:143b19c1fb05 476 typedef long value_type;
narshu 25:143b19c1fb05 477 typedef value_type base_type;
narshu 25:143b19c1fb05 478 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 479 typedef long long sum_type;
narshu 25:143b19c1fb05 480 #else
narshu 25:143b19c1fb05 481 typedef long sum_type;
narshu 25:143b19c1fb05 482 #endif
narshu 25:143b19c1fb05 483 typedef long diff_type;
narshu 25:143b19c1fb05 484 typedef double float_type;
narshu 25:143b19c1fb05 485 typedef long signed_type;
narshu 25:143b19c1fb05 486
narshu 25:143b19c1fb05 487 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 488 typedef value_type argument_type;
narshu 25:143b19c1fb05 489
narshu 25:143b19c1fb05 490 static inline
narshu 25:143b19c1fb05 491 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 492
narshu 25:143b19c1fb05 493 static inline
narshu 25:143b19c1fb05 494 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 495
narshu 25:143b19c1fb05 496 static inline
narshu 25:143b19c1fb05 497 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 498
narshu 25:143b19c1fb05 499 static inline
narshu 25:143b19c1fb05 500 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 501
narshu 25:143b19c1fb05 502 static inline
narshu 25:143b19c1fb05 503 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 504 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 505 }
narshu 25:143b19c1fb05 506
narshu 25:143b19c1fb05 507 static inline
narshu 25:143b19c1fb05 508 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 509
narshu 25:143b19c1fb05 510 static inline
narshu 25:143b19c1fb05 511 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 512
narshu 25:143b19c1fb05 513 static inline
narshu 25:143b19c1fb05 514 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 515
narshu 25:143b19c1fb05 516 static inline
narshu 25:143b19c1fb05 517 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 518
narshu 25:143b19c1fb05 519 enum { is_complex = false };
narshu 25:143b19c1fb05 520
narshu 25:143b19c1fb05 521 /** Complexity on operations. */
narshu 25:143b19c1fb05 522 enum {
narshu 25:143b19c1fb05 523 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 524 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 525 };
narshu 25:143b19c1fb05 526 };
narshu 25:143b19c1fb05 527
narshu 25:143b19c1fb05 528
narshu 25:143b19c1fb05 529 /**
narshu 25:143b19c1fb05 530 * \class NumericTraits<unsigned long> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 531 * \brief Traits specialized for unsigned long.
narshu 25:143b19c1fb05 532 *
narshu 25:143b19c1fb05 533 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 534 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 535 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 536 * this function is missing here.
narshu 25:143b19c1fb05 537 */
narshu 25:143b19c1fb05 538 template<>
narshu 25:143b19c1fb05 539 struct NumericTraits<unsigned long> {
narshu 25:143b19c1fb05 540 typedef unsigned long value_type;
narshu 25:143b19c1fb05 541 typedef value_type base_type;
narshu 25:143b19c1fb05 542 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 543 typedef unsigned long long sum_type;
narshu 25:143b19c1fb05 544 #else
narshu 25:143b19c1fb05 545 typedef unsigned long sum_type;
narshu 25:143b19c1fb05 546 #endif
narshu 25:143b19c1fb05 547 typedef unsigned long diff_type;
narshu 25:143b19c1fb05 548 typedef double float_type;
narshu 25:143b19c1fb05 549 typedef long signed_type;
narshu 25:143b19c1fb05 550
narshu 25:143b19c1fb05 551 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 552 typedef value_type argument_type;
narshu 25:143b19c1fb05 553
narshu 25:143b19c1fb05 554 static inline
narshu 25:143b19c1fb05 555 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 556
narshu 25:143b19c1fb05 557 static inline
narshu 25:143b19c1fb05 558 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 559
narshu 25:143b19c1fb05 560 static inline
narshu 25:143b19c1fb05 561 base_type abs(argument_type x) { return x; }
narshu 25:143b19c1fb05 562
narshu 25:143b19c1fb05 563 static inline
narshu 25:143b19c1fb05 564 value_type sqrt(argument_type x) {
narshu 25:143b19c1fb05 565 return static_cast<value_type>(std::sqrt(static_cast<float_type>(x)));
narshu 25:143b19c1fb05 566 }
narshu 25:143b19c1fb05 567
narshu 25:143b19c1fb05 568 static inline
narshu 25:143b19c1fb05 569 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 570
narshu 25:143b19c1fb05 571 static inline
narshu 25:143b19c1fb05 572 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 573
narshu 25:143b19c1fb05 574 static inline
narshu 25:143b19c1fb05 575 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 576
narshu 25:143b19c1fb05 577 static inline
narshu 25:143b19c1fb05 578 bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; }
narshu 25:143b19c1fb05 579
narshu 25:143b19c1fb05 580 enum { is_complex = false };
narshu 25:143b19c1fb05 581
narshu 25:143b19c1fb05 582 /** Complexity on operations. */
narshu 25:143b19c1fb05 583 enum {
narshu 25:143b19c1fb05 584 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 585 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 586 };
narshu 25:143b19c1fb05 587 };
narshu 25:143b19c1fb05 588
narshu 25:143b19c1fb05 589
narshu 25:143b19c1fb05 590 /**
narshu 25:143b19c1fb05 591 * \class NumericTraits<float> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 592 * \brief Traits specialized for float.
narshu 25:143b19c1fb05 593 */
narshu 25:143b19c1fb05 594 template<>
narshu 25:143b19c1fb05 595 struct NumericTraits<float> {
narshu 25:143b19c1fb05 596 typedef float value_type;
narshu 25:143b19c1fb05 597 typedef value_type base_type;
narshu 25:143b19c1fb05 598 typedef double sum_type;
narshu 25:143b19c1fb05 599 typedef float diff_type;
narshu 25:143b19c1fb05 600 typedef float float_type;
narshu 25:143b19c1fb05 601 typedef float signed_type;
narshu 25:143b19c1fb05 602
narshu 25:143b19c1fb05 603 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 604 typedef value_type argument_type;
narshu 25:143b19c1fb05 605
narshu 25:143b19c1fb05 606 static inline
narshu 25:143b19c1fb05 607 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 608
narshu 25:143b19c1fb05 609 static inline
narshu 25:143b19c1fb05 610 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 611
narshu 25:143b19c1fb05 612 static inline
narshu 25:143b19c1fb05 613 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 614
narshu 25:143b19c1fb05 615 static inline
narshu 25:143b19c1fb05 616 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 617
narshu 25:143b19c1fb05 618 static inline
narshu 25:143b19c1fb05 619 value_type sqrt(argument_type x) { return std::sqrt(x); }
narshu 25:143b19c1fb05 620
narshu 25:143b19c1fb05 621 static inline
narshu 25:143b19c1fb05 622 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 623
narshu 25:143b19c1fb05 624 static inline
narshu 25:143b19c1fb05 625 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 626
narshu 25:143b19c1fb05 627 static inline
narshu 25:143b19c1fb05 628 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 629
narshu 25:143b19c1fb05 630 static inline
narshu 25:143b19c1fb05 631 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 632 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 633 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 634 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 635
narshu 25:143b19c1fb05 636 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 637 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 638 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 639 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 640 }
narshu 25:143b19c1fb05 641
narshu 25:143b19c1fb05 642 enum { is_complex = false };
narshu 25:143b19c1fb05 643
narshu 25:143b19c1fb05 644 /** Complexity on operations. */
narshu 25:143b19c1fb05 645 enum {
narshu 25:143b19c1fb05 646 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 647 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 648 };
narshu 25:143b19c1fb05 649 };
narshu 25:143b19c1fb05 650
narshu 25:143b19c1fb05 651
narshu 25:143b19c1fb05 652 /**
narshu 25:143b19c1fb05 653 * \class NumericTraits<double> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 654 * \brief Traits specialized for double.
narshu 25:143b19c1fb05 655 */
narshu 25:143b19c1fb05 656 template<>
narshu 25:143b19c1fb05 657 struct NumericTraits<double> {
narshu 25:143b19c1fb05 658 typedef double value_type;
narshu 25:143b19c1fb05 659 typedef value_type base_type;
narshu 25:143b19c1fb05 660 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 661 typedef long double sum_type;
narshu 25:143b19c1fb05 662 #else
narshu 25:143b19c1fb05 663 typedef double sum_type;
narshu 25:143b19c1fb05 664 #endif
narshu 25:143b19c1fb05 665 typedef double diff_type;
narshu 25:143b19c1fb05 666 typedef double float_type;
narshu 25:143b19c1fb05 667 typedef double signed_type;
narshu 25:143b19c1fb05 668
narshu 25:143b19c1fb05 669 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 670 typedef value_type argument_type;
narshu 25:143b19c1fb05 671
narshu 25:143b19c1fb05 672 static inline
narshu 25:143b19c1fb05 673 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 674
narshu 25:143b19c1fb05 675 static inline
narshu 25:143b19c1fb05 676 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 677
narshu 25:143b19c1fb05 678 static inline
narshu 25:143b19c1fb05 679 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 680
narshu 25:143b19c1fb05 681 static inline
narshu 25:143b19c1fb05 682 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 683
narshu 25:143b19c1fb05 684 static inline
narshu 25:143b19c1fb05 685 value_type sqrt(argument_type x) { return std::sqrt(x); }
narshu 25:143b19c1fb05 686
narshu 25:143b19c1fb05 687 static inline
narshu 25:143b19c1fb05 688 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 689
narshu 25:143b19c1fb05 690 static inline
narshu 25:143b19c1fb05 691 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 692
narshu 25:143b19c1fb05 693 static inline
narshu 25:143b19c1fb05 694 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 695
narshu 25:143b19c1fb05 696 static inline
narshu 25:143b19c1fb05 697 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 698 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 699 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 700 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 701
narshu 25:143b19c1fb05 702 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 703 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 704 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 705 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 706 }
narshu 25:143b19c1fb05 707
narshu 25:143b19c1fb05 708 enum { is_complex = false };
narshu 25:143b19c1fb05 709
narshu 25:143b19c1fb05 710 /** Complexity on operations. */
narshu 25:143b19c1fb05 711 enum {
narshu 25:143b19c1fb05 712 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 713 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 714 };
narshu 25:143b19c1fb05 715 };
narshu 25:143b19c1fb05 716
narshu 25:143b19c1fb05 717
narshu 25:143b19c1fb05 718 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 719 /**
narshu 25:143b19c1fb05 720 * \class NumericTraits<long double> NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 721 * \brief Traits specialized for long double.
narshu 25:143b19c1fb05 722 */
narshu 25:143b19c1fb05 723 template<>
narshu 25:143b19c1fb05 724 struct NumericTraits<long double> {
narshu 25:143b19c1fb05 725 typedef long double value_type;
narshu 25:143b19c1fb05 726 typedef value_type base_type;
narshu 25:143b19c1fb05 727 typedef long double sum_type;
narshu 25:143b19c1fb05 728 typedef long double diff_type;
narshu 25:143b19c1fb05 729 typedef long double float_type;
narshu 25:143b19c1fb05 730 typedef long double signed_type;
narshu 25:143b19c1fb05 731
narshu 25:143b19c1fb05 732 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 733 typedef value_type argument_type;
narshu 25:143b19c1fb05 734
narshu 25:143b19c1fb05 735 static inline
narshu 25:143b19c1fb05 736 base_type real(argument_type x) { return x; }
narshu 25:143b19c1fb05 737
narshu 25:143b19c1fb05 738 static inline
narshu 25:143b19c1fb05 739 base_type imag(argument_type) { return 0; }
narshu 25:143b19c1fb05 740
narshu 25:143b19c1fb05 741 static inline
narshu 25:143b19c1fb05 742 value_type conj(argument_type x) { return x; }
narshu 25:143b19c1fb05 743
narshu 25:143b19c1fb05 744 static inline
narshu 25:143b19c1fb05 745 base_type abs(argument_type x) { return std::abs(x); }
narshu 25:143b19c1fb05 746
narshu 25:143b19c1fb05 747 static inline
narshu 25:143b19c1fb05 748 value_type sqrt(argument_type x) { return std::sqrt(x); }
narshu 25:143b19c1fb05 749
narshu 25:143b19c1fb05 750 static inline
narshu 25:143b19c1fb05 751 base_type norm_1(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 752
narshu 25:143b19c1fb05 753 static inline
narshu 25:143b19c1fb05 754 base_type norm_2(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 755
narshu 25:143b19c1fb05 756 static inline
narshu 25:143b19c1fb05 757 base_type norm_inf(argument_type x) { return traits_type::abs(x); }
narshu 25:143b19c1fb05 758
narshu 25:143b19c1fb05 759 static inline
narshu 25:143b19c1fb05 760 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 761 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 762 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 763 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 764
narshu 25:143b19c1fb05 765 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 766 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 767 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 768 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 769 }
narshu 25:143b19c1fb05 770
narshu 25:143b19c1fb05 771 enum { is_complex = false };
narshu 25:143b19c1fb05 772
narshu 25:143b19c1fb05 773 /** Complexity on operations. */
narshu 25:143b19c1fb05 774 enum {
narshu 25:143b19c1fb05 775 ops_plus = 1, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 776 ops_muls = 1 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 777 };
narshu 25:143b19c1fb05 778 };
narshu 25:143b19c1fb05 779 #endif // TVMET_HAVE_LONG_DOUBLE
narshu 25:143b19c1fb05 780
narshu 25:143b19c1fb05 781
narshu 25:143b19c1fb05 782 /*
narshu 25:143b19c1fb05 783 * numeric traits for complex types
narshu 25:143b19c1fb05 784 */
narshu 25:143b19c1fb05 785 #if defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 786
narshu 25:143b19c1fb05 787 /**
narshu 25:143b19c1fb05 788 * \class NumericTraits< std::complex<int> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 789 * \brief Traits specialized for std::complex<int>.
narshu 25:143b19c1fb05 790 */
narshu 25:143b19c1fb05 791 template<>
narshu 25:143b19c1fb05 792 struct NumericTraits< std::complex<int> > {
narshu 25:143b19c1fb05 793 typedef int base_type;
narshu 25:143b19c1fb05 794 typedef std::complex<int> value_type;
narshu 25:143b19c1fb05 795 typedef std::complex<long> sum_type;
narshu 25:143b19c1fb05 796 typedef std::complex<int> diff_type;
narshu 25:143b19c1fb05 797 typedef std::complex<float> float_type;
narshu 25:143b19c1fb05 798 typedef std::complex<int> signed_type;
narshu 25:143b19c1fb05 799
narshu 25:143b19c1fb05 800 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 801 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 802
narshu 25:143b19c1fb05 803 static inline
narshu 25:143b19c1fb05 804 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 805
narshu 25:143b19c1fb05 806 static inline
narshu 25:143b19c1fb05 807 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 808
narshu 25:143b19c1fb05 809 static inline
narshu 25:143b19c1fb05 810 value_type conj(argument_type z) { return std::conj(z); }
narshu 25:143b19c1fb05 811
narshu 25:143b19c1fb05 812 static inline
narshu 25:143b19c1fb05 813 base_type abs(argument_type z) {
narshu 25:143b19c1fb05 814 base_type x = z.real();
narshu 25:143b19c1fb05 815 base_type y = z.imag();
narshu 25:143b19c1fb05 816
narshu 25:143b19c1fb05 817 // XXX probably case of overrun; header complex uses scaling
narshu 25:143b19c1fb05 818 return static_cast<base_type>(NumericTraits<base_type>::sqrt(x * x + y * y));
narshu 25:143b19c1fb05 819 }
narshu 25:143b19c1fb05 820
narshu 25:143b19c1fb05 821 static /* inline */
narshu 25:143b19c1fb05 822 value_type sqrt(argument_type z) {
narshu 25:143b19c1fb05 823 // borrowed and adapted from header complex
narshu 25:143b19c1fb05 824 base_type x = z.real();
narshu 25:143b19c1fb05 825 base_type y = z.imag();
narshu 25:143b19c1fb05 826
narshu 25:143b19c1fb05 827 if(x == base_type()) {
narshu 25:143b19c1fb05 828 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 829 NumericTraits<base_type>::abs(y) / 2);
narshu 25:143b19c1fb05 830 return value_type(t, y < base_type() ? -t : t);
narshu 25:143b19c1fb05 831 }
narshu 25:143b19c1fb05 832 else {
narshu 25:143b19c1fb05 833 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 834 2 * (traits_type::abs(z)
narshu 25:143b19c1fb05 835 + NumericTraits<base_type>::abs(x)));
narshu 25:143b19c1fb05 836 base_type u = t / 2;
narshu 25:143b19c1fb05 837 return x > base_type()
narshu 25:143b19c1fb05 838 ? value_type(u, y / t)
narshu 25:143b19c1fb05 839 : value_type(NumericTraits<base_type>::abs(y) / t, y < base_type() ? -u : u);
narshu 25:143b19c1fb05 840 }
narshu 25:143b19c1fb05 841 }
narshu 25:143b19c1fb05 842
narshu 25:143b19c1fb05 843 static inline
narshu 25:143b19c1fb05 844 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 845 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 846 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 847 }
narshu 25:143b19c1fb05 848
narshu 25:143b19c1fb05 849 static inline
narshu 25:143b19c1fb05 850 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 851
narshu 25:143b19c1fb05 852 static inline
narshu 25:143b19c1fb05 853 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 854 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 855 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 856 }
narshu 25:143b19c1fb05 857
narshu 25:143b19c1fb05 858 static inline
narshu 25:143b19c1fb05 859 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 860 return (traits_type::real(lhs) == traits_type::real(rhs))
narshu 25:143b19c1fb05 861 && (traits_type::imag(lhs) == traits_type::imag(rhs));
narshu 25:143b19c1fb05 862 }
narshu 25:143b19c1fb05 863
narshu 25:143b19c1fb05 864 enum { is_complex = true };
narshu 25:143b19c1fb05 865
narshu 25:143b19c1fb05 866 /** Complexity on operations. */
narshu 25:143b19c1fb05 867 enum {
narshu 25:143b19c1fb05 868 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 869 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 870 };
narshu 25:143b19c1fb05 871 };
narshu 25:143b19c1fb05 872
narshu 25:143b19c1fb05 873
narshu 25:143b19c1fb05 874 /**
narshu 25:143b19c1fb05 875 * \class NumericTraits< std::complex<unsigned int> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 876 * \brief Traits specialized for std::complex<unsigned int>.
narshu 25:143b19c1fb05 877 *
narshu 25:143b19c1fb05 878 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 879 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 880 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 881 * this function is missing here.
narshu 25:143b19c1fb05 882 */
narshu 25:143b19c1fb05 883 template<>
narshu 25:143b19c1fb05 884 struct NumericTraits< std::complex<unsigned int> > {
narshu 25:143b19c1fb05 885 typedef unsigned int base_type;
narshu 25:143b19c1fb05 886 typedef std::complex<unsigned int> value_type;
narshu 25:143b19c1fb05 887 typedef std::complex<unsigned long> sum_type;
narshu 25:143b19c1fb05 888 typedef std::complex<int> diff_type;
narshu 25:143b19c1fb05 889 typedef std::complex<float> float_type;
narshu 25:143b19c1fb05 890 typedef std::complex<int> signed_type;
narshu 25:143b19c1fb05 891
narshu 25:143b19c1fb05 892 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 893 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 894
narshu 25:143b19c1fb05 895 static inline
narshu 25:143b19c1fb05 896 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 897
narshu 25:143b19c1fb05 898 static inline
narshu 25:143b19c1fb05 899 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 900
narshu 25:143b19c1fb05 901 static inline
narshu 25:143b19c1fb05 902 base_type abs(argument_type z) {
narshu 25:143b19c1fb05 903 base_type x = z.real();
narshu 25:143b19c1fb05 904 base_type y = z.imag();
narshu 25:143b19c1fb05 905
narshu 25:143b19c1fb05 906 // XXX probably case of overrun; header complex uses scaling
narshu 25:143b19c1fb05 907 return static_cast<base_type>(NumericTraits<base_type>::sqrt(x * x + y * y));
narshu 25:143b19c1fb05 908 }
narshu 25:143b19c1fb05 909
narshu 25:143b19c1fb05 910 static /* inline */
narshu 25:143b19c1fb05 911 value_type sqrt(argument_type z) {
narshu 25:143b19c1fb05 912 // borrowed and adapted from header complex
narshu 25:143b19c1fb05 913 base_type x = z.real();
narshu 25:143b19c1fb05 914 base_type y = z.imag();
narshu 25:143b19c1fb05 915
narshu 25:143b19c1fb05 916 if(x == base_type()) {
narshu 25:143b19c1fb05 917 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 918 NumericTraits<base_type>::abs(y) / 2);
narshu 25:143b19c1fb05 919 return value_type(t, t);
narshu 25:143b19c1fb05 920 }
narshu 25:143b19c1fb05 921 else {
narshu 25:143b19c1fb05 922 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 923 2 * (traits_type::abs(z)
narshu 25:143b19c1fb05 924 + NumericTraits<base_type>::abs(x)));
narshu 25:143b19c1fb05 925 return value_type(t / 2, y / t);
narshu 25:143b19c1fb05 926 }
narshu 25:143b19c1fb05 927 }
narshu 25:143b19c1fb05 928
narshu 25:143b19c1fb05 929 static inline
narshu 25:143b19c1fb05 930 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 931 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 932 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 933 }
narshu 25:143b19c1fb05 934
narshu 25:143b19c1fb05 935 static inline
narshu 25:143b19c1fb05 936 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 937
narshu 25:143b19c1fb05 938 static inline
narshu 25:143b19c1fb05 939 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 940 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 941 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 942 }
narshu 25:143b19c1fb05 943
narshu 25:143b19c1fb05 944 static inline
narshu 25:143b19c1fb05 945 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 946 return (traits_type::real(lhs) == traits_type::real(rhs))
narshu 25:143b19c1fb05 947 && (traits_type::imag(lhs) == traits_type::imag(rhs));
narshu 25:143b19c1fb05 948 }
narshu 25:143b19c1fb05 949
narshu 25:143b19c1fb05 950 enum { is_complex = true };
narshu 25:143b19c1fb05 951
narshu 25:143b19c1fb05 952 /** Complexity on operations. */
narshu 25:143b19c1fb05 953 enum {
narshu 25:143b19c1fb05 954 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 955 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 956 };
narshu 25:143b19c1fb05 957 };
narshu 25:143b19c1fb05 958
narshu 25:143b19c1fb05 959
narshu 25:143b19c1fb05 960 /**
narshu 25:143b19c1fb05 961 * \class NumericTraits< std::complex<long> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 962 * \brief Traits specialized for std::complex<long>.
narshu 25:143b19c1fb05 963 */
narshu 25:143b19c1fb05 964 template<>
narshu 25:143b19c1fb05 965 struct NumericTraits< std::complex<long> > {
narshu 25:143b19c1fb05 966 typedef long base_type;
narshu 25:143b19c1fb05 967 typedef std::complex<long> value_type;
narshu 25:143b19c1fb05 968 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 969 typedef std::complex<long long> sum_type;
narshu 25:143b19c1fb05 970 #else
narshu 25:143b19c1fb05 971 typedef std::complex<long> sum_type;
narshu 25:143b19c1fb05 972 #endif
narshu 25:143b19c1fb05 973 typedef std::complex<int> diff_type;
narshu 25:143b19c1fb05 974 typedef std::complex<float> float_type;
narshu 25:143b19c1fb05 975 typedef std::complex<int> signed_type;
narshu 25:143b19c1fb05 976
narshu 25:143b19c1fb05 977 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 978 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 979
narshu 25:143b19c1fb05 980 static inline
narshu 25:143b19c1fb05 981 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 982
narshu 25:143b19c1fb05 983 static inline
narshu 25:143b19c1fb05 984 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 985
narshu 25:143b19c1fb05 986 static inline
narshu 25:143b19c1fb05 987 value_type conj(argument_type z) { return std::conj(z); }
narshu 25:143b19c1fb05 988
narshu 25:143b19c1fb05 989 static inline
narshu 25:143b19c1fb05 990 base_type abs(argument_type z) {
narshu 25:143b19c1fb05 991 base_type x = z.real();
narshu 25:143b19c1fb05 992 base_type y = z.imag();
narshu 25:143b19c1fb05 993
narshu 25:143b19c1fb05 994 // XXX probably case of overrun; header complex uses scaling
narshu 25:143b19c1fb05 995 return static_cast<base_type>(NumericTraits<base_type>::sqrt(x * x + y * y));
narshu 25:143b19c1fb05 996 }
narshu 25:143b19c1fb05 997
narshu 25:143b19c1fb05 998 static /* inline */
narshu 25:143b19c1fb05 999 value_type sqrt(argument_type z) {
narshu 25:143b19c1fb05 1000 // borrowed and adapted from header complex
narshu 25:143b19c1fb05 1001 base_type x = z.real();
narshu 25:143b19c1fb05 1002 base_type y = z.imag();
narshu 25:143b19c1fb05 1003
narshu 25:143b19c1fb05 1004 if(x == base_type()) {
narshu 25:143b19c1fb05 1005 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1006 NumericTraits<base_type>::abs(y) / 2);
narshu 25:143b19c1fb05 1007 return value_type(t, y < base_type() ? -t : t);
narshu 25:143b19c1fb05 1008 }
narshu 25:143b19c1fb05 1009 else {
narshu 25:143b19c1fb05 1010 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1011 2 * (traits_type::abs(z)
narshu 25:143b19c1fb05 1012 + NumericTraits<base_type>::abs(x)));
narshu 25:143b19c1fb05 1013 base_type u = t / 2;
narshu 25:143b19c1fb05 1014 return x > base_type()
narshu 25:143b19c1fb05 1015 ? value_type(u, y / t)
narshu 25:143b19c1fb05 1016 : value_type(NumericTraits<base_type>::abs(y) / t, y < base_type() ? -u : u);
narshu 25:143b19c1fb05 1017 }
narshu 25:143b19c1fb05 1018 }
narshu 25:143b19c1fb05 1019
narshu 25:143b19c1fb05 1020 static inline
narshu 25:143b19c1fb05 1021 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 1022 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 1023 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 1024 }
narshu 25:143b19c1fb05 1025
narshu 25:143b19c1fb05 1026 static inline
narshu 25:143b19c1fb05 1027 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 1028
narshu 25:143b19c1fb05 1029 static inline
narshu 25:143b19c1fb05 1030 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 1031 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 1032 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 1033 }
narshu 25:143b19c1fb05 1034
narshu 25:143b19c1fb05 1035 static inline
narshu 25:143b19c1fb05 1036 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 1037 return (traits_type::real(lhs) == traits_type::real(rhs))
narshu 25:143b19c1fb05 1038 && (traits_type::imag(lhs) == traits_type::imag(rhs));
narshu 25:143b19c1fb05 1039 }
narshu 25:143b19c1fb05 1040
narshu 25:143b19c1fb05 1041 enum { is_complex = true };
narshu 25:143b19c1fb05 1042
narshu 25:143b19c1fb05 1043 /** Complexity on operations. */
narshu 25:143b19c1fb05 1044 enum {
narshu 25:143b19c1fb05 1045 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 1046 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 1047 };
narshu 25:143b19c1fb05 1048 };
narshu 25:143b19c1fb05 1049
narshu 25:143b19c1fb05 1050
narshu 25:143b19c1fb05 1051 /**
narshu 25:143b19c1fb05 1052 * \class NumericTraits< std::complex<unsigned long> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 1053 * \brief Traits specialized for std::complex<unsigned long>.
narshu 25:143b19c1fb05 1054 *
narshu 25:143b19c1fb05 1055 * \note Normally it doesn't make sense to call <tt>conj</tt>
narshu 25:143b19c1fb05 1056 * for an unsigned type! An unary minus operator
narshu 25:143b19c1fb05 1057 * applied to unsigned type will result unsigned. Therefore
narshu 25:143b19c1fb05 1058 * this function is missing here.
narshu 25:143b19c1fb05 1059 */
narshu 25:143b19c1fb05 1060 template<>
narshu 25:143b19c1fb05 1061 struct NumericTraits< std::complex<unsigned long> > {
narshu 25:143b19c1fb05 1062 typedef unsigned long base_type;
narshu 25:143b19c1fb05 1063 typedef std::complex<unsigned long> value_type;
narshu 25:143b19c1fb05 1064 #if defined(TVMET_HAVE_LONG_LONG)
narshu 25:143b19c1fb05 1065 typedef std::complex<unsigned long long> sum_type;
narshu 25:143b19c1fb05 1066 #else
narshu 25:143b19c1fb05 1067 typedef std::complex<unsigned long> sum_type;
narshu 25:143b19c1fb05 1068 #endif
narshu 25:143b19c1fb05 1069 typedef std::complex<long> diff_type;
narshu 25:143b19c1fb05 1070 typedef std::complex<float> float_type;
narshu 25:143b19c1fb05 1071 typedef std::complex<long> signed_type;
narshu 25:143b19c1fb05 1072
narshu 25:143b19c1fb05 1073 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 1074 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 1075
narshu 25:143b19c1fb05 1076 static inline
narshu 25:143b19c1fb05 1077 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 1078
narshu 25:143b19c1fb05 1079 static inline
narshu 25:143b19c1fb05 1080 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 1081
narshu 25:143b19c1fb05 1082 static inline
narshu 25:143b19c1fb05 1083 base_type abs(argument_type z) {
narshu 25:143b19c1fb05 1084 base_type x = z.real();
narshu 25:143b19c1fb05 1085 base_type y = z.imag();
narshu 25:143b19c1fb05 1086
narshu 25:143b19c1fb05 1087 // XXX probably case of overrun; header complex uses scaling
narshu 25:143b19c1fb05 1088 return static_cast<base_type>(NumericTraits<base_type>::sqrt(x * x + y * y));
narshu 25:143b19c1fb05 1089 }
narshu 25:143b19c1fb05 1090
narshu 25:143b19c1fb05 1091 static /* inline */
narshu 25:143b19c1fb05 1092 value_type sqrt(argument_type z) {
narshu 25:143b19c1fb05 1093 // borrowed and adapted from header complex
narshu 25:143b19c1fb05 1094 base_type x = z.real();
narshu 25:143b19c1fb05 1095 base_type y = z.imag();
narshu 25:143b19c1fb05 1096
narshu 25:143b19c1fb05 1097 if(x == base_type()) {
narshu 25:143b19c1fb05 1098 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1099 NumericTraits<base_type>::abs(y) / 2);
narshu 25:143b19c1fb05 1100 return value_type(t, t);
narshu 25:143b19c1fb05 1101 }
narshu 25:143b19c1fb05 1102 else {
narshu 25:143b19c1fb05 1103 base_type t = NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1104 2 * (traits_type::abs(z)
narshu 25:143b19c1fb05 1105 + NumericTraits<base_type>::abs(x)));
narshu 25:143b19c1fb05 1106 return value_type(t / 2, y / t);
narshu 25:143b19c1fb05 1107 }
narshu 25:143b19c1fb05 1108 }
narshu 25:143b19c1fb05 1109
narshu 25:143b19c1fb05 1110 static inline
narshu 25:143b19c1fb05 1111 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 1112 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 1113 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 1114 }
narshu 25:143b19c1fb05 1115
narshu 25:143b19c1fb05 1116 static inline
narshu 25:143b19c1fb05 1117 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 1118
narshu 25:143b19c1fb05 1119 static inline
narshu 25:143b19c1fb05 1120 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 1121 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 1122 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 1123 }
narshu 25:143b19c1fb05 1124
narshu 25:143b19c1fb05 1125 static inline
narshu 25:143b19c1fb05 1126 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 1127 return (traits_type::real(lhs) == traits_type::real(rhs))
narshu 25:143b19c1fb05 1128 && (traits_type::imag(lhs) == traits_type::imag(rhs));
narshu 25:143b19c1fb05 1129 }
narshu 25:143b19c1fb05 1130
narshu 25:143b19c1fb05 1131 enum { is_complex = true };
narshu 25:143b19c1fb05 1132
narshu 25:143b19c1fb05 1133 /** Complexity on operations.*/
narshu 25:143b19c1fb05 1134 enum {
narshu 25:143b19c1fb05 1135 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 1136 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 1137 };
narshu 25:143b19c1fb05 1138 };
narshu 25:143b19c1fb05 1139
narshu 25:143b19c1fb05 1140
narshu 25:143b19c1fb05 1141 /**
narshu 25:143b19c1fb05 1142 * \class NumericTraits< std::complex<float> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 1143 * \brief Traits specialized for std::complex<float>.
narshu 25:143b19c1fb05 1144 */
narshu 25:143b19c1fb05 1145 template<>
narshu 25:143b19c1fb05 1146 struct NumericTraits< std::complex<float> > {
narshu 25:143b19c1fb05 1147 typedef float base_type;
narshu 25:143b19c1fb05 1148 typedef std::complex<float> value_type;
narshu 25:143b19c1fb05 1149 typedef std::complex<double> sum_type;
narshu 25:143b19c1fb05 1150 typedef std::complex<float> diff_type;
narshu 25:143b19c1fb05 1151 typedef std::complex<float> float_type;
narshu 25:143b19c1fb05 1152 typedef std::complex<float> signed_type;
narshu 25:143b19c1fb05 1153
narshu 25:143b19c1fb05 1154 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 1155 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 1156
narshu 25:143b19c1fb05 1157 static inline
narshu 25:143b19c1fb05 1158 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 1159
narshu 25:143b19c1fb05 1160 static inline
narshu 25:143b19c1fb05 1161 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 1162
narshu 25:143b19c1fb05 1163 static inline
narshu 25:143b19c1fb05 1164 value_type conj(argument_type z) { return std::conj(z); }
narshu 25:143b19c1fb05 1165
narshu 25:143b19c1fb05 1166 static inline
narshu 25:143b19c1fb05 1167 base_type abs(argument_type z) { return std::abs(z); }
narshu 25:143b19c1fb05 1168
narshu 25:143b19c1fb05 1169 static inline
narshu 25:143b19c1fb05 1170 value_type sqrt(argument_type z) { return std::sqrt(z); }
narshu 25:143b19c1fb05 1171
narshu 25:143b19c1fb05 1172 static inline
narshu 25:143b19c1fb05 1173 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 1174 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 1175 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 1176 }
narshu 25:143b19c1fb05 1177
narshu 25:143b19c1fb05 1178 static inline
narshu 25:143b19c1fb05 1179 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 1180
narshu 25:143b19c1fb05 1181 static inline
narshu 25:143b19c1fb05 1182 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 1183 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 1184 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 1185 }
narshu 25:143b19c1fb05 1186
narshu 25:143b19c1fb05 1187 static inline
narshu 25:143b19c1fb05 1188 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 1189 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 1190 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1191 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 1192
narshu 25:143b19c1fb05 1193 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 1194 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 1195 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 1196 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 1197 }
narshu 25:143b19c1fb05 1198
narshu 25:143b19c1fb05 1199 enum { is_complex = true };
narshu 25:143b19c1fb05 1200
narshu 25:143b19c1fb05 1201 /** Complexity on operations. */
narshu 25:143b19c1fb05 1202 enum {
narshu 25:143b19c1fb05 1203 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 1204 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 1205 };
narshu 25:143b19c1fb05 1206 };
narshu 25:143b19c1fb05 1207
narshu 25:143b19c1fb05 1208
narshu 25:143b19c1fb05 1209 /**
narshu 25:143b19c1fb05 1210 * \class NumericTraits< std::complex<double> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 1211 * \brief Traits specialized for std::complex<double>.
narshu 25:143b19c1fb05 1212 */
narshu 25:143b19c1fb05 1213 template<>
narshu 25:143b19c1fb05 1214 struct NumericTraits< std::complex<double> > {
narshu 25:143b19c1fb05 1215 typedef double base_type;
narshu 25:143b19c1fb05 1216 typedef std::complex<double> value_type;
narshu 25:143b19c1fb05 1217 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 1218 typedef std::complex<long double> sum_type;
narshu 25:143b19c1fb05 1219 #else
narshu 25:143b19c1fb05 1220 typedef std::complex<double> sum_type;
narshu 25:143b19c1fb05 1221 #endif
narshu 25:143b19c1fb05 1222 typedef std::complex<double> diff_type;
narshu 25:143b19c1fb05 1223 typedef std::complex<double> float_type;
narshu 25:143b19c1fb05 1224 typedef std::complex<double> signed_type;
narshu 25:143b19c1fb05 1225
narshu 25:143b19c1fb05 1226 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 1227 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 1228
narshu 25:143b19c1fb05 1229 static inline
narshu 25:143b19c1fb05 1230 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 1231
narshu 25:143b19c1fb05 1232 static inline
narshu 25:143b19c1fb05 1233 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 1234
narshu 25:143b19c1fb05 1235 static inline
narshu 25:143b19c1fb05 1236 value_type conj(argument_type z) { return std::conj(z); }
narshu 25:143b19c1fb05 1237
narshu 25:143b19c1fb05 1238 static inline
narshu 25:143b19c1fb05 1239 base_type abs(argument_type z) { return std::abs(z); }
narshu 25:143b19c1fb05 1240
narshu 25:143b19c1fb05 1241 static inline
narshu 25:143b19c1fb05 1242 value_type sqrt(argument_type z) { return std::sqrt(z); }
narshu 25:143b19c1fb05 1243
narshu 25:143b19c1fb05 1244 static inline
narshu 25:143b19c1fb05 1245 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 1246 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 1247 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 1248 }
narshu 25:143b19c1fb05 1249
narshu 25:143b19c1fb05 1250 static inline
narshu 25:143b19c1fb05 1251 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 1252
narshu 25:143b19c1fb05 1253 static inline
narshu 25:143b19c1fb05 1254 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 1255 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 1256 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 1257 }
narshu 25:143b19c1fb05 1258
narshu 25:143b19c1fb05 1259 static inline
narshu 25:143b19c1fb05 1260 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 1261 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 1262 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1263 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 1264
narshu 25:143b19c1fb05 1265 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 1266 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 1267 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 1268 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 1269 }
narshu 25:143b19c1fb05 1270
narshu 25:143b19c1fb05 1271 enum { is_complex = true };
narshu 25:143b19c1fb05 1272
narshu 25:143b19c1fb05 1273 /** Complexity on operations. */
narshu 25:143b19c1fb05 1274 enum {
narshu 25:143b19c1fb05 1275 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 1276 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 1277 };
narshu 25:143b19c1fb05 1278 };
narshu 25:143b19c1fb05 1279
narshu 25:143b19c1fb05 1280
narshu 25:143b19c1fb05 1281 #if defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 1282 /**
narshu 25:143b19c1fb05 1283 * \class NumericTraits< std::complex<long double> > NumericTraits.h "tvmet/NumericTraits.h"
narshu 25:143b19c1fb05 1284 * \brief Traits specialized for std::complex<double>.
narshu 25:143b19c1fb05 1285 */
narshu 25:143b19c1fb05 1286 template<>
narshu 25:143b19c1fb05 1287 struct NumericTraits< std::complex<long double> > {
narshu 25:143b19c1fb05 1288 typedef long double base_type;
narshu 25:143b19c1fb05 1289 typedef std::complex<long double> value_type;
narshu 25:143b19c1fb05 1290 typedef std::complex<long double> sum_type;
narshu 25:143b19c1fb05 1291 typedef std::complex<long double> diff_type;
narshu 25:143b19c1fb05 1292 typedef std::complex<long double> float_type;
narshu 25:143b19c1fb05 1293 typedef std::complex<long double> signed_type;
narshu 25:143b19c1fb05 1294
narshu 25:143b19c1fb05 1295 typedef NumericTraits<value_type> traits_type;
narshu 25:143b19c1fb05 1296 typedef const value_type& argument_type;
narshu 25:143b19c1fb05 1297
narshu 25:143b19c1fb05 1298 static inline
narshu 25:143b19c1fb05 1299 base_type real(argument_type z) { return std::real(z); }
narshu 25:143b19c1fb05 1300
narshu 25:143b19c1fb05 1301 static inline
narshu 25:143b19c1fb05 1302 base_type imag(argument_type z) { return std::imag(z); }
narshu 25:143b19c1fb05 1303
narshu 25:143b19c1fb05 1304 static inline
narshu 25:143b19c1fb05 1305 value_type conj(argument_type z) { return std::conj(z); }
narshu 25:143b19c1fb05 1306
narshu 25:143b19c1fb05 1307 static inline
narshu 25:143b19c1fb05 1308 base_type abs(argument_type z) { return std::abs(z); }
narshu 25:143b19c1fb05 1309
narshu 25:143b19c1fb05 1310 static inline
narshu 25:143b19c1fb05 1311 value_type sqrt(argument_type z) { return std::sqrt(z); }
narshu 25:143b19c1fb05 1312
narshu 25:143b19c1fb05 1313 static inline
narshu 25:143b19c1fb05 1314 base_type norm_1(argument_type z) {
narshu 25:143b19c1fb05 1315 return NumericTraits<base_type>::abs((traits_type::real(z)))
narshu 25:143b19c1fb05 1316 + NumericTraits<base_type>::abs((traits_type::imag(z)));
narshu 25:143b19c1fb05 1317 }
narshu 25:143b19c1fb05 1318
narshu 25:143b19c1fb05 1319 static inline
narshu 25:143b19c1fb05 1320 base_type norm_2(argument_type z) { return traits_type::abs(z); }
narshu 25:143b19c1fb05 1321
narshu 25:143b19c1fb05 1322 static inline
narshu 25:143b19c1fb05 1323 base_type norm_inf(argument_type z) {
narshu 25:143b19c1fb05 1324 return std::max(NumericTraits<base_type>::abs(traits_type::real(z)),
narshu 25:143b19c1fb05 1325 NumericTraits<base_type>::abs(traits_type::imag(z)));
narshu 25:143b19c1fb05 1326 }
narshu 25:143b19c1fb05 1327
narshu 25:143b19c1fb05 1328 static inline
narshu 25:143b19c1fb05 1329 bool equals(argument_type lhs, argument_type rhs) {
narshu 25:143b19c1fb05 1330 static base_type sqrt_epsilon(
narshu 25:143b19c1fb05 1331 NumericTraits<base_type>::sqrt(
narshu 25:143b19c1fb05 1332 std::numeric_limits<base_type>::epsilon()));
narshu 25:143b19c1fb05 1333
narshu 25:143b19c1fb05 1334 return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon *
narshu 25:143b19c1fb05 1335 std::max(std::max(traits_type::norm_inf(lhs),
narshu 25:143b19c1fb05 1336 traits_type::norm_inf(rhs)),
narshu 25:143b19c1fb05 1337 std::numeric_limits<base_type>::min());
narshu 25:143b19c1fb05 1338 }
narshu 25:143b19c1fb05 1339
narshu 25:143b19c1fb05 1340 enum { is_complex = true };
narshu 25:143b19c1fb05 1341
narshu 25:143b19c1fb05 1342 /** Complexity on operations. */
narshu 25:143b19c1fb05 1343 enum {
narshu 25:143b19c1fb05 1344 ops_plus = 2, /**< Complexity on plus/minus ops. */
narshu 25:143b19c1fb05 1345 ops_muls = 6 /**< Complexity on multiplications. */
narshu 25:143b19c1fb05 1346 };
narshu 25:143b19c1fb05 1347 };
narshu 25:143b19c1fb05 1348 #endif // defined(TVMET_HAVE_LONG_DOUBLE)
narshu 25:143b19c1fb05 1349
narshu 25:143b19c1fb05 1350
narshu 25:143b19c1fb05 1351 #endif // defined(TVMET_HAVE_COMPLEX)
narshu 25:143b19c1fb05 1352
narshu 25:143b19c1fb05 1353
narshu 25:143b19c1fb05 1354 } // namespace tvmet
narshu 25:143b19c1fb05 1355
narshu 25:143b19c1fb05 1356
narshu 25:143b19c1fb05 1357 #endif // TVMET_NUMERIC_TRAITS_H
narshu 25:143b19c1fb05 1358
narshu 25:143b19c1fb05 1359
narshu 25:143b19c1fb05 1360 // Local Variables:
narshu 25:143b19c1fb05 1361 // mode:C++
narshu 25:143b19c1fb05 1362 // tab-width:8
narshu 25:143b19c1fb05 1363 // End: