BUG: incorrect scalar/complex division (#1331)
ENH: define addition/subtraction operations for scalar and complex - required since construct complex from scalar is explicit - additional tests in Test-complex
This commit is contained in:
parent
52df9a2fee
commit
e42cc287de
@ -118,16 +118,15 @@ int main(int argc, char *argv[])
|
|||||||
// Info<< "sqrt : " << sqrt(fld1) << nl;
|
// Info<< "sqrt : " << sqrt(fld1) << nl;
|
||||||
// Info<< "pow(2) : " << pow(fld1, 2) << nl;
|
// Info<< "pow(2) : " << pow(fld1, 2) << nl;
|
||||||
|
|
||||||
Info<< nl << "Elementary complex arithmetic operations:" << nl;
|
#if 1
|
||||||
|
Info<< nl << "## Elementary complex-complex arithmetic operations:" << nl;
|
||||||
{
|
{
|
||||||
complex a(6, 1);
|
const complex a(6, 1);
|
||||||
complex b = a;
|
complex b = a;
|
||||||
|
|
||||||
Info << "Compound assignment operations:" << nl;
|
Info << "# Compound assignment operations:" << nl;
|
||||||
|
|
||||||
// Multiplication
|
Info<< "a = " << a << ", b = " << b << nl;
|
||||||
b *= a;
|
|
||||||
Info<< "b *= a:" << tab << "b=" << b << nl;
|
|
||||||
|
|
||||||
// Addition
|
// Addition
|
||||||
b += a;
|
b += a;
|
||||||
@ -137,56 +136,117 @@ int main(int argc, char *argv[])
|
|||||||
b -= a;
|
b -= a;
|
||||||
Info<< "b -= a:" << tab << "b =" << b << nl;
|
Info<< "b -= a:" << tab << "b =" << b << nl;
|
||||||
|
|
||||||
|
// Multiplication
|
||||||
|
b *= a;
|
||||||
|
Info<< "b *= a:" << tab << "b =" << b << nl;
|
||||||
|
|
||||||
// Division
|
// Division
|
||||||
b /= a;
|
b /= a;
|
||||||
Info<< "b /= a:" << tab << "b =" << b << nl;
|
Info<< "b /= a:" << tab << "b =" << b << nl;
|
||||||
|
|
||||||
Info << "Operations with scalars:" << nl;
|
|
||||||
|
|
||||||
Info<< "b=" << b << nl;
|
|
||||||
|
|
||||||
// Scalar multiplication
|
|
||||||
b *= 2.0;
|
|
||||||
Info<< "b*2 (elementwise multiplication):" << tab << b << nl;
|
|
||||||
|
|
||||||
// Scalar addition
|
|
||||||
b += 1.0;
|
|
||||||
Info<< "b + 1 (only real part):" << tab << b << nl;
|
|
||||||
|
|
||||||
// Scalar subtraction
|
|
||||||
b -= 1.0;
|
|
||||||
Info<< "b - 1 (only real part):" << tab << b << nl;
|
|
||||||
|
|
||||||
// Scalar division
|
|
||||||
b = 1.0/b;
|
|
||||||
Info<< "1/b (elementwise division):" << tab << b << nl;
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Info<< nl << "Other mathematical expressions:" << nl;
|
|
||||||
|
#if 1
|
||||||
|
Info<< nl << "## Elementary complex-scalar arithmetic operations:" << nl;
|
||||||
{
|
{
|
||||||
complex a(4.3, -3.14);
|
const scalar a = 5;
|
||||||
complex b(0, -4.3);
|
complex b(6, 1);
|
||||||
|
|
||||||
Info<< "a=" << a << tab << "b=" << b << nl;
|
Info << "# Non-assignment operations:" << nl;
|
||||||
|
|
||||||
|
Info<< "(scalar) a = " << a << ", b = " << b << nl;
|
||||||
|
|
||||||
|
// Addition
|
||||||
|
b = a + b;
|
||||||
|
Info<< "b = a + b: " << tab << b << nl;
|
||||||
|
|
||||||
|
b = b + a;
|
||||||
|
Info<< "b = b + a: " << tab << b << nl;
|
||||||
|
|
||||||
|
// Subtraction
|
||||||
|
b = a - b;
|
||||||
|
Info<< "b = a - b: " << tab << b << nl;
|
||||||
|
|
||||||
|
b = b - a;
|
||||||
|
Info<< "b = b - a: " << tab << b << nl;
|
||||||
|
|
||||||
|
// Multiplication
|
||||||
|
b = a*b;
|
||||||
|
Info<< "b = a*b: " << tab << b << nl;
|
||||||
|
|
||||||
|
b = b*a;
|
||||||
|
Info<< "b = b*a: " << tab << b << nl;
|
||||||
|
|
||||||
|
// Division
|
||||||
|
b = a/b;
|
||||||
|
Info<< "b = a/b = scalar(a)/b = complex(a)/b:" << tab << b << nl;
|
||||||
|
|
||||||
|
b = b/a;
|
||||||
|
Info<< "b = b/a: " << tab << b << nl;
|
||||||
|
|
||||||
|
|
||||||
|
Info << "# Compound assignment operations:" << nl;
|
||||||
|
|
||||||
|
Info<< "(scalar) a = " << a << ", b = " << b << nl;
|
||||||
|
|
||||||
|
// Addition: complex+scalar
|
||||||
|
b += a;
|
||||||
|
Info<< "b += a (only real part):" << tab << b << nl;
|
||||||
|
|
||||||
|
// Subtraction: complex-scalar
|
||||||
|
b -= a;
|
||||||
|
Info<< "b -= a (only real part):" << tab << b << nl;
|
||||||
|
|
||||||
|
// Multiplication: complex*scalar
|
||||||
|
b *= a;
|
||||||
|
Info<< "b *= a (real and imag parts):" << tab << b << nl;
|
||||||
|
|
||||||
|
// Division: complex/scalar
|
||||||
|
b /= a;
|
||||||
|
Info<< "b /= a (real and imag parts):" << tab << b << nl;
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
Info<< nl << "## Other mathematical expressions:" << nl;
|
||||||
|
{
|
||||||
|
const complex a(4.3, -3.14);
|
||||||
|
const complex b(0, -4.3);
|
||||||
|
const complex c(-4.3, 0);
|
||||||
|
|
||||||
|
Info<< "a = " << a << ", b = " << b << ", c = " << c << nl;
|
||||||
|
|
||||||
// Square-root
|
// Square-root
|
||||||
//Info<< "sqrt(a)=" << sqrt(a) << tab << "sqrt(b)=" << sqrt(b) << nl;
|
Info<< "sqrt(a) = " << Foam::sqrt(a) << ", "
|
||||||
|
<< "sqrt(b) = " << Foam::sqrt(b) << ", "
|
||||||
|
<< "sqrt(c) = " << Foam::sqrt(c) << nl;
|
||||||
|
|
||||||
// Square
|
// Square
|
||||||
Info<< "sqr(a)=" << sqr(a) << tab << "sqr(b)=" << sqr(b) << nl;
|
Info<< "sqr(a) = " << sqr(a) << ", "
|
||||||
|
<< "sqr(b) = " << sqr(b) << ", "
|
||||||
|
<< "sqr(c) = " << sqr(c) << nl;
|
||||||
|
|
||||||
// n^th power
|
// n^th power
|
||||||
//Info<< "pow(a,-1)=" << pow(a,-1) << tab
|
Info<< "pow(a, -1) = " << pow(a, -1) << ", "
|
||||||
// << "pow(b,-1)=" << pow(b,-1) << nl;
|
<< "pow(b, -1) = " << pow(b, -1) << ", "
|
||||||
|
<< "pow(c, -1) = " << pow(c, -1) << nl;
|
||||||
|
|
||||||
// Exponential
|
// Exponential
|
||||||
//Info<< "exp(a)=" << exp(a) << tab << "exp(b)=" << exp(b) << nl;
|
Info<< "exp(a) = " << exp(a) << ", "
|
||||||
|
<< "exp(b) = " << exp(b) << ", "
|
||||||
|
<< "exp(c) = " << exp(c) << nl;
|
||||||
|
|
||||||
// Natural logarithm
|
// Natural logarithm
|
||||||
//Info<< "log(a)=" << log(a) << tab << "log(b)=" << log(b) << nl;
|
Info<< "log(a) = " << log(a) << ", "
|
||||||
}
|
<< "log(b) = " << log(b) << ", "
|
||||||
|
<< "log(c) = " << log(c) << nl;
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Info<< nl << "End" << nl;
|
|
||||||
|
|
||||||
// Make some changes
|
// Make some changes
|
||||||
{
|
{
|
||||||
|
@ -60,13 +60,17 @@ inline const complex& min(const complex&, const complex&);
|
|||||||
inline const complex& max(const complex&, const complex&);
|
inline const complex& max(const complex&, const complex&);
|
||||||
inline complex limit(const complex&, const complex&);
|
inline complex limit(const complex&, const complex&);
|
||||||
inline const complex& sum(const complex&);
|
inline const complex& sum(const complex&);
|
||||||
inline complex operator+(const complex&, const complex&);
|
|
||||||
inline complex operator-(const complex&);
|
inline complex operator-(const complex&);
|
||||||
|
inline complex operator+(const complex&, const complex&);
|
||||||
|
inline complex operator+(const complex&, const scalar);
|
||||||
|
inline complex operator+(const scalar, const complex&);
|
||||||
inline complex operator-(const complex&, const complex&);
|
inline complex operator-(const complex&, const complex&);
|
||||||
|
inline complex operator-(const complex&, const scalar);
|
||||||
|
inline complex operator-(const scalar, const complex&);
|
||||||
inline complex operator*(const complex&, const complex&);
|
inline complex operator*(const complex&, const complex&);
|
||||||
inline complex operator/(const complex&, const complex&);
|
|
||||||
inline complex operator*(const scalar, const complex&);
|
|
||||||
inline complex operator*(const complex&, const scalar);
|
inline complex operator*(const complex&, const scalar);
|
||||||
|
inline complex operator*(const scalar, const complex&);
|
||||||
|
inline complex operator/(const complex&, const complex&);
|
||||||
inline complex operator/(const complex&, const scalar);
|
inline complex operator/(const complex&, const scalar);
|
||||||
inline complex operator/(const scalar, const complex&);
|
inline complex operator/(const scalar, const complex&);
|
||||||
|
|
||||||
@ -174,15 +178,19 @@ public:
|
|||||||
//- Assign zero
|
//- Assign zero
|
||||||
inline void operator=(const Foam::zero);
|
inline void operator=(const Foam::zero);
|
||||||
|
|
||||||
inline void operator+=(const complex& c);
|
//- Assign scalar (imag = zero)
|
||||||
inline void operator-=(const complex& c);
|
|
||||||
inline void operator*=(const complex& c);
|
|
||||||
inline void operator/=(const complex& c);
|
|
||||||
|
|
||||||
inline void operator=(const scalar s);
|
inline void operator=(const scalar s);
|
||||||
|
|
||||||
|
inline void operator+=(const complex& c);
|
||||||
inline void operator+=(const scalar s);
|
inline void operator+=(const scalar s);
|
||||||
|
|
||||||
|
inline void operator-=(const complex& c);
|
||||||
inline void operator-=(const scalar s);
|
inline void operator-=(const scalar s);
|
||||||
|
|
||||||
|
inline void operator*=(const complex& c);
|
||||||
inline void operator*=(const scalar s);
|
inline void operator*=(const scalar s);
|
||||||
|
|
||||||
|
inline void operator/=(const complex& c);
|
||||||
inline void operator/=(const scalar s);
|
inline void operator/=(const scalar s);
|
||||||
|
|
||||||
inline bool operator==(const complex& c) const;
|
inline bool operator==(const complex& c) const;
|
||||||
@ -209,14 +217,21 @@ public:
|
|||||||
|
|
||||||
// Friend Operators
|
// Friend Operators
|
||||||
|
|
||||||
friend complex operator+(const complex& c1, const complex& c2);
|
|
||||||
friend complex operator-(const complex& c);
|
friend complex operator-(const complex& c);
|
||||||
friend complex operator-(const complex& c1, const complex& c2);
|
|
||||||
friend complex operator*(const complex& c1, const complex& c2);
|
|
||||||
friend complex operator/(const complex& c1, const complex& c2);
|
|
||||||
|
|
||||||
friend complex operator*(const scalar s, const complex& c);
|
friend complex operator+(const complex& c1, const complex& c2);
|
||||||
|
friend complex operator+(const complex& c, const scalar s);
|
||||||
|
friend complex operator+(const scalar s, const complex& c);
|
||||||
|
|
||||||
|
friend complex operator-(const complex& c1, const complex& c2);
|
||||||
|
friend complex operator-(const complex& c, const scalar s);
|
||||||
|
friend complex operator-(const scalar s, const complex& c);
|
||||||
|
|
||||||
|
friend complex operator*(const complex& c1, const complex& c2);
|
||||||
friend complex operator*(const complex& c, const scalar s);
|
friend complex operator*(const complex& c, const scalar s);
|
||||||
|
friend complex operator*(const scalar s, const complex& c);
|
||||||
|
|
||||||
|
friend complex operator/(const complex& c1, const complex& c2);
|
||||||
friend complex operator/(const complex& c, const scalar s);
|
friend complex operator/(const complex& c, const scalar s);
|
||||||
friend complex operator/(const scalar s, const complex& c);
|
friend complex operator/(const scalar s, const complex& c);
|
||||||
};
|
};
|
||||||
|
@ -129,6 +129,13 @@ inline void Foam::complex::operator=(const Foam::zero)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Foam::complex::operator=(const scalar s)
|
||||||
|
{
|
||||||
|
re = s;
|
||||||
|
im = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator+=(const complex& c)
|
inline void Foam::complex::operator+=(const complex& c)
|
||||||
{
|
{
|
||||||
re += c.re;
|
re += c.re;
|
||||||
@ -136,6 +143,12 @@ inline void Foam::complex::operator+=(const complex& c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Foam::complex::operator+=(const scalar s)
|
||||||
|
{
|
||||||
|
re += s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator-=(const complex& c)
|
inline void Foam::complex::operator-=(const complex& c)
|
||||||
{
|
{
|
||||||
re -= c.re;
|
re -= c.re;
|
||||||
@ -143,37 +156,18 @@ inline void Foam::complex::operator-=(const complex& c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator*=(const complex& c)
|
|
||||||
{
|
|
||||||
*this = (*this)*c;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator/=(const complex& c)
|
|
||||||
{
|
|
||||||
*this = *this/c;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator=(const scalar s)
|
|
||||||
{
|
|
||||||
re = s;
|
|
||||||
im = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator+=(const scalar s)
|
|
||||||
{
|
|
||||||
re += s;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator-=(const scalar s)
|
inline void Foam::complex::operator-=(const scalar s)
|
||||||
{
|
{
|
||||||
re -= s;
|
re -= s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Foam::complex::operator*=(const complex& c)
|
||||||
|
{
|
||||||
|
*this = (*this)*c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator*=(const scalar s)
|
inline void Foam::complex::operator*=(const scalar s)
|
||||||
{
|
{
|
||||||
re *= s;
|
re *= s;
|
||||||
@ -181,6 +175,12 @@ inline void Foam::complex::operator*=(const scalar s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Foam::complex::operator/=(const complex& c)
|
||||||
|
{
|
||||||
|
*this = *this/c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void Foam::complex::operator/=(const scalar s)
|
inline void Foam::complex::operator/=(const scalar s)
|
||||||
{
|
{
|
||||||
re /= s;
|
re /= s;
|
||||||
@ -288,6 +288,12 @@ inline complex transform(const Tensor<scalar>&, const complex c)
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
inline complex operator-(const complex& c)
|
||||||
|
{
|
||||||
|
return complex(-c.re, -c.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline complex operator+(const complex& c1, const complex& c2)
|
inline complex operator+(const complex& c1, const complex& c2)
|
||||||
{
|
{
|
||||||
return complex
|
return complex
|
||||||
@ -298,13 +304,15 @@ inline complex operator+(const complex& c1, const complex& c2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline complex operator-(const complex& c)
|
inline complex operator+(const complex& c, const scalar s)
|
||||||
{
|
{
|
||||||
return complex
|
return complex(c.re + s, c.im);
|
||||||
(
|
}
|
||||||
-c.re,
|
|
||||||
-c.im
|
|
||||||
);
|
inline complex operator+(const scalar s, const complex& c)
|
||||||
|
{
|
||||||
|
return complex(c.re + s, c.im);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -318,6 +326,18 @@ inline complex operator-(const complex& c1, const complex& c2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline complex operator-(const complex& c, const scalar s)
|
||||||
|
{
|
||||||
|
return complex(c.re - s, c.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline complex operator-(const scalar s, const complex& c)
|
||||||
|
{
|
||||||
|
return complex(s - c.re, -c.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline complex operator*(const complex& c1, const complex& c2)
|
inline complex operator*(const complex& c1, const complex& c2)
|
||||||
{
|
{
|
||||||
return complex
|
return complex
|
||||||
@ -328,6 +348,18 @@ inline complex operator*(const complex& c1, const complex& c2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline complex operator*(const complex& c, const scalar s)
|
||||||
|
{
|
||||||
|
return complex(s*c.re, s*c.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline complex operator*(const scalar s, const complex& c)
|
||||||
|
{
|
||||||
|
return complex(s*c.re, s*c.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline complex operator/(const complex& c1, const complex& c2)
|
inline complex operator/(const complex& c1, const complex& c2)
|
||||||
{
|
{
|
||||||
const scalar sqrC2 = magSqr(c2);
|
const scalar sqrC2 = magSqr(c2);
|
||||||
@ -340,18 +372,6 @@ inline complex operator/(const complex& c1, const complex& c2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline complex operator*(const scalar s, const complex& c)
|
|
||||||
{
|
|
||||||
return complex(s*c.re, s*c.im);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline complex operator*(const complex& c, const scalar s)
|
|
||||||
{
|
|
||||||
return complex(s*c.re, s*c.im);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline complex operator/(const complex& c, const scalar s)
|
inline complex operator/(const complex& c, const scalar s)
|
||||||
{
|
{
|
||||||
return complex(c.re/s, c.im/s);
|
return complex(c.re/s, c.im/s);
|
||||||
@ -360,7 +380,7 @@ inline complex operator/(const complex& c, const scalar s)
|
|||||||
|
|
||||||
inline complex operator/(const scalar s, const complex& c)
|
inline complex operator/(const scalar s, const complex& c)
|
||||||
{
|
{
|
||||||
return complex(s/c.re, s/c.im);
|
return complex(s)/c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user