cubic_splines.cpp
Go to the documentation of this file.
```00001
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012
00013 #include <string>
00014 #include <gtest/gtest.h>
00015 #include <ecl/containers/array.hpp>
00016 #include "../../include/ecl/geometry/cubic_spline.hpp"
00017
00018 //#include <iostream>
00019 //#include <ecl/formatters/floats.hpp>
00020 //#include <ecl/formatters/strings.hpp>
00021
00022 /*****************************************************************************
00023 ** Using
00024 *****************************************************************************/
00025
00026 //using std::cout; using std::endl;
00027 using std::string;
00028 using ecl::Array;
00029 //using ecl::RightAlign;
00030 //using ecl::Format;
00031 using ecl::CubicPolynomial;
00032 using ecl::CubicSpline;
00033
00034 /*****************************************************************************
00035 ** Tests
00036 *****************************************************************************/
00037
00038 TEST(CubicSplinesTests,derivativeHeuristic) {
00039     Array<double> x_set(3);
00040     Array<double> y_set(3);
00041     x_set << 0.688792, 1.15454, 1.67894;
00042     y_set << -0.75, -1.2, -1.30;
00043     double ydot_0 = -0.5/x_set[0];
00044     double ydot_f = 1.04/3.72;
00045
00046     CubicSpline cubic = CubicSpline::DerivativeHeuristic(x_set, y_set, ydot_0, ydot_f);
00047     // cout << cubic << endl;
00048     //
00049     // -2.08 + 5.96x^1 + -7.85x^2 + 2.90x^3 <---- first cubic
00050     // -0.22 + -0.72x^1 + -0.46x^2 + 0.30x^3 <---- second cubic
00051     //
00052     const Array<double>& domain = cubic.domain();
00053     EXPECT_EQ(0.688792, domain[0]);
00054     EXPECT_EQ(1.15454, domain[1]);
00055     EXPECT_EQ(1.67894, domain[2]);
00056     const CubicPolynomial &p1 = cubic.polynomials()[0];
00057     const CubicPolynomial &p2 = cubic.polynomials()[1];
00058     // allow for some roundoff errors
00059     EXPECT_GT(-2.07,p1.coefficients()[0]); EXPECT_LT(-2.09,p1.coefficients()[0]);
00060     EXPECT_GT(5.97,p1.coefficients()[1]); EXPECT_LT(5.95,p1.coefficients()[1]);
00061     EXPECT_GT(-7.84,p1.coefficients()[2]); EXPECT_LT(-7.86,p1.coefficients()[2]);
00062     EXPECT_GT(2.91,p1.coefficients()[3]); EXPECT_LT(2.89,p1.coefficients()[3]);
00063
00064     EXPECT_GT(-0.21,p2.coefficients()[0]); EXPECT_LT(-0.23,p2.coefficients()[0]);
00065     EXPECT_GT(-0.71,p2.coefficients()[1]); EXPECT_LT(-0.73,p2.coefficients()[1]);
00066     EXPECT_GT(-0.45,p2.coefficients()[2]); EXPECT_LT(-0.47,p2.coefficients()[2]);
00067     EXPECT_GT(0.31,p2.coefficients()[3]); EXPECT_LT(0.29,p2.coefficients()[3]);
00068
00069 //    /*********************
00070 //    ** Output
00071 //    **********************/
00072 //    Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
00073 //    Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
00074 //    cout << string_format("x  ");
00075 //    cout << string_format("y  ");
00076 //    cout << string_format("y' ");
00077 //    cout << string_format("y''") << endl;
00078 //    int n = 50;
00079 //    for ( int i = 0; i <= n; ++i ) {
00080 //        double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
00081 //        cout << format(x);
00082 //        cout << format(cubic(x));
00083 //        cout << format(cubic.derivative(x));
00084 //        cout << format(cubic.dderivative(x)) << endl;
00085 //    }
00086 }
00087
00088
00089 TEST(CubicSplinesTests,continuousHeuristic) {
00090     Array<double> x_set(3);
00091     Array<double> y_set(3);
00092     x_set << 0.688792, 1.15454, 1.67894;
00093     y_set << -0.75, -1.2, -1.30;
00094     double ydot_0 = -0.5/x_set[0];
00095     double ydot_f = 1.04/3.72;
00096         CubicSpline cubic = CubicSpline::ContinuousDerivatives(x_set, y_set, ydot_0, ydot_f);
00097     const CubicPolynomial &p1 = cubic.polynomials()[0];
00098     const CubicPolynomial &p2 = cubic.polynomials()[1];
00099     // allow for some roundoff errors
00100     EXPECT_GT(-1.57,p1.coefficients()[0]); EXPECT_LT(-1.59,p1.coefficients()[0]);
00101     EXPECT_GT(4.10,p1.coefficients()[1]); EXPECT_LT(4.08,p1.coefficients()[1]);
00102     EXPECT_GT(-5.53,p1.coefficients()[2]); EXPECT_LT(-5.55,p1.coefficients()[2]);
00103     EXPECT_GT(2.00,p1.coefficients()[3]); EXPECT_LT(1.98,p1.coefficients()[3]);
00104
00105     EXPECT_GT(2.13,p2.coefficients()[0]); EXPECT_LT(2.11,p2.coefficients()[0]);
00106     EXPECT_GT(-5.51,p2.coefficients()[1]); EXPECT_LT(-5.53,p2.coefficients()[1]);
00107     EXPECT_GT(2.79,p2.coefficients()[2]); EXPECT_LT(2.77,p2.coefficients()[2]);
00108     EXPECT_GT(-0.41,p2.coefficients()[3]); EXPECT_LT(-0.43,p2.coefficients()[3]);
00109 //    /*********************
00110 //    ** Output
00111 //    **********************/
00112 //        std::cout << cubic << std::endl;
00113 //    Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
00114 //    Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
00115 //    cout << string_format("x  ");
00116 //    cout << string_format("y  ");
00117 //    cout << string_format("y' ");
00118 //    cout << string_format("y''") << endl;
00119 //    int n = 50;
00120 //    for ( int i = 0; i <= n; ++i ) {
00121 //        double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
00122 //        cout << format(x);
00123 //        cout << format(cubic_c2(x));
00124 //        cout << format(cubic_c2.derivative(x));
00125 //        cout << format(cubic_c2.dderivative(x)) << endl;
00126 //    }
00127 }
00128
00129 TEST(CubicSplinesTests,naturalSpline) {
00130     Array<double> x_set(3);
00131     Array<double> y_set(3);
00132     x_set << 0.688792, 1.15454, 1.67894;
00133     y_set << -0.75, -1.2, -1.30;
00134
00135     CubicSpline cubic = CubicSpline::Natural(x_set, y_set);
00136     const CubicPolynomial &p1 = cubic.polynomials()[0];
00137     const CubicPolynomial &p2 = cubic.polynomials()[1];
00138     // allow for some roundoff errors
00139     EXPECT_GT(-0.22,p1.coefficients()[0]); EXPECT_LT(-0.24,p1.coefficients()[0]);
00140     EXPECT_GT(0.06,p1.coefficients()[1]); EXPECT_LT(0.04,p1.coefficients()[1]);
00141     EXPECT_GT(-1.73,p1.coefficients()[2]); EXPECT_LT(-1.75,p1.coefficients()[2]);
00142     EXPECT_GT(0.85,p1.coefficients()[3]); EXPECT_LT(0.83,p1.coefficients()[3]);
00143
00144     EXPECT_GT(2.22,p2.coefficients()[0]); EXPECT_LT(2.20,p2.coefficients()[0]);
00145     EXPECT_GT(-6.29,p2.coefficients()[1]); EXPECT_LT(-6.31,p2.coefficients()[1]);
00146     EXPECT_GT(3.77,p2.coefficients()[2]); EXPECT_LT(3.75,p2.coefficients()[2]);
00147     EXPECT_GT(-0.74,p2.coefficients()[3]); EXPECT_LT(-0.76,p2.coefficients()[3]);
00148
00149 //    /*********************
00150 //    ** Output
00151 //    **********************/
00152 //    Format<string> string_format; string_format.width(6); string_format.align(RightAlign);
00153 //    Format<double> format; format.width(6); format.precision(2); format.align(RightAlign);
00154 //    cout << string_format("x  ");
00155 //    cout << string_format("y  ");
00156 //    cout << string_format("y' ");
00157 //    cout << string_format("y''") << endl;
00158 //    int n = 50;
00159 //    for ( int i = 0; i <= n; ++i ) {
00160 //        double x = x_set[0] + i*(x_set.back()-x_set.front())/n;
00161 //        cout << format(x);
00162 //        cout << format(cubic(x));
00163 //        cout << format(cubic.derivative(x));
00164 //        cout << format(cubic.dderivative(x)) << endl;
00165 //    }
00166 }
00167
00168 /*****************************************************************************
00169 ** Main program
00170 *****************************************************************************/
00171
00172 int main(int argc, char **argv) {
00173