13 #include <gtest/gtest.h> 18 const double eps = 1e-9;
25 double coefs_roots[][6] = {
26 { 1 ,-2 , 1 , 2, 1.0, 1.0 },
27 { 1 , 0 ,-1 , 2, -1.0, 1.0 },
28 { 1 ,-1 ,-56 , 2, -7.0, 8.0 },
29 { 5.0 , 0 , 1 , 0, 0, 0 },
30 { 2.0 , 0 , 0 , 2, 0, 0 }
33 const unsigned int nTests =
sizeof(coefs_roots)/
sizeof(coefs_roots[0]);
35 for (
unsigned int i=0;i<nTests;i++)
37 const double a = coefs_roots[i][0],
b = coefs_roots[i][1],
c = coefs_roots[i][2];
38 const int num_roots_good =
static_cast<int>( coefs_roots[i][3] );
39 const double r1_good = coefs_roots[i][4], r2_good = coefs_roots[i][5];
46 EXPECT_EQ(num_roots,num_roots_good);
48 EXPECT_NEAR(r1,r1_good,
eps) << sTestStr;
51 EXPECT_NEAR(r2,r2_good,
eps) << sTestStr;
61 double coefs_roots[][7] = {
62 { -6 ,11 , -6 , 3, 1.0, 2.0, 3.0 },
63 { 2 ,3 , 4 , 1, -1.650629191439386, 0, 0 },
64 { 0 ,-91 , -90 , 3, -1.0, -9.0, 10.0 }
67 const unsigned int nTests =
sizeof(coefs_roots)/
sizeof(coefs_roots[0]);
69 for (
unsigned int i=0;i<nTests;i++)
71 const double a = coefs_roots[i][0],
b = coefs_roots[i][1],
c = coefs_roots[i][2];
72 const int num_roots_good =
static_cast<int>( coefs_roots[i][3] );
73 const double roots_good[3] = { coefs_roots[i][4], coefs_roots[i][5], coefs_roots[i][6] };
80 EXPECT_EQ(num_roots,num_roots_good);
81 for (
int k=0;k<num_roots;k++) {
83 for (
int j=0;j<num_roots;j++)
84 if (std::abs(roots[k]-roots_good[j])<
eps)
87 EXPECT_TRUE(match) << sTestStr <<
"k: " << k << std::endl;
97 double coefs_roots[][9] = {
98 {-10 , 35 , -50 , 24 , 4, 1.0, 2.0, 3.0, 4.0 },
99 {-14 , 35 , 50 , 0 , 4, -1, 0, 5, 10 }
102 const unsigned int nTests =
sizeof(coefs_roots)/
sizeof(coefs_roots[0]);
104 for (
unsigned int i=0;i<nTests;i++)
106 const double a = coefs_roots[i][0],
b = coefs_roots[i][1],
c = coefs_roots[i][2], d = coefs_roots[i][3];
107 const int num_roots_good =
static_cast<int>( coefs_roots[i][4] );
108 const double roots_good[4] = { coefs_roots[i][5], coefs_roots[i][6], coefs_roots[i][7], coefs_roots[i][8] };
115 EXPECT_EQ(num_roots,num_roots_good);
116 for (
int k=0;k<num_roots;k++) {
118 for (
int j=0;j<num_roots;j++)
119 if (std::abs(roots[k]-roots_good[j])<
eps)
122 EXPECT_TRUE(match) << sTestStr <<
"k: " << k << std::endl;
int BASE_IMPEXP solve_poly3(double *x, double a, double b, double c) MRPT_NO_THROWS
Solves cubic equation x^3 + a*x^2 + b*x + c = 0.
int BASE_IMPEXP solve_poly4(double *x, double a, double b, double c, double d) MRPT_NO_THROWS
Solves quartic equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method. ...
TEST(poly_roots, solve_poly2)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
int BASE_IMPEXP solve_poly2(double a, double b, double c, double &r1, double &r2) MRPT_NO_THROWS
Solves equation a*x^2 + b*x + c = 0.
GLubyte GLubyte GLubyte a