diff --git a/src/Math/DualComplex.h b/src/Math/DualComplex.h index 417d17a1f..10e3a7a5a 100644 --- a/src/Math/DualComplex.h +++ b/src/Math/DualComplex.h @@ -124,6 +124,33 @@ template class DualComplex: public Dual> { return (*this)/length(); } + /** + * @brief Inverted dual complex number + * + * See invertedNormalized() which is faster for normalized dual complex + * numbers. @f[ + * \hat c^{-1} = \frac{\hat c^*}{|\hat c|^2} + * @f] + */ + inline DualComplex inverted() const { + return complexConjugated()/lengthSquared(); + } + + /** + * @brief Inverted normalized dual complex number + * + * Equivalent to complexConjugated(). Expects that the complex number + * is normalized. @f[ + * \hat c^{-1} = \frac{\hat c^*}{|\hat c|^2} = \hat c^* + * @f] + * @see inverted() + */ + inline DualComplex invertedNormalized() const { + CORRADE_ASSERT(lengthSquared() == Dual(1), + "Math::DualComplex::invertedNormalized(): dual complex number must be normalized", {}); + return complexConjugated(); + } + MAGNUM_DUAL_SUBCLASS_IMPLEMENTATION(DualComplex, Complex) private: diff --git a/src/Math/Test/CMakeLists.txt b/src/Math/Test/CMakeLists.txt index 6a88a4676..1e1570ddd 100644 --- a/src/Math/Test/CMakeLists.txt +++ b/src/Math/Test/CMakeLists.txt @@ -28,6 +28,7 @@ set_target_properties( MathMatrix3Test MathMatrix4Test MathComplexTest + MathDualComplexTest MathQuaternionTest MathDualQuaternionTest PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT) diff --git a/src/Math/Test/DualComplexTest.cpp b/src/Math/Test/DualComplexTest.cpp index c82f310e5..15b9644f9 100644 --- a/src/Math/Test/DualComplexTest.cpp +++ b/src/Math/Test/DualComplexTest.cpp @@ -36,6 +36,8 @@ class DualComplexTest: public Corrade::TestSuite::Tester { void complexConjugated(); void dualConjugated(); void conjugated(); + void inverted(); + void invertedNormalized(); void debug(); }; @@ -57,6 +59,8 @@ DualComplexTest::DualComplexTest() { &DualComplexTest::complexConjugated, &DualComplexTest::dualConjugated, &DualComplexTest::conjugated, + &DualComplexTest::inverted, + &DualComplexTest::invertedNormalized, &DualComplexTest::debug); } @@ -121,6 +125,30 @@ void DualComplexTest::conjugated() { CORRADE_COMPARE(a.conjugated(), b); } +void DualComplexTest::inverted() { + DualComplex a({-1.0f, 2.5f}, { 3.0f, -7.5f}); + DualComplex b({-1.0f, -2.5f}, { 3.0f, 7.5f}); + + CORRADE_COMPARE(a*a.inverted(), DualComplex()); + CORRADE_COMPARE(a.inverted(), b/Dual(7.25f, -43.5f)); +} + +void DualComplexTest::invertedNormalized() { + DualComplex a({-1.0f, 2.5f}, { 3.0f, -7.5f}); + DualComplex b({-1.0f, -2.5f}, { 3.0f, 7.5f}); + + std::ostringstream o; + Error::setOutput(&o); + CORRADE_COMPARE(a.invertedNormalized(), DualComplex()); + CORRADE_COMPARE(o.str(), "Math::DualComplex::invertedNormalized(): dual complex number must be normalized\n"); + + DualComplex normalized = a.normalized(); + DualComplex inverted = normalized.invertedNormalized(); + CORRADE_COMPARE(normalized*inverted, DualComplex()); + CORRADE_COMPARE(inverted*normalized, DualComplex()); + CORRADE_COMPARE(inverted, b/Math::sqrt(Dual(7.25f, -43.5f))); +} + void DualComplexTest::debug() { std::ostringstream o;