浏览代码

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

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 9 年前
父节点
当前提交
51d8ed6458
共有 2 个文件被更改,包括 14 次插入13 次删除
  1. +13
    -11
      src/lol/math/polynomial.h
  2. +1
    -2
      src/t/math/polynomial.cpp

+ 13
- 11
src/lol/math/polynomial.h 查看文件

@@ -225,20 +225,22 @@ struct polynomial
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 };

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. */


+ 1
- 2
src/t/math/polynomial.cpp 查看文件

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

// 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[1], -2, 1e-6);
lolunit_assert_doubles_equal(roots1[2], -1, 1e-3);
}

lolunit_declare_test(degree_3_three_roots)


正在加载...
取消
保存