Skip to content

Commit

Permalink
Merge branch 'master' of bansci.com:stator
Browse files Browse the repository at this point in the history
  • Loading branch information
toastedcrumpets committed Sep 2, 2022
2 parents 00e4578 + b34b4cc commit fdb35c5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion stator/symbolic/polynomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,9 @@ namespace sym {

template<class Coeff_t2>
size_t roots(const Coeff_t2& a, const Coeff_t2& b) const {
return std::abs(int(sign_changes(a)) - int(sign_changes(b)));
int sign_changes_a = sign_changes(a);
int sign_changes_b = sign_changes(b);
return std::abs(sign_changes_a - sign_changes_b);
}


Expand Down
6 changes: 6 additions & 0 deletions stator/unit_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
# define UNIT_TEST_CHECK_SMALL(A, TOL) ASSERT_NEAR(A, 0, TOL);
# define UNIT_TEST_ERROR(MSG)

# define DISABLED_UNIT_TEST_CHECK_EQUAL(A, B) {}
# define DISABLED_UNIT_TEST_CHECK(Expr) {}
# define DISABLED_UNIT_TEST_CHECK_CLOSE(A, B, TOL) {}
# define DISABLED_UNIT_TEST_CHECK_SMALL(A, TOL) {}
# define DISABLED_UNIT_TEST_ERROR(MSG) {}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
50 changes: 43 additions & 7 deletions tests/symbolic_poly_solve_roots_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ void compare_roots(T1 roots, T2 actual_roots, Func f, double tolerance = 0.00124
}
}

UNIT_TEST( quartic_root_tests)
{
//Equations with zeros in them are a problem for the real root solver
auto roots = sym::solve_real_roots(sym::Polynomial<4> {1, 0, 0, 1, 1});
UNIT_TEST_CHECK_EQUAL(roots.size(), 0u);
}

UNIT_TEST(poly_quadratic_roots_simple)
{
using namespace sym;
Expand Down Expand Up @@ -693,3 +686,46 @@ UNIT_TEST( generic_solve_real_roots_2 )
//Roots are, -1+-i, 0.5*(1+-i\sqrt{11})
//std::cout << LinBairstowSolve(Polynomial<4>{6,4,3,1,1}, 1e-14) << std::endl;
}

UNIT_TEST( quartic_root_tests)
{
//Equations with zeros in them are an edge case for the bounds check in the Sturm solver
{
auto roots = sym::solve_real_roots(sym::Polynomial<4> {1, 0, 0, 1, 1});
UNIT_TEST_CHECK_EQUAL(roots.size(), 0u);
}


{
//https://www.wolframalpha.com/input?i2d=true&i=35842-x*57720%2Bx*x*59799.999999999985448084771633148-x*x*x*5.2252677024996035907674102414044e-12%2Bx*x*x*x*2.0194839173657902218540251271239e-28
sym::Polynomial<4> g = {
35842,
-57720,
59799.999999999985448084771633148,
-5.2252677024996035907674102414044e-12,
2.0194839173657902218540251271239e-28
};

auto roots = sym::solve_real_roots(g);
DISABLED_UNIT_TEST_CHECK_EQUAL(roots.size(), 0u);

//https://www.wolframalpha.com/input?i2d=true&i=14482.000000000001818989403545856-83591.999999999985448084771633148*x%2B84887.999999999970896169543266296*x*x%2Bx*x*x*1.3022974889306702395208061776649e-11%2Bx*x*x*x*8.0779356694631608874161005084957e-28
sym::Polynomial<4> f = {
14482.000000000001818989403545856,
-83591.999999999985448084771633148,
84887.999999999970896169543266296,
1.3022974889306702395208061776649e-11,
8.0779356694631608874161005084957e-28
};

roots = sym::solve_real_roots(f);
DISABLED_UNIT_TEST_CHECK_EQUAL(roots.size(), 2u);

sym::StackVector<double, 2> f_roots{
0.2243674923730396943773527317913,
0.760365332054441265251282862558
};

compare_roots(roots, f_roots, f);
}
}

0 comments on commit fdb35c5

Please sign in to comment.