From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/units/example/Jamfile.v2 | 25 + src/boost/libs/units/example/autoprefixes.cpp | 148 +++ src/boost/libs/units/example/complex.cpp | 421 ++++++++ src/boost/libs/units/example/composite_output.cpp | 116 +++ src/boost/libs/units/example/conversion.cpp | 124 +++ src/boost/libs/units/example/conversion_factor.cpp | 76 ++ src/boost/libs/units/example/dimension.cpp | 117 +++ .../libs/units/example/heterogeneous_unit.cpp | 87 ++ src/boost/libs/units/example/information.cpp | 100 ++ src/boost/libs/units/example/kitchen_sink.cpp | 542 ++++++++++ src/boost/libs/units/example/lambda.cpp | 157 +++ src/boost/libs/units/example/measurement.hpp | 344 +++++++ .../libs/units/example/non_base_dimension.cpp | 78 ++ src/boost/libs/units/example/performance.cpp | 395 ++++++++ src/boost/libs/units/example/quantity.cpp | 128 +++ src/boost/libs/units/example/quaternion.cpp | 232 +++++ src/boost/libs/units/example/radar_beam_height.cpp | 167 +++ .../units/example/runtime_conversion_factor.cpp | 72 ++ src/boost/libs/units/example/runtime_unit.cpp | 116 +++ .../libs/units/example/runtime_unit_input.txt | 13 + src/boost/libs/units/example/systems.cpp | 1072 ++++++++++++++++++++ src/boost/libs/units/example/temperature.cpp | 98 ++ src/boost/libs/units/example/test_system.hpp | 153 +++ src/boost/libs/units/example/tutorial.cpp | 95 ++ src/boost/libs/units/example/unit.cpp | 73 ++ 25 files changed, 4949 insertions(+) create mode 100644 src/boost/libs/units/example/Jamfile.v2 create mode 100644 src/boost/libs/units/example/autoprefixes.cpp create mode 100644 src/boost/libs/units/example/complex.cpp create mode 100644 src/boost/libs/units/example/composite_output.cpp create mode 100644 src/boost/libs/units/example/conversion.cpp create mode 100644 src/boost/libs/units/example/conversion_factor.cpp create mode 100644 src/boost/libs/units/example/dimension.cpp create mode 100644 src/boost/libs/units/example/heterogeneous_unit.cpp create mode 100644 src/boost/libs/units/example/information.cpp create mode 100644 src/boost/libs/units/example/kitchen_sink.cpp create mode 100644 src/boost/libs/units/example/lambda.cpp create mode 100644 src/boost/libs/units/example/measurement.hpp create mode 100644 src/boost/libs/units/example/non_base_dimension.cpp create mode 100644 src/boost/libs/units/example/performance.cpp create mode 100644 src/boost/libs/units/example/quantity.cpp create mode 100644 src/boost/libs/units/example/quaternion.cpp create mode 100644 src/boost/libs/units/example/radar_beam_height.cpp create mode 100644 src/boost/libs/units/example/runtime_conversion_factor.cpp create mode 100644 src/boost/libs/units/example/runtime_unit.cpp create mode 100644 src/boost/libs/units/example/runtime_unit_input.txt create mode 100644 src/boost/libs/units/example/systems.cpp create mode 100644 src/boost/libs/units/example/temperature.cpp create mode 100644 src/boost/libs/units/example/test_system.hpp create mode 100644 src/boost/libs/units/example/tutorial.cpp create mode 100644 src/boost/libs/units/example/unit.cpp (limited to 'src/boost/libs/units/example') diff --git a/src/boost/libs/units/example/Jamfile.v2 b/src/boost/libs/units/example/Jamfile.v2 new file mode 100644 index 00000000..b918205a --- /dev/null +++ b/src/boost/libs/units/example/Jamfile.v2 @@ -0,0 +1,25 @@ +# Jamfile.v2 +# +# Copyright (c) 2007-2008 +# Steven Watanabe +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt + +import testing ; +import path ; + +project boost/units/example : + : requirements $(BOOST_ROOT) ../../.. all +; + +files = [ path.glob . : *.cpp : performance.* runtime_unit.* ] ; + +for local file in $(files) +{ + run $(file) ; +} + +compile performance.cpp ; +run runtime_unit.cpp : + +#include +#include +#include +#include +#include + +struct byte_base_unit : boost::units::base_unit +{ + static constexpr const char* name() { return("byte"); } + static constexpr const char* symbol() { return("b"); } +}; + +struct thing_base_unit : boost::units::base_unit +{ + static constexpr const char* name() { return("thing"); } + static constexpr const char* symbol() { return(""); } +}; + +struct euro_base_unit : boost::units::base_unit +{ + static constexpr const char* name() { return("EUR"); } + static constexpr const char* symbol() { return("€"); } +}; + +int main() +{ + using std::cout; + using std::endl; + + using namespace boost::units; + using namespace boost::units::si; + + //[autoprefixes_snippet_1 + using boost::units::binary_prefix; + using boost::units::engineering_prefix; + using boost::units::no_prefix; + + quantity l = 2.345 * meters; // A quantity of length, in units of meters. + cout << engineering_prefix << l << endl; // Outputs "2.345 m". + l = 1000.0 * l; // Increase it by 1000, so expect a k prefix. + // Note that a double 1000.0 is required - an integer will fail to compile. + cout << engineering_prefix << l << endl; // Output autoprefixed with k to "2.345 km". + + quantity e = kilograms * pow<2>(l / seconds); // A quantity of energy. + cout << engineering_prefix << e << endl; // 5.49902 MJ + cout << name_format << engineering_prefix << e << endl; // 5.49902 megaJoule + //] [/autoprefixes_snippet_1] + + //[autoprefixes_snippet_2 + // Don't forget that the units name or symbol format specification is persistent. + cout << symbol_format << endl; // Resets the format to the default symbol format. + + quantity b = 2048. * byte_base_unit::unit_type(); + cout << engineering_prefix << b << endl; // 2.048 kb + cout << symbol_format << binary_prefix << b << endl; // "2 Kib" + //] [/autoprefixes_snippet_2] + + // Note that scalar dimensionless values are *not* prefixed automatically by the engineering_prefix or binary_prefix iostream manipulators. + //[autoprefixes_snippet_3 + const double s1 = 2345.6; + const long x1 = 23456; + cout << engineering_prefix << s1 << endl; // 2345.6 + cout << engineering_prefix << x1 << endl; // 23456 + + cout << binary_prefix << s1 << endl; // 2345.6 + cout << binary_prefix << x1 << endl; // 23456 + //] [/autoprefixes_snippet_3] + + //[autoprefixes_snippet_4 + const length L; // A unit of length (but not a quantity of length). + cout << L << endl; // Default length unit is meter, + // but default is symbol format so output is just "m". + cout << name_format << L << endl; // default length name is "meter". + //] [/autoprefixes_snippet_4] + + //[autoprefixes_snippet_5 + no_prefix(cout); // Clear any prefix flag. + cout << no_prefix << endl; // Clear any prefix flag using `no_prefix` manipulator. + //] [/autoprefixes_snippet_5] + + //[autoprefixes_snippet_6 + cout << boost::units::get_autoprefix(cout) << endl; // 8 is `autoprefix_binary` from `enum autoprefix_mode`. + cout << boost::units::get_format(cout) << endl; // 1 is `name_fmt` from `enum format_mode`. + //] [/autoprefixes_snippet_6] + + + quantity t = 2048. * thing_base_unit::unit_type(); + cout << name_format << engineering_prefix << t << endl; // 2.048 kilothing + cout << symbol_format << engineering_prefix << t << endl; // 2.048 k + + cout << binary_prefix << t << endl; // "2 Ki" + + quantity ce = 2048. * euro_base_unit::unit_type(); + cout << name_format << engineering_prefix << ce << endl; // 2.048 kiloEUR + cout << symbol_format << engineering_prefix << ce << endl; // 2.048 k€ + + + return 0; +} // int main() + diff --git a/src/boost/libs/units/example/complex.cpp b/src/boost/libs/units/example/complex.cpp new file mode 100644 index 00000000..d3579a06 --- /dev/null +++ b/src/boost/libs/units/example/complex.cpp @@ -0,0 +1,421 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief complex.cpp + +\details +Demonstrate a complex number class that functions correctly with quantities. + +Output: +@verbatim + +//[complex_output_1 ++L = 2 + 1 i m +-L = -2 + -1 i m +L+L = 4 + 2 i m +L-L = 0 + 0 i m +L*L = 3 + 4 i m^2 +L/L = 1 + 0 i dimensionless +L^3 = 2 + 11 i m^3 +L^(3/2) = 2.56713 + 2.14247 i m^(3/2) +3vL = 1.29207 + 0.201294 i m^(1/3) +(3/2)vL = 1.62894 + 0.520175 i m^(2/3) +//] + +//[complex_output_2 ++L = 2 m + 1 m i +-L = -2 m + -1 m i +L+L = 4 m + 2 m i +L-L = 0 m + 0 m i +L*L = 3 m^2 + 4 m^2 i +L/L = 1 dimensionless + 0 dimensionless i +L^3 = 2 m^3 + 11 m^3 i +L^(3/2) = 2.56713 m^(3/2) + 2.14247 m^(3/2) i +3vL = 1.29207 m^(1/3) + 0.201294 m^(1/3) i +(3/2)vL = 1.62894 m^(2/3) + 0.520175 m^(2/3) i +//] + +@endverbatim +**/ + +#include +#include +#include + +#include + +#include +#include +#include + +#include "test_system.hpp" + +//[complex_class_snippet_1 +namespace boost { + +namespace units { + +/// replacement complex class +template +class complex +{ + public: + typedef complex this_type; + + constexpr complex(const T& r = 0,const T& i = 0) : r_(r),i_(i) { } + constexpr complex(const this_type& source) : r_(source.r_),i_(source.i_) { } + + constexpr this_type& operator=(const this_type& source) + { + if (this == &source) return *this; + + r_ = source.r_; + i_ = source.i_; + + return *this; + } + + constexpr T& real() { return r_; } + constexpr T& imag() { return i_; } + + constexpr const T& real() const { return r_; } + constexpr const T& imag() const { return i_; } + + constexpr this_type& operator+=(const T& val) + { + r_ += val; + return *this; + } + + constexpr this_type& operator-=(const T& val) + { + r_ -= val; + return *this; + } + + constexpr this_type& operator*=(const T& val) + { + r_ *= val; + i_ *= val; + return *this; + } + + constexpr this_type& operator/=(const T& val) + { + r_ /= val; + i_ /= val; + return *this; + } + + constexpr this_type& operator+=(const this_type& source) + { + r_ += source.r_; + i_ += source.i_; + return *this; + } + + constexpr this_type& operator-=(const this_type& source) + { + r_ -= source.r_; + i_ -= source.i_; + return *this; + } + + constexpr this_type& operator*=(const this_type& source) + { + *this = *this * source; + return *this; + } + + constexpr this_type& operator/=(const this_type& source) + { + *this = *this / source; + return *this; + } + + private: + T r_,i_; +}; + +} + +} + +#if BOOST_UNITS_HAS_BOOST_TYPEOF + +#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() + +BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::complex, 1) + +#endif + +namespace boost { + +namespace units { + +template +constexpr +complex::type> +operator+(const complex& x) +{ + typedef typename unary_plus_typeof_helper::type type; + + return complex(x.real(),x.imag()); +} + +template +constexpr +complex::type> +operator-(const complex& x) +{ + typedef typename unary_minus_typeof_helper::type type; + + return complex(-x.real(),-x.imag()); +} + +template +constexpr +complex::type> +operator+(const complex& x,const complex& y) +{ + typedef typename boost::units::add_typeof_helper::type type; + + return complex(x.real()+y.real(),x.imag()+y.imag()); +} + +template +constexpr +complex::type> +operator-(const complex& x,const complex& y) +{ + typedef typename boost::units::subtract_typeof_helper::type type; + + return complex(x.real()-y.real(),x.imag()-y.imag()); +} + +template +constexpr +complex::type> +operator*(const complex& x,const complex& y) +{ + typedef typename boost::units::multiply_typeof_helper::type type; + + return complex(x.real()*y.real() - x.imag()*y.imag(), + x.real()*y.imag() + x.imag()*y.real()); + +// fully correct implementation has more complex return type +// +// typedef typename boost::units::multiply_typeof_helper::type xy_type; +// +// typedef typename boost::units::add_typeof_helper< +// xy_type,xy_type>::type xy_plus_xy_type; +// typedef typename +// boost::units::subtract_typeof_helper::type +// xy_minus_xy_type; +// +// BOOST_STATIC_ASSERT((boost::is_same::value == true)); +// +// return complex(x.real()*y.real()-x.imag()*y.imag(), +// x.real()*y.imag()+x.imag()*y.real()); +} + +template +constexpr +complex::type> +operator/(const complex& x,const complex& y) +{ + // naive implementation of complex division + typedef typename boost::units::divide_typeof_helper::type type; + + return complex((x.real()*y.real()+x.imag()*y.imag())/ + (y.real()*y.real()+y.imag()*y.imag()), + (x.imag()*y.real()-x.real()*y.imag())/ + (y.real()*y.real()+y.imag()*y.imag())); + +// fully correct implementation has more complex return type +// +// typedef typename boost::units::multiply_typeof_helper::type xy_type; +// typedef typename boost::units::multiply_typeof_helper::type yy_type; +// +// typedef typename boost::units::add_typeof_helper::type +// xy_plus_xy_type; +// typedef typename boost::units::subtract_typeof_helper< +// xy_type,xy_type>::type xy_minus_xy_type; +// +// typedef typename boost::units::divide_typeof_helper< +// xy_plus_xy_type,yy_type>::type xy_plus_xy_over_yy_type; +// typedef typename boost::units::divide_typeof_helper< +// xy_minus_xy_type,yy_type>::type xy_minus_xy_over_yy_type; +// +// BOOST_STATIC_ASSERT((boost::is_same::value == true)); +// +// return complex( +// (x.real()*y.real()+x.imag()*y.imag())/ +// (y.real()*y.real()+y.imag()*y.imag()), +// (x.imag()*y.real()-x.real()*y.imag())/ +// (y.real()*y.real()+y.imag()*y.imag())); +} + +template +complex +pow(const complex& x,const Y& y) +{ + std::complex tmp(x.real(),x.imag()); + + tmp = std::pow(tmp,y); + + return complex(tmp.real(),tmp.imag()); +} + +template +std::ostream& operator<<(std::ostream& os,const complex& val) +{ + os << val.real() << " + " << val.imag() << " i"; + + return os; +} + +/// specialize power typeof helper for complex +template +struct power_typeof_helper,static_rational > +{ + typedef complex< + typename power_typeof_helper >::type + > type; + + static type value(const complex& x) + { + const static_rational rat; + + const Y m = Y(rat.numerator())/Y(rat.denominator()); + + return boost::units::pow(x,m); + } +}; + +/// specialize root typeof helper for complex +template +struct root_typeof_helper,static_rational > +{ + typedef complex< + typename root_typeof_helper >::type + > type; + + static type value(const complex& x) + { + const static_rational rat; + + const Y m = Y(rat.denominator())/Y(rat.numerator()); + + return boost::units::pow(x,m); + } +}; + +/// specialize power typeof helper for complex > +template +struct power_typeof_helper >,static_rational > +{ + typedef typename + power_typeof_helper >::type value_type; + typedef typename + power_typeof_helper >::type unit_type; + typedef quantity quantity_type; + typedef complex type; + + static type value(const complex >& x) + { + const complex tmp = + pow >(complex(x.real().value(), + x.imag().value())); + + return type(quantity_type::from_value(tmp.real()), + quantity_type::from_value(tmp.imag())); + } +}; + +/// specialize root typeof helper for complex > +template +struct root_typeof_helper >,static_rational > +{ + typedef typename + root_typeof_helper >::type value_type; + typedef typename + root_typeof_helper >::type unit_type; + typedef quantity quantity_type; + typedef complex type; + + static type value(const complex >& x) + { + const complex tmp = + root >(complex(x.real().value(), + x.imag().value())); + + return type(quantity_type::from_value(tmp.real()), + quantity_type::from_value(tmp.imag())); + } +}; + +} // namespace units + +} // namespace boost +//] + +int main(void) +{ + using namespace boost::units; + using namespace boost::units::test; + + { + //[complex_snippet_1 + typedef quantity > length_dimension; + + const length_dimension L(complex(2.0,1.0)*meters); + //] + + std::cout << "+L = " << +L << std::endl + << "-L = " << -L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L*L = " << L*L << std::endl + << "L/L = " << L/L << std::endl + << "L^3 = " << pow<3>(L) << std::endl + << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl + << "3vL = " << root<3>(L) << std::endl + << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl + << std::endl; + } + + { + //[complex_snippet_2 + typedef complex > length_dimension; + + const length_dimension L(2.0*meters,1.0*meters); + //] + + std::cout << "+L = " << +L << std::endl + << "-L = " << -L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L*L = " << L*L << std::endl + << "L/L = " << L/L << std::endl + << "L^3 = " << pow<3>(L) << std::endl + << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl + << "3vL = " << root<3>(L) << std::endl + << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl + << std::endl; + } + + return 0; +} diff --git a/src/boost/libs/units/example/composite_output.cpp b/src/boost/libs/units/example/composite_output.cpp new file mode 100644 index 00000000..022ea4e6 --- /dev/null +++ b/src/boost/libs/units/example/composite_output.cpp @@ -0,0 +1,116 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief composite_output.cpp + +\details An example of textual representations of units. + +Output: +@verbatim + +//[conversion_output_output +2 dyn +2 dyn +2 dyne +cm g s^-1 +centimeter gram second^-1 +dyn +dyne +n +nano +n +nano +F +farad +1 F +1 farad +nF +nanofarad +1 nF +1 nanofarad +n(cm g s^-1) +nano(centimeter gram second^-1) +//] + +@endverbatim +**/ +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +namespace boost { + +namespace units { + +//[composite_output_snippet_1 + +std::string name_string(const cgs::force&) +{ + return "dyne"; +} + +std::string symbol_string(const cgs::force&) +{ + return "dyn"; +} + +//] + +} + +} + +int main() +{ + using namespace boost::units; + using boost::units::cgs::centimeter; + using boost::units::cgs::gram; + using boost::units::cgs::second; + using boost::units::cgs::dyne; + + //[composite_output_snippet_2] + std::cout << 2.0 * dyne << std::endl + << symbol_format << 2.0 * dyne << std::endl + << name_format << 2.0 * dyne << std::endl + << symbol_format << gram*centimeter/second << std::endl + << name_format << gram*centimeter/second << std::endl + << symbol_format << gram*centimeter/(second*second) << std::endl + << name_format << gram*centimeter/(second*second) << std::endl + << symbol_string(scale<10,static_rational<-9> >()) << std::endl + << name_string(scale<10,static_rational<-9> >()) << std::endl + << symbol_format << si::nano << std::endl + << name_format << si::nano << std::endl + << symbol_format << si::farad << std::endl + << name_format << si::farad << std::endl + << symbol_format << 1.0*si::farad << std::endl + << name_format << 1.0*si::farad << std::endl + << symbol_format << si::farad*si::nano << std::endl + << name_format << si::farad*si::nano << std::endl + << symbol_format << 1.0*si::farad*si::nano << std::endl + << name_format << 1.0*si::farad*si::nano << std::endl + << symbol_format << si::nano*gram*centimeter/second << std::endl + << name_format << si::nano*gram*centimeter/second << std::endl; + //] + + return 0; +} diff --git a/src/boost/libs/units/example/conversion.cpp b/src/boost/libs/units/example/conversion.cpp new file mode 100644 index 00000000..0299dfdc --- /dev/null +++ b/src/boost/libs/units/example/conversion.cpp @@ -0,0 +1,124 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief conversion.cpp + +\details +Test explicit and implicit unit conversion. + +Output: +@verbatim + +//[conversion_output_1 +L1 = 2 m +L2 = 2 m +L3 = 2 m +L4 = 200 cm +L5 = 5 m +L6 = 4 m +L7 = 200 cm +//] + +//[conversion_output_2 +volume (m^3) = 1 m^3 +volume (cm^3) = 1e+06 cm^3 +volume (m^3) = 1 m^3 + +energy (joules) = 1 J +energy (ergs) = 1e+07 erg +energy (joules) = 1 J + +velocity (2 m/s) = 2 m s^-1 +velocity (2 cm/s) = 0.02 m s^-1 +//] + +@endverbatim +**/ + +#include + +#include +#include +#include +#include +#include +#include + +using namespace boost::units; + +int main() +{ + // test quantity_cast + { + // implicit value_type conversions + //[conversion_snippet_1 + quantity L1 = quantity(int(2.5)*si::meters); + quantity L2(quantity(2.5*si::meters)); + //] + + //[conversion_snippet_3 + quantity L3 = static_cast >(L1); + //] + + //[conversion_snippet_4 + quantity L4 = static_cast >(L1); + //] + + quantity L5(4*si::meters), + L6(5*si::meters); + quantity L7(L1); + + swap(L5,L6); + + std::cout << "L1 = " << L1 << std::endl + << "L2 = " << L2 << std::endl + << "L3 = " << L3 << std::endl + << "L4 = " << L4 << std::endl + << "L5 = " << L5 << std::endl + << "L6 = " << L6 << std::endl + << "L7 = " << L7 << std::endl + << std::endl; + } + + // test explicit unit system conversion + { + //[conversion_snippet_5 + quantity vs(1.0*pow<3>(si::meter)); + quantity vc(vs); + quantity vs2(vc); + + quantity es(1.0*si::joule); + quantity ec(es); + quantity es2(ec); + + quantity v1 = 2.0*si::meters/si::second, + v2(2.0*cgs::centimeters/cgs::second); + //] + + std::cout << "volume (m^3) = " << vs << std::endl + << "volume (cm^3) = " << vc << std::endl + << "volume (m^3) = " << vs2 << std::endl + << std::endl; + + std::cout << "energy (joules) = " << es << std::endl + << "energy (ergs) = " << ec << std::endl + << "energy (joules) = " << es2 << std::endl + << std::endl; + + std::cout << "velocity (2 m/s) = " << v1 << std::endl + << "velocity (2 cm/s) = " << v2 << std::endl + << std::endl; + } + + return 0; +} diff --git a/src/boost/libs/units/example/conversion_factor.cpp b/src/boost/libs/units/example/conversion_factor.cpp new file mode 100644 index 00000000..4401e477 --- /dev/null +++ b/src/boost/libs/units/example/conversion_factor.cpp @@ -0,0 +1,76 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief conversion_factor.cpp + +\details An example of using conversion_factor. + +Output: +@verbatim + +//[conversion_factor_output +1e-005 +100 +1e-005 +100 +0.01 +//] + +@endverbatim +**/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + using namespace boost; + using namespace boost::units; + + //[conversion_factor_snippet_1 + + double dyne_to_newton = + conversion_factor(cgs::dyne,si::newton); + std::cout << dyne_to_newton << std::endl; + + double force_over_mass_conversion = + conversion_factor(si::newton/si::kilogram,cgs::dyne/cgs::gram); + std::cout << force_over_mass_conversion << std::endl; + + double momentum_conversion = + conversion_factor(cgs::momentum(),si::momentum()); + std::cout << momentum_conversion << std::endl; + + double momentum_over_mass_conversion = + conversion_factor(si::momentum()/si::mass(),cgs::momentum()/cgs::gram); + std::cout << momentum_over_mass_conversion << std::endl; + + double acceleration_conversion = + conversion_factor(cgs::gal,si::meter_per_second_squared); + std::cout << acceleration_conversion << std::endl; + + //] + + return 0; +} diff --git a/src/boost/libs/units/example/dimension.cpp b/src/boost/libs/units/example/dimension.cpp new file mode 100644 index 00000000..7f7ffb6b --- /dev/null +++ b/src/boost/libs/units/example/dimension.cpp @@ -0,0 +1,117 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief dimension.cpp + +\details +Test dimension list manipulation. + +Output: +@verbatim + +//[dimension_output +length_dimension = list >, dimensionless_type> +mass_dimension = list >, dimensionless_type> +time_dimension = list >, dimensionless_type> +energy_dimension = list >, list >, list >, dimensionless_type> > > +LM_type = list >, list >, dimensionless_type> > +L_T_type = list >, list >, dimensionless_type> > +V_type = list >, list >, dimensionless_type> > +//] + +@endverbatim +**/ + +#include +#include + +#include + +#include + +#include "test_system.hpp" + +namespace mpl = boost::mpl; + +int main(void) +{ + using namespace boost::units; + + BOOST_MPL_ASSERT((boost::is_same< + length_dimension, + mpl::push_front< + dimensionless_type, + dim > + >::type + >)); + BOOST_MPL_ASSERT((boost::is_same< + mass_dimension, + mpl::push_front< + dimensionless_type, + dim > + >::type + >)); + BOOST_MPL_ASSERT((boost::is_same > >::type, + dim > >::type, + dim > >::type>)); + + std::cout << "length_dimension = " + << simplify_typename(length_dimension()) << std::endl + << "mass_dimension = " + << simplify_typename(mass_dimension()) << std::endl + << "time_dimension = " + << simplify_typename(time_dimension()) << std::endl + << "energy_dimension = " + << simplify_typename(energy_dimension()) << std::endl; + + //[dimension_snippet_1 + typedef mpl::times::type LM_type; + typedef mpl::divides::type L_T_type; + typedef static_root< + mpl::divides::type, + static_rational<2> + >::type V_type; + //] + + BOOST_MPL_ASSERT((boost::is_same > >::type, + dim > >::type>)); + + BOOST_MPL_ASSERT((boost::is_same > >::type, + dim > >::type>)); + + BOOST_MPL_ASSERT((boost::is_same > >::type, + dim > >::type>)); + + std::cout << "LM_type = " << simplify_typename(LM_type()) << std::endl + << "L_T_type = " << simplify_typename(L_T_type()) << std::endl + << "V_type = " << simplify_typename(V_type()) << std::endl; + + return 0; +} diff --git a/src/boost/libs/units/example/heterogeneous_unit.cpp b/src/boost/libs/units/example/heterogeneous_unit.cpp new file mode 100644 index 00000000..7a4f1123 --- /dev/null +++ b/src/boost/libs/units/example/heterogeneous_unit.cpp @@ -0,0 +1,87 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief heterogeneous_unit.cpp + +\details +Test heterogeneous units and quantities. + +Output: +@verbatim + +//[heterogeneous_unit_output_1 +1.5 m +1 g +1.5 m g +1.5 m g^-1 + +1 N +1 kg s^-2 + +1 cm kg s^-2 +1 cm m^-1 kg s^-2 +//] + +//[heterogeneous_unit_output_2 +1.5 cm m +0.015 m^2 +//] + +@endverbatim +**/ + +#define MCS_USE_DEMANGLING +//#define MCS_USE_BOOST_REGEX_DEMANGLING + +#include + +#include +#include +#include +#include +#include +#include + +using namespace boost::units; + +int main() +{ + //[heterogeneous_unit_snippet_1 + quantity L(1.5*si::meter); + quantity M(1.0*cgs::gram); + + std::cout << L << std::endl + << M << std::endl + << L*M << std::endl + << L/M << std::endl + << std::endl; + + std::cout << 1.0*si::meter*si::kilogram/pow<2>(si::second) << std::endl + << 1.0*si::meter*si::kilogram/pow<2>(si::second)/si::meter + << std::endl << std::endl; + + std::cout << 1.0*cgs::centimeter*si::kilogram/pow<2>(si::second) << std::endl + << 1.0*cgs::centimeter*si::kilogram/pow<2>(si::second)/si::meter + << std::endl << std::endl; + //] + + //[heterogeneous_unit_snippet_2 + quantity A(1.5*si::meter*cgs::centimeter); + + std::cout << 1.5*si::meter*cgs::centimeter << std::endl + << A << std::endl + << std::endl; + //] + + return 0; +} diff --git a/src/boost/libs/units/example/information.cpp b/src/boost/libs/units/example/information.cpp new file mode 100644 index 00000000..21ae3f91 --- /dev/null +++ b/src/boost/libs/units/example/information.cpp @@ -0,0 +1,100 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2014 Erik Erlandson +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +//#include + +/** +\file + +\brief information.cpp + +\details +Demonstrate information unit system. + +Output: +@verbatim +bytes= 1.25e+08 B +bits= 8e+06 b +nats= 4605.17 nat +1024 bytes in a kibi-byte +8.38861e+06 bits in a mebi-byte +0.000434294 hartleys in a milli-nat +entropy in bits= 1 b +entropy in nats= 0.693147 nat +entropy in hartleys= 0.30103 Hart +entropy in shannons= 1 Sh +entropy in bytes= 0.125 B +@endverbatim +**/ + +#include +#include +using std::cout; +using std::endl; +using std::log; + +#include +#include +#include +namespace bu = boost::units; +using bu::quantity; +using bu::conversion_factor; + +// SI prefixes +#include +namespace si = boost::units::si; + +// information unit system +#include +using namespace bu::information; + +// Define a function for the entropy of a bernoulli trial. +// The formula is computed using natural log, so the units are in nats. +// The user provides the desired return unit, the only restriction being that it +// must be a unit of information. Conversion to the requested return unit is +// accomplished automatically by the boost::units library. +template +constexpr +quantity > +bernoulli_entropy(double p, const bu::unit&) { + typedef bu::unit requested_unit; + return quantity((-(p*log(p) + (1-p)*log(1-p)))*nats); +} + +int main(int argc, char** argv) { + // a quantity of information (default in units of bytes) + quantity nbytes(1 * si::giga * bit); + cout << "bytes= " << nbytes << endl; + + // a quantity of information, stored as bits + quantity nbits(1 * si::mega * byte); + cout << "bits= " << nbits << endl; + + // a quantity of information, stored as nats + quantity nnats(2 * si::kilo * hartleys); + cout << "nats= " << nnats << endl; + + // how many bytes are in a kibi-byte? + cout << conversion_factor(kibi * byte, byte) << " bytes in a kibi-byte" << endl; + + // how many bits are in a mebi-byte? + cout << conversion_factor(mebi * byte, bit) << " bits in a mebi-byte" << endl; + + // how many hartleys are in a milli-nat? + cout << conversion_factor(si::milli * nat, hartley) << " hartleys in a milli-nat" << endl; + + // compute the entropy of a fair coin flip, in various units of information: + cout << "entropy in bits= " << bernoulli_entropy(0.5, bits) << endl; + cout << "entropy in nats= " << bernoulli_entropy(0.5, nats) << endl; + cout << "entropy in hartleys= " << bernoulli_entropy(0.5, hartleys) << endl; + cout << "entropy in shannons= " << bernoulli_entropy(0.5, shannons) << endl; + cout << "entropy in bytes= " << bernoulli_entropy(0.5, bytes) << endl; + + return 0; +} diff --git a/src/boost/libs/units/example/kitchen_sink.cpp b/src/boost/libs/units/example/kitchen_sink.cpp new file mode 100644 index 00000000..ece126bc --- /dev/null +++ b/src/boost/libs/units/example/kitchen_sink.cpp @@ -0,0 +1,542 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief kitchen_sink.cpp + +\details +More extensive quantity tests. + +Output: +@verbatim + +//[kitchen_sink_output_1 +S1 : 2 +X1 : 2 +X2 : (4/3) +U1 : N +U2 : J +Q1 : 1 N +Q2 : 2 J +//] + +//[kitchen_sink_output_2 +U1*S1 : 2 N +S1*U1 : 2 N +U1/S1 : 0.5 N +S1/U1 : 2 m^-1 kg^-1 s^2 +//] + +//[kitchen_sink_output_3 +U1+U1 : N +U1-U1 : N +U1*U1 : m^2 kg^2 s^-4 +U1/U1 : dimensionless +U1*U2 : m^3 kg^2 s^-4 +U1/U2 : m^-1 +U1^X : m^2 kg^2 s^-4 +X1vU1 : m^(1/2) kg^(1/2) s^-1 +U1^X2 : m^(4/3) kg^(4/3) s^(-8/3) +X2vU1 : m^(3/4) kg^(3/4) s^(-3/2) +//] + +//[kitchen_sink_output_4 +Q1*S1 : 2 N +S1*Q1 : 2 N +Q1/S1 : 0.5 N +S1/Q1 : 2 m^-1 kg^-1 s^2 +//] + +//[kitchen_sink_output_5 +U1*Q1 : 1 m^2 kg^2 s^-4 +Q1*U1 : 1 m^2 kg^2 s^-4 +U1/Q1 : 1 dimensionless +Q1/U1 : 1 dimensionless +//] + +//[kitchen_sink_output_6 ++Q1 : 1 N +-Q1 : -1 N +Q1+Q1 : 2 N +Q1-Q1 : 0 N +Q1*Q1 : 1 m^2 kg^2 s^-4 +Q1/Q1 : 1 dimensionless +Q1*Q2 : 2 m^3 kg^2 s^-4 +Q1/Q2 : 0.5 m^-1 +Q1^X1 : 1 m^2 kg^2 s^-4 +X1vQ1 : 1 m^(1/2) kg^(1/2) s^-1 +Q1^X2 : 1 m^(4/3) kg^(4/3) s^(-8/3) +X2vQ1 : 1 m^(3/4) kg^(3/4) s^(-3/2) +//] + +//[kitchen_sink_output_7 +l1 == l2 false +l1 != l2 true +l1 <= l2 true +l1 < l2 true +l1 >= l2 false +l1 > l2 false +//] + +dimless = 1 + +//[kitchen_sink_output_8 +v1 = 2 m s^-1 +//] + +//[kitchen_sink_output_9 +F = 1 N +dx = 1 m +E = 1 J +//] + +//[kitchen_sink_output_10 +r = 5e-07 m +P = 101325 Pa +V = 5.23599e-19 m^3 +T = 310 K +n = 2.05835e-17 mol +R = 8.314472 m^2 kg s^-2 K^-1 mol^-1 (rel. unc. = 1.8e-06) +//] + +//[kitchen_sink_output_11 +theta = 0.375 rd +sin(theta) = 0.366273 dimensionless +asin(sin(theta)) = 0.375 rd +//] + +//[kitchen_sink_output_12 +V = (12.5,0) V +I = (3,4) A +Z = (1.5,-2) Ohm +I*Z = (12.5,0) V +//] + +//[kitchen_sink_output_13 +x+y-w = 0.48(+/-0.632772) m +w*x = 9.04(+/-0.904885) m^2 +x/y = 0.666667(+/-0.149071) dimensionless +//] + +//[kitchen_sink_output_14 +w*y^2/(u*x)^2 = 10.17(+/-3.52328) m^-1 +w/(u*x)^(1/2) = 3.19612(+/-0.160431) dimensionless +//] + +//[kitchen_sink_output_15 +I*w = m^2 kg s^-1 rad^-1 +I*w/L = dimensionless +I*w^2 = J +//] + +//[kitchen_sink_output_16 +1 F +1 kat +1 S +1 C +1 V +1 J +1 N +1 Hz +1 lx +1 H +1 lm +1 Wb +1 T +1 W +1 Pa +1 Ohm +//] + +//[kitchen_sink_output_18 +1 farad +1 katal +1 siemen +1 coulomb +1 volt +1 joule +1 newton +1 hertz +1 lux +1 henry +1 lumen +1 weber +1 tesla +1 watt +1 pascal +1 ohm +//] + +@endverbatim +**/ + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include "measurement.hpp" + +namespace boost { + +namespace units { + +//[kitchen_sink_function_snippet_3 +/// the physical definition of work - computed for an arbitrary unit system +template +constexpr +quantity,Y> +work(quantity,Y> F, + quantity,Y> dx) +{ + return F*dx; +} +//] + +//[kitchen_sink_function_snippet_4 +/// the ideal gas law in si units +template +constexpr +quantity +idealGasLaw(const quantity& P, + const quantity& V, + const quantity& T) +{ + using namespace boost::units::si; + + using namespace constants::codata; + return (P*V/(R*T)); +} +//] + +} // namespace units + +} // namespace boost + +int main() +{ + using namespace boost::units; + using namespace boost::units::si; + + { + //[kitchen_sink_snippet_1 + /// scalar + const double s1 = 2; + + const long x1 = 2; + const static_rational<4,3> x2; + + /// define some units + force u1 = newton; + energy u2 = joule; + + /// define some quantities + quantity q1(1.0*u1); + quantity q2(2.0*u2); + //] + + /// check scalar, unit, and quantity io + std::cout << "S1 : " << s1 << std::endl + << "X1 : " << x1 << std::endl + << "X2 : " << x2 << std::endl + << "U1 : " << u1 << std::endl + << "U2 : " << u2 << std::endl + << "Q1 : " << q1 << std::endl + << "Q2 : " << q2 << std::endl + << std::endl; + + /// check scalar-unit algebra + std::cout //<< "U1+S1 : " << u1+s1 << std::endl // illegal + //<< "S1+U1 : " << s1+u1 << std::endl // illegal + //<< "U1-S1 : " << u1-s1 << std::endl // illegal + //<< "S1-U1 : " << s1-u1 << std::endl // illegal + << "U1*S1 : " << u1*s1 << std::endl + << "S1*U1 : " << s1*u1 << std::endl + << "U1/S1 : " << u1/s1 << std::endl + << "S1/U1 : " << s1/u1 << std::endl + << std::endl; + + /// check unit-unit algebra + std::cout << "U1+U1 : " << u1+u1 << std::endl + << "U1-U1 : " << u1-u1 << std::endl + << "U1*U1 : " << u1*u1 << std::endl + << "U1/U1 : " << u1/u1 << std::endl + //<< "U1+U2 : " << u1+u2 << std::endl // illegal + //<< "U1-U2 : " << u1-u2 << std::endl // illegal + << "U1*U2 : " << u1*u2 << std::endl + << "U1/U2 : " << u1/u2 << std::endl + << "U1^X : " << pow<2>(u1) << std::endl + << "X1vU1 : " << root<2>(u1) << std::endl + << "U1^X2 : " << pow >(u1) << std::endl + << "X2vU1 : " << root >(u1) << std::endl + << std::endl; + + /// check scalar-quantity algebra + std::cout //<< "Q1+S1 : " << q1+s1 << std::endl // illegal + //<< "S1+Q1 : " << s1+q1 << std::endl // illegal + //<< "Q1-S1 : " << q1-s1 << std::endl // illegal + //<< "S1-Q1 : " << s1-q1 << std::endl // illegal + << "Q1*S1 : " << q1*s1 << std::endl + << "S1*Q1 : " << s1*q1 << std::endl + << "Q1/S1 : " << q1/s1 << std::endl + << "S1/Q1 : " << s1/q1 << std::endl + << std::endl; + + /// check unit-quantity algebra + std::cout //<< "U1+Q1 : " << u1+q1 << std::endl // illegal + //<< "Q1+U1 : " << q1+u1 << std::endl // illegal + //<< "U1-Q1 : " << u1-q1 << std::endl // illegal + //<< "Q1-U1 : " << q1-u1 << std::endl // illegal + << "U1*Q1 : " << u1*q1 << std::endl + << "Q1*U1 : " << q1*u1 << std::endl + << "U1/Q1 : " << u1/q1 << std::endl + << "Q1/U1 : " << q1/u1 << std::endl + << std::endl; + + /// check quantity-quantity algebra + std::cout << "+Q1 : " << +q1 << std::endl + << "-Q1 : " << -q1 << std::endl + << "Q1+Q1 : " << q1+q1 << std::endl + << "Q1-Q1 : " << q1-q1 << std::endl + << "Q1*Q1 : " << q1*q1 << std::endl + << "Q1/Q1 : " << q1/q1 << std::endl + //<< "Q1+Q2 : " << q1+q2 << std::endl // illegal + //<< "Q1-Q2 : " << q1-q2 << std::endl // illegal + << "Q1*Q2 : " << q1*q2 << std::endl + << "Q1/Q2 : " << q1/q2 << std::endl + << "Q1^X1 : " << pow<2>(q1) << std::endl + << "X1vQ1 : " << root<2>(q1) << std::endl + << "Q1^X2 : " << pow >(q1) << std::endl + << "X2vQ1 : " << root >(q1) << std::endl + << std::endl; + + //[kitchen_sink_snippet_2 + /// check comparison tests + quantity l1(1.0*meter), + l2(2.0*meters); + //] + + std::cout << std::boolalpha + << "l1 == l2" << "\t" << (l1 == l2) << std::endl + << "l1 != l2" << "\t" << (l1 != l2) << std::endl + << "l1 <= l2" << "\t" << (l1 <= l2) << std::endl + << "l1 < l2 " << "\t" << (l1 < l2) << std::endl + << "l1 >= l2" << "\t" << (l1 >= l2) << std::endl + << "l1 > l2 " << "\t" << (l1 > l2) << std::endl + << std::endl; + + //[kitchen_sink_snippet_3 + /// check implicit unit conversion from dimensionless to value_type + const double dimless = (q1/q1); + //] + + std::cout << "dimless = " << dimless << std::endl + << std::endl; + + quantity v1 = 2.0*meters/second; + + std::cout << "v1 = " << v1 << std::endl + << std::endl; + + //[kitchen_sink_snippet_4 + /// test calcuation of work + quantity F(1.0*newton); + quantity dx(1.0*meter); + quantity E(work(F,dx)); + //] + + std::cout << "F = " << F << std::endl + << "dx = " << dx << std::endl + << "E = " << E << std::endl + << std::endl; + + { + //[kitchen_sink_snippet_5 + /// test ideal gas law + quantity T = (273.+37.)*kelvin; + quantity P = 1.01325e5*pascals; + quantity r = 0.5e-6*meters; + quantity V = (4.0/3.0)*3.141592*pow<3>(r); + quantity n(idealGasLaw(P,V,T)); + //] + + std::cout << "r = " << r << std::endl + << "P = " << P << std::endl + << "V = " << V << std::endl + << "T = " << T << std::endl + << "n = " << n << std::endl + #if BOOST_UNITS_HAS_TYPEOF + << "R = " << constants::codata::R << std::endl + #else + << "no typeof" << std::endl + #endif // BOOST_UNITS_HAS_TYPEOF + << std::endl; + } + + //[kitchen_sink_snippet_6 + /// test trig stuff + quantity theta = 0.375*radians; + quantity sin_theta = sin(theta); + quantity thetap = asin(sin_theta); + //] + + std::cout << "theta = " << theta << std::endl + << "sin(theta) = " << sin_theta << std::endl + << "asin(sin(theta)) = " << thetap << std::endl + << std::endl; + + /// test implicit conversion of dimensionless to value + double tmp = sin_theta; + + tmp = sin_theta; + + /// test implicit conversion from value to dimensionless + quantity tmpp = tmp; + + tmpp = tmp; + + /// check complex quantities + typedef std::complex complex_type; + + //[kitchen_sink_snippet_7 + quantity v = complex_type(12.5,0.0)*volts; + quantity i = complex_type(3.0,4.0)*amperes; + quantity z = complex_type(1.5,-2.0)*ohms; + //] + + std::cout << "V = " << v << std::endl + << "I = " << i << std::endl + << "Z = " << z << std::endl + << "I*Z = " << i*z << std::endl + << std::endl; + + /// check quantities using user-defined type encapsulating error propagation + + //[kitchen_sink_snippet_8 + quantity > + u(measurement(1.0,0.0)*meters), + w(measurement(4.52,0.02)*meters), + x(measurement(2.0,0.2)*meters), + y(measurement(3.0,0.6)*meters); + //] + + std::cout << "x+y-w = " << x+y-w << std::endl + << "w*x = " << w*x << std::endl + << "x/y = " << x/y << std::endl + << "w*y^2/(u*x)^2 = " << w*y*y/pow<2>(u*x) << std::endl + << "w/(u*x)^(1/2) = " << w/pow< static_rational<1,2> >(u*x) + << std::endl << std::endl; + } + + /// check moment of inertia/angular momentum/rotational energy + + //[kitchen_sink_snippet_9 + std::cout << symbol_format + << "I*w = " << moment_of_inertia()*angular_velocity() << std::endl + << "I*w/L = " << moment_of_inertia()*angular_velocity()/angular_momentum() << std::endl + << "I*w^2 = " << moment_of_inertia()*pow<2>(angular_velocity()) << std::endl + << std::endl; + //] + + //[kitchen_sink_snippet_10 +// std::cout << typename_format +// << quantity(1.0*farad) << std::endl +// << quantity(1.0*katal) << std::endl +// << quantity(1.0*siemen) << std::endl +// << quantity(1.0*coulomb) << std::endl +// << quantity(1.0*volt) << std::endl +// << quantity(1.0*joule) << std::endl +// << quantity(1.0*newton) << std::endl +// << quantity(1.0*hertz) << std::endl +// << quantity(1.0*lux) << std::endl +// << quantity(1.0*henry) << std::endl +// << quantity(1.0*lumen) << std::endl +// << quantity(1.0*weber) << std::endl +// << quantity(1.0*tesla) << std::endl +// << quantity(1.0*watt) << std::endl +// << quantity(1.0*pascals) << std::endl +// << quantity(1.0*ohm) << std::endl +// << std::endl; + //] + + //[kitchen_sink_snippet_11 +// std::cout << raw_format +// << quantity(1.0*farad) << std::endl +// << quantity(1.0*katal) << std::endl +// << quantity(1.0*siemen) << std::endl +// << quantity(1.0*coulomb) << std::endl +// << quantity(1.0*volt) << std::endl +// << quantity(1.0*joule) << std::endl +// << quantity(1.0*newton) << std::endl +// << quantity(1.0*hertz) << std::endl +// << quantity(1.0*lux) << std::endl +// << quantity(1.0*henry) << std::endl +// << quantity(1.0*lumen) << std::endl +// << quantity(1.0*weber) << std::endl +// << quantity(1.0*tesla) << std::endl +// << quantity(1.0*watt) << std::endl +// << quantity(1.0*pascals) << std::endl +// << quantity(1.0*ohm) << std::endl +// << std::endl; + //] + + //[kitchen_sink_snippet_12 + std::cout << symbol_format + << quantity(1.0*farad) << std::endl + << quantity(1.0*katal) << std::endl + << quantity(1.0*siemen) << std::endl + << quantity(1.0*coulomb) << std::endl + << quantity(1.0*volt) << std::endl + << quantity(1.0*joule) << std::endl + << quantity(1.0*newton) << std::endl + << quantity(1.0*hertz) << std::endl + << quantity(1.0*lux) << std::endl + << quantity(1.0*henry) << std::endl + << quantity(1.0*lumen) << std::endl + << quantity(1.0*weber) << std::endl + << quantity(1.0*tesla) << std::endl + << quantity(1.0*watt) << std::endl + << quantity(1.0*pascals) << std::endl + << quantity(1.0*ohm) << std::endl + << std::endl; + //] + + //[kitchen_sink_snippet_13 + std::cout << name_format + << quantity(1.0*farad) << std::endl + << quantity(1.0*katal) << std::endl + << quantity(1.0*siemen) << std::endl + << quantity(1.0*coulomb) << std::endl + << quantity(1.0*volt) << std::endl + << quantity(1.0*joule) << std::endl + << quantity(1.0*newton) << std::endl + << quantity(1.0*hertz) << std::endl + << quantity(1.0*lux) << std::endl + << quantity(1.0*henry) << std::endl + << quantity(1.0*lumen) << std::endl + << quantity(1.0*weber) << std::endl + << quantity(1.0*tesla) << std::endl + << quantity(1.0*watt) << std::endl + << quantity(1.0*pascals) << std::endl + << quantity(1.0*ohm) << std::endl + << std::endl; + //] + + return 0; +} diff --git a/src/boost/libs/units/example/lambda.cpp b/src/boost/libs/units/example/lambda.cpp new file mode 100644 index 00000000..0fbac916 --- /dev/null +++ b/src/boost/libs/units/example/lambda.cpp @@ -0,0 +1,157 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// $Id: lambda.cpp 27 2008-06-16 14:50:58Z maehne $ + +//////////////////////////////////////////////////////////////////////// +/// +/// \file lambda.cpp +/// +/// \brief Example demonstrating the usage of Boost.Units' quantity, +/// unit, and absolute types in functors created with the +/// Boost.Lambda library and stored in Boost.Function objects. +/// +/// \author Torsten Maehne +/// \date 2008-06-04 +/// +/// A mechanical, electrical, geometrical, and thermal example +/// demonstrate how to use Boost.Units' quantity, unit, and absolute +/// types in lambda expressions. The resulting functors can be stored +/// in boost::function objects. It is also shown how to work around a +/// limitation of Boost.Lambda's bind() to help it to find the correct +/// overloaded function by specifying its signature with a +/// static_cast. +/// +//////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include +#include + +// Include boost/units/lambda.hpp instead of boost/lambda/lambda.hpp +// for a convenient usage of Boost.Units' quantity, unit, and absolute +// types in lambda expressions. The header augments Boost.Lambda's +// return type detuction system to recognize the new types so that not +// for each arithmetic operation the return type needs to be +// explicitely specified. +#include + +#include + +static const double pi = 3.14159265358979323846; + +//[lambda_snippet_1 + +int main(int argc, char **argv) { + + using namespace std; + namespace bl = boost::lambda; + namespace bu = boost::units; + namespace si = boost::units::si; + + + //////////////////////////////////////////////////////////////////////// + // Mechanical example: linear accelerated movement + //////////////////////////////////////////////////////////////////////// + + // Initial condition variables for acceleration, speed, and displacement + bu::quantity a = 2.0 * si::meters_per_second_squared; + bu::quantity v = 1.0 * si::meters_per_second; + bu::quantity s0 = 0.5 * si::meter; + + // Displacement over time + boost::function (bu::quantity) > + s = 0.5 * bl::var(a) * bl::_1 * bl::_1 + + bl::var(v) * bl::_1 + + bl::var(s0); + + cout << "Linear accelerated movement:" << endl + << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl + << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl + << endl; + + // Change initial conditions + a = 1.0 * si::meters_per_second_squared; + v = 2.0 * si::meters_per_second; + s0 = -1.5 * si::meter; + + cout << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl + << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl + << endl; + + + //////////////////////////////////////////////////////////////////////// + // Electrical example: oscillating current + //////////////////////////////////////////////////////////////////////// + + // Constants for the current amplitude, frequency, and offset current + const bu::quantity iamp = 1.5 * si::ampere; + const bu::quantity f = 1.0e3 * si::hertz; + const bu::quantity i0 = 0.5 * si::ampere; + + // The invocation of the sin function needs to be postponed using + // bind to specify the oscillation function. A lengthy static_cast + // to the function pointer referencing boost::units::sin() is needed + // to avoid an "unresolved overloaded function type" error. + boost::function (bu::quantity) > + i = iamp + * bl::bind(static_cast::type (*)(const bu::quantity&)>(bu::sin), + 2.0 * pi * si::radian * f * bl::_1) + + i0; + + cout << "Oscillating current:" << endl + << "iamp = " << iamp << ", f = " << f << ", i0 = " << i0 << endl + << "i(1.25e-3 * si::second) = " << i(1.25e-3 * si::second) << endl + << endl; + + + //////////////////////////////////////////////////////////////////////// + // Geometric example: area calculation for a square + //////////////////////////////////////////////////////////////////////// + + // Length constant + const bu::quantity l = 1.5 * si::meter; + + // Again an ugly static_cast is needed to bind pow<2> to the first + // function argument. + boost::function (bu::quantity) > + A = bl::bind(static_cast (*)(const bu::quantity&)>(bu::pow<2>), + bl::_1); + + cout << "Area of a square:" << endl + << "A(" << l <<") = " << A(l) << endl << endl; + + + //////////////////////////////////////////////////////////////////////// + // Thermal example: temperature difference of two absolute temperatures + //////////////////////////////////////////////////////////////////////// + + // Absolute temperature constants + const bu::quantity > + Tref = 273.15 * bu::absolute(); + const bu::quantity > + Tamb = 300.00 * bu::absolute(); + + boost::function (bu::quantity >, + bu::quantity >)> + dT = bl::_2 - bl::_1; + + cout << "Temperature difference of two absolute temperatures:" << endl + << "dT(" << Tref << ", " << Tamb << ") = " << dT(Tref, Tamb) << endl + << endl; + + + return 0; +} +//] diff --git a/src/boost/libs/units/example/measurement.hpp b/src/boost/libs/units/example/measurement.hpp new file mode 100644 index 00000000..72063023 --- /dev/null +++ b/src/boost/libs/units/example/measurement.hpp @@ -0,0 +1,344 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_UNITS_MEASUREMENT_HPP +#define BOOST_UNITS_MEASUREMENT_HPP + +#include +#include +#include +#include + +#include +#include + +namespace boost { + +namespace units { + +namespace sqr_namespace /**/ { + +template +constexpr +Y sqr(Y val) +{ return val*val; } + +} // namespace + +using sqr_namespace::sqr; + +template +class measurement +{ + public: + typedef measurement this_type; + typedef Y value_type; + + constexpr measurement(const value_type& val = value_type(), + const value_type& err = value_type()) : + value_(val), + uncertainty_(std::abs(err)) + { } + + constexpr measurement(const this_type& source) : + value_(source.value_), + uncertainty_(source.uncertainty_) + { } + + //~measurement() { } + + constexpr this_type& operator=(const this_type& source) + { + if (this == &source) return *this; + + value_ = source.value_; + uncertainty_ = source.uncertainty_; + + return *this; + } + + constexpr operator value_type() const { return value_; } + + constexpr value_type value() const { return value_; } + constexpr value_type uncertainty() const { return uncertainty_; } + constexpr value_type lower_bound() const { return value_-uncertainty_; } + constexpr value_type upper_bound() const { return value_+uncertainty_; } + + constexpr this_type& operator+=(const value_type& val) + { + value_ += val; + return *this; + } + + constexpr this_type& operator-=(const value_type& val) + { + value_ -= val; + return *this; + } + + constexpr this_type& operator*=(const value_type& val) + { + value_ *= val; + uncertainty_ *= val; + return *this; + } + + constexpr this_type& operator/=(const value_type& val) + { + value_ /= val; + uncertainty_ /= val; + return *this; + } + + constexpr this_type& operator+=(const this_type& /*source*/); + constexpr this_type& operator-=(const this_type& /*source*/); + constexpr this_type& operator*=(const this_type& /*source*/); + constexpr this_type& operator/=(const this_type& /*source*/); + + private: + value_type value_, + uncertainty_; +}; + +} + +} + +#if BOOST_UNITS_HAS_BOOST_TYPEOF + +BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::measurement, 1) + +#endif + +namespace boost { + +namespace units { + +template +inline +constexpr +measurement& +measurement::operator+=(const this_type& source) +{ + uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_)); + value_ += source.value_; + + return *this; +} + +template +inline +constexpr +measurement& +measurement::operator-=(const this_type& source) +{ + uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_)); + value_ -= source.value_; + + return *this; +} + +template +inline +constexpr +measurement& +measurement::operator*=(const this_type& source) +{ + uncertainty_ = (value_*source.value_)* + std::sqrt(sqr(uncertainty_/value_)+ + sqr(source.uncertainty_/source.value_)); + value_ *= source.value_; + + return *this; +} + +template +inline +constexpr +measurement& +measurement::operator/=(const this_type& source) +{ + uncertainty_ = (value_/source.value_)* + std::sqrt(sqr(uncertainty_/value_)+ + sqr(source.uncertainty_/source.value_)); + value_ /= source.value_; + + return *this; +} + +// value_type op measurement +template +inline +constexpr +measurement +operator+(Y lhs,const measurement& rhs) +{ + return (measurement(lhs,Y(0))+=rhs); +} + +template +inline +constexpr +measurement +operator-(Y lhs,const measurement& rhs) +{ + return (measurement(lhs,Y(0))-=rhs); +} + +template +inline +constexpr +measurement +operator*(Y lhs,const measurement& rhs) +{ + return (measurement(lhs,Y(0))*=rhs); +} + +template +inline +constexpr +measurement +operator/(Y lhs,const measurement& rhs) +{ + return (measurement(lhs,Y(0))/=rhs); +} + +// measurement op value_type +template +inline +constexpr +measurement +operator+(const measurement& lhs,Y rhs) +{ + return (measurement(lhs)+=measurement(rhs,Y(0))); +} + +template +inline +constexpr +measurement +operator-(const measurement& lhs,Y rhs) +{ + return (measurement(lhs)-=measurement(rhs,Y(0))); +} + +template +inline +constexpr +measurement +operator*(const measurement& lhs,Y rhs) +{ + return (measurement(lhs)*=measurement(rhs,Y(0))); +} + +template +inline +constexpr +measurement +operator/(const measurement& lhs,Y rhs) +{ + return (measurement(lhs)/=measurement(rhs,Y(0))); +} + +// measurement op measurement +template +inline +constexpr +measurement +operator+(const measurement& lhs,const measurement& rhs) +{ + return (measurement(lhs)+=rhs); +} + +template +inline +constexpr +measurement +operator-(const measurement& lhs,const measurement& rhs) +{ + return (measurement(lhs)-=rhs); +} + +template +inline +constexpr +measurement +operator*(const measurement& lhs,const measurement& rhs) +{ + return (measurement(lhs)*=rhs); +} + +template +inline +constexpr +measurement +operator/(const measurement& lhs,const measurement& rhs) +{ + return (measurement(lhs)/=rhs); +} + +/// specialize power typeof helper +template +struct power_typeof_helper,static_rational > +{ + typedef measurement< + typename power_typeof_helper >::type + > type; + + static constexpr type value(const measurement& x) + { + const static_rational rat; + + const Y m = Y(rat.numerator())/Y(rat.denominator()), + newval = std::pow(x.value(),m), + err = newval*std::sqrt(std::pow(m*x.uncertainty()/x.value(),2)); + + return type(newval,err); + } +}; + +/// specialize root typeof helper +template +struct root_typeof_helper,static_rational > +{ + typedef measurement< + typename root_typeof_helper >::type + > type; + + static constexpr type value(const measurement& x) + { + const static_rational rat; + + const Y m = Y(rat.denominator())/Y(rat.numerator()), + newval = std::pow(x.value(),m), + err = newval*std::sqrt(std::pow(m*x.uncertainty()/x.value(),2)); + + return type(newval,err); + } +}; + +// stream output +template +inline +std::ostream& operator<<(std::ostream& os,const measurement& val) +{ + boost::io::ios_precision_saver precision_saver(os); + boost::io::ios_flags_saver flags_saver(os); + + os << val.value() << "(+/-" << val.uncertainty() << ")"; + + return os; +} + +} // namespace units + +} // namespace boost + +#endif // BOOST_UNITS_MEASUREMENT_HPP diff --git a/src/boost/libs/units/example/non_base_dimension.cpp b/src/boost/libs/units/example/non_base_dimension.cpp new file mode 100644 index 00000000..4476a34e --- /dev/null +++ b/src/boost/libs/units/example/non_base_dimension.cpp @@ -0,0 +1,78 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2007-2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief non_base_dimension.cpp + +\details +Another example of user-defined units with conversions. + +Output: +@verbatim + +//[non_base_dimension_output +//] + +@endverbatim +**/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { + +namespace units { + +//[non_base_dimension_snippet_1 + +struct imperial_gallon_tag : + base_unit { }; + +typedef make_system::type imperial; + +typedef unit imperial_gallon; + +struct us_gallon_tag : base_unit { }; + +typedef make_system::type us; + +typedef unit us_gallon; + +//] + +} // namespace units + +} // namespace boost + +BOOST_UNITS_DEFINE_CONVERSION_FACTOR(boost::units::imperial_gallon_tag, + boost::units::us_gallon_tag, + double, 1.2009499255); + +using namespace boost::units; + +int main(void) +{ + quantity ig1(1.0*imperial_gallon()); + quantity ug1(1.0*us_gallon()); + + quantity ig2(ug1); + quantity ug2(ig1); + + return 0; +} diff --git a/src/boost/libs/units/example/performance.cpp b/src/boost/libs/units/example/performance.cpp new file mode 100644 index 00000000..fd2abe10 --- /dev/null +++ b/src/boost/libs/units/example/performance.cpp @@ -0,0 +1,395 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief performance.cpp + +\details +Test runtime performance. + +Output: +@verbatim + +multiplying ublas::matrix(1000, 1000) : 25.03 seconds +multiplying ublas::matrix(1000, 1000) : 24.49 seconds +tiled_matrix_multiply(1000, 1000) : 1.12 seconds +tiled_matrix_multiply(1000, 1000) : 1.16 seconds +solving y' = 1 - x + 4 * y with double: 1.97 seconds +solving y' = 1 - x + 4 * y with quantity: 1.84 seconds + +@endverbatim +**/ + +#define _SCL_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef BOOST_MSVC +#pragma warning(push) +#pragma warning(disable:4267; disable:4127; disable:4244; disable:4100) +#endif + +#include + +#ifdef BOOST_MSVC +#pragma warning(pop) +#endif + +#include +#include +#include +#include + +enum { + tile_block_size = 24 +}; + +template +void tiled_multiply_carray_inner(T0* first, + T1* second, + Out* out, + int totalwidth, + int width2, + int height1, + int common) { + for(int j = 0; j < height1; ++j) { + for(int i = 0; i < width2; ++i) { + Out value = out[j * totalwidth + i]; + for(int k = 0; k < common; ++k) { + value += first[k + totalwidth * j] * second[k * totalwidth + i]; + } + out[j * totalwidth + i] = value; + } + } +} + +template +void tiled_multiply_carray_outer(T0* first, + T1* second, + Out* out, + int width2, + int height1, + int common) { + std::fill_n(out, width2 * height1, Out()); + int j = 0; + for(; j < height1 - tile_block_size; j += tile_block_size) { + int i = 0; + for(; i < width2 - tile_block_size; i += tile_block_size) { + int k = 0; + for(; k < common - tile_block_size; k += tile_block_size) { + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + tile_block_size, + tile_block_size, + tile_block_size); + } + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + tile_block_size, + tile_block_size, + common - k); + } + int k = 0; + for(; k < common - tile_block_size; k += tile_block_size) { + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, width2 - i, + tile_block_size, + tile_block_size); + } + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, width2 - i, + tile_block_size, + common - k); + } + int i = 0; + for(; i < width2 - tile_block_size; i += tile_block_size) { + int k = 0; + for(; k < common - tile_block_size; k += tile_block_size) { + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + tile_block_size, + height1 - j, + tile_block_size); + } + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + tile_block_size, + height1 - j, + common - k); + } + int k = 0; + for(; k < common - tile_block_size; k += tile_block_size) { + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + width2 - i, + height1 - j, + tile_block_size); + } + tiled_multiply_carray_inner( + &first[k + width2 * j], + &second[k * width2 + i], + &out[j * width2 + i], + width2, + width2 - i, + height1 - j, + common - k); +} + +enum { max_value = 1000}; + +template +BOOST_CXX14_CONSTEXPR +R solve_differential_equation(F f, T lower, T upper, N steps, R start) { + typedef typename F::template result::type f_result; + T h = (upper - lower) / (1.0*steps); + for(N i = N(); i < steps; ++i) { + R y = start; + T x = lower + h * (1.0*i); + f_result k1 = f(x, y); + f_result k2 = f(x + h / 2.0, y + h * k1 / 2.0); + f_result k3 = f(x + h / 2.0, y + h * k2 / 2.0); + f_result k4 = f(x + h, y + h * k3); + start = y + h * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; + } + return(start); +} + +using namespace boost::units; + +//y' = 1 - x + 4 * y +struct f { + template struct result; + + BOOST_CONSTEXPR double operator()(const double& x, const double& y) const { + return(1.0 - x + 4.0 * y); + } + + boost::units::quantity + BOOST_CONSTEXPR operator()(const quantity& x, + const quantity& y) const { + using namespace boost::units; + using namespace si; + return(1.0 * meters / second - + x * meters / pow<2>(seconds) + + 4.0 * y / seconds ); + } +}; + +template<> +struct f::result { + typedef double type; +}; + +template<> +struct f::result, quantity > { + typedef quantity type; +}; + + + +//y' = 1 - x + 4 * y +//y' - 4 * y = 1 - x +//e^(-4 * x) * (dy - 4 * y * dx) = e^(-4 * x) * (1 - x) * dx +//d/dx(y * e ^ (-4 * x)) = e ^ (-4 * x) (1 - x) * dx + +//d/dx(y * e ^ (-4 * x)) = e ^ (-4 * x) * dx - x * e ^ (-4 * x) * dx +//d/dx(y * e ^ (-4 * x)) = d/dx((-3/16 + 1/4 * x) * e ^ (-4 * x)) +//y * e ^ (-4 * x) = (-3/16 + 1/4 * x) * e ^ (-4 * x) + C +//y = (-3/16 + 1/4 * x) + C/e ^ (-4 * x) +//y = 1/4 * x - 3/16 + C * e ^ (4 * x) + +//y(0) = 1 +//1 = - 3/16 + C +//C = 19/16 +//y(x) = 1/4 * x - 3/16 + 19/16 * e ^ (4 * x) + + + +int main() { + boost::numeric::ublas::matrix ublas_result; + { + boost::numeric::ublas::matrix m1(max_value, max_value); + boost::numeric::ublas::matrix m2(max_value, max_value); + std::srand(1492); + for(int i = 0; i < max_value; ++i) { + for(int j = 0; j < max_value; ++j) { + m1(i,j) = std::rand(); + m2(i,j) = std::rand(); + } + } + std::cout << "multiplying ublas::matrix(" + << max_value << ", " << max_value << ") : "; + boost::timer timer; + ublas_result = (prod(m1, m2)); + std::cout << timer.elapsed() << " seconds" << std::endl; + } + typedef boost::numeric::ublas::matrix< + boost::units::quantity + > matrix_type; + matrix_type ublas_resultq; + { + matrix_type m1(max_value, max_value); + matrix_type m2(max_value, max_value); + std::srand(1492); + for(int i = 0; i < max_value; ++i) { + for(int j = 0; j < max_value; ++j) { + m1(i,j) = std::rand(); + m2(i,j) = std::rand(); + } + } + std::cout << "multiplying ublas::matrix(" + << max_value << ", " << max_value << ") : "; + boost::timer timer; + ublas_resultq = (prod(m1, m2)); + std::cout << timer.elapsed() << " seconds" << std::endl; + } + std::vector cresult(max_value * max_value); + { + std::vector m1(max_value * max_value); + std::vector m2(max_value * max_value); + std::srand(1492); + for(int i = 0; i < max_value * max_value; ++i) { + m1[i] = std::rand(); + m2[i] = std::rand(); + } + std::cout << "tiled_matrix_multiply(" + << max_value << ", " << max_value << ") : "; + boost::timer timer; + tiled_multiply_carray_outer( + &m1[0], + &m2[0], + &cresult[0], + max_value, + max_value, + max_value); + std::cout << timer.elapsed() << " seconds" << std::endl; + } + std::vector< + boost::units::quantity + > cresultq(max_value * max_value); + { + std::vector< + boost::units::quantity + > m1(max_value * max_value); + std::vector< + boost::units::quantity + > m2(max_value * max_value); + std::srand(1492); + for(int i = 0; i < max_value * max_value; ++i) { + m1[i] = std::rand() * boost::units::si::newtons; + m2[i] = std::rand() * boost::units::si::meters; + } + std::cout << "tiled_matrix_multiply(" + << max_value << ", " << max_value << ") : "; + boost::timer timer; + tiled_multiply_carray_outer( + &m1[0], + &m2[0], + &cresultq[0], + max_value, + max_value, + max_value); + std::cout << timer.elapsed() << " seconds" << std::endl; + } + for(int i = 0; i < max_value; ++i) { + for(int j = 0; j < max_value; ++j) { + const double diff = + std::abs(ublas_result(i,j) - cresult[i * max_value + j]); + if(diff > ublas_result(i,j) /1e14) { + std::cout << std::setprecision(15) << "Uh Oh. ublas_result(" + << i << "," << j << ") = " << ublas_result(i,j) + << std::endl + << "cresult[" << i << " * " << max_value << " + " + << j << "] = " << cresult[i * max_value + j] + << std::endl; + return(EXIT_FAILURE); + } + } + } + { + std::vector values(1000); + std::cout << "solving y' = 1 - x + 4 * y with double: "; + boost::timer timer; + for(int i = 0; i < 1000; ++i) { + const double x = .1 * i; + values[i] = solve_differential_equation(f(), 0.0, x, i * 100, 1.0); + } + std::cout << timer.elapsed() << " seconds" << std::endl; + for(int i = 0; i < 1000; ++i) { + const double x = .1 * i; + const double value = 1.0/4.0 * x - 3.0/16.0 + 19.0/16.0 * std::exp(4 * x); + if(std::abs(values[i] - value) > value / 1e9) { + std::cout << std::setprecision(15) << "i = : " << i + << ", value = " << value << " approx = " << values[i] + << std::endl; + return(EXIT_FAILURE); + } + } + } + { + using namespace boost::units; + using namespace si; + std::vector > values(1000); + std::cout << "solving y' = 1 - x + 4 * y with quantity: "; + boost::timer timer; + for(int i = 0; i < 1000; ++i) { + const quantity x = .1 * i * seconds; + values[i] = solve_differential_equation( + f(), + 0.0 * seconds, + x, + i * 100, + 1.0 * meters); + } + std::cout << timer.elapsed() << " seconds" << std::endl; + for(int i = 0; i < 1000; ++i) { + const double x = .1 * i; + const quantity value = + (1.0/4.0 * x - 3.0/16.0 + 19.0/16.0 * std::exp(4 * x)) * meters; + if(abs(values[i] - value) > value / 1e9) { + std::cout << std::setprecision(15) << "i = : " << i + << ", value = " << value << " approx = " + << values[i] << std::endl; + return(EXIT_FAILURE); + } + } + } +} diff --git a/src/boost/libs/units/example/quantity.cpp b/src/boost/libs/units/example/quantity.cpp new file mode 100644 index 00000000..e3d3f18a --- /dev/null +++ b/src/boost/libs/units/example/quantity.cpp @@ -0,0 +1,128 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief quantity.cpp + +\details +Test quantity algebra. + +Output: +@verbatim + +//[quantity_output_double +L = 2 m +L+L = 4 m +L-L = 0 m +L*L = 4 m^2 +L/L = 1 dimensionless +L*meter = 2 m^2 +kilograms*(L/seconds)*(L/seconds) = 4 m^2 kg s^-2 +kilograms*(L/seconds)^2 = 4 m^2 kg s^-2 +L^3 = 8 m^3 +L^(3/2) = 2.82843 m^(3/2) +2vL = 1.41421 m^(1/2) +(3/2)vL = 1.5874 m^(2/3) +//] + +//[quantity_output_complex +L = (3,4) m +L+L = (6,8) m +L-L = (0,0) m +L*L = (-7,24) m^2 +L/L = (1,0) dimensionless +L*meter = (3,4) m^2 +kilograms*(L/seconds)*(L/seconds) = (-7,24) m^2 kg s^-2 +kilograms*(L/seconds)^2 = (-7,24) m^2 kg s^-2 +L^3 = (-117,44) m^3 +L^(3/2) = (2,11) m^(3/2) +2vL = (2,1) m^(1/2) +(3/2)vL = (2.38285,1.69466) m^(2/3) +//] + +@endverbatim +**/ + +#include +#include + +#include + +#include + +#include +#include +#include + +#include "test_system.hpp" + +int main(void) +{ + using namespace boost::units; + using namespace boost::units::test; + + { + //[quantity_snippet_1 + quantity L = 2.0*meters; // quantity of length + quantity E = kilograms*pow<2>(L/seconds); // quantity of energy + //] + + std::cout << "L = " << L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L*L = " << L*L << std::endl + << "L/L = " << L/L << std::endl + << "L*meter = " << L*meter << std::endl + << "kilograms*(L/seconds)*(L/seconds) = " + << kilograms*(L/seconds)*(L/seconds) << std::endl + << "kilograms*(L/seconds)^2 = " + << kilograms*pow<2>(L/seconds) << std::endl + << "L^3 = " + << pow<3>(L) << std::endl + << "L^(3/2) = " + << pow >(L) << std::endl + << "2vL = " + << root<2>(L) << std::endl + << "(3/2)vL = " + << root >(L) << std::endl + << std::endl; + } + + { + //[quantity_snippet_2 + quantity > L(std::complex(3.0,4.0)*meters); + quantity > E(kilograms*pow<2>(L/seconds)); + //] + + std::cout << "L = " << L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L*L = " << L*L << std::endl + << "L/L = " << L/L << std::endl + << "L*meter = " << L*meter << std::endl + << "kilograms*(L/seconds)*(L/seconds) = " + << kilograms*(L/seconds)*(L/seconds) << std::endl + << "kilograms*(L/seconds)^2 = " + << kilograms*pow<2>(L/seconds) << std::endl + << "L^3 = " + << pow<3>(L) << std::endl + << "L^(3/2) = " + << pow >(L) << std::endl + << "2vL = " + << root<2>(L) << std::endl + << "(3/2)vL = " + << root >(L) << std::endl + << std::endl; + } + + return 0; +} diff --git a/src/boost/libs/units/example/quaternion.cpp b/src/boost/libs/units/example/quaternion.cpp new file mode 100644 index 00000000..a2e3e91b --- /dev/null +++ b/src/boost/libs/units/example/quaternion.cpp @@ -0,0 +1,232 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2007-2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief quaternion.cpp + +\details +Demonstrate interoperability with Boost.Quaternion. + +Output: +@verbatim + +//[quaternion_output_1 ++L = (4,3,2,1) m +-L = (-4,-3,-2,-1) m +L+L = (8,6,4,2) m +L-L = (0,0,0,0) m +L*L = (2,24,16,8) m^2 +L/L = (1,0,0,0) dimensionless +L^3 = (-104,102,68,34) m^3 +//] + +//[quaternion_output_2 ++L = (4 m,3 m,2 m,1 m) +-L = (-4 m,-3 m,-2 m,-1 m) +L+L = (8 m,6 m,4 m,2 m) +L-L = (0 m,0 m,0 m,0 m) +L^3 = (-104 m^3,102 m^3,68 m^3,34 m^3) +//] + +@endverbatim +**/ + +#include + +#include +#include + +#include +#include +#include + +#include "test_system.hpp" + +#if BOOST_UNITS_HAS_BOOST_TYPEOF + +#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() + +BOOST_TYPEOF_REGISTER_TEMPLATE(boost::math::quaternion, 1) + +#endif + +namespace boost { + +namespace units { + +//[quaternion_class_snippet_1a +/// specialize power typeof helper +template +struct power_typeof_helper,static_rational > +{ + // boost::math::quaternion only supports integer powers + BOOST_STATIC_ASSERT(D==1); + + typedef boost::math::quaternion< + typename power_typeof_helper >::type + > type; + + static type value(const boost::math::quaternion& x) + { + return boost::math::pow(x,static_cast(N)); + } +}; +//] + +//[quaternion_class_snippet_1b +/// specialize root typeof helper +template +struct root_typeof_helper,static_rational > +{ + // boost::math::quaternion only supports integer powers + BOOST_STATIC_ASSERT(N==1); + + typedef boost::math::quaternion< + typename root_typeof_helper >::type + > type; + + static type value(const boost::math::quaternion& x) + { + return boost::math::pow(x,static_cast(D)); + } +}; +//] + +//[quaternion_class_snippet_2a +/// specialize power typeof helper for quaternion > +template +struct power_typeof_helper< + boost::math::quaternion >, + static_rational > +{ + typedef typename power_typeof_helper< + Y, + static_rational + >::type value_type; + + typedef typename power_typeof_helper< + Unit, + static_rational + >::type unit_type; + + typedef quantity quantity_type; + typedef boost::math::quaternion type; + + static type value(const boost::math::quaternion >& x) + { + const boost::math::quaternion tmp = + pow >(boost::math::quaternion( + x.R_component_1().value(), + x.R_component_2().value(), + x.R_component_3().value(), + x.R_component_4().value())); + + return type(quantity_type::from_value(tmp.R_component_1()), + quantity_type::from_value(tmp.R_component_2()), + quantity_type::from_value(tmp.R_component_3()), + quantity_type::from_value(tmp.R_component_4())); + } +}; +//] + +//[quaternion_class_snippet_2b +/// specialize root typeof helper for quaternion > +template +struct root_typeof_helper< + boost::math::quaternion >, + static_rational > +{ + typedef typename root_typeof_helper< + Y, + static_rational + >::type value_type; + + typedef typename root_typeof_helper< + Unit, + static_rational + >::type unit_type; + + typedef quantity quantity_type; + typedef boost::math::quaternion type; + + static type value(const boost::math::quaternion >& x) + { + const boost::math::quaternion tmp = + root >(boost::math::quaternion( + x.R_component_1().value(), + x.R_component_2().value(), + x.R_component_3().value(), + x.R_component_4().value())); + + return type(quantity_type::from_value(tmp.R_component_1()), + quantity_type::from_value(tmp.R_component_2()), + quantity_type::from_value(tmp.R_component_3()), + quantity_type::from_value(tmp.R_component_4())); + } +}; +//] + +} // namespace units + +} // namespace boost + +int main(void) +{ + using boost::math::quaternion; + using namespace boost::units; + using namespace boost::units::test; + using boost::units::pow; + + { + //[quaternion_snippet_1 + typedef quantity > length_dimension; + + length_dimension L(quaternion(4.0,3.0,2.0,1.0)*meters); + //] + + std::cout << "+L = " << +L << std::endl + << "-L = " << -L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L*L = " << L*L << std::endl + << "L/L = " << L/L << std::endl + // unfortunately, without qualification msvc still + // finds boost::math::pow by ADL. + << "L^3 = " << boost::units::pow<3>(L) << std::endl +// << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl +// << "3vL = " << root<3>(L) << std::endl +// << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl + << std::endl; + } + + { + //[quaternion_snippet_2 + typedef quaternion > length_dimension; + + length_dimension L(4.0*meters,3.0*meters,2.0*meters,1.0*meters); + //] + + std::cout << "+L = " << +L << std::endl + << "-L = " << -L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl +// << "L*L = " << L*L << std::endl +// << "L/L = " << L/L << std::endl + << "L^3 = " << boost::units::pow<3>(L) << std::endl +// << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl +// << "3vL = " << root<3>(L) << std::endl +// << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl + << std::endl; + } + + return 0; +} diff --git a/src/boost/libs/units/example/radar_beam_height.cpp b/src/boost/libs/units/example/radar_beam_height.cpp new file mode 100644 index 00000000..00f1f902 --- /dev/null +++ b/src/boost/libs/units/example/radar_beam_height.cpp @@ -0,0 +1,167 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief radar_beam_height.cpp + +\details +Demonstrate library usage for user test cases suggested by Michael Fawcett. + +Output: +@verbatim + +//[radar_beam_height_output +radar range : 300 nmi +earth radius : 6.37101e+06 m +beam height 1 : 18169.7 m +beam height 2 : 9.81085 nmi +beam height 3 : 18169.7 m +beam height 4 : 9.81085 nmi +beam height approx : 59488.4 ft +beam height approx : 18132.1 m +//] + +@endverbatim +**/ + +#include + +#include +#include +#include +#include +#include + +using boost::units::length_dimension; +using boost::units::pow; +using boost::units::root; +using boost::units::quantity; +using boost::units::unit; + +//[radar_beam_height_class_snippet_1 +namespace nautical { + +struct length_base_unit : + boost::units::base_unit +{ + static std::string name() { return "nautical mile"; } + static std::string symbol() { return "nmi"; } +}; + +typedef boost::units::make_system::type system; + +/// unit typedefs +typedef unit length; + +static const length mile,miles; + +} // namespace nautical + +// helper for conversions between nautical length and si length +BOOST_UNITS_DEFINE_CONVERSION_FACTOR(nautical::length_base_unit, + boost::units::si::meter_base_unit, + double, 1.852e3); +//] + +//[radar_beam_height_class_snippet_2 +namespace imperial { + +struct length_base_unit : + boost::units::base_unit +{ + static std::string name() { return "foot"; } + static std::string symbol() { return "ft"; } +}; + +typedef boost::units::make_system::type system; + +/// unit typedefs +typedef unit length; + +static const length foot,feet; + +} // imperial + +// helper for conversions between imperial length and si length +BOOST_UNITS_DEFINE_CONVERSION_FACTOR(imperial::length_base_unit, + boost::units::si::meter_base_unit, + double, 1.0/3.28083989501312); +//] + +// radar beam height functions +//[radar_beam_height_function_snippet_1 +template +constexpr +quantity,T> +radar_beam_height(const quantity,T>& radar_range, + const quantity,T>& earth_radius, + T k = 4.0/3.0) +{ + return quantity,T> + (pow<2>(radar_range)/(2.0*k*earth_radius)); +} +//] + +//[radar_beam_height_function_snippet_2 +template +constexpr +return_type +radar_beam_height(const quantity,T>& radar_range, + const quantity,T>& earth_radius, + T k = 4.0/3.0) +{ + // need to decide which system to use for calculation + return pow<2>(static_cast(radar_range)) + / (2.0*k*static_cast(earth_radius)); +} +//] + +//[radar_beam_height_function_snippet_3 +constexpr +quantity +radar_beam_height(const quantity& range) +{ + return quantity + (pow<2>(range/(1.23*nautical::miles/root<2>(imperial::feet)))); +} +//] + +int main(void) +{ + using namespace boost::units; + using namespace boost::units::si; + using namespace nautical; + + //[radar_beam_height_snippet_1 + const quantity radar_range(300.0*miles); + const quantity earth_radius(6371.0087714*kilo*meters); + + const quantity beam_height_1(radar_beam_height(quantity(radar_range),earth_radius)); + const quantity beam_height_2(radar_beam_height(radar_range,quantity(earth_radius))); + const quantity beam_height_3(radar_beam_height< quantity >(radar_range,earth_radius)); + const quantity beam_height_4(radar_beam_height< quantity >(radar_range,earth_radius)); + //] + + std::cout << "radar range : " << radar_range << std::endl + << "earth radius : " << earth_radius << std::endl + << "beam height 1 : " << beam_height_1 << std::endl + << "beam height 2 : " << beam_height_2 << std::endl + << "beam height 3 : " << beam_height_3 << std::endl + << "beam height 4 : " << beam_height_4 << std::endl + << "beam height approx : " << radar_beam_height(radar_range) + << std::endl + << "beam height approx : " + << quantity(radar_beam_height(radar_range)) + << std::endl << std::endl; + + return 0; +} diff --git a/src/boost/libs/units/example/runtime_conversion_factor.cpp b/src/boost/libs/units/example/runtime_conversion_factor.cpp new file mode 100644 index 00000000..f814c884 --- /dev/null +++ b/src/boost/libs/units/example/runtime_conversion_factor.cpp @@ -0,0 +1,72 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +//[runtime_conversion_factor_snippet_1 + +using boost::units::base_dimension; +using boost::units::base_unit; + +static const long currency_base = 1; + +struct currency_base_dimension : base_dimension {}; + +typedef currency_base_dimension::dimension_type currency_type; + +template +struct currency_base_unit : + base_unit, currency_type, currency_base + N> {}; + +typedef currency_base_unit<0> us_dollar_base_unit; +typedef currency_base_unit<1> euro_base_unit; + +typedef us_dollar_base_unit::unit_type us_dollar; +typedef euro_base_unit::unit_type euro; + +// an array of all possible conversions +double conversion_factors[2][2] = { + {1.0, 1.0}, + {1.0, 1.0} +}; + +double get_conversion_factor(long from, long to) { + return(conversion_factors[from][to]); +} + +void set_conversion_factor(long from, long to, double value) { + conversion_factors[from][to] = value; + conversion_factors[to][from] = 1.0 / value; +} + +BOOST_UNITS_DEFINE_CONVERSION_FACTOR_TEMPLATE((long N1)(long N2), + currency_base_unit, + currency_base_unit, + double, get_conversion_factor(N1, N2)); + +//] + +int main() { + boost::units::quantity dollars = 2.00 * us_dollar(); + boost::units::quantity euros(dollars); + set_conversion_factor(0, 1, 2.0); + dollars = static_cast >(euros); + set_conversion_factor(0, 1, .5); + euros = static_cast >(dollars); + double value = euros.value(); // = .5 + if(value != .5) { + return(1); + } else { + return(0); + } +} diff --git a/src/boost/libs/units/example/runtime_unit.cpp b/src/boost/libs/units/example/runtime_unit.cpp new file mode 100644 index 00000000..5777facd --- /dev/null +++ b/src/boost/libs/units/example/runtime_unit.cpp @@ -0,0 +1,116 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include + +//[runtime_unit_snippet_1 + +namespace { + +using namespace boost::units; +using imperial::foot_base_unit; + +std::map > known_units; + +} + +quantity calculate(const quantity& t) +{ + return(boost::units::hypot(t, 2.0 * si::meters)); +} + +int main() +{ + known_units["meter"] = 1.0 * si::meters; + known_units["centimeter"] = .01 * si::meters; + known_units["foot"] = + conversion_factor(foot_base_unit::unit_type(), si::meter) * si::meter; + + std::string output_type("meter"); + std::string input; + + while((std::cout << "> ") && (std::cin >> input)) + { + if(!input.empty() && input[0] == '#') + { + std::getline(std::cin, input); + } + else if(input == "exit") + { + break; + } + else if(input == "help") + { + std::cout << "type \"exit\" to exit\n" + "type \"return 'unit'\" to set the return units\n" + "type \"'number' 'unit'\" to do a simple calculation" + << std::endl; + } + else if(input == "return") + { + if(std::cin >> input) + { + if(known_units.find(input) != known_units.end()) + { + output_type = input; + std::cout << "Done." << std::endl; + } + else + { + std::cout << "Unknown unit \"" << input << "\"" + << std::endl; + } + } + else + { + break; + } + } + else + { + try + { + double value = boost::lexical_cast(input); + + if(std::cin >> input) + { + if(known_units.find(input) != known_units.end()) + { + std::cout << static_cast( + calculate(value * known_units[input]) / + known_units[output_type]) + << ' ' << output_type << std::endl; + } + else + { + std::cout << "Unknown unit \"" << input << "\"" + << std::endl; + } + } + else + { + break; + } + } + catch(...) + { + std::cout << "Input error" << std::endl; + } + } + } +} + +//] diff --git a/src/boost/libs/units/example/runtime_unit_input.txt b/src/boost/libs/units/example/runtime_unit_input.txt new file mode 100644 index 00000000..6c109e6c --- /dev/null +++ b/src/boost/libs/units/example/runtime_unit_input.txt @@ -0,0 +1,13 @@ +# runtime_unit_input.txt +# +# Copyright (c) 2007-2008 Steven Watanabe +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +return foot +2.0 centimeter +return centimeter +3.0 meter +exit diff --git a/src/boost/libs/units/example/systems.cpp b/src/boost/libs/units/example/systems.cpp new file mode 100644 index 00000000..cbbce743 --- /dev/null +++ b/src/boost/libs/units/example/systems.cpp @@ -0,0 +1,1072 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief systems.cpp + +\details +Test various non-si units + +Output: +@verbatim + +@endverbatim +**/ + +#define BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(namespace_,unit_name_,dimension_) \ +namespace boost { \ +namespace units { \ +namespace namespace_ { \ +typedef make_system::type unit_name_ ## system_; \ +typedef unit unit_name_ ## _ ## dimension_; \ +static constexpr unit_name_ ## _ ## dimension_ unit_name_ ## s; \ +} \ +} \ +} \ + +#include +#include +#include + +#include +#include +#include + +#include +#include + +// angle base units +#include +#include +#include +#include +#include +#include +#include + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,arcminute,plane_angle) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,arcsecond,plane_angle) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,degree,plane_angle) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,gradian,plane_angle) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,radian,plane_angle) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,revolution,plane_angle) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(angle,steradian,solid_angle) + +// astronomical base units +#include +#include +#include +#include +#include +#include +#include + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,astronomical_unit,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,light_second,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,light_minute,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,light_hour,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,light_day,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,light_year,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(astronomical,parsec,length) + +// imperial base units +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,thou,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,inch,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,foot,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,yard,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,furlong,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,mile,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,league,length) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,grain,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,drachm,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,ounce,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,pound,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,stone,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,quarter,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,hundredweight,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,ton,mass) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,fluid_ounce,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,gill,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,pint,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,quart,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(imperial,gallon,volume) + +// metric base units +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,angstrom,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,fermi,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,micron,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,nautical_mile,length) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,ton,mass) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,day,time) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,hour,time) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,minute,time) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,year,time) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,knot,velocity) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,are,area) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,barn,area) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,hectare,area) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,liter,volume) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,atmosphere,pressure) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,bar,pressure) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,mmHg,pressure) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(metric,torr,pressure) + +// us base units + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,mil,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,inch,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,foot,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,yard,length) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,mile,length) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,grain,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,dram,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,ounce,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,pound,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,hundredweight,mass) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,ton,mass) + +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,minim,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,fluid_dram,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,teaspoon,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,tablespoon,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,fluid_ounce,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,gill,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,cup,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,pint,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,quart,volume) +BOOST_UNITS_DEFINE_SINGLE_UNIT_SYSTEM(us,gallon,volume) + +int main(void) +{ + using namespace boost::units; + + { + using namespace boost::units::angle; + + std::cout << "Testing angle base units..." << std::endl; + + quantity as(1.0*arcseconds); + quantity am(1.0*arcminutes); + quantity d(1.0*degrees); + quantity g(1.0*gradians); + quantity r(1.0*radians); + quantity rev(1.0*revolutions); + + std::cout << as << " = " << quantity(as) << std::endl + << am << " = " << quantity(am) << std::endl + << d << " = " << quantity(d) << std::endl + << g << " = " << quantity(g) << std::endl + << r << " = " << quantity(r) << std::endl + << rev << " = " << quantity(rev) << std::endl + << std::endl; + + std::cout << rev << "/" << as << " = " << quantity(rev/as) << std::endl + << rev << "/" << am << " = " << quantity(rev/am) << std::endl + << rev << "/" << d << " = " << quantity(rev/d) << std::endl + << rev << "/" << g << " = " << quantity(rev/g) << std::endl + << rev << "/" << r << " = " << quantity(rev/r) << std::endl + << std::endl; + + // conversions only work with exponent of +/- 1 in scaled_base_unit? + std::cout << as << " = " << quantity(as) << std::endl + << am << " = " << quantity(am) << std::endl + << d << " = " << quantity(d) << std::endl + << rev << " = " << quantity(rev) << std::endl + << std::endl; + + // conversions only work with exponent of +/- 1 in scaled_base_unit? see arcsecond.hpp + std::cout << as << " = " << quantity(as) << std::endl + << am << " = " << quantity(am) << std::endl + << d << " = " << quantity(d) << std::endl + << rev << " = " << quantity(rev) << std::endl + << std::endl; + + std::cout << as << " = " << quantity(as) << std::endl + << am << " = " << quantity(am) << std::endl + << d << " = " << quantity(d) << std::endl + << rev << " = " << quantity(rev) << std::endl + << std::endl; + + std::cout << as << " = " << quantity(as) << std::endl + << am << " = " << quantity(am) << std::endl + << d << " = " << quantity(d) << std::endl + << rev << " = " << quantity(rev) << std::endl + << std::endl; + + quantity sa1(1.0*steradians); + + std::cout << sa1 << std::endl + << std::endl; + } + + { + using namespace boost::units::astronomical; + + std::cout << "Testing astronomical base units..." << std::endl; + + quantity ls(1.0*light_seconds); + quantity lm(1.0*light_minutes); + quantity au(1.0*astronomical_units); + quantity lh(1.0*light_hours); + quantity ld(1.0*light_days); + quantity ly(1.0*light_years); + quantity ps(1.0*parsecs); + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << au << " = " << quantity(au) << std::endl + << lh << " = " << quantity(lh) << std::endl + << ld << " = " << quantity(ld) << std::endl + << ly << " = " << quantity(ly) << std::endl + << ps << " = " << quantity(ps) << std::endl + << std::endl; + + std::cout << ly << "/" << ls << " = " << quantity(ly/ls) << std::endl + << ly << "/" << lm << " = " << quantity(ly/lm) << std::endl + << ly << "/" << au << " = " << quantity(ly/au) << std::endl + << ly << "/" << lh << " = " << quantity(ly/ld) << std::endl + << ly << "/" << ld << " = " << quantity(ly/lh) << std::endl + << ly << "/" << ps << " = " << quantity(ly/ps) << std::endl + << std::endl; + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << lh << " = " << quantity(lh) << std::endl + << ld << " = " << quantity(ld) << std::endl + << ly << " = " << quantity(ly) << std::endl + << std::endl; + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << lh << " = " << quantity(lh) << std::endl + << ld << " = " << quantity(ld) << std::endl + << ly << " = " << quantity(ly) << std::endl + << std::endl; + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << lh << " = " << quantity(lh) << std::endl + << ld << " = " << quantity(ld) << std::endl + << ly << " = " << quantity(ly) << std::endl + << std::endl; + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << lh << " = " << quantity(lh) << std::endl + << ld << " = " << quantity(ld) << std::endl + << ly << " = " << quantity(ly) << std::endl + << std::endl; + + std::cout << ls << " = " << quantity(ls) << std::endl + << lm << " = " << quantity(lm) << std::endl + << lh << " = " << quantity(ld) << std::endl + << ld << " = " << quantity(lh) << std::endl + << ly << " = " << quantity(ly) << std::endl + << std::endl; + } + + { + using namespace boost::units::imperial; + + std::cout << "Testing imperial base units..." << std::endl; + + quantity iml1(1.0*thous); + quantity iml2(1.0*inchs); + quantity iml3(1.0*foots); + quantity iml4(1.0*yards); + quantity iml5(1.0*furlongs); + quantity iml6(1.0*miles); + quantity iml7(1.0*leagues); + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml7 << "/" << iml1 << " = " << quantity(iml7/iml1) << std::endl + << iml7 << "/" << iml2 << " = " << quantity(iml7/iml2) << std::endl + << iml7 << "/" << iml3 << " = " << quantity(iml7/iml3) << std::endl + << iml7 << "/" << iml4 << " = " << quantity(iml7/iml4) << std::endl + << iml7 << "/" << iml5 << " = " << quantity(iml7/iml5) << std::endl + << iml7 << "/" << iml6 << " = " << quantity(iml7/iml6) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << iml6 << " = " << quantity(iml6) << std::endl + << iml7 << " = " << quantity(iml7) << std::endl + << std::endl; + + quantity imm1(1.0*grains); + quantity imm2(1.0*drachms); + quantity imm3(1.0*ounces); + quantity imm4(1.0*pounds); + quantity imm5(1.0*stones); + quantity imm6(1.0*quarters); + quantity imm7(1.0*hundredweights); + quantity imm8(1.0*tons); + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm8 << "/" << imm1 << " = " << quantity(imm8/imm1) << std::endl + << imm8 << "/" << imm2 << " = " << quantity(imm8/imm2) << std::endl + << imm8 << "/" << imm3 << " = " << quantity(imm8/imm3) << std::endl + << imm8 << "/" << imm4 << " = " << quantity(imm8/imm4) << std::endl + << imm8 << "/" << imm5 << " = " << quantity(imm8/imm5) << std::endl + << imm8 << "/" << imm6 << " = " << quantity(imm8/imm6) << std::endl + << imm8 << "/" << imm7 << " = " << quantity(imm8/imm7) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << imm7 << " = " << quantity(imm7) << std::endl + << imm8 << " = " << quantity(imm8) << std::endl + << std::endl; + + quantity imv1(1.0*fluid_ounces); + quantity imv2(1.0*gills); + quantity imv3(1.0*pints); + quantity imv4(1.0*quarts); + quantity imv5(1.0*gallons); + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + + std::cout << imv5 << "/" << imv1 << " = " << quantity(imv5/imv1) << std::endl + << imv5 << "/" << imv2 << " = " << quantity(imv5/imv2) << std::endl + << imv5 << "/" << imv3 << " = " << quantity(imv5/imv3) << std::endl + << imv5 << "/" << imv4 << " = " << quantity(imv5/imv4) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << std::endl; + } + + { + using namespace boost::units::metric; + + std::cout << "Testing metric base units..." << std::endl; + + quantity ml1(1.0*fermis); + quantity ml2(1.0*angstroms); + quantity ml3(1.0*microns); + quantity ml4(1.0*nautical_miles); + + std::cout << ml1 << " = " << quantity(ml1) << std::endl + << ml2 << " = " << quantity(ml2) << std::endl + << ml3 << " = " << quantity(ml3) << std::endl + << ml4 << " = " << quantity(ml4) << std::endl + << std::endl; + + std::cout << ml4 << "/" << ml1 << " = " << quantity(ml4/ml1) << std::endl + << ml4 << "/" << ml2 << " = " << quantity(ml4/ml2) << std::endl + << ml4 << "/" << ml3 << " = " << quantity(ml4/ml3) << std::endl + << std::endl; + + std::cout << ml1 << " = " << quantity(ml1) << std::endl + << ml2 << " = " << quantity(ml2) << std::endl + << ml3 << " = " << quantity(ml3) << std::endl + << ml4 << " = " << quantity(ml4) << std::endl + << std::endl; + + std::cout << ml1 << " = " << quantity(ml1) << std::endl + << ml2 << " = " << quantity(ml2) << std::endl + << ml3 << " = " << quantity(ml3) << std::endl + << ml4 << " = " << quantity(ml4) << std::endl + << std::endl; + + std::cout << ml1 << " = " << quantity(ml1) << std::endl + << ml2 << " = " << quantity(ml2) << std::endl + << ml3 << " = " << quantity(ml3) << std::endl + << ml4 << " = " << quantity(ml4) << std::endl + << std::endl; + + std::cout << ml1 << " = " << quantity(ml1) << std::endl + << ml2 << " = " << quantity(ml2) << std::endl + << ml3 << " = " << quantity(ml3) << std::endl + << ml4 << " = " << quantity(ml4) << std::endl + << std::endl; + + quantity mm1(1.0*tons); + + std::cout << mm1 << " = " << quantity(mm1) << std::endl + //<< quantity(mm1) << std::endl // this should work... + << std::endl; + + quantity mt1(1.0*minutes); + quantity mt2(1.0*hours); + quantity mt3(1.0*days); + quantity mt4(1.0*years); + + std::cout << mt1 << " = " << quantity(mt1) << std::endl + << mt2 << " = " << quantity(mt2) << std::endl + << mt3 << " = " << quantity(mt3) << std::endl + << mt4 << " = " << quantity(mt4) << std::endl + << std::endl; + + std::cout << mt4 << "/" << mt1 << " = " << quantity(mt4/mt1) << std::endl + << mt4 << "/" << mt2 << " = " << quantity(mt4/mt2) << std::endl + << mt4 << "/" << mt3 << " = " << quantity(mt4/mt3) << std::endl + << std::endl; + + std::cout << mt1 << " = " << quantity(mt1) << std::endl + << mt2 << " = " << quantity(mt2) << std::endl + << mt3 << " = " << quantity(mt3) << std::endl + << mt4 << " = " << quantity(mt4) << std::endl + << std::endl; + + std::cout << mt1 << " = " << quantity(mt1) << std::endl + << mt2 << " = " << quantity(mt2) << std::endl + << mt3 << " = " << quantity(mt3) << std::endl + << mt4 << " = " << quantity(mt4) << std::endl + << std::endl; + + std::cout << mt1 << " = " << quantity(mt1) << std::endl + << mt2 << " = " << quantity(mt2) << std::endl + << mt3 << " = " << quantity(mt3) << std::endl + << mt4 << " = " << quantity(mt4) << std::endl + << std::endl; + + std::cout << mt1 << " = " << quantity(mt1) << std::endl + << mt2 << " = " << quantity(mt2) << std::endl + << mt3 << " = " << quantity(mt3) << std::endl + << mt4 << " = " << quantity(mt4) << std::endl + << std::endl; + + quantity ms1(1.0*knots); + + std::cout << ms1 << " = " << quantity(ms1) << std::endl + << std::endl; + + quantity ma1(1.0*barns); + quantity ma2(1.0*ares); + quantity ma3(1.0*hectares); + + std::cout << ma1 << " = " << quantity(ma1) << std::endl + << ma2 << " = " << quantity(ma2) << std::endl + << ma3 << " = " << quantity(ma3) << std::endl + << std::endl; + + std::cout << ma3 << "/" << ma1 << " = " << quantity(ma3/ma1) << std::endl + << ma3 << "/" << ma2 << " = " << quantity(ma3/ma2) << std::endl + << std::endl; + + std::cout << ma1 << " = " << quantity(ma1) << std::endl + << ma2 << " = " << quantity(ma2) << std::endl + << ma3 << " = " << quantity(ma3) << std::endl + << std::endl; + + std::cout << ma1 << " = " << quantity(ma1) << std::endl + << ma2 << " = " << quantity(ma2) << std::endl + << ma3 << " = " << quantity(ma3) << std::endl + << std::endl; + + std::cout << ma1 << " = " << quantity(ma1) << std::endl + << ma2 << " = " << quantity(ma2) << std::endl + << ma3 << " = " << quantity(ma3) << std::endl + << std::endl; + + quantity mv1(1.0*liters); + + std::cout << mv1 << " = " << quantity(mv1) << std::endl + << std::endl; + + quantity mp1(1.0*mmHgs); + quantity mp2(1.0*torrs); + quantity mp3(1.0*bars); + quantity mp4(1.0*atmospheres); + + std::cout << mp1 << " = " << quantity(mp1) << std::endl + << mp2 << " = " << quantity(mp2) << std::endl + << mp3 << " = " << quantity(mp3) << std::endl + << mp4 << " = " << quantity(mp4) << std::endl + << std::endl; + + std::cout << mp4 << "/" << mp1 << " = " << quantity(mp4/mp1) << std::endl + << mp4 << "/" << mp2 << " = " << quantity(mp4/mp2) << std::endl + << mp4 << "/" << mp3 << " = " << quantity(mp4/mp3) << std::endl + << std::endl; + + std::cout << mp1 << " = " << quantity(mp1) << std::endl + << mp2 << " = " << quantity(mp2) << std::endl + << mp3 << " = " << quantity(mp3) << std::endl + << mp4 << " = " << quantity(mp4) << std::endl + << std::endl; + + std::cout << mp1 << " = " << quantity(mp1) << std::endl + << mp2 << " = " << quantity(mp2) << std::endl + << mp3 << " = " << quantity(mp3) << std::endl + << mp4 << " = " << quantity(mp4) << std::endl + << std::endl; + + std::cout << mp1 << " = " << quantity(mp1) << std::endl + << mp2 << " = " << quantity(mp2) << std::endl + << mp3 << " = " << quantity(mp3) << std::endl + << mp4 << " = " << quantity(mp4) << std::endl + << std::endl; + + std::cout << mp1 << " = " << quantity(mp1) << std::endl + << mp2 << " = " << quantity(mp2) << std::endl + << mp3 << " = " << quantity(mp3) << std::endl + << mp4 << " = " << quantity(mp4) << std::endl + << std::endl; + } + + { + using namespace boost::units::us; + + std::cout << "Testing U.S. customary base units..." << std::endl; + + quantity iml1(1.0*mils); + quantity iml2(1.0*inchs); + quantity iml3(1.0*foots); + quantity iml4(1.0*yards); + quantity iml5(1.0*miles); + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + std::cout << iml5 << "/" << iml1 << " = " << quantity(iml5/iml1) << std::endl + << iml5 << "/" << iml2 << " = " << quantity(iml5/iml2) << std::endl + << iml5 << "/" << iml3 << " = " << quantity(iml5/iml3) << std::endl + << iml5 << "/" << iml4 << " = " << quantity(iml5/iml4) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + std::cout << iml1 << " = " << quantity(iml1) << std::endl + << iml2 << " = " << quantity(iml2) << std::endl + << iml3 << " = " << quantity(iml3) << std::endl + << iml4 << " = " << quantity(iml4) << std::endl + << iml5 << " = " << quantity(iml5) << std::endl + << std::endl; + + quantity imm1(1.0*grains); + quantity imm2(1.0*drams); + quantity imm3(1.0*ounces); + quantity imm4(1.0*pounds); + quantity imm5(1.0*hundredweights); + quantity imm6(1.0*tons); + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm6 << "/" << imm1 << " = " << quantity(imm6/imm1) << std::endl + << imm6 << "/" << imm2 << " = " << quantity(imm6/imm2) << std::endl + << imm6 << "/" << imm3 << " = " << quantity(imm6/imm3) << std::endl + << imm6 << "/" << imm4 << " = " << quantity(imm6/imm4) << std::endl + << imm6 << "/" << imm5 << " = " << quantity(imm6/imm5) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + std::cout << imm1 << " = " << quantity(imm1) << std::endl + << imm2 << " = " << quantity(imm2) << std::endl + << imm3 << " = " << quantity(imm3) << std::endl + << imm4 << " = " << quantity(imm4) << std::endl + << imm5 << " = " << quantity(imm5) << std::endl + << imm6 << " = " << quantity(imm6) << std::endl + << std::endl; + + quantity imv1(1.0*minims); + quantity imv2(1.0*fluid_drams); + quantity imv3(1.0*teaspoons); + quantity imv4(1.0*tablespoons); + quantity imv5(1.0*fluid_ounces); + quantity imv6(1.0*gills); + quantity imv7(1.0*cups); + quantity imv8(1.0*pints); + quantity imv9(1.0*quarts); + quantity imv10(1.0*gallons); + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv10 << "/" << imv1 << " = " << quantity(imv10/imv1) << std::endl + << imv10 << "/" << imv2 << " = " << quantity(imv10/imv2) << std::endl + << imv10 << "/" << imv3 << " = " << quantity(imv10/imv3) << std::endl + << imv10 << "/" << imv4 << " = " << quantity(imv10/imv4) << std::endl + << imv10 << "/" << imv5 << " = " << quantity(imv10/imv5) << std::endl + << imv10 << "/" << imv6 << " = " << quantity(imv10/imv6) << std::endl + << imv10 << "/" << imv7 << " = " << quantity(imv10/imv7) << std::endl + << imv10 << "/" << imv8 << " = " << quantity(imv10/imv8) << std::endl + << imv10 << "/" << imv9 << " = " << quantity(imv10/imv9) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + + std::cout << imv1 << " = " << quantity(imv1) << std::endl + << imv2 << " = " << quantity(imv2) << std::endl + << imv3 << " = " << quantity(imv3) << std::endl + << imv4 << " = " << quantity(imv4) << std::endl + << imv5 << " = " << quantity(imv5) << std::endl + << imv6 << " = " << quantity(imv6) << std::endl + << imv7 << " = " << quantity(imv7) << std::endl + << imv8 << " = " << quantity(imv8) << std::endl + << imv9 << " = " << quantity(imv9) << std::endl + << imv10 << " = " << quantity(imv10) << std::endl + << std::endl; + } + + return 0; +} diff --git a/src/boost/libs/units/example/temperature.cpp b/src/boost/libs/units/example/temperature.cpp new file mode 100644 index 00000000..f6b43421 --- /dev/null +++ b/src/boost/libs/units/example/temperature.cpp @@ -0,0 +1,98 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief temperature.cpp + +\details +Conversions between Fahrenheit and Kelvin for absolute temperatures and +temperature differences. + +Output: +@verbatim + +//[ temperature_output_1 +{ 32 } F +{ 273.15 } K +{ 273.15 } K +[ 32 ] F +[ 17.7778 ] K +[ 17.7778 ] K +//] + +@endverbatim +**/ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace boost::units; + +namespace boost { + +namespace units { + +namespace fahrenheit { + +//[temperature_snippet_1 +typedef temperature::fahrenheit_base_unit::unit_type temperature; +typedef get_system::type system; + +BOOST_UNITS_STATIC_CONSTANT(degree,temperature); +BOOST_UNITS_STATIC_CONSTANT(degrees,temperature); +//] + +} // fahrenheit + +} // namespace units + +} // namespace boost + +int main() +{ + //[temperature_snippet_3 + quantity > T1p( + 32.0*absolute()); + quantity T1v( + 32.0*fahrenheit::degrees); + + quantity > T2p(T1p); + quantity T2v(T1v); + //] + + typedef conversion_helper< + quantity >, + quantity > > absolute_conv_type; + typedef conversion_helper< + quantity, + quantity > relative_conv_type; + + std::cout << T1p << std::endl + << absolute_conv_type::convert(T1p) << std::endl + << T2p << std::endl + << T1v << std::endl + << relative_conv_type::convert(T1v) << std::endl + << T2v << std::endl + << std::endl; + + return 0; +} diff --git a/src/boost/libs/units/example/test_system.hpp b/src/boost/libs/units/example/test_system.hpp new file mode 100644 index 00000000..0eaaa973 --- /dev/null +++ b/src/boost/libs/units/example/test_system.hpp @@ -0,0 +1,153 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef MCS_TEST_SYSTEM_HPP +#define MCS_TEST_SYSTEM_HPP + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { + +namespace units { + +//[test_system_snippet_1 + +/// base dimension of length +struct length_base_dimension : base_dimension { }; +/// base dimension of mass +struct mass_base_dimension : base_dimension { }; +/// base dimension of time +struct time_base_dimension : base_dimension { }; + +//] + +#if 0 +//[test_system_snippet_2 + +typedef make_dimension_list< + boost::mpl::list< dim< length_base_dimension,static_rational<1> > > +>::type length_dimension; + +typedef make_dimension_list< + boost::mpl::list< dim< mass_base_dimension,static_rational<1> > > +>::type mass_dimension; + +typedef make_dimension_list< + boost::mpl::list< dim< time_base_dimension,static_rational<1> > > +>::type time_dimension; + +//] +#endif + +//[test_system_snippet_3 +typedef length_base_dimension::dimension_type length_dimension; +typedef mass_base_dimension::dimension_type mass_dimension; +typedef time_base_dimension::dimension_type time_dimension; +//] + +#if 0 +//[test_system_snippet_4 + +typedef make_dimension_list< + boost::mpl::list< dim< length_base_dimension,static_rational<2> > > +>::type area_dimension; + +typedef make_dimension_list< + boost::mpl::list< dim< mass_base_dimension,static_rational<1> >, + dim< length_base_dimension,static_rational<2> >, + dim< time_base_dimension,static_rational<-2> > > +>::type energy_dimension; + +//] +#endif + +//[test_system_snippet_5 +typedef derived_dimension::type area_dimension; +typedef derived_dimension::type energy_dimension; +//] + +namespace test { + +//[test_system_snippet_6 + +struct meter_base_unit : base_unit { }; +struct kilogram_base_unit : base_unit { }; +struct second_base_unit : base_unit { }; + +typedef make_system< + meter_base_unit, + kilogram_base_unit, + second_base_unit>::type mks_system; + +/// unit typedefs +typedef unit dimensionless; + +typedef unit length; +typedef unit mass; +typedef unit time; + +typedef unit area; +typedef unit energy; +//] + +//[test_system_snippet_7 +/// unit constants +BOOST_UNITS_STATIC_CONSTANT(meter,length); +BOOST_UNITS_STATIC_CONSTANT(meters,length); +BOOST_UNITS_STATIC_CONSTANT(kilogram,mass); +BOOST_UNITS_STATIC_CONSTANT(kilograms,mass); +BOOST_UNITS_STATIC_CONSTANT(second,time); +BOOST_UNITS_STATIC_CONSTANT(seconds,time); + +BOOST_UNITS_STATIC_CONSTANT(square_meter,area); +BOOST_UNITS_STATIC_CONSTANT(square_meters,area); +BOOST_UNITS_STATIC_CONSTANT(joule,energy); +BOOST_UNITS_STATIC_CONSTANT(joules,energy); +//] + +} // namespace test + +//[test_system_snippet_8 +template<> struct base_unit_info +{ + static std::string name() { return "meter"; } + static std::string symbol() { return "m"; } +}; +//] + +template<> struct base_unit_info +{ + static std::string name() { return "kilogram"; } + static std::string symbol() { return "kg"; } +}; + +template<> struct base_unit_info +{ + static std::string name() { return "second"; } + static std::string symbol() { return "s"; } +}; + +} // namespace units + +} // namespace boost + +#endif // MCS_TEST_SYSTEM_HPP diff --git a/src/boost/libs/units/example/tutorial.cpp b/src/boost/libs/units/example/tutorial.cpp new file mode 100644 index 00000000..acda5ee9 --- /dev/null +++ b/src/boost/libs/units/example/tutorial.cpp @@ -0,0 +1,95 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file tutorial.cpp + +\brief Basic tutorial using SI units. + +\details +Tutorial +Defines a function that computes the work, in joules, +done by exerting a force in newtons over a specified distance +in meters and outputs the result to std::cout. + +Also code for computing the complex impedance +using std::complex as the value type. + +Output: +@verbatim +//[tutorial_output +F = 2 N +dx = 2 m +E = 4 J + +V = (12.5,0) V +I = (3,4) A +Z = (1.5,-2) Ohm +I*Z = (12.5,0) V +I*Z == V? true +//] +@endverbatim +*/ + +//[tutorial_code +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::units; +using namespace boost::units::si; + +constexpr +quantity +work(const quantity& F, const quantity& dx) +{ + return F * dx; // Defines the relation: work = force * distance. +} + +int main() +{ + /// Test calculation of work. + quantity F(2.0 * newton); // Define a quantity of force. + quantity dx(2.0 * meter); // and a distance, + quantity E(work(F,dx)); // and calculate the work done. + + std::cout << "F = " << F << std::endl + << "dx = " << dx << std::endl + << "E = " << E << std::endl + << std::endl; + + /// Test and check complex quantities. + typedef std::complex complex_type; // double real and imaginary parts. + + // Define some complex electrical quantities. + quantity v = complex_type(12.5, 0.0) * volts; + quantity i = complex_type(3.0, 4.0) * amperes; + quantity z = complex_type(1.5, -2.0) * ohms; + + std::cout << "V = " << v << std::endl + << "I = " << i << std::endl + << "Z = " << z << std::endl + // Calculate from Ohm's law voltage = current * resistance. + << "I * Z = " << i * z << std::endl + // Check defined V is equal to calculated. + << "I * Z == V? " << std::boolalpha << (i * z == v) << std::endl + << std::endl; + return 0; +} +//] diff --git a/src/boost/libs/units/example/unit.cpp b/src/boost/libs/units/example/unit.cpp new file mode 100644 index 00000000..934d5573 --- /dev/null +++ b/src/boost/libs/units/example/unit.cpp @@ -0,0 +1,73 @@ +// Boost.Units - A C++ library for zero-overhead dimensional analysis and +// unit/quantity manipulation and conversion +// +// Copyright (C) 2003-2008 Matthias Christian Schabel +// Copyright (C) 2008 Steven Watanabe +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** +\file + +\brief unit.cpp + +\details +Test unit algebra. + +Output: +@verbatim + +//[unit_output +L = m +L+L = m +L-L = m +L/L = dimensionless +meter*meter = m^2 +M*(L/T)*(L/T) = m^2 kg s^-2 +M*(L/T)^2 = m^2 kg s^-2 +L^3 = m^3 +L^(3/2) = m^(3/2) +2vM = kg^(1/2) +(3/2)vM = kg^(2/3) +//] + +@endverbatim +**/ + +#include + +#include "test_system.hpp" + +#include + +int main() +{ + using namespace boost::units; + using namespace boost::units::test; + + //[unit_snippet_1 + const length L; + const mass M; + // needs to be namespace-qualified because of global time definition + const boost::units::test::time T; + const energy E; + //] + + std::cout << "L = " << L << std::endl + << "L+L = " << L+L << std::endl + << "L-L = " << L-L << std::endl + << "L/L = " << L/L << std::endl + << "meter*meter = " << meter*meter << std::endl + << "M*(L/T)*(L/T) = " << M*(L/T)*(L/T) << std::endl + << "M*(L/T)^2 = " << M*pow<2>(L/T) << std::endl + << "L^3 = " << pow<3>(L) << std::endl + << "L^(3/2) = " << pow >(L) + << std::endl + << "2vM = " << root<2>(M) << std::endl + << "(3/2)vM = " << root >(M) + << std::endl; + + return 0; +} -- cgit v1.2.3