DGtal  1.5.beta
testLagrangeInterpolation.cpp
Go to the documentation of this file.
1
31 #include <iostream>
32 #include <vector>
33 #include <algorithm>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/math/LagrangeInterpolation.h"
36 #include "DGtal/math/MPolynomial.h"
37 #include "DGtalCatch.h"
39
40 using namespace std;
41 using namespace DGtal;
42
43
45 // Functions for testing class LagrangeInterpolation.
47
48 SCENARIO( "LagrangeInterpolation< int64_t > unit tests", "[lagrange_interpolation]" )
49 {
50  typedef DGtal::int64_t Ring;
51  typedef LagrangeInterpolation< Ring > LInterp;
52  typedef LInterp::Polynomial Polynomial;
53
54  GIVEN( "A polynomial of degree 2 and 3 interpolation points" ) {
55  Polynomial P1 = mmonomial<Ring>( 2 ); // P1(x) = x^2
56  std::vector< Ring > X = { 1, 2, 3 };
57  std::vector< Ring > Y;
58  for ( auto x : X ) Y.push_back( P1( x ) );
59  LInterp L1( X );
60  CAPTURE( L1 );
61  THEN( "Its Lagrange polynomial is itself" ) {
62  auto Lag1 = L1.polynomial( Y );
63  REQUIRE( Lag1 == P1 * L1.denominator() );
64  }
65  }
66
67  GIVEN( "A polynomial of degree 3 and 4 interpolation points" ) {
68  Polynomial P2 = mmonomial<Ring>( 3 ) + 3*mmonomial<Ring>( 1 ) ; // P1(x) = x^3+3x
69  std::vector< Ring > X = { -1, 1, 2, 3 };
70  std::vector< Ring > Y;
71  for ( auto x : X ) Y.push_back( P2( x ) );
72  LInterp L2( X );
73  CAPTURE( L2 );
74  THEN( "Its Lagrange polynomial is itself" ) {
75  auto Lag2 = L2.polynomial( Y );
76  REQUIRE( Lag2 == P2 * L2.denominator() );
77  }
78  }
79
80  GIVEN( "3 interpolation points (0,1), (1,3), (2,6)" ) {
81  std::vector< Ring > X = { 0, 1, 2 };
82  std::vector< Ring > Y = { 1, 3, 6 };
83  LInterp L3( X );
84  CAPTURE( L3 );
85  THEN( "Its Lagrange polynomial is 1/2*(2+3x+x^2)" ) {
86  auto Lag3 = L3.polynomial( Y );
87  Polynomial Exp3 = mmonomial<Ring>( 2 ) + 3 * mmonomial<Ring>( 1 ) + 2;
88  REQUIRE( Lag3 == Exp3 );
89  }
90  }
91
92  GIVEN( "3 interpolation points (0,1), (1,7), (2,20)" ) {
93  std::vector< Ring > X = { 0, 1, 2 };
94  std::vector< Ring > Y = { 1, 7, 20 };
95  LInterp L4( X );
96  CAPTURE( L4 );
97  THEN( "Its Lagrange polynomial is 1/2*(2+5x+7x^2)" ) {
98  auto Lag4 = L4.polynomial( Y );
99  Polynomial Exp4 = 7 * mmonomial<Ring>( 2 ) + 5 * mmonomial<Ring>( 1 ) + 2;
100  REQUIRE( Lag4 == Exp4 );
101  }
102  }
103 }
Aim: This class implements Lagrange basis functions and Lagrange interpolation.
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
CAPTURE(thicknessHV)
GIVEN("A cubical complex with random 3-cells")
SCENARIO("LagrangeInterpolation< int64_t > unit tests", "[lagrange_interpolation]")
REQUIRE(domain.isInside(aPoint))