Browse Source

polynomial: Improving tests for double/triple roots in order 3

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 9 years ago
parent
commit
51d8ed6458
2 changed files with 14 additions and 13 deletions
  1. +13
    -11
      src/lol/math/polynomial.h
  2. +1
    -2
      src/t/math/polynomial.cpp

+ 13
- 11
src/lol/math/polynomial.h View File

@@ -225,20 +225,22 @@ struct polynomial
solutions[i] = u_norm * cos(u_angle) + v_norm * cos(v_angle); solutions[i] = u_norm * cos(u_angle) + v_norm * cos(v_angle);
} }


if (delta < 0) // 3 real solutions
return array<T> { solutions[0] - k,
solutions[1] - k,
solutions[2] - k };

if (delta > 0) // 1 real solution
if (a == d && b == c && b == T(3)*a) // triple solution
return array<T> { solutions[0] - k }; return array<T> { solutions[0] - k };


if (u_norm > 0) // 2 real solutions
return array<T> { solutions[0] - k,
solutions[1] - k };
// if root of the derivative is also root of the current polynomial, we have a double root.
for (auto root : polynomial<T>{c, T(2)*b, T(3)*a}.roots())
if (eval(root) == T(0))
return array<T> { solutions[0] - k,
solutions[1] - k };


// one triple solution
return array<T> { solutions[0] - k };
// we have 3 or 1 root depending on delta sign
if (delta > 0)
return array<T> { solutions[0] - k };
else // if (delta < 0) 3 real solutions
return array<T> { solutions[0] - k,
solutions[1] - k,
solutions[2] - k };
} }


/* It is an error to reach this point. */ /* It is an error to reach this point. */


+ 1
- 2
src/t/math/polynomial.cpp View File

@@ -287,10 +287,9 @@ lolunit_declare_fixture(polynomial_test)
auto roots1 = p.roots(); auto roots1 = p.roots();


// Should have 2 solutions only, but precision leads to 3 solutions // Should have 2 solutions only, but precision leads to 3 solutions
lolunit_assert_equal(roots1.count(), 3);
lolunit_assert_equal(roots1.count(), 2);
lolunit_assert_doubles_equal(roots1[0], -1, 1e-3); lolunit_assert_doubles_equal(roots1[0], -1, 1e-3);
lolunit_assert_doubles_equal(roots1[1], -2, 1e-6); lolunit_assert_doubles_equal(roots1[1], -2, 1e-6);
lolunit_assert_doubles_equal(roots1[2], -1, 1e-3);
} }


lolunit_declare_test(degree_3_three_roots) lolunit_declare_test(degree_3_three_roots)


Loading…
Cancel
Save