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  const scalar s(mag(c));
214  return s < ROOTVSMALL ? Zero : c/s;
215 }
217 
218 //- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
219 inline scalar csign(const complex& c)
220 {
221  return equal(c.real(), 0) ? sign(c.imag()) : sign(c.real());
222 }
223 
224 
225 inline const complex& min(const complex& c1, const complex& c2)
226 {
227  return (c1.magSqr() < c2.magSqr()) ? c1 : c2;
228 }
229 
230 
231 inline const complex& max(const complex& c1, const complex& c2)
232 {
233  return (c1.magSqr() < c2.magSqr()) ? c2 : c1;
234 }
235 
236 
237 inline complex limit(const complex& c1, const complex& c2)
238 {
239  return complex
240  (
241  limit(c1.real(), c2.real()),
242  limit(c1.imag(), c2.imag())
243  );
244 }
245 
246 
247 //- Linear interpolation of complex a and b by factor t
248 inline complex lerp(const complex& a, const complex& b, const scalar t)
249 {
250  const scalar onet = (1-t);
251 
252  return complex
253  (
254  onet*a.real() + t*b.real(),
255  onet*a.imag() + t*b.imag()
256  );
257 }
258 
260 inline const complex& sum(const complex& c)
261 {
262  return c;
263 }
264 
266 template<class Cmpt> class Tensor;
267 
268 //- No-op rotational transform for complex
269 inline complex transform(const Tensor<scalar>&, const complex c)
270 {
271  return c;
272 }
273 
274 
275 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
276 
277 inline complex operator-(const complex& c)
278 {
279  return complex(-c.re, -c.im);
280 }
281 
282 
283 inline complex operator+(const complex& c1, const complex& c2)
284 {
285  return complex
286  (
287  c1.re + c2.re,
288  c1.im + c2.im
289  );
290 }
291 
292 
293 inline complex operator+(const complex& c, const scalar s)
294 {
295  return complex(c.re + s, c.im);
296 }
297 
298 
299 inline complex operator+(const scalar s, const complex& c)
300 {
301  return complex(c.re + s, c.im);
302 }
303 
304 
305 inline complex operator-(const complex& c1, const complex& c2)
306 {
307  return complex
308  (
309  c1.re - c2.re,
310  c1.im - c2.im
311  );
312 }
313 
314 
315 inline complex operator-(const complex& c, const scalar s)
316 {
317  return complex(c.re - s, c.im);
318 }
319 
320 
321 inline complex operator-(const scalar s, const complex& c)
322 {
323  return complex(s - c.re, -c.im);
324 }
325 
326 
327 inline complex operator*(const complex& c1, const complex& c2)
328 {
329  return complex
330  (
331  c1.re*c2.re - c1.im*c2.im,
332  c1.im*c2.re + c1.re*c2.im
333  );
334 }
335 
336 
337 inline complex operator*(const complex& c, const scalar s)
338 {
339  return complex(s*c.re, s*c.im);
340 }
341 
342 
343 inline complex operator*(const scalar s, const complex& c)
344 {
345  return complex(s*c.re, s*c.im);
346 }
347 
348 
349 inline complex operator/(const complex& c1, const complex& c2)
350 {
351  const scalar sqrC2 = c2.magSqr();
352 
353  return complex
354  (
355  (c1.re*c2.re + c1.im*c2.im)/sqrC2,
356  (c1.im*c2.re - c1.re*c2.im)/sqrC2
357  );
358 }
359 
360 
361 inline complex operator/(const complex& c, const scalar s)
362 {
363  return complex(c.re/s, c.im/s);
364 }
365 
366 
367 inline complex operator/(const scalar s, const complex& c)
368 {
369  return complex(s)/c;
370 }
371 
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 } // End namespace Foam
376 
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
380 // Complex transcendental functions
381 namespace Foam
382 {
383  #define transFunc(func) \
384  inline complex func(const complex& c) \
385  { \
386  return std:: func (static_cast<std::complex<scalar>>(c)); \
387  }
388 
406 // Special treatment for pow()
407 inline complex pow(const complex& x, const complex& y)
408 {
409  return std::pow
410  (
411  static_cast<std::complex<scalar>>(x),
412  static_cast<std::complex<scalar>>(y)
413  );
414 }
415 
416 
417 // Combinations of complex and integral/float
418 #define powFuncs(type2) \
419  inline complex pow(const complex& x, const type2 y) \
420  { \
421  return std::pow(static_cast<std::complex<scalar>>(x), y); \
422  } \
423  \
424  inline complex pow(const type2 x, const complex& y) \
425  { \
426  return std::pow \
427  ( \
428  static_cast<std::complex<scalar>>(x), \
429  static_cast<std::complex<scalar>>(y) \
430  ); \
431  }
432 
433 powFuncs(int)
434 powFuncs(long)
435 #if defined(__APPLE__) && WM_LABEL_SIZE == 64
436 powFuncs(int64_t)
437 #endif
438 powFuncs(float)
439 powFuncs(double)
441 
442 inline complex pow3(const complex& c)
443 {
444  return c*sqr(c);
445 }
446 
447 
448 inline complex pow4(const complex& c)
449 {
450  return sqr(sqr(c));
451 }
452 
453 
454 inline complex pow5(const complex& c)
455 {
456  return c*pow4(c);
457 }
458 
459 
460 inline complex pow6(const complex& c)
461 {
462  return pow3(sqr(c));
463 }
464 
465 
466 inline complex pow025(const complex& c)
467 {
468  return sqrt(sqrt(c));
469 }
470 
471 } // End namespace Foam
472 
473 #undef transFunc
474 #undef powFuncs
475 
476 // ************************************************************************* //
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:440
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:743
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:234
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:216
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:265
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:419
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133