Browse Source

Math: length and normalization of DualComplex.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
d7bfd91398
  1. 29
      src/Math/DualComplex.h
  2. 27
      src/Math/Test/DualComplexTest.cpp

29
src/Math/DualComplex.h

@ -95,6 +95,35 @@ template<class T> class DualComplex: public Dual<Complex<T>> {
return {this->real().conjugated(), {-this->dual().real(), this->dual().imaginary()}};
}
/**
* @brief %Complex number length squared
*
* Should be used instead of length() for comparing complex number
* length with other values, because it doesn't compute the square root. @f[
* |\hat c|^2 = \sqrt{\hat c^* \hat c}^2 = c_0 \cdot c_0 + \epsilon 2 (c_0 \cdot c_\epsilon)
* @f]
*/
inline Dual<T> lengthSquared() const {
return {this->real().dot(), T(2)*Complex<T>::dot(this->real(), this->dual())};
}
/**
* @brief %Dual quaternion length
*
* See lengthSquared() which is faster for comparing length with other
* values. @f[
* |\hat c| = \sqrt{\hat{c^*} \hat c} = |c_0| + \epsilon \frac{c_0 \cdot c_\epsilon}{|c_0|}
* @f]
*/
inline Dual<T> length() const {
return Math::sqrt(lengthSquared());
}
/** @brief Normalized dual complex number (of unit length) */
inline DualComplex<T> normalized() const {
return (*this)/length();
}
MAGNUM_DUAL_SUBCLASS_IMPLEMENTATION(DualComplex, Complex)
private:

27
src/Math/Test/DualComplexTest.cpp

@ -29,6 +29,10 @@ class DualComplexTest: public Corrade::TestSuite::Tester {
void constExpressions();
void lengthSquared();
void length();
void normalized();
void complexConjugated();
void dualConjugated();
void conjugated();
@ -37,6 +41,7 @@ class DualComplexTest: public Corrade::TestSuite::Tester {
};
typedef Math::Complex<float> Complex;
typedef Math::Dual<float> Dual;
typedef Math::DualComplex<float> DualComplex;
DualComplexTest::DualComplexTest() {
@ -45,6 +50,10 @@ DualComplexTest::DualComplexTest() {
&DualComplexTest::constExpressions,
&DualComplexTest::lengthSquared,
&DualComplexTest::length,
&DualComplexTest::normalized,
&DualComplexTest::complexConjugated,
&DualComplexTest::dualConjugated,
&DualComplexTest::conjugated,
@ -60,6 +69,7 @@ void DualComplexTest::construct() {
void DualComplexTest::constructDefault() {
CORRADE_COMPARE(DualComplex(), DualComplex({1.0f, 0.0f}, {0.0f, 0.0f}));
CORRADE_COMPARE(DualComplex().length(), 1.0f);
}
void DualComplexTest::constExpressions() {
@ -76,6 +86,23 @@ void DualComplexTest::constExpressions() {
CORRADE_COMPARE(d, DualComplex({-1.0f, 2.5f}, {3.0f, -7.5f}));
}
void DualComplexTest::lengthSquared() {
DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f});
CORRADE_COMPARE(a.lengthSquared(), Dual(10.0f, -13.0f));
}
void DualComplexTest::length() {
DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f});
CORRADE_COMPARE(a.length(), Dual(3.162278f, -2.05548f));
}
void DualComplexTest::normalized() {
DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f});
DualComplex b({-0.316228f, 0.948683f}, {-0.0474342f, -0.0158114f});
CORRADE_COMPARE(a.normalized().length(), 1.0f);
CORRADE_COMPARE(a.normalized(), b);
}
void DualComplexTest::complexConjugated() {
DualComplex a({-1.0f, 2.5f}, {3.0f, -7.5f});
DualComplex b({-1.0f, -2.5f}, {3.0f, 7.5f});

Loading…
Cancel
Save