oct-cmplx.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1995-2012 John W. Eaton
00004 Copyright (C) 2009 VZLU Prague, a.s.
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_oct_cmplx_h)
00025 #define octave_oct_cmplx_h 1
00026 
00027 #include <complex>
00028 
00029 typedef std::complex<double> Complex;
00030 typedef std::complex<float> FloatComplex;
00031 
00032 // For complex-complex and complex-real comparisons, we use the following ordering:
00033 // compare absolute values first; if they match, compare phase angles.
00034 // This is partially inconsistent with M*b, which compares complex numbers only
00035 // by their real parts; OTOH, it uses the same definition for max/min and sort.
00036 // The abs/arg comparison is definitely more useful (the other one is emulated rather
00037 // trivially), so let's be consistent and use that all over.
00038 
00039 #define DEF_COMPLEXR_COMP(OP, OPS) \
00040 template <class T> \
00041 inline bool operator OP (const std::complex<T>& a, const std::complex<T>& b) \
00042 { \
00043   FLOAT_TRUNCATE const T ax = std::abs (a), bx = std::abs (b); \
00044   if (ax == bx) \
00045     { \
00046       FLOAT_TRUNCATE const T ay = std::arg (a), by = std::arg (b); \
00047       return ay OP by; \
00048     } \
00049   else \
00050     return ax OPS bx; \
00051 } \
00052 template <class T> \
00053 inline bool operator OP (const std::complex<T>& a, T b) \
00054 { \
00055   FLOAT_TRUNCATE const T ax = std::abs (a), bx = std::abs (b); \
00056   if (ax == bx) \
00057     { \
00058       FLOAT_TRUNCATE const T ay = std::arg (a); \
00059       return ay OP 0; \
00060     } \
00061   else \
00062     return ax OPS bx; \
00063 } \
00064 template <class T> \
00065 inline bool operator OP (T a, const std::complex<T>& b) \
00066 { \
00067   FLOAT_TRUNCATE const T ax = std::abs (a), bx = std::abs (b); \
00068   if (ax == bx) \
00069     { \
00070       FLOAT_TRUNCATE const T by = std::arg (b); \
00071       return 0 OP by; \
00072     } \
00073   else \
00074     return ax OPS bx; \
00075 }
00076 
00077 DEF_COMPLEXR_COMP (>, >)
00078 DEF_COMPLEXR_COMP (<, <)
00079 DEF_COMPLEXR_COMP (<=, <)
00080 DEF_COMPLEXR_COMP (>=, >)
00081 
00082 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines