From 278670d07504d94768d7e917b8b42d1c80841138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Fri, 29 Mar 2019 20:21:17 +0900 Subject: [PATCH 1/6] add Richardson extrapolation and its test. --- recipe/quadrature/richardson_extrapolation.cc | 48 +++++++++++++++ recipe/quadrature/richardson_extrapolation.h | 18 ++++++ .../richardson_extrapolation_test.cc | 60 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 recipe/quadrature/richardson_extrapolation.cc create mode 100644 recipe/quadrature/richardson_extrapolation.h create mode 100644 recipe/quadrature/richardson_extrapolation_test.cc diff --git a/recipe/quadrature/richardson_extrapolation.cc b/recipe/quadrature/richardson_extrapolation.cc new file mode 100644 index 0000000..3be79d3 --- /dev/null +++ b/recipe/quadrature/richardson_extrapolation.cc @@ -0,0 +1,48 @@ +#include "recipe/quadrature/richardson_extrapolation.h" +#include +#include +#include "recipe/quadrature/quadrature.h" + +namespace recipe { +namespace quadrature { + +double RichardsonExtrapolation(QuadratureRule quadrature_rule, + double (*integrand)(double), + double left_endpoint, double right_endpoint, + unsigned int num_of_partition) { + assert(right_endpoint < left_endpoint); + + double approximator_of_half_step_size = + 0; // richardson extrapolation uses the approximation value of a half + // step size. + double approximator_of_normal_step_size = + 0; // the approximation value of a normal step size. + unsigned int approx_order = 0; + + switch (quadrature_rule) { + case Trapezoidal: { + approx_order = 2; + approximator_of_normal_step_size = recipe::quadrature::TrapezoidalRule( + integrand, left_endpoint, right_endpoint, num_of_partition); + approximator_of_half_step_size = recipe::quadrature::TrapezoidalRule( + integrand, left_endpoint, right_endpoint, 2 * num_of_partition); + break; + } + case Simpson: { + approx_order = 4; + approximator_of_normal_step_size = recipe::quadrature::SimpsonRule( + integrand, left_endpoint, right_endpoint, num_of_partition); + approximator_of_half_step_size = recipe::quadrature::SimpsonRule( + integrand, left_endpoint, right_endpoint, 2 * num_of_partition); + break; + } + default: { + assert("An undefined quadrature rule was spesified."); + } + } + return (std::pow(2.0, approx_order) * approximator_of_half_step_size - + approximator_of_normal_step_size) / + (pow(2.0, approx_order) - 1.0); +}; +} // namespace quadrature +} // namespace recipe diff --git a/recipe/quadrature/richardson_extrapolation.h b/recipe/quadrature/richardson_extrapolation.h new file mode 100644 index 0000000..052fbfc --- /dev/null +++ b/recipe/quadrature/richardson_extrapolation.h @@ -0,0 +1,18 @@ +namespace recipe{ +namespace quadrature { +/*! \enum QuadratureRule + * + * QuadratureRule enumerator lists all the quadrature rules that have been implemented. + * Avairable are: Trapezoidal, Simpson. + * Please put a name in the definition when you implements a new quadrature rule. + * + */ +enum QuadratureRule { + Trapezoidal, + Simpson +}; + +double RichardsonExtrapolation(QuadratureRule quadrature_rule, double (*integrand)(double), + double left_endpoint, double right_endpoint, unsigned int num_of_partition); +}//namespace richardson +}//namespace recipe diff --git a/recipe/quadrature/richardson_extrapolation_test.cc b/recipe/quadrature/richardson_extrapolation_test.cc new file mode 100644 index 0000000..7710c59 --- /dev/null +++ b/recipe/quadrature/richardson_extrapolation_test.cc @@ -0,0 +1,60 @@ +#include "recipe/quadrature/richardson_extrapolation.h" +#include +#include "recipe/test_util/test_util.h" + +namespace recipe { +namespace quadrature { + +double MonomOf6thDegree(double argument){return argument*argument*argument*argument*argument*argument;} // f(x)=x^6 +double MonomOf8thDegree(double argument){return argument*argument*argument*argument*argument*argument*argument*argument;} //f(x)=x^8 + +TEST(RichardsonExtrapolation, AssertIntervalForSimpson){ + double left_endpoint=1.0; + double right_endpoint=0; + unsigned int num_of_partition=100; + + EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Simpson,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition)); +} + +TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal){ + double left_endpoint=1.0; + double right_endpoint=0; + unsigned int num_of_partition=100; + + EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Trapezoidal,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition)); +} + +//test for extrapolation with trapezoidal rule on the integrations of the monomial of 6th degrees from 0 to 1. +TEST(RichardsonExtrTest, TrapezoidalWithMonomOf6thDegreeAsIntegrand){ + unsigned int num_of_partition=100; //denoted by N in the following comments. + double left_endpoint=0; //integraton from zero makes it easy to compute the closed form for polynomial integrands. + double right_endpoint=1.0; //denoted by a. + + double approximator=recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Trapezoidal,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition); + + //error turns out to be bounded by a^7/(24N^4). + double error_boundary=1.0/(24.0*num_of_partition*num_of_partition*num_of_partition*num_of_partition); + + EXPECT_NEAR(approximator, 1.0/7.0,error_boundary); +} + +//test for extrapolation with Simpson rule on the integrations of the monomial of 8th degrees from 0 to 1. +TEST(RichardsonExtrTest, SimpsonWithMonomOf8thDegreeAsIntegrand){ + unsigned int num_of_partition=100; //denoted by N in the following comments. + double left_endpoint=0; //integraton from zero makes it easy to compute the closed form for polynomial integrands. + double right_endpoint=1.0; //denoted by a. + + double approximator=recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Simpson,&MonomOf8thDegree,left_endpoint,right_endpoint,num_of_partition); + + //error turns out to be bounded by 5a^9/(95N^6). + double error_boundary=5.0/(95.0*num_of_partition*num_of_partition*num_of_partition*num_of_partition*num_of_partition*num_of_partition); + + EXPECT_NEAR(approximator, 1.0/9.0,error_boundary); +} + +} +}//namespace recipe From b34eafabf91aeab6bfd7a0183b723d05207b02ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Sun, 31 Mar 2019 19:22:12 +0900 Subject: [PATCH 2/6] format files. --- recipe/quadrature/richardson_extrapolation.h | 23 +++-- .../richardson_extrapolation_test.cc | 90 +++++++++++-------- 2 files changed, 65 insertions(+), 48 deletions(-) diff --git a/recipe/quadrature/richardson_extrapolation.h b/recipe/quadrature/richardson_extrapolation.h index 052fbfc..e3cb998 100644 --- a/recipe/quadrature/richardson_extrapolation.h +++ b/recipe/quadrature/richardson_extrapolation.h @@ -1,18 +1,17 @@ -namespace recipe{ +namespace recipe { namespace quadrature { /*! \enum QuadratureRule * - * QuadratureRule enumerator lists all the quadrature rules that have been implemented. - * Avairable are: Trapezoidal, Simpson. - * Please put a name in the definition when you implements a new quadrature rule. + * QuadratureRule enumerator lists all the quadrature rules that have been + * implemented. Avairable are: Trapezoidal, Simpson. Please put a name in the + * definition when you implements a new quadrature rule. * */ -enum QuadratureRule { - Trapezoidal, - Simpson -}; +enum QuadratureRule { Trapezoidal, Simpson }; -double RichardsonExtrapolation(QuadratureRule quadrature_rule, double (*integrand)(double), - double left_endpoint, double right_endpoint, unsigned int num_of_partition); -}//namespace richardson -}//namespace recipe +double RichardsonExtrapolation(QuadratureRule quadrature_rule, + double (*integrand)(double), + double left_endpoint, double right_endpoint, + unsigned int num_of_partition); +} // namespace quadrature +} // namespace recipe diff --git a/recipe/quadrature/richardson_extrapolation_test.cc b/recipe/quadrature/richardson_extrapolation_test.cc index 7710c59..f3e5048 100644 --- a/recipe/quadrature/richardson_extrapolation_test.cc +++ b/recipe/quadrature/richardson_extrapolation_test.cc @@ -5,56 +5,74 @@ namespace recipe { namespace quadrature { -double MonomOf6thDegree(double argument){return argument*argument*argument*argument*argument*argument;} // f(x)=x^6 -double MonomOf8thDegree(double argument){return argument*argument*argument*argument*argument*argument*argument*argument;} //f(x)=x^8 +double MonomOf6thDegree(double argument) { + return argument * argument * argument * argument * argument * argument; +} // f(x)=x^6 +double MonomOf8thDegree(double argument) { + return argument * argument * argument * argument * argument * argument * + argument * argument; +} // f(x)=x^8 -TEST(RichardsonExtrapolation, AssertIntervalForSimpson){ - double left_endpoint=1.0; - double right_endpoint=0; - unsigned int num_of_partition=100; +TEST(RichardsonExtrapolation, AssertIntervalForSimpson) { + double left_endpoint = 1.0; + double right_endpoint = 0; + unsigned int num_of_partition = 100; - EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( - recipe::quadrature::QuadratureRule::Simpson,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition)); + EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Simpson, &MonomOf6thDegree, + left_endpoint, right_endpoint, num_of_partition)); } -TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal){ - double left_endpoint=1.0; - double right_endpoint=0; - unsigned int num_of_partition=100; +TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal) { + double left_endpoint = 1.0; + double right_endpoint = 0; + unsigned int num_of_partition = 100; - EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( - recipe::quadrature::QuadratureRule::Trapezoidal,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition)); + EXPECT_ASSERT_FAIL(recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Trapezoidal, &MonomOf6thDegree, + left_endpoint, right_endpoint, num_of_partition)); } -//test for extrapolation with trapezoidal rule on the integrations of the monomial of 6th degrees from 0 to 1. -TEST(RichardsonExtrTest, TrapezoidalWithMonomOf6thDegreeAsIntegrand){ - unsigned int num_of_partition=100; //denoted by N in the following comments. - double left_endpoint=0; //integraton from zero makes it easy to compute the closed form for polynomial integrands. - double right_endpoint=1.0; //denoted by a. +// test for extrapolation with trapezoidal rule on the integrations of the +// monomial of 6th degrees from 0 to 1. +TEST(RichardsonExtrTest, TrapezoidalWithMonomOf6thDegreeAsIntegrand) { + unsigned int num_of_partition = + 100; // denoted by N in the following comments. + double left_endpoint = 0; // integraton from zero makes it easy to compute + // the closed form for polynomial integrands. + double right_endpoint = 1.0; // denoted by a. - double approximator=recipe::quadrature::RichardsonExtrapolation( - recipe::quadrature::QuadratureRule::Trapezoidal,&MonomOf6thDegree,left_endpoint,right_endpoint,num_of_partition); + double approximator = recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Trapezoidal, &MonomOf6thDegree, + left_endpoint, right_endpoint, num_of_partition); - //error turns out to be bounded by a^7/(24N^4). - double error_boundary=1.0/(24.0*num_of_partition*num_of_partition*num_of_partition*num_of_partition); + // error turns out to be bounded by a^7/(24N^4). + double error_boundary = 1.0 / (24.0 * num_of_partition * num_of_partition * + num_of_partition * num_of_partition); - EXPECT_NEAR(approximator, 1.0/7.0,error_boundary); + EXPECT_NEAR(approximator, 1.0 / 7.0, error_boundary); } -//test for extrapolation with Simpson rule on the integrations of the monomial of 8th degrees from 0 to 1. -TEST(RichardsonExtrTest, SimpsonWithMonomOf8thDegreeAsIntegrand){ - unsigned int num_of_partition=100; //denoted by N in the following comments. - double left_endpoint=0; //integraton from zero makes it easy to compute the closed form for polynomial integrands. - double right_endpoint=1.0; //denoted by a. +// test for extrapolation with Simpson rule on the integrations of the monomial +// of 8th degrees from 0 to 1. +TEST(RichardsonExtrTest, SimpsonWithMonomOf8thDegreeAsIntegrand) { + unsigned int num_of_partition = + 100; // denoted by N in the following comments. + double left_endpoint = 0; // integraton from zero makes it easy to compute + // the closed form for polynomial integrands. + double right_endpoint = 1.0; // denoted by a. - double approximator=recipe::quadrature::RichardsonExtrapolation( - recipe::quadrature::QuadratureRule::Simpson,&MonomOf8thDegree,left_endpoint,right_endpoint,num_of_partition); + double approximator = recipe::quadrature::RichardsonExtrapolation( + recipe::quadrature::QuadratureRule::Simpson, &MonomOf8thDegree, + left_endpoint, right_endpoint, num_of_partition); - //error turns out to be bounded by 5a^9/(95N^6). - double error_boundary=5.0/(95.0*num_of_partition*num_of_partition*num_of_partition*num_of_partition*num_of_partition*num_of_partition); + // error turns out to be bounded by 5a^9/(95N^6). + double error_boundary = + 5.0 / (95.0 * num_of_partition * num_of_partition * num_of_partition * + num_of_partition * num_of_partition * num_of_partition); - EXPECT_NEAR(approximator, 1.0/9.0,error_boundary); + EXPECT_NEAR(approximator, 1.0 / 9.0, error_boundary); } -} -}//namespace recipe +} // namespace quadrature +} // namespace recipe From be3aa72c56e2f41ae89c2da18ae3891cadaf7607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Sun, 31 Mar 2019 19:41:57 +0900 Subject: [PATCH 3/6] format the richardson test file once more. --- recipe/quadrature/richardson_extrapolation_test.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/recipe/quadrature/richardson_extrapolation_test.cc b/recipe/quadrature/richardson_extrapolation_test.cc index f3e5048..d890dcd 100644 --- a/recipe/quadrature/richardson_extrapolation_test.cc +++ b/recipe/quadrature/richardson_extrapolation_test.cc @@ -37,9 +37,9 @@ TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal) { // monomial of 6th degrees from 0 to 1. TEST(RichardsonExtrTest, TrapezoidalWithMonomOf6thDegreeAsIntegrand) { unsigned int num_of_partition = - 100; // denoted by N in the following comments. - double left_endpoint = 0; // integraton from zero makes it easy to compute - // the closed form for polynomial integrands. + 100; // denoted by N in the following comments. + double left_endpoint = 0; // integraton from zero makes it easy to compute + // the closed form for polynomial integrands. double right_endpoint = 1.0; // denoted by a. double approximator = recipe::quadrature::RichardsonExtrapolation( @@ -57,9 +57,9 @@ TEST(RichardsonExtrTest, TrapezoidalWithMonomOf6thDegreeAsIntegrand) { // of 8th degrees from 0 to 1. TEST(RichardsonExtrTest, SimpsonWithMonomOf8thDegreeAsIntegrand) { unsigned int num_of_partition = - 100; // denoted by N in the following comments. - double left_endpoint = 0; // integraton from zero makes it easy to compute - // the closed form for polynomial integrands. + 100; // denoted by N in the following comments. + double left_endpoint = 0; // integraton from zero makes it easy to compute + // the closed form for polynomial integrands. double right_endpoint = 1.0; // denoted by a. double approximator = recipe::quadrature::RichardsonExtrapolation( From ac2b2f989ed09372700e6dcddbfc316357720f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Sun, 31 Mar 2019 20:24:29 +0900 Subject: [PATCH 4/6] modifiy asset statement with a little formatting. --- recipe/quadrature/richardson_extrapolation.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/recipe/quadrature/richardson_extrapolation.cc b/recipe/quadrature/richardson_extrapolation.cc index 3be79d3..81caa93 100644 --- a/recipe/quadrature/richardson_extrapolation.cc +++ b/recipe/quadrature/richardson_extrapolation.cc @@ -10,7 +10,7 @@ double RichardsonExtrapolation(QuadratureRule quadrature_rule, double (*integrand)(double), double left_endpoint, double right_endpoint, unsigned int num_of_partition) { - assert(right_endpoint < left_endpoint); + assert(left_endpoint < right_endpoint); double approximator_of_half_step_size = 0; // richardson extrapolation uses the approximation value of a half @@ -36,9 +36,7 @@ double RichardsonExtrapolation(QuadratureRule quadrature_rule, integrand, left_endpoint, right_endpoint, 2 * num_of_partition); break; } - default: { - assert("An undefined quadrature rule was spesified."); - } + default: { assert("An undefined quadrature rule was spesified."); } } return (std::pow(2.0, approx_order) * approximator_of_half_step_size - approximator_of_normal_step_size) / From a34e3abae083971ba56b28669853021e232498e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Sun, 31 Mar 2019 20:56:20 +0900 Subject: [PATCH 5/6] add things for assertion test to pass under release build --- recipe/quadrature/richardson_extrapolation_test.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/recipe/quadrature/richardson_extrapolation_test.cc b/recipe/quadrature/richardson_extrapolation_test.cc index d890dcd..7e1910f 100644 --- a/recipe/quadrature/richardson_extrapolation_test.cc +++ b/recipe/quadrature/richardson_extrapolation_test.cc @@ -13,6 +13,7 @@ double MonomOf8thDegree(double argument) { argument * argument; } // f(x)=x^8 +#ifndef NDEBUG TEST(RichardsonExtrapolation, AssertIntervalForSimpson) { double left_endpoint = 1.0; double right_endpoint = 0; @@ -22,7 +23,9 @@ TEST(RichardsonExtrapolation, AssertIntervalForSimpson) { recipe::quadrature::QuadratureRule::Simpson, &MonomOf6thDegree, left_endpoint, right_endpoint, num_of_partition)); } +#endif +#ifndef NDEBUG TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal) { double left_endpoint = 1.0; double right_endpoint = 0; @@ -32,6 +35,7 @@ TEST(RichardsonExtrapolation, AssertIntervalForTrapezoidal) { recipe::quadrature::QuadratureRule::Trapezoidal, &MonomOf6thDegree, left_endpoint, right_endpoint, num_of_partition)); } +#endif // test for extrapolation with trapezoidal rule on the integrations of the // monomial of 6th degrees from 0 to 1. From 331ace4898adef6d87dee4fae94c5a2e66705ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRyo?= Date: Sun, 31 Mar 2019 21:36:06 +0900 Subject: [PATCH 6/6] modify "spesified" to "specified" --- recipe/quadrature/richardson_extrapolation.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipe/quadrature/richardson_extrapolation.cc b/recipe/quadrature/richardson_extrapolation.cc index 81caa93..94ec21e 100644 --- a/recipe/quadrature/richardson_extrapolation.cc +++ b/recipe/quadrature/richardson_extrapolation.cc @@ -36,7 +36,7 @@ double RichardsonExtrapolation(QuadratureRule quadrature_rule, integrand, left_endpoint, right_endpoint, 2 * num_of_partition); break; } - default: { assert("An undefined quadrature rule was spesified."); } + default: { assert("An undefined quadrature rule was specified."); } } return (std::pow(2.0, approx_order) * approximator_of_half_step_size - approximator_of_normal_step_size) /