cubic_splines.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <string>
14 #include <gtest/gtest.h>
15 #include <ecl/containers/array.hpp>
16 #include "../../include/ecl/geometry/cubic_spline.hpp"
17 
18 //#include <iostream>
19 //#include <ecl/formatters/floats.hpp>
20 //#include <ecl/formatters/strings.hpp>
21 
22 /*****************************************************************************
23 ** Using
24 *****************************************************************************/
25 
26 //using std::cout; using std::endl;
27 using std::string;
28 using ecl::Array;
29 //using ecl::RightAlign;
30 //using ecl::Format;
32 using ecl::CubicSpline;
33 
34 /*****************************************************************************
35 ** Tests
36 *****************************************************************************/
37 
38 TEST(CubicSplinesTests,derivativeHeuristic) {
39  Array<double> x_set(3);
40  Array<double> y_set(3);
41  x_set << 0.688792, 1.15454, 1.67894;
42  y_set << -0.75, -1.2, -1.30;
43  double ydot_0 = -0.5/x_set[0];
44  double ydot_f = 1.04/3.72;
45 
46  CubicSpline cubic = CubicSpline::DerivativeHeuristic(x_set, y_set, ydot_0, ydot_f);
47  // cout << cubic << endl;
48  //
49  // -2.08 + 5.96x^1 + -7.85x^2 + 2.90x^3 <---- first cubic
50  // -0.22 + -0.72x^1 + -0.46x^2 + 0.30x^3 <---- second cubic
51  //
52  const Array<double>& domain = cubic.domain();
53  EXPECT_EQ(0.688792, domain[0]);
54  EXPECT_EQ(1.15454, domain[1]);
55  EXPECT_EQ(1.67894, domain[2]);
56  const CubicPolynomial &p1 = cubic.polynomials()[0];
57  const CubicPolynomial &p2 = cubic.polynomials()[1];
58  // allow for some roundoff errors
59  EXPECT_GT(-2.07,p1.coefficients()[0]); EXPECT_LT(-2.09,p1.coefficients()[0]);
60  EXPECT_GT(5.97,p1.coefficients()[1]); EXPECT_LT(5.95,p1.coefficients()[1]);
61  EXPECT_GT(-7.84,p1.coefficients()[2]); EXPECT_LT(-7.86,p1.coefficients()[2]);
62  EXPECT_GT(2.91,p1.coefficients()[3]); EXPECT_LT(2.89,p1.coefficients()[3]);
63 
64  EXPECT_GT(-0.21,p2.coefficients()[0]); EXPECT_LT(-0.23,p2.coefficients()[0]);
65  EXPECT_GT(-0.71,p2.coefficients()[1]); EXPECT_LT(-0.73,p2.coefficients()[1]);
66  EXPECT_GT(-0.45,p2.coefficients()[2]); EXPECT_LT(-0.47,p2.coefficients()[2]);
67  EXPECT_GT(0.31,p2.coefficients()[3]); EXPECT_LT(0.29,p2.coefficients()[3]);
68 
69 // /*********************
70 // ** Output
71 // **********************/
72 // Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
73 // Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
74 // cout << string_format("x ");
75 // cout << string_format("y ");
76 // cout << string_format("y' ");
77 // cout << string_format("y''") << endl;
78 // int n = 50;
79 // for ( int i = 0; i <= n; ++i ) {
80 // double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
81 // cout << format(x);
82 // cout << format(cubic(x));
83 // cout << format(cubic.derivative(x));
84 // cout << format(cubic.dderivative(x)) << endl;
85 // }
86 }
87 
88 
89 TEST(CubicSplinesTests,continuousHeuristic) {
90  Array<double> x_set(3);
91  Array<double> y_set(3);
92  x_set << 0.688792, 1.15454, 1.67894;
93  y_set << -0.75, -1.2, -1.30;
94  double ydot_0 = -0.5/x_set[0];
95  double ydot_f = 1.04/3.72;
96  CubicSpline cubic = CubicSpline::ContinuousDerivatives(x_set, y_set, ydot_0, ydot_f);
97  const CubicPolynomial &p1 = cubic.polynomials()[0];
98  const CubicPolynomial &p2 = cubic.polynomials()[1];
99  // allow for some roundoff errors
100  EXPECT_GT(-1.57,p1.coefficients()[0]); EXPECT_LT(-1.59,p1.coefficients()[0]);
101  EXPECT_GT(4.10,p1.coefficients()[1]); EXPECT_LT(4.08,p1.coefficients()[1]);
102  EXPECT_GT(-5.53,p1.coefficients()[2]); EXPECT_LT(-5.55,p1.coefficients()[2]);
103  EXPECT_GT(2.00,p1.coefficients()[3]); EXPECT_LT(1.98,p1.coefficients()[3]);
104 
105  EXPECT_GT(2.13,p2.coefficients()[0]); EXPECT_LT(2.11,p2.coefficients()[0]);
106  EXPECT_GT(-5.51,p2.coefficients()[1]); EXPECT_LT(-5.53,p2.coefficients()[1]);
107  EXPECT_GT(2.79,p2.coefficients()[2]); EXPECT_LT(2.77,p2.coefficients()[2]);
108  EXPECT_GT(-0.41,p2.coefficients()[3]); EXPECT_LT(-0.43,p2.coefficients()[3]);
109 // /*********************
110 // ** Output
111 // **********************/
112 // std::cout << cubic << std::endl;
113 // Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
114 // Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
115 // cout << string_format("x ");
116 // cout << string_format("y ");
117 // cout << string_format("y' ");
118 // cout << string_format("y''") << endl;
119 // int n = 50;
120 // for ( int i = 0; i <= n; ++i ) {
121 // double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
122 // cout << format(x);
123 // cout << format(cubic_c2(x));
124 // cout << format(cubic_c2.derivative(x));
125 // cout << format(cubic_c2.dderivative(x)) << endl;
126 // }
127 }
128 
129 TEST(CubicSplinesTests,naturalSpline) {
130  Array<double> x_set(3);
131  Array<double> y_set(3);
132  x_set << 0.688792, 1.15454, 1.67894;
133  y_set << -0.75, -1.2, -1.30;
134 
135  CubicSpline cubic = CubicSpline::Natural(x_set, y_set);
136  const CubicPolynomial &p1 = cubic.polynomials()[0];
137  const CubicPolynomial &p2 = cubic.polynomials()[1];
138  // allow for some roundoff errors
139  EXPECT_GT(-0.22,p1.coefficients()[0]); EXPECT_LT(-0.24,p1.coefficients()[0]);
140  EXPECT_GT(0.06,p1.coefficients()[1]); EXPECT_LT(0.04,p1.coefficients()[1]);
141  EXPECT_GT(-1.73,p1.coefficients()[2]); EXPECT_LT(-1.75,p1.coefficients()[2]);
142  EXPECT_GT(0.85,p1.coefficients()[3]); EXPECT_LT(0.83,p1.coefficients()[3]);
143 
144  EXPECT_GT(2.22,p2.coefficients()[0]); EXPECT_LT(2.20,p2.coefficients()[0]);
145  EXPECT_GT(-6.29,p2.coefficients()[1]); EXPECT_LT(-6.31,p2.coefficients()[1]);
146  EXPECT_GT(3.77,p2.coefficients()[2]); EXPECT_LT(3.75,p2.coefficients()[2]);
147  EXPECT_GT(-0.74,p2.coefficients()[3]); EXPECT_LT(-0.76,p2.coefficients()[3]);
148 
149 // /*********************
150 // ** Output
151 // **********************/
152 // Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
153 // Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
154 // cout << string_format("x ");
155 // cout << string_format("y ");
156 // cout << string_format("y' ");
157 // cout << string_format("y''") << endl;
158 // int n = 50;
159 // for ( int i = 0; i <= n; ++i ) {
160 // double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
161 // cout << format(x);
162 // cout << format(cubic(x));
163 // cout << format(cubic.derivative(x));
164 // cout << format(cubic.dderivative(x)) << endl;
165 // }
166 }
167 
168 /*****************************************************************************
169 ** Main program
170 *****************************************************************************/
171 
172 int main(int argc, char **argv) {
173 
174  testing::InitGoogleTest(&argc,argv);
175  return RUN_ALL_TESTS();
176 }
177 
178 
179 
180 
TEST(CubicSplinesTests, derivativeHeuristic)
const Array< double > & domain()
The discretised domain for this spline.
Storage container for a cubic spline interpolation.
const Array< CubicPolynomial > & polynomials()
The polynomial sequence.
int main(int argc, char **argv)
Polynomial< 3 > CubicPolynomial
Mathematical term for 3rd order polynomials.
Definition: polynomial.hpp:389


ecl_geometry
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:37