complex.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::complex
29 
30 Description
31  A complex number, similar to the C++ complex type.
32 
33 SourceFiles
34  complexI.H
35  complex.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef complex_H
40 #define complex_H
41 
42 #include "scalar.H"
43 #include "word.H"
44 #include "zero.H"
45 #include "contiguous.H"
46 #include <complex>
47 #include <type_traits>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 class complex;
56 
57 inline scalar magSqr(const complex&);
58 inline scalar mag(const complex&);
59 inline complex sqr(const complex&);
60 inline const complex& min(const complex&, const complex&);
61 inline const complex& max(const complex&, const complex&);
62 inline complex limit(const complex&, const complex&);
63 inline const complex& sum(const complex&);
64 inline complex operator-(const complex&);
65 inline complex operator+(const complex&, const complex&);
66 inline complex operator+(const complex&, const scalar);
67 inline complex operator+(const scalar, const complex&);
68 inline complex operator-(const complex&, const complex&);
69 inline complex operator-(const complex&, const scalar);
70 inline complex operator-(const scalar, const complex&);
71 inline complex operator*(const complex&, const complex&);
72 inline complex operator*(const complex&, const scalar);
73 inline complex operator*(const scalar, const complex&);
74 inline complex operator/(const complex&, const complex&);
75 inline complex operator/(const complex&, const scalar);
76 inline complex operator/(const scalar, const complex&);
77 
78 
79 /*---------------------------------------------------------------------------*\
80  Class complex Declaration
81 \*---------------------------------------------------------------------------*/
82 
83 class complex
84 {
85  // Private Data
86 
87  //- Real and imaginary parts
88  scalar re, im;
89 
90 
91 public:
92 
93  // Constructors
94 
95  //- Construct null as zero-initialized
96  inline constexpr complex() noexcept;
97 
98  //- Default copy constructor
99  complex(const complex&) = default;
100 
101  //- Construct zero-initialized from zero class
102  inline constexpr complex(const Foam::zero) noexcept;
103 
104  //- Construct from real component
105  inline explicit constexpr complex(const scalar r) noexcept;
106 
107  //- Construct from real and imaginary parts
108  inline constexpr complex(const scalar r, const scalar i) noexcept;
109 
110  //- Construct from std::complex
111  inline complex(const std::complex<float>& c);
112 
113  //- Construct from std::complex
114  inline complex(const std::complex<double>& c);
115 
116  //- Construct from Istream
117  explicit complex(Istream& is);
118 
119 
120  // Member Functions
121 
122  // STL getter/setter
123 
124  //- Real part of complex number - STL naming
125  constexpr scalar real() const
126  {
127  return re;
128  }
129 
130  //- Imaginary part of complex number - STL naming
131  constexpr scalar imag() const
132  {
133  return im;
134  }
135 
136  //- Set real part of complex number - STL naming
137  inline void real(scalar val);
138 
139  //- Set imaginary part of complex number - STL naming
140  inline void imag(scalar val);
141 
142 
143  // Access
144 
145  //- Real part of complex number
146  inline scalar Re() const;
147 
148  //- Imaginary part of complex number
149  inline scalar Im() const;
150 
151 
152  // Edit
153 
154  //- Real part of complex number
155  inline scalar& Re();
156 
157  //- Imaginary part of complex number
158  inline scalar& Im();
159 
160 
161  // Operations
162 
163  //- Complex conjugate
164  inline complex conjugate() const;
165 
166 
167  // Member Operators
168 
169  //- Conversion to std::complex
170  inline operator std::complex<scalar>() const
171  {
172  return std::complex<scalar>(re, im);
173  }
174 
175 
176  //- Copy assignment
177  inline void operator=(const complex& c);
178 
179  //- Assign zero
180  inline void operator=(const Foam::zero);
181 
182  //- Assign scalar (imag = zero)
183  inline void operator=(const scalar s);
184 
185  inline void operator+=(const complex& c);
186  inline void operator+=(const scalar s);
187 
188  inline void operator-=(const complex& c);
189  inline void operator-=(const scalar s);
190 
191  inline void operator*=(const complex& c);
192  inline void operator*=(const scalar s);
193 
194  inline void operator/=(const complex& c);
195  inline void operator/=(const scalar s);
196 
197  inline bool operator==(const complex& c) const;
198  inline bool operator!=(const complex& c) const;
199 
200 
201  // Friend Functions
202 
203  friend scalar magSqr(const complex& c);
204  friend scalar mag(const complex& c);
205  friend complex sqr(const complex& c);
206 
207  //- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
208  friend complex sign(const complex& c);
209 
210  //- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
211  friend scalar csign(const complex& c);
212 
213  friend const complex& min(const complex& c1, const complex& c2);
214  friend const complex& max(const complex& c1, const complex& c2);
215  friend complex limit(const complex& c1, const complex& c2);
216  friend const complex& sum(const complex& c);
217 
218 
219  // Friend Operators
220 
221  friend complex operator-(const complex& c);
222 
223  friend complex operator+(const complex& c1, const complex& c2);
224  friend complex operator+(const complex& c, const scalar s);
225  friend complex operator+(const scalar s, const complex& c);
226 
227  friend complex operator-(const complex& c1, const complex& c2);
228  friend complex operator-(const complex& c, const scalar s);
229  friend complex operator-(const scalar s, const complex& c);
230 
231  friend complex operator*(const complex& c1, const complex& c2);
232  friend complex operator*(const complex& c, const scalar s);
233  friend complex operator*(const scalar s, const complex& c);
234 
235  friend complex operator/(const complex& c1, const complex& c2);
236  friend complex operator/(const complex& c, const scalar s);
237  friend complex operator/(const scalar s, const complex& c);
238 };
239 
240 
241 /*---------------------------------------------------------------------------*\
242  Class pTraits<complex> Declaration
243 \*---------------------------------------------------------------------------*/
244 
245 // Template specialisation for pTraits<complex>
246 template<>
247 class pTraits<complex>
248 {
249  complex p_;
250 
251 public:
252 
253  //- Component type
254  typedef complex cmptType;
255 
256  //- Magnitude type
257  typedef scalar magType;
258 
259  //- Equivalent type of labels used for valid component indexing
260  typedef label labelType;
261 
262 
263  // Member Constants
264 
265  //- Dimensionality of space
266  static constexpr direction dim = 3;
267 
268  //- Rank of complex is 0
269  static constexpr direction rank = 0;
270 
271  //- Number of components in complex is 2
272  static constexpr direction nComponents = 2;
273 
274 
275  // Static Data Members
276 
277  static const char* const typeName;
278  static const char* const componentNames[];
279 
280  static const complex zero;
281  static const complex one;
282  static const complex min;
283  static const complex max;
284  static const complex rootMin;
285  static const complex rootMax;
286 
287 
288  // Constructors
289 
290  //- Construct from primitive
291  explicit pTraits(const complex& val);
292 
293  //- Construct from Istream
294  pTraits(Istream& is);
295 
296 
297  // Member Functions
298 
299  //- Access to the value
300  operator complex() const
301  {
302  return p_;
303  }
304 
305  //- Access to the value
306  operator complex&()
307  {
308  return p_;
309  }
310 };
311 
312 
313 /*---------------------------------------------------------------------------*\
314  Namespace Detail
315 \*---------------------------------------------------------------------------*/
316 
317 namespace Detail
318 {
319  // Helper functions for complex, in Detail namespace to avoid possible
320  // name collisions (could change in the future)
321 
322  //- The 'conjugate' of non-complex returns itself (pass-through)
323  //- it does not return a complex!
324  template<class T>
325  typename std::enable_if
326  <
327  !std::is_same<complex, T>::value,
328  const T&
329  >::type conj(const T& val)
330  {
331  return val;
332  }
333 
334  //- The conjugate of a complex number
335  template<class T>
336  typename std::enable_if
337  <
338  std::is_same<complex, T>::value,
340  >::type conj(const T& val)
341  {
342  return val.conjugate();
343  }
344 
345 } // End namespace Detail
346 
347 
348 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
349 
350 //- Contiguous data for complex
351 template<> struct is_contiguous<complex> : std::true_type {};
352 
353 //- Contiguous scalar data for complex
354 template<> struct is_contiguous_scalar<complex> : std::true_type {};
355 
356 
357 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
358 
360 Ostream& operator<<(Ostream& os, const complex& c);
361 
362 //- Complex conjugate
363 inline complex operator~(const complex& c);
364 
365 
366 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
367 
368 //- Return string representation of complex
369 word name(const complex& c);
370 
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 
374 } // End namespace Foam
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 #include "complexI.H"
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #endif
383 
384 // ************************************************************************* //
Foam::complex::limit
friend complex limit(const complex &c1, const complex &c2)
Definition: complexI.H:270
Foam::pTraits< complex >::zero
static const complex zero
complex (0,0)
Definition: complex.H:279
Foam::pTraits< complex >::min
static const complex min
complex (-VGREAT,-VGREAT)
Definition: complex.H:281
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::roots::complex
Definition: Roots.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::complex::operator+
friend complex operator+(const complex &c1, const complex &c2)
Definition: complexI.H:298
Foam::complex::operator-=
void operator-=(const complex &c)
Definition: complexI.H:153
Foam::complex::operator==
bool operator==(const complex &c) const
Definition: complexI.H:192
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::complex::mag
friend scalar mag(const complex &c)
Definition: complexI.H:223
Foam::complex::operator!=
bool operator!=(const complex &c) const
Definition: complexI.H:198
Foam::complex::imag
constexpr scalar imag() const
Imaginary part of complex number - STL naming.
Definition: complex.H:130
Foam::pTraits< complex >::magType
scalar magType
Magnitude type.
Definition: complex.H:256
Foam::pTraits< complex >::labelType
label labelType
Equivalent type of labels used for valid component indexing.
Definition: complex.H:259
Foam::complex::sign
friend complex sign(const complex &c)
sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:235
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::one
A class representing the concept of 1 (one), which can be used to avoid manipulating objects that are...
Definition: one.H:60
Foam::complex::conjugate
complex conjugate() const
Complex conjugate.
Definition: complexI.H:111
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::complex::csign
friend scalar csign(const complex &c)
csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:242
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
complexI.H
Foam::complex::operator*=
void operator*=(const complex &c)
Definition: complexI.H:166
Foam::constant::physicoChemical::c1
const dimensionedScalar c1
First radiation constant: default SI units: [W/m2].
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::complex::operator-
friend complex operator-(const complex &c)
Definition: complexI.H:292
Foam::complex::operator*
friend complex operator*(const complex &c1, const complex &c2)
Definition: complexI.H:342
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::pTraits::pTraits
pTraits(const PrimitiveType &p)
Construct from primitive.
Definition: pTraits.H:62
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::complex::sum
const friend complex & sum(const complex &c)
Definition: complexI.H:276
Foam::pTraits< complex >::one
static const complex one
complex (1,0)
Definition: complex.H:280
Foam::complex::operator+=
void operator+=(const complex &c)
Definition: complexI.H:140
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
zero.H
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::complex::sqr
friend complex sqr(const complex &c)
Definition: complexI.H:229
scalar.H
Foam::complex::real
constexpr scalar real() const
Real part of complex number - STL naming.
Definition: complex.H:124
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::complex
A complex number, similar to the C++ complex type.
Definition: complex.H:82
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
Foam::pTraits< complex >::rootMax
static const complex rootMax
complex (ROOTVGREAT, ROOTVGREAT)
Definition: complex.H:284
Foam::is_contiguous_scalar
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:91
Foam::complex::max
const friend complex & max(const complex &c1, const complex &c2)
Definition: complexI.H:259
Foam::operator/
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
Definition: dimensionedScalar.C:68
Foam::pTraits< complex >::max
static const complex max
complex (VGREAT,VGREAT)
Definition: complex.H:282
Foam::operator~
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:735
Foam::pTraits< complex >::typeName
static const char *const typeName
Definition: complex.H:276
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::Detail::conj
std::enable_if< !std::is_same< complex, T >::value, const T & >::type conj(const T &val)
Definition: complex.H:328
contiguous.H
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::limit
complex limit(const complex &, const complex &)
Definition: complexI.H:270
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::complex::magSqr
friend scalar magSqr(const complex &c)
Definition: complexI.H:217
Foam::complex::operator=
void operator=(const complex &c)
Copy assignment.
Definition: complexI.H:119
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
Foam::complex::min
const friend complex & min(const complex &c1, const complex &c2)
Definition: complexI.H:248
Foam::pTraits< complex >::cmptType
complex cmptType
Component type.
Definition: complex.H:253
Foam::complex::complex
constexpr complex() noexcept
Construct null as zero-initialized.
Definition: complexI.H:31
Foam::complex::Re
scalar Re() const
Real part of complex number.
Definition: complexI.H:87
Foam::pTraits< complex >::rootMin
static const complex rootMin
complex (-ROOTVGREAT, -ROOTVGREAT)
Definition: complex.H:283
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::complex::operator/=
void operator/=(const complex &c)
Definition: complexI.H:179
Foam::complex::operator/
friend complex operator/(const complex &c1, const complex &c2)
Definition: complexI.H:364
Foam::complex::Im
scalar Im() const
Imaginary part of complex number.
Definition: complexI.H:93
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61