GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-cmplx.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1995-2015 John W. Eaton
4 Copyright (C) 2009 VZLU Prague, a.s.
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_oct_cmplx_h)
25 #define octave_oct_cmplx_h 1
26 
27 #include <complex>
28 
29 typedef std::complex<double> Complex;
30 typedef std::complex<float> FloatComplex;
31 
32 // For complex-complex and complex-real comparisons, we use the following
33 // ordering: compare absolute values first; if they match, compare phase angles.
34 // This is partially inconsistent with M*b, which compares complex numbers only
35 // by their real parts; OTOH, it uses the same definition for max/min and sort.
36 // The abs/arg comparison is definitely more useful (the other one is emulated
37 // rather trivially), so let's be consistent and use that all over.
38 
39 // The standard C library function arg() returns [-pi,pi], which creates a
40 // non-unique representation for numbers along the negative real axis branch
41 // cut. Change this to principal value (-pi,pi] by mapping -pi to pi.
42 
43 #define DEF_COMPLEXR_COMP(OP, OPS) \
44 template <class T> \
45 inline bool operator OP (const std::complex<T>& a, const std::complex<T>& b) \
46 { \
47  FLOAT_TRUNCATE const T ax = std::abs (a); \
48  FLOAT_TRUNCATE const T bx = std::abs (b); \
49  if (ax == bx) \
50  { \
51  FLOAT_TRUNCATE const T ay = std::arg (a); \
52  FLOAT_TRUNCATE const T by = std::arg (b); \
53  if (ay == static_cast<T> (-M_PI)) \
54  { \
55  if (by != static_cast<T> (-M_PI)) \
56  return static_cast<T> (M_PI) OP by; \
57  } \
58  else if (by == static_cast<T> (-M_PI)) \
59  { \
60  return ay OP static_cast<T> (M_PI); \
61  } \
62  return ay OP by; \
63  } \
64  else \
65  return ax OPS bx; \
66 } \
67 template <class T> \
68 inline bool operator OP (const std::complex<T>& a, T b) \
69 { \
70  FLOAT_TRUNCATE const T ax = std::abs (a); \
71  FLOAT_TRUNCATE const T bx = std::abs (b); \
72  if (ax == bx) \
73  { \
74  FLOAT_TRUNCATE const T ay = std::arg (a); \
75  if (ay == static_cast<T> (-M_PI)) \
76  return static_cast<T> (M_PI) OP 0; \
77  return ay OP 0; \
78  } \
79  else \
80  return ax OPS bx; \
81 } \
82 template <class T> \
83 inline bool operator OP (T a, const std::complex<T>& b) \
84 { \
85  FLOAT_TRUNCATE const T ax = std::abs (a); \
86  FLOAT_TRUNCATE const T bx = std::abs (b); \
87  if (ax == bx) \
88  { \
89  FLOAT_TRUNCATE const T by = std::arg (b); \
90  if (by == static_cast<T> (-M_PI)) \
91  return 0 OP static_cast<T> (M_PI); \
92  return 0 OP by; \
93  } \
94  else \
95  return ax OPS bx; \
96 }
97 
102 
103 #endif
#define DEF_COMPLEXR_COMP(OP, OPS)
Definition: oct-cmplx.h:43
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
std::complex< double > Complex
Definition: oct-cmplx.h:29