Browse Source

Math: complex number multiplication.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
b509ff02ea
  1. 12
      src/Math/Complex.h
  2. 11
      src/Math/Test/ComplexTest.cpp

12
src/Math/Complex.h

@ -171,6 +171,18 @@ template<class T> class Complex {
return Complex<T>(*this) /= scalar; return Complex<T>(*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<T> operator*(const Complex<T>& other) const {
return {_real*other._real - _imaginary*other._imaginary,
_imaginary*other._real + _real*other._imaginary};
}
private: private:
T _real, _imaginary; T _real, _imaginary;
}; };

11
src/Math/Test/ComplexTest.cpp

@ -33,6 +33,7 @@ class ComplexTest: public Corrade::TestSuite::Tester {
void addSubtract(); void addSubtract();
void negated(); void negated();
void multiplyDivideScalar(); void multiplyDivideScalar();
void multiply();
void debug(); void debug();
}; };
@ -47,6 +48,7 @@ ComplexTest::ComplexTest() {
&ComplexTest::addSubtract, &ComplexTest::addSubtract,
&ComplexTest::negated, &ComplexTest::negated,
&ComplexTest::multiplyDivideScalar, &ComplexTest::multiplyDivideScalar,
&ComplexTest::multiply,
&ComplexTest::debug); &ComplexTest::debug);
} }
@ -117,6 +119,15 @@ void ComplexTest::multiplyDivideScalar() {
CORRADE_COMPARE(-2.0f/a, c); 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() { void ComplexTest::debug() {
std::ostringstream o; std::ostringstream o;

Loading…
Cancel
Save