mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add sign(), csign() methods for complex
- use std::hypot for complex mag() instead of long-hand version - Detail::conj() function for complex or non-complex
This commit is contained in:
@ -30,7 +30,6 @@ License
|
||||
|
||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||
|
||||
const char* const Foam::complex::typeName = "complex";
|
||||
const Foam::complex Foam::complex::zero(0, 0);
|
||||
const Foam::complex Foam::complex::one(1, 0);
|
||||
|
||||
@ -69,7 +68,6 @@ Foam::pTraits<Foam::complex>::pTraits(Istream& is)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::complex::complex(Istream& is)
|
||||
@ -78,7 +76,7 @@ Foam::complex::complex(Istream& is)
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
|
||||
|
||||
Foam::word Foam::name(const complex& c)
|
||||
{
|
||||
|
||||
@ -39,6 +39,7 @@ SourceFiles
|
||||
#define complex_H
|
||||
|
||||
#include <complex>
|
||||
#include <type_traits>
|
||||
#include "scalar.H"
|
||||
#include "word.H"
|
||||
#include "zero.H"
|
||||
@ -91,7 +92,7 @@ public:
|
||||
// Static Data Members
|
||||
|
||||
//- The type name is "complex"
|
||||
static const char* const typeName;
|
||||
static constexpr const char* const typeName = "complex";
|
||||
|
||||
//- A complex zero (0,0)
|
||||
static const complex zero;
|
||||
@ -209,12 +210,17 @@ public:
|
||||
friend scalar magSqr(const complex& c);
|
||||
friend scalar mag(const complex& c);
|
||||
friend complex sqr(const complex& c);
|
||||
friend const complex& min(const complex&, const complex&);
|
||||
friend const complex& max(const complex&, const complex&);
|
||||
|
||||
friend complex limit(const complex&, const complex&);
|
||||
//- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
|
||||
friend complex sign(const complex& c);
|
||||
|
||||
friend const complex& sum(const complex&);
|
||||
//- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
|
||||
friend scalar csign(const complex& c);
|
||||
|
||||
friend const complex& min(const complex& c1, const complex& c2);
|
||||
friend const complex& max(const complex& c1, const complex& c2);
|
||||
friend complex limit(const complex& c1, const complex& c2);
|
||||
friend const complex& sum(const complex& c);
|
||||
|
||||
|
||||
// Friend Operators
|
||||
@ -232,6 +238,10 @@ public:
|
||||
};
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class pTraits<complex> Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
// Template specialisation for pTraits<complex>
|
||||
template<>
|
||||
class pTraits<complex>
|
||||
@ -247,7 +257,7 @@ public:
|
||||
typedef label labelType;
|
||||
|
||||
|
||||
// Member constants
|
||||
// Member Constants
|
||||
|
||||
//- Dimensionality of space
|
||||
static constexpr direction dim = 3;
|
||||
@ -296,6 +306,41 @@ public:
|
||||
};
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Namespace Detail
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
// Helper functions for complex, in Detail namespace to avoid possible
|
||||
// name collisions (could change in the future)
|
||||
|
||||
//- The 'conjugate' of non-complex returns itself (pass-through)
|
||||
//- it does not return a complex!
|
||||
template<class T>
|
||||
typename std::enable_if
|
||||
<
|
||||
!std::is_same<complex, T>::value,
|
||||
const T&
|
||||
>::type conj(const T& val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
//- The conjugate of a complex number
|
||||
template<class T>
|
||||
typename std::enable_if
|
||||
<
|
||||
std::is_same<complex, T>::value,
|
||||
complex
|
||||
>::type conj(const T& val)
|
||||
{
|
||||
return val.conjugate();
|
||||
}
|
||||
|
||||
} // End namespace Detail
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
|
||||
|
||||
Istream& operator>>(Istream& is, complex& c);
|
||||
|
||||
@ -221,13 +221,26 @@ inline scalar magSqr(const complex& c)
|
||||
|
||||
inline scalar mag(const complex& c)
|
||||
{
|
||||
return sqrt(magSqr(c)); // OR std::hypot(c.re, c.im);
|
||||
return std::hypot(c.re, c.im);
|
||||
}
|
||||
|
||||
|
||||
inline complex sqr(const complex& c)
|
||||
{
|
||||
return c * c;
|
||||
return c*c;
|
||||
}
|
||||
|
||||
|
||||
inline complex sign(const complex& c)
|
||||
{
|
||||
const scalar s(mag(c));
|
||||
return s < ROOTVSMALL ? Zero : c/s;
|
||||
}
|
||||
|
||||
|
||||
inline scalar csign(const complex& c)
|
||||
{
|
||||
return equal(c.Re(), 0) ? sign(c.Im()) : sign(c.Re());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user