10 #include <gtest/gtest.h> 17 const double eps = 1e-9;
24 double coefs_roots[][6] = {{1, -2, 1, 2, 1.0, 1.0},
25 {1, 0, -1, 2, -1.0, 1.0},
26 {1, -1, -56, 2, -7.0, 8.0},
28 {2.0, 0, 0, 2, 0, 0}};
30 for (
auto& coefs_root : coefs_roots)
32 const double a = coefs_root[0],
b = coefs_root[1],
c = coefs_root[2];
33 const int num_roots_good =
static_cast<int>(coefs_root[3]);
34 const double r1_good = coefs_root[4], r2_good = coefs_root[5];
40 "\nSolving: %.02f * x^2 + %.02f * x + %.02f = 0\n",
a,
b,
c);
42 EXPECT_EQ(num_roots, num_roots_good);
45 EXPECT_NEAR(r1, r1_good,
eps) << sTestStr;
49 EXPECT_NEAR(r2, r2_good,
eps) << sTestStr;
59 double coefs_roots[][7] = {{-6, 11, -6, 3, 1.0, 2.0, 3.0},
60 {2, 3, 4, 1, -1.650629191439386, 0, 0},
61 {0, -91, -90, 3, -1.0, -9.0, 10.0}};
63 for (
auto& coefs_root : coefs_roots)
65 const double a = coefs_root[0],
b = coefs_root[1],
c = coefs_root[2];
66 const int num_roots_good =
static_cast<int>(coefs_root[3]);
67 const double roots_good[3] = {coefs_root[4], coefs_root[5],
74 "\nSolving: x^3 + %.02f * x^2 + %.02f * x + %.02f = 0\n",
a,
b,
c);
76 EXPECT_EQ(num_roots, num_roots_good);
77 for (
int k = 0; k < num_roots; k++)
80 for (
int j = 0; j < num_roots; j++)
81 if (std::abs(roots[k] - roots_good[j]) <
eps) match =
true;
83 EXPECT_TRUE(match) << sTestStr <<
"k: " << k << std::endl;
93 double coefs_roots[][9] = {{-10, 35, -50, 24, 4, 1.0, 2.0, 3.0, 4.0},
94 {-14, 35, 50, 0, 4, -1, 0, 5, 10}};
96 for (
auto& coefs_root : coefs_roots)
98 const double a = coefs_root[0],
b = coefs_root[1],
c = coefs_root[2],
100 const int num_roots_good =
static_cast<int>(coefs_root[4]);
101 const double roots_good[4] = {coefs_root[5], coefs_root[6],
102 coefs_root[7], coefs_root[8]};
108 "\nSolving: x^4 + %.02f * x^3 + %.02f * x^2 + %.02f * x + %.02f = " 112 EXPECT_EQ(num_roots, num_roots_good);
113 for (
int k = 0; k < num_roots; k++)
116 for (
int j = 0; j < num_roots; j++)
117 if (std::abs(roots[k] - roots_good[j]) <
eps) match =
true;
119 EXPECT_TRUE(match) << sTestStr <<
"k: " << k << std::endl;
TEST(poly_roots, solve_poly2)
int solve_poly4(double *x, double a, double b, double c, double d) noexcept
Solves quartic equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method. ...
GLsizei const GLchar ** string
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLubyte GLubyte GLubyte a
int solve_poly3(double *x, double a, double b, double c) noexcept
Solves cubic equation x^3 + a*x^2 + b*x + c = 0.
int solve_poly2(double a, double b, double c, double &r1, double &r2) noexcept
Solves equation a*x^2 + b*x + c = 0.