polynomials.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <string>
14 #include <gtest/gtest.h>
17 #include "../../include/ecl/geometry/cartesian_point.hpp"
18 #include "../../include/ecl/geometry/pascals_triangle.hpp"
19 #include "../../include/ecl/geometry/polynomial.hpp"
20 
21 /*****************************************************************************
22 ** Using
23 *****************************************************************************/
24 
25 using std::string;
27 using ecl::RightAlign;
28 using ecl::Format;
29 using ecl::Division;
30 using ecl::Intersection;
35 using ecl::Polynomial;
37 
38 /*****************************************************************************
39 ** Tests
40 *****************************************************************************/
41 
42 TEST(PolynomialTests,pascalsTriangle) {
43 
44 // 1 1 1 1
45 // 1 2 3
46 // 1 3
47 // 1
48  PascalsTriangle<3> pascals_triangle_three;
49  PascalsTriangle<3>::const_iterator iter3 = pascals_triangle_three.begin();
50  EXPECT_EQ(2,*(iter3+5));
51 
52 // 1 1 1 1 1 1
53 // 1 2 3 4 5
54 // 1 3 6 10
55 // 1 4 10
56 // 1 5
57 // 1
58  PascalsTriangle<5> pascals_triangle;
59  PascalsTriangle<5>::const_iterator iter5 = pascals_triangle.begin();
60  EXPECT_EQ(3,*(iter5+8));
61 
62 // 1 1 1 1 1
63 // 1 2 3 4
64 // 1 3 6
65 // 1 4
66 // 1
67  PascalsTriangle<4> pascals_triangle_four;
68  PascalsTriangle<4>::const_iterator iter4 = pascals_triangle_four.begin();
69  EXPECT_EQ(4,*(iter4+8));
70 }
71 
72 TEST(PolynomialTests,quadratic) {
73  Polynomial<2> p;
74  p.coefficients() << 0, 1, 1;
75  EXPECT_EQ(0,p.coefficients()[0]);
76  EXPECT_EQ(1,p.coefficients()[1]);
77  EXPECT_EQ(1,p.coefficients()[2]);
78 // std::cout << "p(5.3) = " << p(5.3) << std::endl;
79  // Allow some roundoff error here
80  EXPECT_GT(33.40,p(5.3)); EXPECT_LT(33.38,p(5.3));
81  p.shift_horizontal(2);
82 // std::cout << "p(x-2)(5.3) = " << p(5.3) << std::endl;
83  EXPECT_GT(14.20,p(5.3)); EXPECT_LT(14.18,p(5.3));
84 }
85 
86 TEST(PolynomialTests,cubic) {
88  p.coefficients() << 3, 1, 1, 1;
89 // double maximum = ecl::Maximum<CubicPolynomial>()(0.0, 0.2, p);
90  double maximum = ecl::CubicPolynomial::Maximum(0.0, 0.2, p);
91  //std::cout << p << std::endl;
92  //std::cout << "Maximum [0.0, 0.2]: " << maximum << std::endl;
93  EXPECT_DOUBLE_EQ(maximum, 3.248);
94 }
95 
96 TEST(PolynomialTests,linear_function) {
97  LinearFunction f, g;
98  f = LinearFunction::PointSlopeForm(1.0,3.0,2.0);
99  g = LinearFunction::PointSlopeForm(0.0,4.0,-1.0);
100  CartesianPoint2d point = LinearFunction::Intersection(f,g);
101  EXPECT_DOUBLE_EQ(point.x(),1.0);
102  EXPECT_DOUBLE_EQ(point.y(),3.0);
103  //std::cout << "Intersection: " << point << std::endl;
104  bool caught = false;
105  Intersection<LinearFunction> intersection;
106  try {
107  point = intersection(f,f);
108  } catch ( ecl::StandardException &e ) {
109  // collinear
110  caught = true;
111  }
112  EXPECT_EQ(caught, true);
113  EXPECT_EQ(intersection.fail(),true);
114 }
115 
116 TEST(PolynomialTests,roots) {
117  ecl::Array<double> roots;
118  LinearFunction f = LinearFunction::PointSlopeForm(1.0,3.0,2.0);
119  roots = LinearFunction::Roots(f);
120  EXPECT_EQ(1,roots.size());
121  if ( roots.size() > 0 ) {
122  EXPECT_DOUBLE_EQ(-0.5,roots[0]);
123  }
125  q.coefficients() << 1.0, 2.0, 1.0; // x^2 + 2x + 1 (single root)
126  roots = QuadraticPolynomial::Roots(q);
127  EXPECT_EQ(1,roots.size());
128  if ( roots.size() > 0 ) {
129  EXPECT_DOUBLE_EQ(-1.0,roots[0]);
130  }
131  q.coefficients() << 2.0, 2.0, 1.0; // x^2 + 2x + 2 (no roots)
132  roots = QuadraticPolynomial::Roots(q);
133  EXPECT_EQ(0,roots.size());
134  q.coefficients() << -2.0, 1.0, 1.0; // x^2 + x -2 (two roots)
135  roots = QuadraticPolynomial::Roots(q);
136  EXPECT_EQ(2,roots.size());
137  EXPECT_DOUBLE_EQ(1,roots[0]);
138  EXPECT_DOUBLE_EQ(-2,roots[1]);
139  CubicPolynomial c;
140  c.coefficients() << -6.0, 1.0, 4.0, 1.0; // x^3 + 4x^2 + x - 6 = (x-1)(x+2)(x+3) three roots
141  roots = CubicPolynomial::Roots(c);
142  EXPECT_EQ(3,roots.size());
143  if ( roots.size() == 3 ) {
144  EXPECT_DOUBLE_EQ(1,roots[0]);
145  EXPECT_DOUBLE_EQ(-2,roots[1]);
146  EXPECT_DOUBLE_EQ(-3,roots[2]);
147  }
148  c.coefficients() << 0, 0, 0, 1.0; // x^3 one triple root
149  roots = CubicPolynomial::Roots(c);
150  EXPECT_EQ(1,roots.size());
151  if ( roots.size() == 1 ) {
152  EXPECT_DOUBLE_EQ(0,roots[0]);
153  }
154  c.coefficients() << -1.0, 1.0, -1.0, 1.0; // (x-1)^3 shifted by one (one real, two complex)
155  roots = CubicPolynomial::Roots(c);
156  EXPECT_EQ(1,roots.size());
157  if ( roots.size() == 1 ) {
158  EXPECT_DOUBLE_EQ(1,roots[0]);
159  }
160 }
161 TEST(PolynomialTests,division) {
162  CubicPolynomial c;
163  c.coefficients() << -6.0, 1.0, 4.0, 1.0; // x^3 + 4x^2 + x - 6 = (x-1)(x+2)(x+3) three roots
165  double remainder;
166  QuadraticPolynomial q = CubicPolynomial::Division(c, 1, remainder); // x^2 + 5x + 6
167  EXPECT_DOUBLE_EQ(6.0, q.coefficients()[0]);
168  EXPECT_DOUBLE_EQ(5.0, q.coefficients()[1]);
169  EXPECT_DOUBLE_EQ(1.0, q.coefficients()[2]);
170  EXPECT_DOUBLE_EQ(0.0, remainder);
171  LinearFunction f = QuadraticPolynomial::Division(q, -2, remainder);
172  EXPECT_DOUBLE_EQ(3.0, f.coefficients()[0]); // x + 3
173  EXPECT_DOUBLE_EQ(1.0, f.coefficients()[1]);
174  EXPECT_DOUBLE_EQ(0.0, remainder);
175 }
176 
177 /*****************************************************************************
178 ** Main program
179 *****************************************************************************/
180 
181 int main(int argc, char **argv) {
182 
183  // Haven't got around to gtesting these yet.
184 
185  Format<string> string_format; string_format.width(8); string_format.align(RightAlign);
186  Format<double> format; format.width(8); format.precision(2); format.align(RightAlign);
187 
188  std::cout << std::endl;
189  std::cout << "***********************************************************" << std::endl;
190  std::cout << " Polynomial Blueprints" << std::endl;
191  std::cout << "***********************************************************" << std::endl;
192  std::cout << std::endl;
193 
194  LinearFunction linear_function = ecl::blueprints::LinearInterpolation(0.0,1.0,1.0,2.0);
195  linear_function = LinearFunction::Interpolation(0.0,1.0,1.0,2.0);
196  std::cout << "Linear function [interpolation]: " << linear_function << std::endl;
197  linear_function = ecl::blueprints::LinearPointSlopeForm(1.0,3.0,2.0);
198  linear_function = LinearFunction::PointSlopeForm(1.0,3.0,2.0);
199  std::cout << "Linear function [point-slope]: " << linear_function << std::endl;
200  CubicPolynomial cubic;
201  cubic = ecl::blueprints::CubicDerivativeInterpolation(2.0,0.0,0.0,3.0,1.0,0.0);
202  cubic = CubicPolynomial::SecondDerivativeInterpolation(1.0,0.0,0.0,2.0,1.0,1.0); // same thing
203  std::cout << cubic << std::endl;
204  cubic = CubicPolynomial::DerivativeInterpolation(2.0,0.0,0.0,3.0,1.0,0.0);
205  std::cout << cubic << std::endl;
206  QuinticPolynomial quintic;
207  quintic = QuinticPolynomial::Interpolation(2.0,0.0,0.0,0.0,
208  3.0,1.0,0.0,0.0);
209  std::cout << quintic << std::endl;
210  std::cout << std::endl;
211 
212  std::cout << "Results" << std::endl;
213  std::cout << string_format("cubic");
214  std::cout << string_format("quintic") << std::endl;
215  for ( int i = 0; i <= 20; ++i ) {
216  std::cout << format(cubic(2.0 + 0.05*i));
217  std::cout << format(quintic(0.0 + 0.05*i)) << std::endl;
218 // std::cout << format(quintic(0.0 + 0.05*i)) << std::endl;
219  }
220 
221  std::cout << std::endl;
222  std::cout << "***********************************************************" << std::endl;
223  std::cout << " Polynomial Derivatives" << std::endl;
224  std::cout << "***********************************************************" << std::endl;
225  std::cout << std::endl;
226 
227  Polynomial<2> cubic_derivative = cubic.derivative();
228  Polynomial<1> cubic_dderivative = cubic_derivative.derivative();
229 
230  std::cout << "1st Derivative: " << cubic_derivative << std::endl;
231  std::cout << "2nd Derivative: " << cubic_dderivative << std::endl;
232 
233  testing::InitGoogleTest(&argc,argv);
234  return RUN_ALL_TESTS();
235 }
236 
237 
238 
ecl::Intersection
Primary template functor for the intersection of like functions.
Definition: function_math.hpp:100
ecl::PascalsTriangle
Holds the coefficients for pascal's triangle up to row N.
Definition: pascals_triangle.hpp:63
ecl::Polynomial::shift_horizontal
void shift_horizontal(const double &shift)
Horizontal shift transform.
Definition: polynomial.hpp:402
ecl::CartesianPoint2d
CartesianPoint< double, 2 > CartesianPoint2d
Eigen style convenience handle for x, y, z triples in double format.
Definition: cartesian_point.hpp:505
ecl::RightAlign
RightAlign
ecl::blueprints::CubicDerivativeInterpolation
Blueprint for interpolating a cubic polynomial between end point conditions.
Definition: polynomial.hpp:628
ecl::LinearFunction
Polynomial< 1 > LinearFunction
Mathematical term for 1st order polynomials.
Definition: polynomial.hpp:393
f
void f(int i)
ecl::QuadraticPolynomial
Polynomial< 2 > QuadraticPolynomial
Mathematical term for 2nd order polynomials.
Definition: polynomial.hpp:394
floats.hpp
ecl::StandardException
ecl::Division
Primary template functor for polynomial division.
Definition: function_math.hpp:113
strings.hpp
ecl::Polynomial
Representation of a polynomial function of n-th degree.
Definition: polynomial.hpp:50
main
int main(int argc, char **argv)
Definition: polynomials.cpp:181
ecl::Format
ecl::FunctionMath< CubicPolynomial >
Specialises the function math loader for cubics.
Definition: polynomial.hpp:1319
ecl::QuinticPolynomial
Polynomial< 5 > QuinticPolynomial
Mathematical term for 5th order polynomials.
Definition: polynomial.hpp:396
TEST
TEST(PolynomialTests, pascalsTriangle)
Definition: polynomials.cpp:42
ecl::blueprints::LinearPointSlopeForm
Blueprint for generating a linear function from slope and point pair.
Definition: polynomial.hpp:562
ecl::blueprints::LinearInterpolation
Blueprint for interpolating a linear function connecting end point conditions.
Definition: polynomial.hpp:499
ecl::Array< double >
ecl::PascalsTriangle::begin
const_iterator begin(unsigned int index=0) const
Iterator generator for diagonals of pascals triangle [begin].
Definition: pascals_triangle.hpp:236
ecl::CubicPolynomial
Polynomial< 3 > CubicPolynomial
Mathematical term for 3rd order polynomials.
Definition: polynomial.hpp:395
ecl::Polynomial::coefficients
Coefficients & coefficients()
Handle to the coefficient array, use to initialise the polynomial.
Definition: polynomial.hpp:241
ecl::Array::size
static size_type size()
ecl::Polynomial::derivative
Polynomial< N-1 > derivative() const
Generates the derivative polynomial.
Definition: polynomial.hpp:197


ecl_geometry
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:39