[DEV] add v1.66.0

This commit is contained in:
2018-01-12 21:47:58 +01:00
parent 87059bb1af
commit a97e9ae7d4
49032 changed files with 7668950 additions and 0 deletions

View File

@@ -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 <include>$(BOOST_ROOT) <include>../../.. <warnings>all
;
files = [ path.glob . : *.cpp : performance.* runtime_unit.* ] ;
for local file in $(files)
{
run $(file) ;
}
compile performance.cpp ;
run runtime_unit.cpp : <runtime_unit_input.txt ;

View File

@@ -0,0 +1,148 @@
// 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 Example of using autoprefixes.
\details
Example of using engineering (10^3) and binary (2^10) autoprefixes.
Output:
@verbatim
autoprefixes.cpp
using native typeof
Linking...
Embedding manifest...
Autorun "j:\Cpp\Misc\debug\autoprefixes.exe"
2.345 m
2.345 km
5.49902 MJ
5.49902 megajoule
2.048 kb
2 Kib
2345.6
23456
2345.6
23456
m
meter
0
1
@endverbatim
//[autoprefixes_output
//] [/autoprefixes_output
**/
#include <iostream>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/quantity.hpp>
struct byte_base_unit : boost::units::base_unit<byte_base_unit, boost::units::dimensionless_type, 3>
{
static const char* name() { return("byte"); }
static const char* symbol() { return("b"); }
};
struct thing_base_unit : boost::units::base_unit<thing_base_unit, boost::units::dimensionless_type, 4>
{
static const char* name() { return("thing"); }
static const char* symbol() { return(""); }
};
struct euro_base_unit : boost::units::base_unit<euro_base_unit, boost::units::dimensionless_type, 5>
{
static const char* name() { return("EUR"); }
static const char* symbol() { return("<EFBFBD>"); }
};
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<length> 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<energy> 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<byte_base_unit::unit_type> 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<thing_base_unit::unit_type> 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<euro_base_unit::unit_type> 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()

View File

@@ -0,0 +1,416 @@
// 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 <cmath>
#include <complex>
#include <iostream>
#include <boost/mpl/list.hpp>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include "test_system.hpp"
//[complex_class_snippet_1
namespace boost {
namespace units {
/// replacement complex class
template<class T>
class complex
{
public:
typedef complex<T> this_type;
complex(const T& r = 0,const T& i = 0) : r_(r),i_(i) { }
complex(const this_type& source) : r_(source.r_),i_(source.i_) { }
this_type& operator=(const this_type& source)
{
if (this == &source) return *this;
r_ = source.r_;
i_ = source.i_;
return *this;
}
T& real() { return r_; }
T& imag() { return i_; }
const T& real() const { return r_; }
const T& imag() const { return i_; }
this_type& operator+=(const T& val)
{
r_ += val;
return *this;
}
this_type& operator-=(const T& val)
{
r_ -= val;
return *this;
}
this_type& operator*=(const T& val)
{
r_ *= val;
i_ *= val;
return *this;
}
this_type& operator/=(const T& val)
{
r_ /= val;
i_ /= val;
return *this;
}
this_type& operator+=(const this_type& source)
{
r_ += source.r_;
i_ += source.i_;
return *this;
}
this_type& operator-=(const this_type& source)
{
r_ -= source.r_;
i_ -= source.i_;
return *this;
}
this_type& operator*=(const this_type& source)
{
*this = *this * source;
return *this;
}
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<class X>
complex<typename unary_plus_typeof_helper<X>::type>
operator+(const complex<X>& x)
{
typedef typename unary_plus_typeof_helper<X>::type type;
return complex<type>(x.real(),x.imag());
}
template<class X>
complex<typename unary_minus_typeof_helper<X>::type>
operator-(const complex<X>& x)
{
typedef typename unary_minus_typeof_helper<X>::type type;
return complex<type>(-x.real(),-x.imag());
}
template<class X,class Y>
complex<typename add_typeof_helper<X,Y>::type>
operator+(const complex<X>& x,const complex<Y>& y)
{
typedef typename boost::units::add_typeof_helper<X,Y>::type type;
return complex<type>(x.real()+y.real(),x.imag()+y.imag());
}
template<class X,class Y>
complex<typename boost::units::subtract_typeof_helper<X,Y>::type>
operator-(const complex<X>& x,const complex<Y>& y)
{
typedef typename boost::units::subtract_typeof_helper<X,Y>::type type;
return complex<type>(x.real()-y.real(),x.imag()-y.imag());
}
template<class X,class Y>
complex<typename boost::units::multiply_typeof_helper<X,Y>::type>
operator*(const complex<X>& x,const complex<Y>& y)
{
typedef typename boost::units::multiply_typeof_helper<X,Y>::type type;
return complex<type>(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<X,Y>::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<xy_type,xy_type>::type
// xy_minus_xy_type;
//
// BOOST_STATIC_ASSERT((boost::is_same<xy_plus_xy_type,
// xy_minus_xy_type>::value == true));
//
// return complex<xy_plus_xy_type>(x.real()*y.real()-x.imag()*y.imag(),
// x.real()*y.imag()+x.imag()*y.real());
}
template<class X,class Y>
complex<typename boost::units::divide_typeof_helper<X,Y>::type>
operator/(const complex<X>& x,const complex<Y>& y)
{
// naive implementation of complex division
typedef typename boost::units::divide_typeof_helper<X,Y>::type type;
return complex<type>((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<X,Y>::type xy_type;
// typedef typename boost::units::multiply_typeof_helper<Y,Y>::type yy_type;
//
// typedef typename boost::units::add_typeof_helper<xy_type, xy_type>::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<xy_plus_xy_over_yy_type,
// xy_minus_xy_over_yy_type>::value == true));
//
// return complex<xy_plus_xy_over_yy_type>(
// (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<class Y>
complex<Y>
pow(const complex<Y>& x,const Y& y)
{
std::complex<Y> tmp(x.real(),x.imag());
tmp = std::pow(tmp,y);
return complex<Y>(tmp.real(),tmp.imag());
}
template<class Y>
std::ostream& operator<<(std::ostream& os,const complex<Y>& val)
{
os << val.real() << " + " << val.imag() << " i";
return os;
}
/// specialize power typeof helper for complex<Y>
template<class Y,long N,long D>
struct power_typeof_helper<complex<Y>,static_rational<N,D> >
{
typedef complex<
typename power_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const complex<Y>& x)
{
const static_rational<N,D> rat;
const Y m = Y(rat.numerator())/Y(rat.denominator());
return boost::units::pow(x,m);
}
};
/// specialize root typeof helper for complex<Y>
template<class Y,long N,long D>
struct root_typeof_helper<complex<Y>,static_rational<N,D> >
{
typedef complex<
typename root_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const complex<Y>& x)
{
const static_rational<N,D> rat;
const Y m = Y(rat.denominator())/Y(rat.numerator());
return boost::units::pow(x,m);
}
};
/// specialize power typeof helper for complex<quantity<Unit,Y> >
template<class Y,class Unit,long N,long D>
struct power_typeof_helper<complex<quantity<Unit,Y> >,static_rational<N,D> >
{
typedef typename
power_typeof_helper<Y,static_rational<N,D> >::type value_type;
typedef typename
power_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef complex<quantity_type> type;
static type value(const complex<quantity<Unit,Y> >& x)
{
const complex<value_type> tmp =
pow<static_rational<N,D> >(complex<Y>(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<quantity<Unit,Y> >
template<class Y,class Unit,long N,long D>
struct root_typeof_helper<complex<quantity<Unit,Y> >,static_rational<N,D> >
{
typedef typename
root_typeof_helper<Y,static_rational<N,D> >::type value_type;
typedef typename
root_typeof_helper<Unit,static_rational<N,D> >::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef complex<quantity_type> type;
static type value(const complex<quantity<Unit,Y> >& x)
{
const complex<value_type> tmp =
root<static_rational<N,D> >(complex<Y>(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::math;
using namespace boost::units;
using namespace boost::units::test;
{
//[complex_snippet_1
typedef quantity<length,complex<double> > length_dimension;
length_dimension L(complex<double>(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<quantity<length> > length_dimension;
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;
}

View File

@@ -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 <boost/units/quantity.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/io.hpp>
#include <boost/units/scale.hpp>
#include <boost/units/detail/utility.hpp>
#include <boost/units/systems/si/capacitance.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <iostream>
#include <sstream>
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;
}

View File

@@ -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 <iostream>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/systems/cgs/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
using namespace boost::units;
int main()
{
// test quantity_cast
{
// implicit value_type conversions
//[conversion_snippet_1
quantity<si::length> L1 = quantity<si::length,int>(int(2.5)*si::meters);
quantity<si::length,int> L2(quantity<si::length,double>(2.5*si::meters));
//]
//[conversion_snippet_3
quantity<si::length,int> L3 = static_cast<quantity<si::length,int> >(L1);
//]
//[conversion_snippet_4
quantity<cgs::length> L4 = static_cast<quantity<cgs::length> >(L1);
//]
quantity<si::length,int> L5(4*si::meters),
L6(5*si::meters);
quantity<cgs::length> 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<si::volume> vs(1.0*pow<3>(si::meter));
quantity<cgs::volume> vc(vs);
quantity<si::volume> vs2(vc);
quantity<si::energy> es(1.0*si::joule);
quantity<cgs::energy> ec(es);
quantity<si::energy> es2(ec);
quantity<si::velocity> 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;
}

View File

@@ -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 <iostream>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/cgs/acceleration.hpp>
#include <boost/units/systems/si/acceleration.hpp>
#include <boost/units/systems/si/force.hpp>
#include <boost/units/systems/cgs/force.hpp>
#include <boost/units/systems/si/mass.hpp>
#include <boost/units/systems/cgs/mass.hpp>
#include <boost/units/systems/si/momentum.hpp>
#include <boost/units/systems/cgs/momentum.hpp>
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;
}

View File

@@ -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<dim<length_base_dimension, static_rational<1l, 1l> >, dimensionless_type>
mass_dimension = list<dim<mass_base_dimension, static_rational<1l, 1l> >, dimensionless_type>
time_dimension = list<dim<time_base_dimension, static_rational<1l, 1l> >, dimensionless_type>
energy_dimension = list<dim<length_base_dimension, static_rational<2l, 1l> >, list<dim<mass_base_dimension, static_rational<1l, 1l> >, list<dim<time_base_dimension, static_rational<-2l, 1l> >, dimensionless_type> > >
LM_type = list<dim<length_base_dimension, static_rational<1l, 1l> >, list<dim<mass_base_dimension, static_rational<1l, 1l> >, dimensionless_type> >
L_T_type = list<dim<length_base_dimension, static_rational<1l, 1l> >, list<dim<time_base_dimension, static_rational<-1l, 1l> >, dimensionless_type> >
V_type = list<dim<length_base_dimension, static_rational<1l, 1l> >, list<dim<time_base_dimension, static_rational<-1l, 1l> >, dimensionless_type> >
//]
@endverbatim
**/
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>
#include <iostream>
#include <boost/units/detail/utility.hpp>
#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<length_base_dimension, static_rational<1L, 1L> >
>::type
>));
BOOST_MPL_ASSERT((boost::is_same<
mass_dimension,
mpl::push_front<
dimensionless_type,
dim<mass_base_dimension, static_rational<1L, 1L> >
>::type
>));
BOOST_MPL_ASSERT((boost::is_same<energy_dimension,
mpl::push_front<
mpl::push_front<
mpl::push_front<
dimensionless_type,
dim<time_base_dimension, static_rational<-2L, 1L> > >::type,
dim<mass_base_dimension, static_rational<1L, 1L> > >::type,
dim<length_base_dimension, static_rational<2L, 1L> > >::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<length_dimension,mass_dimension>::type LM_type;
typedef mpl::divides<length_dimension,time_dimension>::type L_T_type;
typedef static_root<
mpl::divides<energy_dimension,mass_dimension>::type,
static_rational<2>
>::type V_type;
//]
BOOST_MPL_ASSERT((boost::is_same<LM_type,
mpl::push_front<
mpl::push_front<
dimensionless_type,
dim<mass_base_dimension, static_rational<1L, 1L> > >::type,
dim<length_base_dimension, static_rational<1L, 1L> > >::type>));
BOOST_MPL_ASSERT((boost::is_same<L_T_type,
mpl::push_front<
mpl::push_front<
dimensionless_type,
dim<time_base_dimension, static_rational<-1L, 1L> > >::type,
dim<length_base_dimension, static_rational<1L, 1L> > >::type>));
BOOST_MPL_ASSERT((boost::is_same<V_type,
mpl::push_front<
mpl::push_front<
dimensionless_type,
dim<time_base_dimension, static_rational<-1L, 1L> > >::type,
dim<length_base_dimension, static_rational<1L, 1L> > >::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;
}

View File

@@ -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 <iostream>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/detail/utility.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
using namespace boost::units;
int main()
{
//[heterogeneous_unit_snippet_1
quantity<si::length> L(1.5*si::meter);
quantity<cgs::mass> 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<si::area> A(1.5*si::meter*cgs::centimeter);
std::cout << 1.5*si::meter*cgs::centimeter << std::endl
<< A << std::endl
<< std::endl;
//]
return 0;
}

View File

@@ -0,0 +1,98 @@
// 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 <boost/units/systems/information.hpp>
/**
\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 <cmath>
#include <iostream>
using std::cout;
using std::endl;
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include <boost/units/conversion.hpp>
namespace bu = boost::units;
using bu::quantity;
using bu::conversion_factor;
// SI prefixes
#include <boost/units/systems/si/prefixes.hpp>
namespace si = boost::units::si;
// information unit system
#include <boost/units/systems/information.hpp>
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 <typename Sys>
quantity<bu::unit<bu::information_dimension, Sys> >
bernoulli_entropy(double p, const bu::unit<bu::information_dimension, Sys>&) {
typedef bu::unit<bu::information_dimension, Sys> requested_unit;
return quantity<requested_unit>((-(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<info> nbytes(1 * si::giga * bit);
cout << "bytes= " << nbytes << endl;
// a quantity of information, stored as bits
quantity<hu::bit::info> nbits(1 * si::mega * byte);
cout << "bits= " << nbits << endl;
// a quantity of information, stored as nats
quantity<hu::nat::info> 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;
}

View File

@@ -0,0 +1,540 @@
// 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 <cmath>
#include <complex>
#include <iostream>
#include <boost/typeof/std/complex.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/codata/physico-chemical_constants.hpp>
#include <boost/units/systems/si/io.hpp>
#include "measurement.hpp"
namespace boost {
namespace units {
//[kitchen_sink_function_snippet_3
/// the physical definition of work - computed for an arbitrary unit system
template<class System,class Y>
quantity<unit<energy_dimension,System>,Y>
work(quantity<unit<force_dimension,System>,Y> F,
quantity<unit<length_dimension,System>,Y> dx)
{
return F*dx;
}
//]
//[kitchen_sink_function_snippet_4
/// the ideal gas law in si units
template<class Y>
quantity<si::amount,Y>
idealGasLaw(const quantity<si::pressure,Y>& P,
const quantity<si::volume,Y>& V,
const quantity<si::temperature,Y>& 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<force> q1(1.0*u1);
quantity<energy> 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<static_rational<4,3> >(u1) << std::endl
<< "X2vU1 : " << root<static_rational<4,3> >(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<static_rational<4,3> >(q1) << std::endl
<< "X2vQ1 : " << root<static_rational<4,3> >(q1) << std::endl
<< std::endl;
//[kitchen_sink_snippet_2
/// check comparison tests
quantity<length> 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<velocity> v1 = 2.0*meters/second;
std::cout << "v1 = " << v1 << std::endl
<< std::endl;
//[kitchen_sink_snippet_4
/// test calcuation of work
quantity<force> F(1.0*newton);
quantity<length> dx(1.0*meter);
quantity<energy> 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<temperature> T = (273.+37.)*kelvin;
quantity<pressure> P = 1.01325e5*pascals;
quantity<length> r = 0.5e-6*meters;
quantity<volume> V = (4.0/3.0)*3.141592*pow<3>(r);
quantity<amount> 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<plane_angle> theta = 0.375*radians;
quantity<dimensionless> sin_theta = sin(theta);
quantity<plane_angle> 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<dimensionless> tmpp = tmp;
tmpp = tmp;
/// check complex quantities
typedef std::complex<double> complex_type;
//[kitchen_sink_snippet_7
quantity<electric_potential,complex_type> v = complex_type(12.5,0.0)*volts;
quantity<current,complex_type> i = complex_type(3.0,4.0)*amperes;
quantity<resistance,complex_type> 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<length,measurement<double> >
u(measurement<double>(1.0,0.0)*meters),
w(measurement<double>(4.52,0.02)*meters),
x(measurement<double>(2.0,0.2)*meters),
y(measurement<double>(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<capacitance>(1.0*farad) << std::endl
// << quantity<catalytic_activity>(1.0*katal) << std::endl
// << quantity<conductance>(1.0*siemen) << std::endl
// << quantity<electric_charge>(1.0*coulomb) << std::endl
// << quantity<electric_potential>(1.0*volt) << std::endl
// << quantity<energy>(1.0*joule) << std::endl
// << quantity<force>(1.0*newton) << std::endl
// << quantity<frequency>(1.0*hertz) << std::endl
// << quantity<illuminance>(1.0*lux) << std::endl
// << quantity<inductance>(1.0*henry) << std::endl
// << quantity<luminous_flux>(1.0*lumen) << std::endl
// << quantity<magnetic_flux>(1.0*weber) << std::endl
// << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
// << quantity<power>(1.0*watt) << std::endl
// << quantity<pressure>(1.0*pascals) << std::endl
// << quantity<resistance>(1.0*ohm) << std::endl
// << std::endl;
//]
//[kitchen_sink_snippet_11
// std::cout << raw_format
// << quantity<capacitance>(1.0*farad) << std::endl
// << quantity<catalytic_activity>(1.0*katal) << std::endl
// << quantity<conductance>(1.0*siemen) << std::endl
// << quantity<electric_charge>(1.0*coulomb) << std::endl
// << quantity<electric_potential>(1.0*volt) << std::endl
// << quantity<energy>(1.0*joule) << std::endl
// << quantity<force>(1.0*newton) << std::endl
// << quantity<frequency>(1.0*hertz) << std::endl
// << quantity<illuminance>(1.0*lux) << std::endl
// << quantity<inductance>(1.0*henry) << std::endl
// << quantity<luminous_flux>(1.0*lumen) << std::endl
// << quantity<magnetic_flux>(1.0*weber) << std::endl
// << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
// << quantity<power>(1.0*watt) << std::endl
// << quantity<pressure>(1.0*pascals) << std::endl
// << quantity<resistance>(1.0*ohm) << std::endl
// << std::endl;
//]
//[kitchen_sink_snippet_12
std::cout << symbol_format
<< quantity<capacitance>(1.0*farad) << std::endl
<< quantity<catalytic_activity>(1.0*katal) << std::endl
<< quantity<conductance>(1.0*siemen) << std::endl
<< quantity<electric_charge>(1.0*coulomb) << std::endl
<< quantity<electric_potential>(1.0*volt) << std::endl
<< quantity<energy>(1.0*joule) << std::endl
<< quantity<force>(1.0*newton) << std::endl
<< quantity<frequency>(1.0*hertz) << std::endl
<< quantity<illuminance>(1.0*lux) << std::endl
<< quantity<inductance>(1.0*henry) << std::endl
<< quantity<luminous_flux>(1.0*lumen) << std::endl
<< quantity<magnetic_flux>(1.0*weber) << std::endl
<< quantity<magnetic_flux_density>(1.0*tesla) << std::endl
<< quantity<power>(1.0*watt) << std::endl
<< quantity<pressure>(1.0*pascals) << std::endl
<< quantity<resistance>(1.0*ohm) << std::endl
<< std::endl;
//]
//[kitchen_sink_snippet_13
std::cout << name_format
<< quantity<capacitance>(1.0*farad) << std::endl
<< quantity<catalytic_activity>(1.0*katal) << std::endl
<< quantity<conductance>(1.0*siemen) << std::endl
<< quantity<electric_charge>(1.0*coulomb) << std::endl
<< quantity<electric_potential>(1.0*volt) << std::endl
<< quantity<energy>(1.0*joule) << std::endl
<< quantity<force>(1.0*newton) << std::endl
<< quantity<frequency>(1.0*hertz) << std::endl
<< quantity<illuminance>(1.0*lux) << std::endl
<< quantity<inductance>(1.0*henry) << std::endl
<< quantity<luminous_flux>(1.0*lumen) << std::endl
<< quantity<magnetic_flux>(1.0*weber) << std::endl
<< quantity<magnetic_flux_density>(1.0*tesla) << std::endl
<< quantity<power>(1.0*watt) << std::endl
<< quantity<pressure>(1.0*pascals) << std::endl
<< quantity<resistance>(1.0*ohm) << std::endl
<< std::endl;
//]
return 0;
}

View File

@@ -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 <iostream>
#include <boost/function.hpp>
#include <boost/units/io.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/absolute.hpp>
// 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 <boost/units/lambda.hpp>
#include <boost/lambda/bind.hpp>
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<si::acceleration> a = 2.0 * si::meters_per_second_squared;
bu::quantity<si::velocity> v = 1.0 * si::meters_per_second;
bu::quantity<si::length> s0 = 0.5 * si::meter;
// Displacement over time
boost::function<bu::quantity<si::length> (bu::quantity<si::time>) >
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<si::current> iamp = 1.5 * si::ampere;
const bu::quantity<si::frequency> f = 1.0e3 * si::hertz;
const bu::quantity<si::current> 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<si::current> (bu::quantity<si::time>) >
i = iamp
* bl::bind(static_cast<bu::dimensionless_quantity<si::system, double>::type (*)(const bu::quantity<si::plane_angle>&)>(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<si::length> 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<si::area> (bu::quantity<si::length>) >
A = bl::bind(static_cast<bu::quantity<si::area> (*)(const bu::quantity<si::length>&)>(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<bu::absolute<si::temperature> >
Tref = 273.15 * bu::absolute<si::temperature>();
const bu::quantity<bu::absolute<si::temperature> >
Tamb = 300.00 * bu::absolute<si::temperature>();
boost::function<bu::quantity<si::temperature> (bu::quantity<bu::absolute<si::temperature> >,
bu::quantity<bu::absolute<si::temperature> >)>
dT = bl::_2 - bl::_1;
cout << "Temperature difference of two absolute temperatures:" << endl
<< "dT(" << Tref << ", " << Tamb << ") = " << dT(Tref, Tamb) << endl
<< endl;
return 0;
}
//]

View File

@@ -0,0 +1,327 @@
// 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 <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <boost/io/ios_state.hpp>
#include <boost/units/static_rational.hpp>
namespace boost {
namespace units {
namespace sqr_namespace /**/ {
template<class Y>
Y sqr(Y val)
{ return val*val; }
} // namespace
using sqr_namespace::sqr;
template<class Y>
class measurement
{
public:
typedef measurement<Y> this_type;
typedef Y value_type;
measurement(const value_type& val = value_type(),
const value_type& err = value_type()) :
value_(val),
uncertainty_(std::abs(err))
{ }
measurement(const this_type& source) :
value_(source.value_),
uncertainty_(source.uncertainty_)
{ }
//~measurement() { }
this_type& operator=(const this_type& source)
{
if (this == &source) return *this;
value_ = source.value_;
uncertainty_ = source.uncertainty_;
return *this;
}
operator value_type() const { return value_; }
value_type value() const { return value_; }
value_type uncertainty() const { return uncertainty_; }
value_type lower_bound() const { return value_-uncertainty_; }
value_type upper_bound() const { return value_+uncertainty_; }
this_type& operator+=(const value_type& val)
{
value_ += val;
return *this;
}
this_type& operator-=(const value_type& val)
{
value_ -= val;
return *this;
}
this_type& operator*=(const value_type& val)
{
value_ *= val;
uncertainty_ *= val;
return *this;
}
this_type& operator/=(const value_type& val)
{
value_ /= val;
uncertainty_ /= val;
return *this;
}
this_type& operator+=(const this_type& /*source*/);
this_type& operator-=(const this_type& /*source*/);
this_type& operator*=(const this_type& /*source*/);
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<class Y>
inline
measurement<Y>&
measurement<Y>::operator+=(const this_type& source)
{
uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_));
value_ += source.value_;
return *this;
}
template<class Y>
inline
measurement<Y>&
measurement<Y>::operator-=(const this_type& source)
{
uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_));
value_ -= source.value_;
return *this;
}
template<class Y>
inline
measurement<Y>&
measurement<Y>::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<class Y>
inline
measurement<Y>&
measurement<Y>::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<class Y>
inline
measurement<Y>
operator+(Y lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs,Y(0))+=rhs);
}
template<class Y>
inline
measurement<Y>
operator-(Y lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs,Y(0))-=rhs);
}
template<class Y>
inline
measurement<Y>
operator*(Y lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs,Y(0))*=rhs);
}
template<class Y>
inline
measurement<Y>
operator/(Y lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs,Y(0))/=rhs);
}
// measurement op value_type
template<class Y>
inline
measurement<Y>
operator+(const measurement<Y>& lhs,Y rhs)
{
return (measurement<Y>(lhs)+=measurement<Y>(rhs,Y(0)));
}
template<class Y>
inline
measurement<Y>
operator-(const measurement<Y>& lhs,Y rhs)
{
return (measurement<Y>(lhs)-=measurement<Y>(rhs,Y(0)));
}
template<class Y>
inline
measurement<Y>
operator*(const measurement<Y>& lhs,Y rhs)
{
return (measurement<Y>(lhs)*=measurement<Y>(rhs,Y(0)));
}
template<class Y>
inline
measurement<Y>
operator/(const measurement<Y>& lhs,Y rhs)
{
return (measurement<Y>(lhs)/=measurement<Y>(rhs,Y(0)));
}
// measurement op measurement
template<class Y>
inline
measurement<Y>
operator+(const measurement<Y>& lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs)+=rhs);
}
template<class Y>
inline
measurement<Y>
operator-(const measurement<Y>& lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs)-=rhs);
}
template<class Y>
inline
measurement<Y>
operator*(const measurement<Y>& lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs)*=rhs);
}
template<class Y>
inline
measurement<Y>
operator/(const measurement<Y>& lhs,const measurement<Y>& rhs)
{
return (measurement<Y>(lhs)/=rhs);
}
/// specialize power typeof helper
template<class Y,long N,long D>
struct power_typeof_helper<measurement<Y>,static_rational<N,D> >
{
typedef measurement<
typename power_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const measurement<Y>& x)
{
const static_rational<N,D> 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<class Y,long N,long D>
struct root_typeof_helper<measurement<Y>,static_rational<N,D> >
{
typedef measurement<
typename root_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const measurement<Y>& x)
{
const static_rational<N,D> 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<class Y>
inline
std::ostream& operator<<(std::ostream& os,const measurement<Y>& 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

View File

@@ -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 <iostream>
#include <boost/units/io.hpp>
#include <boost/units/conversion.hpp>
#include <boost/units/unit.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/physical_dimensions.hpp>
#include <boost/units/base_unit.hpp>
#include <boost/units/make_system.hpp>
namespace boost {
namespace units {
//[non_base_dimension_snippet_1
struct imperial_gallon_tag :
base_unit<imperial_gallon_tag, volume_dimension, 1> { };
typedef make_system<imperial_gallon_tag>::type imperial;
typedef unit<volume_dimension,imperial> imperial_gallon;
struct us_gallon_tag : base_unit<us_gallon_tag, volume_dimension, 2> { };
typedef make_system<us_gallon_tag>::type us;
typedef unit<volume_dimension,us> 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<imperial_gallon> ig1(1.0*imperial_gallon());
quantity<us_gallon> ug1(1.0*us_gallon());
quantity<imperial_gallon> ig2(ug1);
quantity<us_gallon> ug2(ig1);
return 0;
}

View File

@@ -0,0 +1,394 @@
// 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<double>(1000, 1000) : 25.03 seconds
multiplying ublas::matrix<quantity>(1000, 1000) : 24.49 seconds
tiled_matrix_multiply<double>(1000, 1000) : 1.12 seconds
tiled_matrix_multiply<quantity>(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 <cstdlib>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <boost/config.hpp>
#include <boost/timer.hpp>
#include <boost/utility/result_of.hpp>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4267; disable:4127; disable:4244; disable:4100)
#endif
#include <boost/numeric/ublas/matrix.hpp>
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
enum {
tile_block_size = 24
};
template<class T0, class T1, class Out>
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<class T0, class T1, class Out>
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<class F, class T, class N, class R>
R solve_differential_equation(F f, T lower, T upper, N steps, R start) {
typedef typename F::template result<T, R>::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<class Arg1, class Arg2> struct result;
double operator()(const double& x, const double& y) const {
return(1.0 - x + 4.0 * y);
}
boost::units::quantity<boost::units::si::velocity>
operator()(const quantity<si::time>& x,
const quantity<si::length>& 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<double,double> {
typedef double type;
};
template<>
struct f::result<quantity<si::time>, quantity<si::length> > {
typedef quantity<si::velocity> 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<double> ublas_result;
{
boost::numeric::ublas::matrix<double> m1(max_value, max_value);
boost::numeric::ublas::matrix<double> 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<double>("
<< 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<boost::units::si::dimensionless>
> 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<quantity>("
<< max_value << ", " << max_value << ") : ";
boost::timer timer;
ublas_resultq = (prod(m1, m2));
std::cout << timer.elapsed() << " seconds" << std::endl;
}
std::vector<double> cresult(max_value * max_value);
{
std::vector<double> m1(max_value * max_value);
std::vector<double> 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<double>("
<< 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<boost::units::si::energy>
> cresultq(max_value * max_value);
{
std::vector<
boost::units::quantity<boost::units::si::force>
> m1(max_value * max_value);
std::vector<
boost::units::quantity<boost::units::si::length>
> 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<quantity>("
<< 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) {
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<double> values(1000);
std::cout << "solving y' = 1 - x + 4 * y with double: ";
boost::timer timer;
for(int i = 0; i < 1000; ++i) {
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) {
double x = .1 * i;
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<quantity<length> > values(1000);
std::cout << "solving y' = 1 - x + 4 * y with quantity: ";
boost::timer timer;
for(int i = 0; i < 1000; ++i) {
quantity<si::time> 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) {
double x = .1 * i;
quantity<si::length> 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);
}
}
}
}

View File

@@ -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 <complex>
#include <iostream>
#include <boost/mpl/list.hpp>
#include <boost/typeof/std/complex.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include "test_system.hpp"
int main(void)
{
using namespace boost::units;
using namespace boost::units::test;
{
//[quantity_snippet_1
quantity<length> L = 2.0*meters; // quantity of length
quantity<energy> 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<static_rational<3,2> >(L) << std::endl
<< "2vL = "
<< root<2>(L) << std::endl
<< "(3/2)vL = "
<< root<static_rational<3,2> >(L) << std::endl
<< std::endl;
}
{
//[quantity_snippet_2
quantity<length,std::complex<double> > L(std::complex<double>(3.0,4.0)*meters);
quantity<energy,std::complex<double> > 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<static_rational<3,2> >(L) << std::endl
<< "2vL = "
<< root<2>(L) << std::endl
<< "(3/2)vL = "
<< root<static_rational<3,2> >(L) << std::endl
<< std::endl;
}
return 0;
}

View File

@@ -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 <iostream>
#include <boost/math/quaternion.hpp>
#include <boost/mpl/list.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#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<class Y,long N,long D>
struct power_typeof_helper<boost::math::quaternion<Y>,static_rational<N,D> >
{
// boost::math::quaternion only supports integer powers
BOOST_STATIC_ASSERT(D==1);
typedef boost::math::quaternion<
typename power_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const boost::math::quaternion<Y>& x)
{
return boost::math::pow(x,static_cast<int>(N));
}
};
//]
//[quaternion_class_snippet_1b
/// specialize root typeof helper
template<class Y,long N,long D>
struct root_typeof_helper<boost::math::quaternion<Y>,static_rational<N,D> >
{
// boost::math::quaternion only supports integer powers
BOOST_STATIC_ASSERT(N==1);
typedef boost::math::quaternion<
typename root_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const boost::math::quaternion<Y>& x)
{
return boost::math::pow(x,static_cast<int>(D));
}
};
//]
//[quaternion_class_snippet_2a
/// specialize power typeof helper for quaternion<quantity<Unit,Y> >
template<class Unit,long N,long D,class Y>
struct power_typeof_helper<
boost::math::quaternion<quantity<Unit,Y> >,
static_rational<N,D> >
{
typedef typename power_typeof_helper<
Y,
static_rational<N,D>
>::type value_type;
typedef typename power_typeof_helper<
Unit,
static_rational<N,D>
>::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef boost::math::quaternion<quantity_type> type;
static type value(const boost::math::quaternion<quantity<Unit,Y> >& x)
{
const boost::math::quaternion<value_type> tmp =
pow<static_rational<N,D> >(boost::math::quaternion<Y>(
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<quantity<Unit,Y> >
template<class Unit,long N,long D,class Y>
struct root_typeof_helper<
boost::math::quaternion<quantity<Unit,Y> >,
static_rational<N,D> >
{
typedef typename root_typeof_helper<
Y,
static_rational<N,D>
>::type value_type;
typedef typename root_typeof_helper<
Unit,
static_rational<N,D>
>::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef boost::math::quaternion<quantity_type> type;
static type value(const boost::math::quaternion<quantity<Unit,Y> >& x)
{
const boost::math::quaternion<value_type> tmp =
root<static_rational<N,D> >(boost::math::quaternion<Y>(
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,quaternion<double> > length_dimension;
length_dimension L(quaternion<double>(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<quantity<length> > 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;
}

View File

@@ -0,0 +1,166 @@
// 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 <iostream>
#include <boost/units/conversion.hpp>
#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/prefixes.hpp>
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<length_base_unit, length_dimension, 1>
{
static std::string name() { return "nautical mile"; }
static std::string symbol() { return "nmi"; }
};
typedef boost::units::make_system<length_base_unit>::type system;
/// unit typedefs
typedef unit<length_dimension,system> 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<length_base_unit, length_dimension, 2>
{
static std::string name() { return "foot"; }
static std::string symbol() { return "ft"; }
};
typedef boost::units::make_system<length_base_unit>::type system;
/// unit typedefs
typedef unit<length_dimension,system> 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<class System,typename T>
quantity<unit<boost::units::length_dimension,System>,T>
radar_beam_height(const quantity<unit<length_dimension,System>,T>& radar_range,
const quantity<unit<length_dimension,System>,T>& earth_radius,
T k = 4.0/3.0)
{
return quantity<unit<length_dimension,System>,T>
(pow<2>(radar_range)/(2.0*k*earth_radius));
}
//]
//[radar_beam_height_function_snippet_2
template<class return_type,class System1,class System2,typename T>
return_type
radar_beam_height(const quantity<unit<length_dimension,System1>,T>& radar_range,
const quantity<unit<length_dimension,System2>,T>& earth_radius,
T k = 4.0/3.0)
{
// need to decide which system to use for calculation
const return_type rr(radar_range),
er(earth_radius);
return return_type(pow<2>(rr)/(2.0*k*er));
}
//]
//[radar_beam_height_function_snippet_3
quantity<imperial::length>
radar_beam_height(const quantity<nautical::length>& range)
{
return quantity<imperial::length>
(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<nautical::length> radar_range(300.0*miles);
const quantity<si::length> earth_radius(6371.0087714*kilo*meters);
const quantity<si::length> beam_height_1(radar_beam_height(quantity<si::length>(radar_range),earth_radius));
const quantity<nautical::length> beam_height_2(radar_beam_height(radar_range,quantity<nautical::length>(earth_radius)));
const quantity<si::length> beam_height_3(radar_beam_height< quantity<si::length> >(radar_range,earth_radius));
const quantity<nautical::length> beam_height_4(radar_beam_height< quantity<nautical::length> >(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<si::length>(radar_beam_height(radar_range))
<< std::endl << std::endl;
return 0;
}

View File

@@ -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 <boost/units/base_dimension.hpp>
#include <boost/units/base_unit.hpp>
#include <boost/units/unit.hpp>
#include <boost/units/quantity.hpp>
//[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<currency_base_dimension, 1> {};
typedef currency_base_dimension::dimension_type currency_type;
template<long N>
struct currency_base_unit :
base_unit<currency_base_unit<N>, 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<N1>,
currency_base_unit<N2>,
double, get_conversion_factor(N1, N2));
//]
int main() {
boost::units::quantity<us_dollar> dollars = 2.00 * us_dollar();
boost::units::quantity<euro> euros(dollars);
set_conversion_factor(0, 1, 2.0);
dollars = static_cast<boost::units::quantity<us_dollar> >(euros);
set_conversion_factor(0, 1, .5);
euros = static_cast<boost::units::quantity<euro> >(dollars);
double value = euros.value(); // = .5
if(value != .5) {
return(1);
} else {
return(0);
}
}

View File

@@ -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 <map>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/base_units/imperial/foot.hpp>
//[runtime_unit_snippet_1
namespace {
using namespace boost::units;
using imperial::foot_base_unit;
std::map<std::string, quantity<si::length> > known_units;
}
quantity<si::length> calculate(const quantity<si::length>& 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<double>(input);
if(std::cin >> input)
{
if(known_units.find(input) != known_units.end())
{
std::cout << static_cast<double>(
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;
}
}
}
}
//]

View File

@@ -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

File diff suppressed because it is too large Load Diff

View File

@@ -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 <iomanip>
#include <iostream>
#include <boost/units/absolute.hpp>
#include <boost/units/get_system.hpp>
#include <boost/units/io.hpp>
#include <boost/units/unit.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/temperature.hpp>
#include <boost/units/detail/utility.hpp>
#include <boost/units/base_units/temperature/fahrenheit.hpp>
using namespace boost::units;
namespace boost {
namespace units {
namespace fahrenheit {
//[temperature_snippet_1
typedef temperature::fahrenheit_base_unit::unit_type temperature;
typedef get_system<temperature>::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<absolute<fahrenheit::temperature> > T1p(
32.0*absolute<fahrenheit::temperature>());
quantity<fahrenheit::temperature> T1v(
32.0*fahrenheit::degrees);
quantity<absolute<si::temperature> > T2p(T1p);
quantity<si::temperature> T2v(T1v);
//]
typedef conversion_helper<
quantity<absolute<fahrenheit::temperature> >,
quantity<absolute<si::temperature> > > absolute_conv_type;
typedef conversion_helper<
quantity<fahrenheit::temperature>,
quantity<si::temperature> > 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;
}

View File

@@ -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 <boost/mpl/list.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/units/base_dimension.hpp>
#include <boost/units/derived_dimension.hpp>
#include <boost/units/io.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/static_constant.hpp>
#include <boost/units/unit.hpp>
#include <boost/units/base_unit.hpp>
#include <boost/units/make_system.hpp>
namespace boost {
namespace units {
//[test_system_snippet_1
/// base dimension of length
struct length_base_dimension : base_dimension<length_base_dimension,1> { };
/// base dimension of mass
struct mass_base_dimension : base_dimension<mass_base_dimension,2> { };
/// base dimension of time
struct time_base_dimension : base_dimension<time_base_dimension,3> { };
//]
#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<length_base_dimension,2>::type area_dimension;
typedef derived_dimension<mass_base_dimension,1,
length_base_dimension,2,
time_base_dimension,-2>::type energy_dimension;
//]
namespace test {
//[test_system_snippet_6
struct meter_base_unit : base_unit<meter_base_unit, length_dimension, 1> { };
struct kilogram_base_unit : base_unit<kilogram_base_unit, mass_dimension, 2> { };
struct second_base_unit : base_unit<second_base_unit, time_dimension, 3> { };
typedef make_system<
meter_base_unit,
kilogram_base_unit,
second_base_unit>::type mks_system;
/// unit typedefs
typedef unit<dimensionless_type,mks_system> dimensionless;
typedef unit<length_dimension,mks_system> length;
typedef unit<mass_dimension,mks_system> mass;
typedef unit<time_dimension,mks_system> time;
typedef unit<area_dimension,mks_system> area;
typedef unit<energy_dimension,mks_system> 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<test::meter_base_unit>
{
static std::string name() { return "meter"; }
static std::string symbol() { return "m"; }
};
//]
template<> struct base_unit_info<test::kilogram_base_unit>
{
static std::string name() { return "kilogram"; }
static std::string symbol() { return "kg"; }
};
template<> struct base_unit_info<test::second_base_unit>
{
static std::string name() { return "second"; }
static std::string symbol() { return "s"; }
};
} // namespace units
} // namespace boost
#endif // MCS_TEST_SYSTEM_HPP

View File

@@ -0,0 +1,94 @@
// 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<double> 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 <complex>
#include <iostream>
#include <boost/typeof/std/complex.hpp>
#include <boost/units/systems/si/energy.hpp>
#include <boost/units/systems/si/force.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/electric_potential.hpp>
#include <boost/units/systems/si/current.hpp>
#include <boost/units/systems/si/resistance.hpp>
#include <boost/units/systems/si/io.hpp>
using namespace boost::units;
using namespace boost::units::si;
quantity<energy>
work(const quantity<force>& F, const quantity<length>& dx)
{
return F * dx; // Defines the relation: work = force * distance.
}
int main()
{
/// Test calculation of work.
quantity<force> F(2.0 * newton); // Define a quantity of force.
quantity<length> dx(2.0 * meter); // and a distance,
quantity<energy> 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<double> complex_type; // double real and imaginary parts.
// Define some complex electrical quantities.
quantity<electric_potential, complex_type> v = complex_type(12.5, 0.0) * volts;
quantity<current, complex_type> i = complex_type(3.0, 4.0) * amperes;
quantity<resistance, complex_type> 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;
}
//]

View File

@@ -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 <iostream>
#include "test_system.hpp"
#include <boost/units/pow.hpp>
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<static_rational<3,2> >(L)
<< std::endl
<< "2vM = " << root<2>(M) << std::endl
<< "(3/2)vM = " << root<static_rational<3,2> >(M)
<< std::endl;
return 0;
}