roots.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <test_common.h>
  2. #include <igl/cycodebase/roots.h>
  3. TEST_CASE("roots: cubic", "[igl/cycodebase]" )
  4. {
  5. // t^3 - 6*t^2 + 11*t - 6
  6. {
  7. Eigen::Vector<double,4> coef(4);
  8. coef << -6,11,-6,1;
  9. Eigen::Vector<double,3> R;
  10. const int nr = igl::cycodebase::roots(coef, 0.0, 4.0, R);
  11. REQUIRE(nr == 3);
  12. REQUIRE(R[0] == Approx(1.0).margin(1e-12));
  13. REQUIRE(R[1] == Approx(2.0).margin(1e-12));
  14. REQUIRE(R[2] == Approx(3.0).margin(1e-12));
  15. }
  16. // With dynamic size
  17. {
  18. Eigen::VectorXd coef(4);
  19. coef << -6,11,-6,1;
  20. {
  21. Eigen::VectorXd R;
  22. const int nr = igl::cycodebase::roots(coef, 0.0, 4.0, R);
  23. REQUIRE(nr == 3);
  24. REQUIRE(R.size() == 3);
  25. REQUIRE(R[0] == Approx(1.0).margin(1e-12));
  26. REQUIRE(R[1] == Approx(2.0).margin(1e-12));
  27. REQUIRE(R[2] == Approx(3.0).margin(1e-12));
  28. }
  29. // Only pick the first root
  30. {
  31. Eigen::VectorXd R;
  32. const int nr = igl::cycodebase::roots(coef, 0.0, 1.5, R);
  33. REQUIRE(nr == 1);
  34. REQUIRE(R.size() == 3);
  35. REQUIRE(R[0] == Approx(1.0).margin(1e-12));
  36. REQUIRE(std::isnan(R[1]));
  37. REQUIRE(std::isnan(R[2]));
  38. }
  39. }
  40. }