|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r77794 - in sandbox/big_number/libs/multiprecision/example/generic_numerics_examples: . generic_numerics generic_numerics_src
From: e_float_at_[hidden]
Date: 2012-04-06 18:09:09
Author: christopher_kormanyos
Date: 2012-04-06 18:09:09 EDT (Fri, 06 Apr 2012)
New Revision: 77794
URL: http://svn.boost.org/trac/boost/changeset/77794
Log:
Added examples of generic numeric programming using built-in and MP types.
Added:
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/ (props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics/ (props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics/generic_numerics.pdf (contents, props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/ (props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_area_of_a_circle.cpp (contents, props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_derivative.cpp (contents, props changed)
sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_integral.cpp (contents, props changed)
Added: sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics/generic_numerics.pdf
==============================================================================
Binary file. No diff available.
Added: sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_area_of_a_circle.cpp
==============================================================================
--- (empty file)
+++ sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_area_of_a_circle.cpp 2012-04-06 18:09:09 EDT (Fri, 06 Apr 2012)
@@ -0,0 +1,45 @@
+#include <boost/math/constants/constants.hpp>
+
+using boost::math::constants::pi;
+
+template<typename T>
+inline T area_of_a_circle(T r)
+{
+ return pi<T>() * (r * r);
+}
+
+#include <iostream>
+#include <iomanip>
+#include <boost/multiprecision/cpp_dec_float.hpp>
+
+using boost::multiprecision::cpp_dec_float_50;
+
+int main(int, char**)
+{
+ const float r_f(float(123) / 100);
+ const float a_f = area_of_a_circle(r_f);
+
+ const double r_d(double(123) / 100);
+ const double a_d = area_of_a_circle(r_d);
+
+ const cpp_dec_float_50 r_mp(cpp_dec_float_50(123) / 100);
+ const cpp_dec_float_50 a_mp = area_of_a_circle(r_mp);
+
+ // 4.75292
+ std::cout
+ << std::setprecision(std::numeric_limits<float>::digits10)
+ << a_f
+ << std::endl;
+
+ // 4.752915525616
+ std::cout
+ << std::setprecision(std::numeric_limits<double>::digits10)
+ << a_d
+ << std::endl;
+
+ // 4.7529155256159981904701331745635599135018975843146
+ std::cout
+ << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
+ << a_mp
+ << std::endl;
+}
Added: sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_derivative.cpp
==============================================================================
--- (empty file)
+++ sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_derivative.cpp 2012-04-06 18:09:09 EDT (Fri, 06 Apr 2012)
@@ -0,0 +1,79 @@
+template<typename value_type,
+ typename function_type>
+value_type derivative(const value_type x,
+ const value_type dx,
+ function_type func)
+{
+ // Compute d/dx[func(*first)] using a three-point
+ // central difference rule of O(dx^6).
+
+ const value_type dx1 = dx;
+ const value_type dx2 = dx1 * 2;
+ const value_type dx3 = dx1 * 3;
+
+ const value_type m1 = ( func(x + dx1)
+ - func(x - dx1)) / 2;
+ const value_type m2 = ( func(x + dx2)
+ - func(x - dx2)) / 4;
+ const value_type m3 = ( func(x + dx3)
+ - func(x - dx3)) / 6;
+
+ const value_type fifteen_m1 = 15 * m1;
+ const value_type six_m2 = 6 * m2;
+ const value_type ten_dx1 = 10 * dx1;
+
+ return ((fifteen_m1 - six_m2) + m3) / ten_dx1;
+}
+
+#include <iostream>
+#include <iomanip>
+#include <boost/multiprecision/cpp_dec_float.hpp>
+#include <boost/math/constants/constants.hpp>
+
+using boost::math::constants::pi;
+using boost::multiprecision::cpp_dec_float_50;
+
+int main(int, char**)
+{
+ const float d_f =
+ derivative(float(pi<float>() / 3),
+ 0.01F,
+ [](const float x) -> float
+ {
+ return ::sin(x);
+ });
+
+ const double d_d =
+ derivative(double(pi<double>() / 3),
+ 0.001,
+ [](const double x) -> double
+ {
+ return ::sin(x);
+ });
+
+ const cpp_dec_float_50 d_mp =
+ derivative(cpp_dec_float_50(pi<cpp_dec_float_50>() / 3),
+ cpp_dec_float_50(1.0E-9),
+ [](const cpp_dec_float_50 x) -> cpp_dec_float_50
+ {
+ return boost::multiprecision::sin(x);
+ });
+
+ // 0.500003
+ std::cout
+ << std::setprecision(std::numeric_limits<float>::digits10)
+ << d_f
+ << std::endl;
+
+ // 0.499999999999888
+ std::cout
+ << std::setprecision(std::numeric_limits<double>::digits10)
+ << d_d
+ << std::endl;
+
+ // 0.50000000000000000000000000000000000000000003925935
+ std::cout
+ << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
+ << d_mp
+ << std::endl;
+}
Added: sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_integral.cpp
==============================================================================
--- (empty file)
+++ sandbox/big_number/libs/multiprecision/example/generic_numerics_examples/generic_numerics_src/generic_integral.cpp 2012-04-06 18:09:09 EDT (Fri, 06 Apr 2012)
@@ -0,0 +1,104 @@
+template<typename value_type,
+ typename function_type>
+inline value_type integral(const value_type a,
+ const value_type b,
+ const value_type tol,
+ function_type func)
+{
+ unsigned n = 1U;
+
+ value_type h = (b - a);
+ value_type I = (func(a) + func(b)) * (h / 2);
+
+ for(unsigned k = 0U; k < 8U; k++)
+ {
+ h /= 2;
+
+ value_type sum(0);
+ for(unsigned j = 1U; j <= n; j++)
+ {
+ sum += func(a + (value_type((j * 2) - 1) * h));
+ }
+
+ const value_type I0 = I;
+ I = (I / 2) + (h * sum);
+
+ const value_type ratio = I0 / I;
+ const value_type delta = ratio - 1;
+ const value_type delta_abs = ((delta < 0) ? -delta : delta);
+
+ if((k > 1U) && (delta_abs < tol))
+ {
+ break;
+ }
+
+ n *= 2U;
+ }
+
+ return I;
+}
+
+#include <iostream>
+#include <iomanip>
+#include <boost/multiprecision/cpp_dec_float.hpp>
+#include <boost/math/constants/constants.hpp>
+
+template<typename value_type>
+class cyl_bessel_j_integral_rep
+{
+public:
+ cyl_bessel_j_integral_rep(const unsigned N,
+ const value_type& X) : n(N), x(X) { }
+
+ value_type operator()(const value_type& t) const
+ {
+ // pi * Jn(x) = Int_0^pi [cos(x * sin(t) - n*t) dt]
+ return cos(x * sin(t) - (n * t));
+ }
+
+private:
+ const unsigned n;
+ const value_type x;
+};
+
+using boost::math::constants::pi;
+typedef boost::multiprecision::cpp_dec_float_50 mp_type;
+
+int main(int, char**)
+{
+ const float j2_f =
+ integral(0.0F,
+ pi<float>(),
+ 0.01F,
+ cyl_bessel_j_integral_rep<float>(2U, 1.23F)) / pi<float>();
+
+ const double j2_d =
+ integral(0.0,
+ pi<double>(),
+ 0.0001,
+ cyl_bessel_j_integral_rep<double>(2U, 1.23)) / pi<double>();
+
+ const mp_type j2_mp =
+ integral(mp_type(0),
+ pi<mp_type>(),
+ mp_type(1.0E-20),
+ cyl_bessel_j_integral_rep<mp_type>(2U, mp_type(123) / 100)) / pi<mp_type>();
+
+ // 0.166369
+ std::cout
+ << std::setprecision(std::numeric_limits<float>::digits10)
+ << j2_f
+ << std::endl;
+
+ // 0.166369383786814
+ std::cout
+ << std::setprecision(std::numeric_limits<double>::digits10)
+ << j2_d
+ << std::endl;
+
+ // 0.16636938378681407351267852431513159437103348245333
+ std::cout
+ << std::setprecision(std::numeric_limits<mp_type>::digits10)
+ << j2_mp
+ << std::endl;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk