| 1 | // Boost.Units - A C++ library for zero-overhead dimensional analysis and |
| 2 | // unit/quantity manipulation and conversion |
| 3 | // |
| 4 | // Copyright (C) 2003-2008 Matthias Christian Schabel |
| 5 | // Copyright (C) 2007-2008 Steven Watanabe |
| 6 | // |
| 7 | // Distributed under the Boost Software License, Version 1.0. (See |
| 8 | // accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | |
| 11 | #include <boost/type_traits/is_same.hpp> |
| 12 | #include <boost/mpl/assert.hpp> |
| 13 | |
| 14 | #include <boost/units/base_unit.hpp> |
| 15 | #include <boost/units/derived_dimension.hpp> |
| 16 | #include <boost/units/make_system.hpp> |
| 17 | #include <boost/units/operators.hpp> |
| 18 | #include <boost/units/reduce_unit.hpp> |
| 19 | #include <boost/units/unit.hpp> |
| 20 | |
| 21 | #include <boost/units/physical_dimensions/current.hpp> |
| 22 | #include <boost/units/physical_dimensions/electric_potential.hpp> |
| 23 | #include <boost/units/physical_dimensions/energy.hpp> |
| 24 | #include <boost/units/physical_dimensions/force.hpp> |
| 25 | #include <boost/units/physical_dimensions/length.hpp> |
| 26 | #include <boost/units/physical_dimensions/mass.hpp> |
| 27 | #include <boost/units/physical_dimensions/time.hpp> |
| 28 | |
| 29 | namespace test_system1 { |
| 30 | |
| 31 | // the base units in the system will be: |
| 32 | // |
| 33 | // volts = m^2 kg s^-2 C^-1 |
| 34 | // newtons = m kg s^-2 |
| 35 | // joules = m^2 kg s^-2 |
| 36 | |
| 37 | // we will find the representation of m^-1 C^-1 = V N J^-2 = m^-1 C^-1 |
| 38 | |
| 39 | // reducing the system should generate the matrix equation |
| 40 | // 2 1 2 |
| 41 | // 1 1 1 x = c |
| 42 | // -2 -2 -2 |
| 43 | // -1 0 0 |
| 44 | |
| 45 | struct volt : boost::units::base_unit<volt, boost::units::electric_potential_dimension, 1> {}; |
| 46 | struct newton : boost::units::base_unit<newton, boost::units::force_dimension, 2> {}; |
| 47 | struct joule : boost::units::base_unit<joule, boost::units::energy_dimension, 3> {}; |
| 48 | |
| 49 | typedef boost::units::make_system<volt, newton, joule>::type complicated_system; |
| 50 | |
| 51 | typedef boost::units::derived_dimension< |
| 52 | boost::units::length_base_dimension, -1, |
| 53 | boost::units::time_base_dimension, -1, |
| 54 | boost::units::current_base_dimension, -1 |
| 55 | >::type dimension; |
| 56 | |
| 57 | typedef boost::units::reduce_unit<boost::units::unit<dimension, complicated_system> >::type reduced; |
| 58 | |
| 59 | typedef boost::units::divide_typeof_helper< |
| 60 | boost::units::multiply_typeof_helper<volt::unit_type, newton::unit_type>::type, |
| 61 | boost::units::power_typeof_helper<joule::unit_type, boost::units::static_rational<2> >::type |
| 62 | >::type expected; |
| 63 | |
| 64 | void test() { |
| 65 | BOOST_MPL_ASSERT((boost::is_same<reduced, expected>)); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | namespace test_system2 { |
| 71 | |
| 72 | // the base units in the system will be: |
| 73 | // |
| 74 | // kilograms = kg |
| 75 | // meters = m |
| 76 | |
| 77 | // we will find the representation of m and kg |
| 78 | |
| 79 | // reducing the system should generate the matrix equation |
| 80 | // 0 1 |
| 81 | // 1 0 x = c |
| 82 | |
| 83 | struct kilogram : boost::units::base_unit<kilogram, boost::units::mass_dimension, 4> {}; |
| 84 | struct meter : boost::units::base_unit<meter, boost::units::length_dimension, 5> {}; |
| 85 | |
| 86 | typedef boost::units::make_system<meter, kilogram>::type mk_system; |
| 87 | |
| 88 | typedef boost::units::reduce_unit<boost::units::unit<boost::units::mass_dimension, mk_system> >::type mass_unit; |
| 89 | typedef boost::units::reduce_unit<boost::units::unit<boost::units::length_dimension, mk_system> >::type length_unit; |
| 90 | |
| 91 | void test() { |
| 92 | BOOST_MPL_ASSERT((boost::is_same<mass_unit, kilogram::unit_type>)); |
| 93 | BOOST_MPL_ASSERT((boost::is_same<length_unit, meter::unit_type>)); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |