complexI.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-2023 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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 inline constexpr Foam::complex::complex() noexcept
32 :
33  re(0),
34  im(0)
35 {}
36 
37 
39 :
40  re(0),
41  im(0)
42 {}
43 
44 
45 inline constexpr Foam::complex::complex(const scalar r) noexcept
46 :
47  re(r),
48  im(0)
49 {}
50 
51 
52 inline constexpr Foam::complex::complex(const scalar r, const scalar i) noexcept
53 :
54  re(r),
55  im(i)
56 {}
57 
58 
59 inline Foam::complex::complex(const std::complex<float>& c)
60 :
61  re(c.real()),
62  im(c.imag())
63 {}
64 
65 
66 inline Foam::complex::complex(const std::complex<double>& c)
67 :
68  re(c.real()),
69  im(c.imag())
70 {}
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 {
77  return complex(re, -im);
78 }
79 
80 
81 inline Foam::scalar Foam::complex::magnitude() const
82 {
83  return std::hypot(re, im);
84 }
85 
86 
87 inline Foam::scalar Foam::complex::magSqr() const
88 {
89  return (re*re + im*im);
90 }
91 
92 
93 inline Foam::scalar Foam::complex::cmptSum() const noexcept
94 {
95  return (re + im);
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
100 
102 {
103  re = 0;
104  im = 0;
105 }
106 
107 
108 inline void Foam::complex::operator=(const scalar s)
109 {
110  re = s;
111  im = 0;
112 }
113 
114 
116 {
117  re += c.re;
118  im += c.im;
119 }
120 
122 inline void Foam::complex::operator+=(const scalar s)
123 {
124  re += s;
125 }
126 
127 
129 {
130  re -= c.re;
131  im -= c.im;
132 }
133 
135 inline void Foam::complex::operator-=(const scalar s)
136 {
137  re -= s;
138 }
139 
141 inline void Foam::complex::operator*=(const complex& c)
142 {
143  *this = (*this)*c;
144 }
145 
146 
147 inline void Foam::complex::operator*=(const scalar s)
148 {
149  re *= s;
150  im *= s;
151 }
152 
154 inline void Foam::complex::operator/=(const complex& c)
155 {
156  *this = *this/c;
157 }
158 
159 
160 inline void Foam::complex::operator/=(const scalar s)
161 {
162  re /= s;
163  im /= s;
164 }
165 
167 inline bool Foam::complex::operator==(const complex& c) const
168 {
169  return (equal(re, c.re) && equal(im, c.im));
170 }
171 
172 
173 inline bool Foam::complex::operator!=(const complex& c) const
174 {
175  return !operator==(c);
176 }
177 
178 
179 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
180 
181 inline Foam::complex Foam::operator~(const complex& c)
182 {
183  return c.conjugate();
184 }
186 
187 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
188 
189 namespace Foam
190 {
192 inline scalar mag(const complex& c)
193 {
194  return c.magnitude();
195 }
196 
198 inline scalar magSqr(const complex& c)
199 {
200  return c.magSqr();
201 }
202 
203 
204 inline complex sqr(const complex& c)
205 {
206  return c*c;
207 }
208 
209 
210 //- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
211 inline complex sign(const complex& c)
212 {
213  // TBD: Use volatile to avoid aggressive branch optimization
214  const scalar s(mag(c));
215  return s < ROOTVSMALL ? Zero : c/s;
216 }
218 
219 //- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
220 inline scalar csign(const complex& c)
221 {
222  return equal(c.real(), 0) ? sign(c.imag()) : sign(c.real());
223 }
224 
225 
226 inline const complex& min(const complex& c1, const complex& c2)
227 {
228  return (c1.magSqr() < c2.magSqr()) ? c1 : c2;
229 }
230 
231 
232 inline const complex& max(const complex& c1, const complex& c2)
233 {
234  return (c1.magSqr() < c2.magSqr()) ? c2 : c1;
235 }
236 
237 
238 inline complex limit(const complex& c1, const complex& c2)
239 {
240  return complex
241  (
242  limit(c1.real(), c2.real()),
243  limit(c1.imag(), c2.imag())
244  );
245 }
246 
247 
248 //- Linear interpolation of complex a and b by factor t
249 inline complex lerp(const complex& a, const complex& b, const scalar t)
250 {
251  const scalar onet = (1-t);
252 
253  return complex
254  (
255  onet*a.real() + t*b.real(),
256  onet*a.imag() + t*b.imag()
257  );
258 }
259 
261 inline const complex& sum(const complex& c)
262 {
263  return c;
264 }
265 
267 template<class Cmpt> class Tensor;
268 
269 //- No-op rotational transform for complex
270 inline complex transform(const Tensor<scalar>&, const complex c)
271 {
272  return c;
273 }
274 
275 
276 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
277 
278 inline complex operator-(const complex& c)
279 {
280  return complex(-c.re, -c.im);
281 }
282 
283 
284 inline complex operator+(const complex& c1, const complex& c2)
285 {
286  return complex
287  (
288  c1.re + c2.re,
289  c1.im + c2.im
290  );
291 }
292 
293 
294 inline complex operator+(const complex& c, const scalar s)
295 {
296  return complex(c.re + s, c.im);
297 }
298 
299 
300 inline complex operator+(const scalar s, const complex& c)
301 {
302  return complex(c.re + s, c.im);
303 }
304 
305 
306 inline complex operator-(const complex& c1, const complex& c2)
307 {
308  return complex
309  (
310  c1.re - c2.re,
311  c1.im - c2.im
312  );
313 }
314 
315 
316 inline complex operator-(const complex& c, const scalar s)
317 {
318  return complex(c.re - s, c.im);
319 }
320 
321 
322 inline complex operator-(const scalar s, const complex& c)
323 {
324  return complex(s - c.re, -c.im);
325 }
326 
327 
328 inline complex operator*(const complex& c1, const complex& c2)
329 {
330  return complex
331  (
332  c1.re*c2.re - c1.im*c2.im,
333  c1.im*c2.re + c1.re*c2.im
334  );
335 }
336 
337 
338 inline complex operator*(const complex& c, const scalar s)
339 {
340  return complex(s*c.re, s*c.im);
341 }
342 
343 
344 inline complex operator*(const scalar s, const complex& c)
345 {
346  return complex(s*c.re, s*c.im);
347 }
348 
349 
350 inline complex operator/(const complex& c1, const complex& c2)
351 {
352  const scalar sqrC2 = c2.magSqr();
353 
354  return complex
355  (
356  (c1.re*c2.re + c1.im*c2.im)/sqrC2,
357  (c1.im*c2.re - c1.re*c2.im)/sqrC2
358  );
359 }
360 
361 
362 inline complex operator/(const complex& c, const scalar s)
363 {
364  return complex(c.re/s, c.im/s);
365 }
366 
367 
368 inline complex operator/(const scalar s, const complex& c)
369 {
370  return complex(s)/c;
371 }
372 
373 
374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 
376 } // End namespace Foam
377 
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 // Complex transcendental functions
382 namespace Foam
383 {
384  #define transFunc(func) \
385  inline complex func(const complex& c) \
386  { \
387  return std:: func (static_cast<std::complex<scalar>>(c)); \
388  }
389 
407 // Special treatment for pow()
408 inline complex pow(const complex& x, const complex& y)
409 {
410  return std::pow
411  (
412  static_cast<std::complex<scalar>>(x),
413  static_cast<std::complex<scalar>>(y)
414  );
415 }
416 
417 
418 // Combinations of complex and integral/float
419 #define powFuncs(type2) \
420  inline complex pow(const complex& x, const type2 y) \
421  { \
422  return std::pow(static_cast<std::complex<scalar>>(x), y); \
423  } \
424  \
425  inline complex pow(const type2 x, const complex& y) \
426  { \
427  return std::pow \
428  ( \
429  static_cast<std::complex<scalar>>(x), \
430  static_cast<std::complex<scalar>>(y) \
431  ); \
432  }
433 
434 powFuncs(int)
435 powFuncs(long)
436 #if defined(__APPLE__) && WM_LABEL_SIZE == 64
437 powFuncs(int64_t)
438 #endif
439 powFuncs(float)
440 powFuncs(double)
442 
443 inline complex pow3(const complex& c)
444 {
445  return c*sqr(c);
446 }
447 
448 
449 inline complex pow4(const complex& c)
450 {
451  return sqr(sqr(c));
452 }
453 
454 
455 inline complex pow5(const complex& c)
456 {
457  return c*pow4(c);
458 }
459 
460 
461 inline complex pow6(const complex& c)
462 {
463  return pow3(sqr(c));
464 }
465 
466 
467 inline complex pow025(const complex& c)
468 {
469  return sqrt(sqrt(c));
470 }
471 
472 } // End namespace Foam
473 
474 #undef transFunc
475 #undef powFuncs
476 
477 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar tanh(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)
complex pow(const double x, const complex &y)
Definition: complexI.H:441
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
void operator/=(const complex &c)
Definition: complexI.H:147
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:676
dimensionedScalar log(const dimensionedScalar &ds)
bool operator!=(const complex &c) const
Definition: complexI.H:166
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
const dimensionedScalar re
Classical electron radius: default SI units: [m].
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensionedScalar pow025(const dimensionedScalar &ds)
void operator+=(const complex &c)
Definition: complexI.H:108
constexpr scalar imag() const noexcept
Imaginary part of complex number - STL naming.
Definition: complex.H:155
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionedScalar asin(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
scalar y
dimensionedScalar acosh(const dimensionedScalar &ds)
complex & operator=(const complex &) noexcept=default
Copy assignment.
dimensionedScalar cos(const dimensionedScalar &ds)
dimensionedScalar exp(const dimensionedScalar &ds)
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
dimensionedScalar asinh(const dimensionedScalar &ds)
complex limit(const complex &c1, const complex &c2)
Definition: complexI.H:235
void operator*=(const complex &c)
Definition: complexI.H:134
dimensionedScalar atanh(const dimensionedScalar &ds)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
void operator-=(const complex &c)
Definition: complexI.H:121
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
const direction noexcept
Definition: Scalar.H:258
dimensionedScalar sin(const dimensionedScalar &ds)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
complex conjugate() const
Complex conjugate.
Definition: complexI.H:68
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
scalar magSqr() const
The L2-norm squared of complex.
Definition: complexI.H:80
dimensionedScalar pow3(const dimensionedScalar &ds)
constexpr complex() noexcept
Default construct, as zero-initialized.
Definition: complexI.H:24
scalar cmptSum() const noexcept
The sum of real/imag components.
Definition: complexI.H:86
scalar magnitude() const
The magnitude (L2-norm) of complex. Called magnitude() instead mag(), which looks too much like imag(...
Definition: complexI.H:74
dimensionedScalar sinh(const dimensionedScalar &ds)
dimensionedScalar atan(const dimensionedScalar &ds)
const dimensionedScalar c
Speed of light in a vacuum.
dimensionedScalar pow4(const dimensionedScalar &ds)
const dimensionedScalar c1
First radiation constant: default SI units: [W/m2].
dimensionedScalar pow6(const dimensionedScalar &ds)
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
scalar csign(const complex &c)
csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:217
A complex number, similar to the C++ complex type.
Definition: complex.H:70
transFunc(sqrt) transFunc(cbrt) transFunc(exp) transFunc(log) transFunc(log10) transFunc(sin) transFunc(cos) transFunc(tan) transFunc(asin) transFunc(acos) transFunc(atan) transFunc(sinh) transFunc(cosh) transFunc(tanh) transFunc(asinh) transFunc(acosh) transFunc(atanh) transFunc(erf) transFunc(erfc) transFunc(lgamma) transFunc(tgamma) besselFunc(j0) besselFunc(j1) besselFunc(y0) besselFunc(y1) besselFunc2(jn) besselFunc2(yn) inline Scalar &setComponent(Scalar &val
Non-const access to scalar-type (has no components)
dimensionedScalar cosh(const dimensionedScalar &ds)
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
Definition: complexI.H:266
constexpr scalar real() const noexcept
Real part of complex number - STL naming.
Definition: complex.H:150
bool operator==(const complex &c) const
Definition: complexI.H:160
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))
dimensionedScalar tan(const dimensionedScalar &ds)
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:521
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
dimensionedScalar log10(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
#define powFuncs(type2)
Definition: complexI.H:420
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127