From f1bd09024b66f2e56f52b8a84babc3cb922b4447 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Mon, 23 Jul 2007 07:38:07 +0000 Subject: [PATCH] Big overhaul and simplification of the NumericTraits system. Add a utility header for random numbers (might be merged into NumericTraits) --- tvmet-1.7.1/include/tvmet/NumericTraits.h | 418 ++++------------------ tvmet-1.7.1/include/tvmet/util/Random.h | 88 +++++ 2 files changed, 163 insertions(+), 343 deletions(-) create mode 100644 tvmet-1.7.1/include/tvmet/util/Random.h diff --git a/tvmet-1.7.1/include/tvmet/NumericTraits.h b/tvmet-1.7.1/include/tvmet/NumericTraits.h index 4c3d6a277..53540fcdc 100644 --- a/tvmet-1.7.1/include/tvmet/NumericTraits.h +++ b/tvmet-1.7.1/include/tvmet/NumericTraits.h @@ -27,193 +27,79 @@ #if defined(EIGEN_USE_COMPLEX) # include #endif + #include #include #include - +#include namespace tvmet { - /** * \class NumericTraits NumericTraits.h "tvmet/NumericTraits.h" * \brief Traits for integral types for operations. * * For each type we have to specialize this traits. - * - * \note Keep in mind that the long types long long and long double doesn't - * have traits. This is due to the sum_type. We can't give a guarantee - * that there is a type of holding the sum. Therefore using this traits - * is only safe if you have long long resp. long double types by - * working on long ints and doubles. Otherwise you will get not expected - * result for some circumstances. Anyway, you can use big integer/float - * libraries and specialize the traits by your own. - * - * \todo The abs function of complex can have an - * overrun due to numeric computation. Solve it (someone - * using value_type=long here?) */ template struct NumericTraits { - typedef T base_type; - typedef T value_type; - typedef value_type sum_type; - typedef value_type diff_type; - typedef value_type float_type; - typedef value_type signed_type; - - typedef NumericTraits traits_type; - typedef const value_type& argument_type; + typedef T real_type; + typedef T value_type; + typedef T float_type; + typedef const T & argument_type; static inline - base_type real(argument_type x); + real_type real(argument_type x); static inline - base_type imag(argument_type x); + real_type imag(argument_type x); static inline value_type conj(argument_type x); static inline - base_type abs(argument_type x); + real_type abs(argument_type x); static inline value_type sqrt(argument_type x); - static inline - base_type norm_1(argument_type x) { - return NumericTraits::abs(traits_type::real(x)) - + NumericTraits::abs(traits_type::imag(x)); - } - - static inline - base_type norm_2(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_inf(argument_type x) { - return std::max(NumericTraits::abs(traits_type::real(x)), - NumericTraits::abs(traits_type::imag(x))); - } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - static base_type sqrt_epsilon( - NumericTraits::sqrt( - std::numeric_limits::epsilon())); - - return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon * - std::max(std::max(traits_type::norm_inf(lhs), - traits_type::norm_inf(rhs)), - std::numeric_limits::min()); - } + enum{ is_complex = false }; }; - /* - * numeric traits for standard types + * numeric traits for built-in types */ - -/** - * \class NumericTraits NumericTraits.h "tvmet/NumericTraits.h" - * \brief Traits specialized for char. - */ -template<> -struct NumericTraits { - typedef char value_type; - typedef value_type base_type; - typedef long sum_type; - typedef int diff_type; - typedef float float_type; - typedef char signed_type; - - typedef NumericTraits traits_type; - typedef value_type argument_type; - - static inline - base_type real(argument_type x) { return x; } - - static inline - base_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } - - static inline - value_type conj(argument_type x) { return x; } - - static inline - base_type abs(argument_type x) { return std::abs(x); } - - static inline - value_type sqrt(argument_type x) { - return static_cast(std::sqrt(static_cast(x))); - } - - static inline - base_type norm_1(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_2(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_inf(argument_type x) { return traits_type::abs(x); } - - static inline - bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; } - - enum { is_complex = false }; - - /** Complexity on operations. */ - enum { - ops_plus = 1, /**< Complexity on plus/minus ops. */ - ops_muls = 1 /**< Complexity on multiplications. */ - }; -}; - - /** * \class NumericTraits NumericTraits.h "tvmet/NumericTraits.h" * \brief Traits specialized for int. */ template<> struct NumericTraits { - typedef int value_type; - typedef value_type base_type; - typedef long sum_type; - typedef int diff_type; - typedef double float_type; - typedef int signed_type; - - typedef NumericTraits traits_type; - typedef value_type argument_type; + typedef int value_type; + typedef value_type real_type; + typedef double float_type; + typedef value_type argument_type; static inline - base_type real(argument_type x) { return x; } + real_type real(argument_type x) { return x; } static inline - base_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } + real_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } static inline value_type conj(argument_type x) { return x; } - static inline - base_type abs(argument_type x) { return std::abs(x); } - static inline value_type sqrt(argument_type x) { return static_cast(std::sqrt(static_cast(x))); } - + static inline - base_type norm_1(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_2(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_inf(argument_type x) { return traits_type::abs(x); } - - static inline - bool equals(argument_type lhs, argument_type rhs) { return lhs == rhs; } + value_type abs(argument_type x) { + return std::abs(x); + } enum { is_complex = false }; @@ -224,57 +110,34 @@ struct NumericTraits { }; }; - /** * \class NumericTraits NumericTraits.h "tvmet/NumericTraits.h" * \brief Traits specialized for float. */ template<> struct NumericTraits { - typedef float value_type; - typedef value_type base_type; - typedef double sum_type; - typedef float diff_type; - typedef float float_type; - typedef float signed_type; - - typedef NumericTraits traits_type; - typedef value_type argument_type; + typedef float value_type; + typedef value_type real_type; + typedef value_type float_type; + typedef value_type argument_type; static inline - base_type real(argument_type x) { return x; } + real_type real(argument_type x) { return x; } static inline - base_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } + real_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } static inline value_type conj(argument_type x) { return x; } static inline - base_type abs(argument_type x) { return std::abs(x); } + value_type sqrt(argument_type x) { + return std::sqrt(x); + } static inline - value_type sqrt(argument_type x) { return std::sqrt(x); } - - static inline - base_type norm_1(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_2(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_inf(argument_type x) { return traits_type::abs(x); } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - static base_type sqrt_epsilon( - NumericTraits::sqrt( - std::numeric_limits::epsilon())); - - return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon * - std::max(std::max(traits_type::norm_inf(lhs), - traits_type::norm_inf(rhs)), - std::numeric_limits::min()); + value_type abs(argument_type x) { + return std::abs(x); } enum { is_complex = false }; @@ -293,50 +156,28 @@ struct NumericTraits { */ template<> struct NumericTraits { - typedef double value_type; - typedef value_type base_type; - typedef double sum_type; - typedef double diff_type; - typedef double float_type; - typedef double signed_type; - - typedef NumericTraits traits_type; - typedef value_type argument_type; + typedef double value_type; + typedef value_type real_type; + typedef value_type float_type; + typedef value_type argument_type; static inline - base_type real(argument_type x) { return x; } + real_type real(argument_type x) { return x; } static inline - base_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } + real_type imag(argument_type x) { TVMET_UNUSED(x); return 0; } static inline value_type conj(argument_type x) { return x; } static inline - base_type abs(argument_type x) { return std::abs(x); } + value_type sqrt(argument_type x) { + return std::sqrt(x); + } static inline - value_type sqrt(argument_type x) { return std::sqrt(x); } - - static inline - base_type norm_1(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_2(argument_type x) { return traits_type::abs(x); } - - static inline - base_type norm_inf(argument_type x) { return traits_type::abs(x); } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - static base_type sqrt_epsilon( - NumericTraits::sqrt( - std::numeric_limits::epsilon())); - - return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon * - std::max(std::max(traits_type::norm_inf(lhs), - traits_type::norm_inf(rhs)), - std::numeric_limits::min()); + value_type abs(argument_type x) { + return std::abs(x); } enum { is_complex = false }; @@ -360,75 +201,30 @@ struct NumericTraits { */ template<> struct NumericTraits< std::complex > { - typedef int base_type; - typedef std::complex value_type; - typedef std::complex sum_type; - typedef std::complex diff_type; - typedef std::complex float_type; - typedef std::complex signed_type; - - typedef NumericTraits traits_type; - typedef const value_type& argument_type; + typedef std::complex value_type; + typedef int real_type; + typedef std::complex float_type; + typedef const value_type& argument_type; static inline - base_type real(argument_type z) { return std::real(z); } + real_type real(argument_type z) { return std::real(z); } static inline - base_type imag(argument_type z) { return std::imag(z); } + real_type imag(argument_type z) { return std::imag(z); } static inline value_type conj(argument_type z) { return std::conj(z); } static inline - base_type abs(argument_type z) { - base_type x = z.real(); - base_type y = z.imag(); - - // XXX probably case of overrun; header complex uses scaling - return static_cast(NumericTraits::sqrt(x * x + y * y)); - } - - static /* inline */ - value_type sqrt(argument_type z) { - // borrowed and adapted from header complex - base_type x = z.real(); - base_type y = z.imag(); - - if(x == base_type()) { - base_type t = NumericTraits::sqrt( - NumericTraits::abs(y) / 2); - return value_type(t, y < base_type() ? -t : t); - } - else { - base_type t = NumericTraits::sqrt( - 2 * (traits_type::abs(z) - + NumericTraits::abs(x))); - base_type u = t / 2; - return x > base_type() - ? value_type(u, y / t) - : value_type(NumericTraits::abs(y) / t, y < base_type() ? -u : u); - } + real_type abs(argument_type z) { + // the use of ceil() guarantees e.g. that abs(real(x)) <= abs(x), + // and that abs(x)==0 if and only if x==0. + return static_cast(std::ceil(std::abs(static_cast(x)))); } static inline - base_type norm_1(argument_type z) { - return NumericTraits::abs((traits_type::real(z))) - + NumericTraits::abs((traits_type::imag(z))); - } - - static inline - base_type norm_2(argument_type z) { return traits_type::abs(z); } - - static inline - base_type norm_inf(argument_type z) { - return std::max(NumericTraits::abs(traits_type::real(z)), - NumericTraits::abs(traits_type::imag(z))); - } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - return (traits_type::real(lhs) == traits_type::real(rhs)) - && (traits_type::imag(lhs) == traits_type::imag(rhs)); + value_type sqrt(argument_type x) { + return static_cast(std::sqrt(static_cast(x))); } enum { is_complex = true }; @@ -447,56 +243,28 @@ struct NumericTraits< std::complex > { */ template<> struct NumericTraits< std::complex > { - typedef float base_type; - typedef std::complex value_type; - typedef std::complex sum_type; - typedef std::complex diff_type; - typedef std::complex float_type; - typedef std::complex signed_type; - - typedef NumericTraits traits_type; - typedef const value_type& argument_type; + typedef std::complex value_type; + typedef float real_type; + typedef value_type float_type; + typedef const value_type& argument_type; static inline - base_type real(argument_type z) { return std::real(z); } + real_type real(argument_type z) { return std::real(z); } static inline - base_type imag(argument_type z) { return std::imag(z); } + real_type imag(argument_type z) { return std::imag(z); } static inline value_type conj(argument_type z) { return std::conj(z); } static inline - base_type abs(argument_type z) { return std::abs(z); } - - static inline - value_type sqrt(argument_type z) { return std::sqrt(z); } - - static inline - base_type norm_1(argument_type z) { - return NumericTraits::abs((traits_type::real(z))) - + NumericTraits::abs((traits_type::imag(z))); + value_type sqrt(argument_type x) { + return std::sqrt(x); } static inline - base_type norm_2(argument_type z) { return traits_type::abs(z); } - - static inline - base_type norm_inf(argument_type z) { - return std::max(NumericTraits::abs(traits_type::real(z)), - NumericTraits::abs(traits_type::imag(z))); - } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - static base_type sqrt_epsilon( - NumericTraits::sqrt( - std::numeric_limits::epsilon())); - - return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon * - std::max(std::max(traits_type::norm_inf(lhs), - traits_type::norm_inf(rhs)), - std::numeric_limits::min()); + value_type abs(argument_type x) { + return std::abs(x); } enum { is_complex = true }; @@ -515,56 +283,28 @@ struct NumericTraits< std::complex > { */ template<> struct NumericTraits< std::complex > { - typedef double base_type; - typedef std::complex value_type; - typedef std::complex sum_type; - typedef std::complex diff_type; - typedef std::complex float_type; - typedef std::complex signed_type; - - typedef NumericTraits traits_type; - typedef const value_type& argument_type; + typedef std::complex value_type; + typedef double real_type; + typedef value_type float_type; + typedef const value_type& argument_type; static inline - base_type real(argument_type z) { return std::real(z); } + real_type real(argument_type z) { return std::real(z); } static inline - base_type imag(argument_type z) { return std::imag(z); } + real_type imag(argument_type z) { return std::imag(z); } static inline value_type conj(argument_type z) { return std::conj(z); } static inline - base_type abs(argument_type z) { return std::abs(z); } - - static inline - value_type sqrt(argument_type z) { return std::sqrt(z); } - - static inline - base_type norm_1(argument_type z) { - return NumericTraits::abs((traits_type::real(z))) - + NumericTraits::abs((traits_type::imag(z))); + value_type sqrt(argument_type x) { + return std::sqrt(x); } static inline - base_type norm_2(argument_type z) { return traits_type::abs(z); } - - static inline - base_type norm_inf(argument_type z) { - return std::max(NumericTraits::abs(traits_type::real(z)), - NumericTraits::abs(traits_type::imag(z))); - } - - static inline - bool equals(argument_type lhs, argument_type rhs) { - static base_type sqrt_epsilon( - NumericTraits::sqrt( - std::numeric_limits::epsilon())); - - return traits_type::norm_inf(lhs - rhs) < sqrt_epsilon * - std::max(std::max(traits_type::norm_inf(lhs), - traits_type::norm_inf(rhs)), - std::numeric_limits::min()); + value_type abs(argument_type x) { + return std::abs(x); } enum { is_complex = true }; @@ -576,16 +316,8 @@ struct NumericTraits< std::complex > { }; }; - #endif // defined(EIGEN_USE_COMPLEX) - } // namespace tvmet - #endif // TVMET_NUMERIC_TRAITS_H - - -// Local Variables: -// mode:C++ -// End: diff --git a/tvmet-1.7.1/include/tvmet/util/Random.h b/tvmet-1.7.1/include/tvmet/util/Random.h new file mode 100644 index 000000000..5f62b4729 --- /dev/null +++ b/tvmet-1.7.1/include/tvmet/util/Random.h @@ -0,0 +1,88 @@ +/* This file is part of Eigen, a C++ template library for linear algebra + * Copyright (C) 2006-2007 Benoit Jacob + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: SelfTest.cc,v 1.1 2004/04/24 11:55:15 opetzold Exp $ + */ + +#ifndef TVMET_UTIL_RANDOM_H +#define TVMET_UTIL_RANDOM_H + +#ifdef __GNUC__ +# if __GNUC__>=4 +# define EIGEN_WITH_GCC_4_OR_LATER +# endif +#endif + +#include + +#ifdef EIGEN_USE_COMPLEX +#include +#endif + +namespace tvmet { + +namespace util { + +/** Stores in x a random int between -RAND_MAX/2 and RAND_MAX/2 */ +inline void pickRandom( int & x ) +{ + x = rand() - RAND_MAX / 2; +} + + +/** Stores in x a random float between -1.0 and 1.0 */ +inline void pickRandom( float & x ) +{ + x = 2.0f * rand() / RAND_MAX - 1.0f; +} + +/** Stores in x a random double between -1.0 and 1.0 */ +inline void pickRandom( double & x ) +{ + x = 2.0 * rand() / RAND_MAX - 1.0; +} + +#ifdef EIGEN_USE_COMPLEX +/** Stores in the real and imaginary parts of x + * random values between -1.0 and 1.0 */ +template void pickRandom( std::complex & x ) +{ +#ifdef EIGEN_WITH_GCC_4_OR_LATER + pickRandom( x.real() ); + pickRandom( x.imag() ); +#else // workaround by David Faure for MacOS 10.3 and GCC 3.3, commit 630812 + T r = x.real(); + T i = x.imag(); + pickRandom( r ); + pickRandom( i ); + x = std::complex(r,i); +#endif +} +#endif // EIGEN_USE_COMPLEX + +template T someRandom() +{ + T t; + pickRandom(t); + return t; +} + +} // namespace util + +} // namespace tvmet + +#endif // TVMET_UTIL_RANDOM_H