[35] | 1 | /*! |
---|
| 2 | * \file |
---|
| 3 | * \brief Miscellaneous functions - header file |
---|
| 4 | * \author Tony Ottosson, Adam Piatyszek and Conrad Sanderson |
---|
| 5 | * |
---|
| 6 | * ------------------------------------------------------------------------- |
---|
| 7 | * |
---|
| 8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
---|
| 9 | * and communications classes and functions |
---|
| 10 | * |
---|
| 11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
---|
| 12 | * |
---|
| 13 | * This program is free software; you can redistribute it and/or modify |
---|
| 14 | * it under the terms of the GNU General Public License as published by |
---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 16 | * (at your option) any later version. |
---|
| 17 | * |
---|
| 18 | * This program is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this program; if not, write to the Free Software |
---|
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 26 | * |
---|
| 27 | * ------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #ifndef MISC_H |
---|
| 31 | #define MISC_H |
---|
| 32 | |
---|
| 33 | #ifndef _MSC_VER |
---|
| 34 | # include <itpp/config.h> |
---|
| 35 | #else |
---|
| 36 | # include <itpp/config_msvc.h> |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | #include <complex> |
---|
| 40 | #include <string> |
---|
| 41 | #include <limits> |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | namespace std { |
---|
| 45 | |
---|
| 46 | //! \cond |
---|
| 47 | |
---|
| 48 | #ifndef HAVE_STD_ISINF |
---|
| 49 | #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF) |
---|
| 50 | inline int isinf(double x) { return ::isinf(x); } |
---|
| 51 | #elif defined(FPCLASS) |
---|
| 52 | inline int isinf(double x) |
---|
| 53 | { |
---|
| 54 | if (::fpclass(a) == FP_NINF) return -1; |
---|
| 55 | else if (::fpclass(a) == FP_PINF) return 1; |
---|
| 56 | else return 0; |
---|
| 57 | } |
---|
| 58 | #else |
---|
| 59 | inline int isinf(double x) |
---|
| 60 | { |
---|
| 61 | if ((x == x) && ((x - x) != 0.0)) return (x < 0.0 ? -1 : 1); |
---|
| 62 | else return 0; |
---|
| 63 | } |
---|
| 64 | #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF) |
---|
| 65 | # define HAVE_STD_ISINF 1 |
---|
| 66 | #endif // #ifndef HAVE_STD_ISINF |
---|
| 67 | |
---|
| 68 | #ifndef HAVE_STD_ISNAN |
---|
| 69 | #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN) |
---|
| 70 | inline int isnan(double x) { return ::isnan(x); } |
---|
| 71 | #else |
---|
| 72 | inline int isnan(double x) { return ((x != x) ? 1 : 0); } |
---|
| 73 | #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN) |
---|
| 74 | # define HAVE_STD_ISNAN 1 |
---|
| 75 | #endif // #ifndef HAVE_STD_ISNAN |
---|
| 76 | |
---|
| 77 | #ifndef HAVE_STD_ISFINITE |
---|
| 78 | #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE) |
---|
| 79 | inline int isfinite(double x) { return ::isfinite(x); } |
---|
| 80 | #elif defined(HAVE_FINITE) |
---|
| 81 | inline int isfinite(double x) { return ::finite(x); } |
---|
| 82 | #else |
---|
| 83 | inline int isfinite(double x) |
---|
| 84 | { |
---|
| 85 | return ((!std::isnan(x) && !std::isinf(x)) ? 1 : 0); |
---|
| 86 | } |
---|
| 87 | #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE) |
---|
| 88 | # define HAVE_STD_ISFINITE 1 |
---|
| 89 | #endif // #ifndef HAVE_STD_ISFINITE |
---|
| 90 | |
---|
| 91 | //! \endcond |
---|
| 92 | |
---|
| 93 | //! Output stream operator for complex numbers |
---|
| 94 | template <class T> |
---|
| 95 | std::ostream& operator<<(std::ostream &os, const std::complex<T> &x) |
---|
| 96 | { |
---|
| 97 | os << x.real(); |
---|
| 98 | ios::fmtflags saved_format = os.setf(ios::showpos); |
---|
| 99 | os << x.imag(); |
---|
| 100 | os.setf(saved_format, ios::showpos); |
---|
| 101 | return os << 'i'; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | //! Input stream operator for complex numbers |
---|
| 105 | template <class T> |
---|
| 106 | std::istream& operator>>(std::istream &is, std::complex<T> &x) |
---|
| 107 | { |
---|
| 108 | T re, im; |
---|
| 109 | char c; |
---|
| 110 | is >> c; |
---|
| 111 | if (c == '(') { |
---|
| 112 | is >> re >> c; |
---|
| 113 | if (c == ',') { |
---|
| 114 | is >> im >> c; |
---|
| 115 | if (c == ')') { |
---|
| 116 | x = complex<T>(re, im); |
---|
| 117 | } else { |
---|
| 118 | is.setstate(ios_base::failbit); |
---|
| 119 | } |
---|
| 120 | } else if (c == ')') { |
---|
| 121 | x = complex<T>(re, T(0)); |
---|
| 122 | } else { |
---|
| 123 | is.setstate(ios_base::failbit); |
---|
| 124 | } |
---|
| 125 | } else { |
---|
| 126 | is.putback(c); |
---|
| 127 | is >> re; |
---|
| 128 | if (!is.eof() && ((c = is.peek()) == '+' || c == '-')) { |
---|
| 129 | is >> im >> c; |
---|
| 130 | if (c == 'i') { |
---|
| 131 | x = complex<T>(re, im); |
---|
| 132 | } else { |
---|
| 133 | is.setstate(ios_base::failbit); |
---|
| 134 | } |
---|
| 135 | } else { |
---|
| 136 | x = complex<T>(re, T(0)); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | return is; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | } // namespace std |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | namespace itpp { |
---|
| 146 | |
---|
| 147 | //! Constant Pi |
---|
| 148 | const double pi = 3.14159265358979323846; |
---|
| 149 | |
---|
| 150 | //! Constant 2*Pi |
---|
| 151 | const double m_2pi = 2 * pi; |
---|
| 152 | |
---|
| 153 | //! Constant eps |
---|
| 154 | const double eps = std::numeric_limits<double>::epsilon(); |
---|
| 155 | |
---|
| 156 | //! \addtogroup miscfunc |
---|
| 157 | //!@{ |
---|
| 158 | |
---|
| 159 | //! Return true if x is an integer |
---|
| 160 | inline bool is_int(double x) |
---|
| 161 | { |
---|
| 162 | double dummy; |
---|
| 163 | return (modf(x, &dummy) == 0.0); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | //! Return true if x is an even integer |
---|
| 167 | inline bool is_even(int x) { return ((x&1) == 0); } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | //! Returns IT++ library version number, e.g. "3.7.1". |
---|
| 171 | std::string itpp_version(); |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | //! Returns machine endianness: big-endian = true; little-endian = false |
---|
| 175 | bool check_big_endianness(); |
---|
| 176 | |
---|
| 177 | //!@} |
---|
| 178 | |
---|
| 179 | } //namespace itpp |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | #endif // #ifndef MISC_H |
---|