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-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::complex
29
30Description
31 A complex number, similar to the C++ complex type.
32
33SourceFiles
34 complexI.H
35 complex.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_primitives_complex_H
40#define Foam_primitives_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
51namespace Foam
52{
53
54// Forward Declarations
55class complex;
56
57inline scalar magSqr(const complex&);
58inline scalar mag(const complex&);
59inline complex sqr(const complex&);
60inline const complex& min(const complex&, const complex&);
61inline const complex& max(const complex&, const complex&);
62inline complex limit(const complex&, const complex&);
63inline const complex& sum(const complex&);
64inline complex operator-(const complex&);
65inline complex operator+(const complex&, const complex&);
66inline complex operator+(const complex&, const scalar);
67inline complex operator+(const scalar, const complex&);
68inline complex operator-(const complex&, const complex&);
69inline complex operator-(const complex&, const scalar);
70inline complex operator-(const scalar, const complex&);
71inline complex operator*(const complex&, const complex&);
72inline complex operator*(const complex&, const scalar);
73inline complex operator*(const scalar, const complex&);
74inline complex operator/(const complex&, const complex&);
75inline complex operator/(const complex&, const scalar);
76inline complex operator/(const scalar, const complex&);
77
78
79/*---------------------------------------------------------------------------*\
80 Class complex Declaration
81\*---------------------------------------------------------------------------*/
83class complex
84{
85 // Private Data
86
87 //- Real and imaginary parts
88 scalar re, im;
89
90
91public:
92
93 // Generated Methods
94
95 //- Copy construct
96 complex(const complex&) = default;
97
98 //- Copy assignment
99 complex& operator=(const complex&) = default;
100
101 //- Move construct
102 complex(complex&&) = default;
103
104 //- Move assignment
105 complex& operator=(complex&&) = default;
106
107
108 // Constructors
109
110 //- Default construct, as zero-initialized
111 inline constexpr complex() noexcept;
112
113 //- Construct zero-initialized from zero class
114 inline constexpr complex(const Foam::zero) noexcept;
115
116 //- Construct from real component
117 inline explicit constexpr complex(const scalar r) noexcept;
118
119 //- Construct from real and imaginary parts
120 inline constexpr complex(const scalar r, const scalar i) noexcept;
121
122 //- Implicit construct from std::complex
123 inline complex(const std::complex<float>& c);
124
125 //- Implicit construct from std::complex
126 inline complex(const std::complex<double>& c);
127
128 //- Construct from Istream
129 explicit complex(Istream& is);
130
131
132 // Member Functions
133
134 // STL getter/setter
135
136 //- Real part of complex number - STL naming
137 constexpr scalar real() const
138 {
139 return re;
140 }
141
142 //- Imaginary part of complex number - STL naming
143 constexpr scalar imag() const
144 {
145 return im;
146 }
147
148 //- Set real part of complex number - STL naming
149 inline void real(scalar val);
150
151 //- Set imaginary part of complex number - STL naming
152 inline void imag(scalar val);
153
154
155 // Access
156
157 //- Real part of complex number
158 inline scalar Re() const;
159
160 //- Imaginary part of complex number
161 inline scalar Im() const;
162
163
164 // Edit
165
166 //- Real part of complex number
167 inline scalar& Re();
168
169 //- Imaginary part of complex number
170 inline scalar& Im();
171
172
173 // Operations
174
175 //- Complex conjugate
176 inline complex conjugate() const;
177
178
179 // Member Operators
180
181 //- Implicit conversion to std::complex
182 operator std::complex<scalar>() const
183 {
184 return std::complex<scalar>(re, im);
185 }
186
187
188 //- Assign zero
189 inline void operator=(const Foam::zero);
190
191 //- Assign scalar (imag = zero)
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 void operator-=(const complex& c);
198 inline void operator-=(const scalar s);
199
200 inline void operator*=(const complex& c);
201 inline void operator*=(const scalar s);
202
203 inline void operator/=(const complex& c);
204 inline void operator/=(const scalar s);
205
206 inline bool operator==(const complex& c) const;
207 inline bool operator!=(const complex& c) const;
208
209
210 // Friend Functions
211
212 friend scalar magSqr(const complex& c);
213 friend scalar mag(const complex& c);
214 friend complex sqr(const complex& c);
215
216 //- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
217 friend complex sign(const complex& c);
218
219 //- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
220 friend scalar csign(const complex& c);
221
222 friend const complex& min(const complex& c1, const complex& c2);
223 friend const complex& max(const complex& c1, const complex& c2);
224 friend complex limit(const complex& c1, const complex& c2);
225 friend const complex& sum(const complex& c);
226
227
228 // Friend Operators
229
230 friend complex operator-(const complex& c);
231
232 friend complex operator+(const complex& c1, const complex& c2);
233 friend complex operator+(const complex& c, const scalar s);
234 friend complex operator+(const scalar s, const complex& c);
235
236 friend complex operator-(const complex& c1, const complex& c2);
237 friend complex operator-(const complex& c, const scalar s);
238 friend complex operator-(const scalar s, const complex& c);
239
240 friend complex operator*(const complex& c1, const complex& c2);
241 friend complex operator*(const complex& c, const scalar s);
242 friend complex operator*(const scalar s, const complex& c);
243
244 friend complex operator/(const complex& c1, const complex& c2);
245 friend complex operator/(const complex& c, const scalar s);
246 friend complex operator/(const scalar s, const complex& c);
247};
248
249
250/*---------------------------------------------------------------------------*\
251 Class pTraits<complex> Declaration
252\*---------------------------------------------------------------------------*/
253
254// Template specialisation for pTraits<complex>
255template<>
256class pTraits<complex>
257{
258 complex p_;
259
260public:
261
262 // Typedefs
263
264 //- Component type
265 typedef complex cmptType;
266
267 //- Magnitude type
268 typedef scalar magType;
269
270 //- Equivalent type of labels used for valid component indexing
271 typedef label labelType;
272
273
274 // Member Constants
275
276 //- Dimensionality of space
277 static constexpr direction dim = 3;
278
279 //- Rank of complex is 0
280 static constexpr direction rank = 0;
281
282 //- Number of components in complex is 2
283 static constexpr direction nComponents = 2;
284
285
286 // Static Data Members
288 static const char* const typeName;
289 static const char* const componentNames[];
291 static const complex zero;
292 static const complex one;
293 static const complex min;
294 static const complex max;
295 static const complex rootMin;
296 static const complex rootMax;
297
298
299 // Constructors
300
301 //- Copy construct from primitive
302 explicit pTraits(const complex& val)
303 :
304 p_(val)
305 {}
306
307
308 //- Read construct from Istream
309 explicit pTraits(Istream& is);
310
311
312 // Member Functions
313
314 //- Return the value
315 operator complex() const noexcept
316 {
317 return p_;
318 }
319
320 //- Access the value
321 operator complex&() noexcept
322 {
323 return p_;
324 }
325};
326
327
328/*---------------------------------------------------------------------------*\
329 Namespace Detail
330\*---------------------------------------------------------------------------*/
331
332namespace Detail
333{
334 // Helper functions for complex, in Detail namespace to avoid possible
335 // name collisions (could change in the future)
336
337 //- The 'conjugate' of non-complex returns itself (pass-through)
338 //- it does not return a complex!
339 template<class T>
340 typename std::enable_if
341 <
342 !std::is_same<complex, T>::value,
343 const T&
344 >::type conj(const T& val)
345 {
346 return val;
347 }
348
349 //- The conjugate of a complex number
350 template<class T>
351 typename std::enable_if
352 <
353 std::is_same<complex, T>::value,
355 >::type conj(const T& val)
356 {
357 return val.conjugate();
358 }
359
360} // End namespace Detail
361
362
363// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
364
365//- Contiguous data for complex
366template<> struct is_contiguous<complex> : std::true_type {};
367
368//- Contiguous scalar data for complex
369template<> struct is_contiguous_scalar<complex> : std::true_type {};
370
371
372// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
373
375Ostream& operator<<(Ostream& os, const complex& c);
376
377//- Complex conjugate
378inline complex operator~(const complex& c);
379
380
381// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
382
383//- Return string representation of complex
384word name(const complex& c);
385
386
387// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388
389} // End namespace Foam
390
391// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392
393#include "complexI.H"
394
395// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396
397#endif
398
399// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A complex number, similar to the C++ complex type.
Definition: complex.H:83
friend complex operator-(const complex &c)
Definition: complexI.H:285
complex conjugate() const
Complex conjugate.
Definition: complexI.H:111
complex & operator=(complex &&)=default
Move assignment.
friend complex limit(const complex &c1, const complex &c2)
Definition: complexI.H:263
friend const complex & max(const complex &c1, const complex &c2)
Definition: complexI.H:252
complex(const complex &)=default
Copy construct.
complex & operator=(const complex &)=default
Copy assignment.
void operator+=(const complex &c)
Definition: complexI.H:133
friend complex operator+(const complex &c1, const complex &c2)
Definition: complexI.H:291
constexpr scalar imag() const
Imaginary part of complex number - STL naming.
Definition: complex.H:142
friend const complex & min(const complex &c1, const complex &c2)
Definition: complexI.H:241
friend complex operator*(const complex &c1, const complex &c2)
Definition: complexI.H:335
constexpr complex() noexcept
Default construct, as zero-initialized.
Definition: complexI.H:31
friend complex sqr(const complex &c)
Definition: complexI.H:222
scalar Im() const
Imaginary part of complex number.
Definition: complexI.H:93
void operator-=(const complex &c)
Definition: complexI.H:146
friend scalar csign(const complex &c)
csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:235
scalar Re() const
Real part of complex number.
Definition: complexI.H:87
void operator*=(const complex &c)
Definition: complexI.H:159
friend complex sign(const complex &c)
sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:228
bool operator!=(const complex &c) const
Definition: complexI.H:191
friend scalar mag(const complex &c)
Definition: complexI.H:216
void operator/=(const complex &c)
Definition: complexI.H:172
friend complex operator/(const complex &c1, const complex &c2)
Definition: complexI.H:357
constexpr scalar real() const
Real part of complex number - STL naming.
Definition: complex.H:136
friend scalar magSqr(const complex &c)
Definition: complexI.H:210
bool operator==(const complex &c) const
Definition: complexI.H:185
complex(complex &&)=default
Move construct.
friend const complex & sum(const complex &c)
Definition: complexI.H:269
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
static const char *const typeName
Definition: complex.H:287
static const complex zero
complex (0,0)
Definition: complex.H:290
static const complex rootMax
complex (ROOTVGREAT, ROOTVGREAT)
Definition: complex.H:295
pTraits(const complex &val)
Copy construct from primitive.
Definition: complex.H:301
static const complex one
complex (1,0)
Definition: complex.H:291
static const complex min
complex (-VGREAT,-VGREAT)
Definition: complex.H:292
complex cmptType
Component type.
Definition: complex.H:264
static const complex max
complex (VGREAT,VGREAT)
Definition: complex.H:293
scalar magType
Magnitude type.
Definition: complex.H:267
pTraits(Istream &is)
Read construct from Istream.
label labelType
Equivalent type of labels used for valid component indexing.
Definition: complex.H:270
static const complex rootMin
complex (-ROOTVGREAT, -ROOTVGREAT)
Definition: complex.H:294
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
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))
std::enable_if<!std::is_same< complex, T >::value, constT & >::type conj(const T &val)
Definition: complex.H:343
@ complex
Definition: Roots.H:57
Namespace for OpenFOAM.
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:762
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dimensionedSymmTensor sqr(const dimensionedVector &dv)
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
complex limit(const complex &, const complex &)
Definition: complexI.H:263
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< 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:598
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Istream & operator>>(Istream &, directionInfo &)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
uint8_t direction
Definition: direction.H:56
const direction noexcept
Definition: Scalar.H:223
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:94
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78