Browse Source

Math: conjugation and inversion of complex numbers.

Also updated related Quaternion and DualQuaternion documentation.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
fc8750c51f
  1. 41
      src/Math/Complex.h
  2. 1
      src/Math/DualQuaternion.h
  3. 1
      src/Math/Quaternion.h
  4. 1
      src/Math/Test/CMakeLists.txt
  5. 39
      src/Math/Test/ComplexTest.cpp

41
src/Math/Complex.h

@ -19,6 +19,8 @@
* @brief Class Magnum::Math::Complex * @brief Class Magnum::Math::Complex
*/ */
#include <limits>
#include <Utility/Assert.h>
#include <Utility/Debug.h> #include <Utility/Debug.h>
#include "Math/MathTypeTraits.h" #include "Math/MathTypeTraits.h"
@ -225,6 +227,45 @@ template<class T> class Complex {
return (*this)/length(); return (*this)/length();
} }
/**
* @brief Conjugated complex number
*
* @f[
* \overline c = a - ib
* @f]
*/
inline Complex<T> conjugated() const {
return {_real, -_imaginary};
}
/**
* @brief Inverted complex number
*
* See invertedNormalized() which is faster for normalized
* complex numbers. @f[
* c^{-1} = \frac{\overline c}{c \overline c} = \frac{\overline c}{a^2 + b^2}
* @f]
*/
inline Complex<T> inverted() const {
return conjugated()/dot();
}
/**
* @brief Inverted normalized complex number
*
* Equivalent to conjugated(). Expects that the complex number is
* normalized. @f[
* c^{-1} = \frac{\overline c}{c \overline c} = \frac{\overline c}{a^2 + b^2} = \overline c
* @f]
* @see inverted()
*/
inline Complex<T> invertedNormalized() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),
"Math::Complex::invertedNormalized(): complex number must be normalized",
Complex<T>(std::numeric_limits<T>::quiet_NaN(), std::numeric_limits<T>::quiet_NaN()));
return conjugated();
}
private: private:
T _real, _imaginary; T _real, _imaginary;
}; };

1
src/Math/DualQuaternion.h

@ -236,6 +236,7 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
* normalized. @f[ * normalized. @f[
* \hat q^{-1} = \frac{\hat q^*}{||\hat q||^2} = \hat q^* * \hat q^{-1} = \frac{\hat q^*}{||\hat q||^2} = \hat q^*
* @f] * @f]
* @see inverted()
*/ */
inline DualQuaternion<T> invertedNormalized() const { inline DualQuaternion<T> invertedNormalized() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(lengthSquared(), T(1)), CORRADE_ASSERT(MathTypeTraits<T>::equals(lengthSquared(), T(1)),

1
src/Math/Quaternion.h

@ -391,6 +391,7 @@ template<class T> class Quaternion {
* normalized. @f[ * normalized. @f[
* q^{-1} = \frac{q^*}{|q|^2} = q^* * q^{-1} = \frac{q^*}{|q|^2} = q^*
* @f] * @f]
* @see inverted()
*/ */
inline Quaternion<T> invertedNormalized() const { inline Quaternion<T> invertedNormalized() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)), CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),

1
src/Math/Test/CMakeLists.txt

@ -26,6 +26,7 @@ set_target_properties(
MathVectorTest MathVectorTest
MathMatrix3Test MathMatrix3Test
MathMatrix4Test MathMatrix4Test
MathComplexTest
MathDualQuaternionTest MathDualQuaternionTest
MathQuaternionTest MathQuaternionTest
PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT) PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT)

39
src/Math/Test/ComplexTest.cpp

@ -40,6 +40,10 @@ class ComplexTest: public Corrade::TestSuite::Tester {
void length(); void length();
void normalized(); void normalized();
void conjugated();
void inverted();
void invertedNormalized();
void debug(); void debug();
}; };
@ -60,6 +64,10 @@ ComplexTest::ComplexTest() {
&ComplexTest::length, &ComplexTest::length,
&ComplexTest::normalized, &ComplexTest::normalized,
&ComplexTest::conjugated,
&ComplexTest::inverted,
&ComplexTest::invertedNormalized,
&ComplexTest::debug); &ComplexTest::debug);
} }
@ -162,6 +170,37 @@ void ComplexTest::normalized() {
CORRADE_COMPARE(a.normalized().length(), 1.0f); CORRADE_COMPARE(a.normalized().length(), 1.0f);
} }
void ComplexTest::conjugated() {
CORRADE_COMPARE(Complex(-3.0f, 4.5f).conjugated(), Complex(-3.0f, -4.5f));
}
void ComplexTest::inverted() {
Complex a(-3.0f, 4.0f);
Complex b(-0.12f, -0.16f);
Complex inverted = a.inverted();
CORRADE_COMPARE(a*inverted, Complex());
CORRADE_COMPARE(inverted*a, Complex());
CORRADE_COMPARE(inverted, b);
}
void ComplexTest::invertedNormalized() {
std::ostringstream o;
Error::setOutput(&o);
Complex a(-0.6f, 0.8f);
Complex b(-0.6f, -0.8f);
Complex notInverted = (a*2).invertedNormalized();
CORRADE_VERIFY(notInverted != notInverted);
CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n");
Complex inverted = a.invertedNormalized();
CORRADE_COMPARE(a*inverted, Complex());
CORRADE_COMPARE(inverted*a, Complex());
CORRADE_COMPARE(inverted, b);
}
void ComplexTest::debug() { void ComplexTest::debug() {
std::ostringstream o; std::ostringstream o;

Loading…
Cancel
Save