DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testLagrangeInterpolation.cpp File Reference
#include <iostream>
#include <vector>
#include <algorithm>
#include "DGtal/base/Common.h"
#include "DGtal/math/LagrangeInterpolation.h"
#include "DGtal/math/MPolynomial.h"
#include "DGtalCatch.h"

Go to the source code of this file.

Functions

 SCENARIO ("LagrangeInterpolation< int64_t > unit tests", "[lagrange_interpolation]")
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2021/08/24

Functions for testing class LagrangeInterpolation.

This file is part of the DGtal library.

Definition in file testLagrangeInterpolation.cpp.

Function Documentation

◆ SCENARIO()

SCENARIO ( "LagrangeInterpolation< int64_t > unit tests"  ,
""  [lagrange_interpolation] 
)

Definition at line 48 of file testLagrangeInterpolation.cpp.

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.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
CAPTURE(thicknessHV)
GIVEN("A cubical complex with random 3-cells")
REQUIRE(domain.isInside(aPoint))

References CAPTURE(), GIVEN(), and REQUIRE().