lambda.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // $Id: lambda.cpp 27 2008-06-16 14:50:58Z maehne $
  11. ////////////////////////////////////////////////////////////////////////
  12. ///
  13. /// \file lambda.cpp
  14. ///
  15. /// \brief Example demonstrating the usage of Boost.Units' quantity,
  16. /// unit, and absolute types in functors created with the
  17. /// Boost.Lambda library and stored in Boost.Function objects.
  18. ///
  19. /// \author Torsten Maehne
  20. /// \date 2008-06-04
  21. ///
  22. /// A mechanical, electrical, geometrical, and thermal example
  23. /// demonstrate how to use Boost.Units' quantity, unit, and absolute
  24. /// types in lambda expressions. The resulting functors can be stored
  25. /// in boost::function objects. It is also shown how to work around a
  26. /// limitation of Boost.Lambda's bind() to help it to find the correct
  27. /// overloaded function by specifying its signature with a
  28. /// static_cast.
  29. ///
  30. ////////////////////////////////////////////////////////////////////////
  31. #include <iostream>
  32. #include <boost/function.hpp>
  33. #include <boost/units/io.hpp>
  34. #include <boost/units/cmath.hpp>
  35. #include <boost/units/pow.hpp>
  36. #include <boost/units/systems/si.hpp>
  37. #include <boost/units/absolute.hpp>
  38. // Include boost/units/lambda.hpp instead of boost/lambda/lambda.hpp
  39. // for a convenient usage of Boost.Units' quantity, unit, and absolute
  40. // types in lambda expressions. The header augments Boost.Lambda's
  41. // return type detuction system to recognize the new types so that not
  42. // for each arithmetic operation the return type needs to be
  43. // explicitely specified.
  44. #include <boost/units/lambda.hpp>
  45. #include <boost/lambda/bind.hpp>
  46. static const double pi = 3.14159265358979323846;
  47. //[lambda_snippet_1
  48. int main(int argc, char **argv) {
  49. using namespace std;
  50. namespace bl = boost::lambda;
  51. namespace bu = boost::units;
  52. namespace si = boost::units::si;
  53. ////////////////////////////////////////////////////////////////////////
  54. // Mechanical example: linear accelerated movement
  55. ////////////////////////////////////////////////////////////////////////
  56. // Initial condition variables for acceleration, speed, and displacement
  57. bu::quantity<si::acceleration> a = 2.0 * si::meters_per_second_squared;
  58. bu::quantity<si::velocity> v = 1.0 * si::meters_per_second;
  59. bu::quantity<si::length> s0 = 0.5 * si::meter;
  60. // Displacement over time
  61. boost::function<bu::quantity<si::length> (bu::quantity<si::time>) >
  62. s = 0.5 * bl::var(a) * bl::_1 * bl::_1
  63. + bl::var(v) * bl::_1
  64. + bl::var(s0);
  65. cout << "Linear accelerated movement:" << endl
  66. << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl
  67. << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl
  68. << endl;
  69. // Change initial conditions
  70. a = 1.0 * si::meters_per_second_squared;
  71. v = 2.0 * si::meters_per_second;
  72. s0 = -1.5 * si::meter;
  73. cout << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl
  74. << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl
  75. << endl;
  76. ////////////////////////////////////////////////////////////////////////
  77. // Electrical example: oscillating current
  78. ////////////////////////////////////////////////////////////////////////
  79. // Constants for the current amplitude, frequency, and offset current
  80. const bu::quantity<si::current> iamp = 1.5 * si::ampere;
  81. const bu::quantity<si::frequency> f = 1.0e3 * si::hertz;
  82. const bu::quantity<si::current> i0 = 0.5 * si::ampere;
  83. // The invocation of the sin function needs to be postponed using
  84. // bind to specify the oscillation function. A lengthy static_cast
  85. // to the function pointer referencing boost::units::sin() is needed
  86. // to avoid an "unresolved overloaded function type" error.
  87. boost::function<bu::quantity<si::current> (bu::quantity<si::time>) >
  88. i = iamp
  89. * bl::bind(static_cast<bu::dimensionless_quantity<si::system, double>::type (*)(const bu::quantity<si::plane_angle>&)>(bu::sin),
  90. 2.0 * pi * si::radian * f * bl::_1)
  91. + i0;
  92. cout << "Oscillating current:" << endl
  93. << "iamp = " << iamp << ", f = " << f << ", i0 = " << i0 << endl
  94. << "i(1.25e-3 * si::second) = " << i(1.25e-3 * si::second) << endl
  95. << endl;
  96. ////////////////////////////////////////////////////////////////////////
  97. // Geometric example: area calculation for a square
  98. ////////////////////////////////////////////////////////////////////////
  99. // Length constant
  100. const bu::quantity<si::length> l = 1.5 * si::meter;
  101. // Again an ugly static_cast is needed to bind pow<2> to the first
  102. // function argument.
  103. boost::function<bu::quantity<si::area> (bu::quantity<si::length>) >
  104. A = bl::bind(static_cast<bu::quantity<si::area> (*)(const bu::quantity<si::length>&)>(bu::pow<2>),
  105. bl::_1);
  106. cout << "Area of a square:" << endl
  107. << "A(" << l <<") = " << A(l) << endl << endl;
  108. ////////////////////////////////////////////////////////////////////////
  109. // Thermal example: temperature difference of two absolute temperatures
  110. ////////////////////////////////////////////////////////////////////////
  111. // Absolute temperature constants
  112. const bu::quantity<bu::absolute<si::temperature> >
  113. Tref = 273.15 * bu::absolute<si::temperature>();
  114. const bu::quantity<bu::absolute<si::temperature> >
  115. Tamb = 300.00 * bu::absolute<si::temperature>();
  116. boost::function<bu::quantity<si::temperature> (bu::quantity<bu::absolute<si::temperature> >,
  117. bu::quantity<bu::absolute<si::temperature> >)>
  118. dT = bl::_2 - bl::_1;
  119. cout << "Temperature difference of two absolute temperatures:" << endl
  120. << "dT(" << Tref << ", " << Tamb << ") = " << dT(Tref, Tamb) << endl
  121. << endl;
  122. return 0;
  123. }
  124. //]