GNU Octave  4.2.1
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-2017 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 "octave-config.h"
28 
29 #include <complex>
30 
31 typedef std::complex<double> Complex;
32 typedef std::complex<float> FloatComplex;
33 
34 // For complex-complex and complex-real comparisons, we use the following
35 // ordering: compare absolute values first; if they match, compare phase angles.
36 // This is partially inconsistent with M*b, which compares complex numbers only
37 // by their real parts; OTOH, it uses the same definition for max/min and sort.
38 // The abs/arg comparison is definitely more useful (the other one is emulated
39 // rather trivially), so let's be consistent and use that all over.
40 
41 // The standard C library function arg() returns [-pi,pi], which creates a
42 // non-unique representation for numbers along the negative real axis branch
43 // cut. Change this to principal value (-pi,pi] by mapping -pi to pi.
44 
45 #define DEF_COMPLEXR_COMP(OP, OPS) \
46  template <typename T> \
47  inline bool operator OP (const std::complex<T>& a, const std::complex<T>& b) \
48  { \
49  OCTAVE_FLOAT_TRUNCATE const T ax = std::abs (a); \
50  OCTAVE_FLOAT_TRUNCATE const T bx = std::abs (b); \
51  if (ax == bx) \
52  { \
53  OCTAVE_FLOAT_TRUNCATE const T ay = std::arg (a); \
54  OCTAVE_FLOAT_TRUNCATE const T by = std::arg (b); \
55  if (ay == static_cast<T> (-M_PI)) \
56  { \
57  if (by != static_cast<T> (-M_PI)) \
58  return static_cast<T> (M_PI) OP by; \
59  } \
60  else if (by == static_cast<T> (-M_PI)) \
61  { \
62  return ay OP static_cast<T> (M_PI); \
63  } \
64  return ay OP by; \
65  } \
66  else \
67  return ax OPS bx; \
68  } \
69  template <typename T> \
70  inline bool operator OP (const std::complex<T>& a, T b) \
71  { \
72  OCTAVE_FLOAT_TRUNCATE const T ax = std::abs (a); \
73  OCTAVE_FLOAT_TRUNCATE const T bx = std::abs (b); \
74  if (ax == bx) \
75  { \
76  OCTAVE_FLOAT_TRUNCATE const T ay = std::arg (a); \
77  if (ay == static_cast<T> (-M_PI)) \
78  return static_cast<T> (M_PI) OP 0; \
79  return ay OP 0; \
80  } \
81  else \
82  return ax OPS bx; \
83  } \
84  template <typename T> \
85  inline bool operator OP (T a, const std::complex<T>& b) \
86  { \
87  OCTAVE_FLOAT_TRUNCATE const T ax = std::abs (a); \
88  OCTAVE_FLOAT_TRUNCATE const T bx = std::abs (b); \
89  if (ax == bx) \
90  { \
91  OCTAVE_FLOAT_TRUNCATE const T by = std::arg (b); \
92  if (by == static_cast<T> (-M_PI)) \
93  return 0 OP static_cast<T> (M_PI); \
94  return 0 OP by; \
95  } \
96  else \
97  return ax OPS bx; \
98  }
99 
104 
105 #endif
#define DEF_COMPLEXR_COMP(OP, OPS)
Definition: oct-cmplx.h:45
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31