diff --git a/src/Math/Complex.h b/src/Math/Complex.h index 06ba2f59f..71710f371 100644 --- a/src/Math/Complex.h +++ b/src/Math/Complex.h @@ -171,6 +171,18 @@ template class Complex { return Complex(*this) /= scalar; } + /** + * @brief Multiply with complex number + * + * @f[ + * c_0 c_1 = (a_0 + ib_0)(a_1 + ib_1) = (a_0 a_1 - b_0 b_1) + i(a_1 b_0 + a_0 b_1) + * @f] + */ + inline Complex operator*(const Complex& other) const { + return {_real*other._real - _imaginary*other._imaginary, + _imaginary*other._real + _real*other._imaginary}; + } + private: T _real, _imaginary; }; diff --git a/src/Math/Test/ComplexTest.cpp b/src/Math/Test/ComplexTest.cpp index f4c352252..8c06bcf2f 100644 --- a/src/Math/Test/ComplexTest.cpp +++ b/src/Math/Test/ComplexTest.cpp @@ -33,6 +33,7 @@ class ComplexTest: public Corrade::TestSuite::Tester { void addSubtract(); void negated(); void multiplyDivideScalar(); + void multiply(); void debug(); }; @@ -47,6 +48,7 @@ ComplexTest::ComplexTest() { &ComplexTest::addSubtract, &ComplexTest::negated, &ComplexTest::multiplyDivideScalar, + &ComplexTest::multiply, &ComplexTest::debug); } @@ -117,6 +119,15 @@ void ComplexTest::multiplyDivideScalar() { CORRADE_COMPARE(-2.0f/a, c); } +void ComplexTest::multiply() { + Complex a( 5.0f, 3.0f); + Complex b( 6.0f, -7.0f); + Complex c(51.0f, -17.0f); + + CORRADE_COMPARE(a*b, c); + CORRADE_COMPARE(b*a, c); +} + void ComplexTest::debug() { std::ostringstream o;