tutorial_1.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <iostream>
  11. #include <boost/units/units_fwd.hpp>
  12. #include <boost/units/base_dimension.hpp>
  13. #include <boost/units/base_unit.hpp>
  14. #include <boost/units/derived_dimension.hpp>
  15. #include <boost/units/make_system.hpp>
  16. #include <boost/units/io.hpp>
  17. #include <boost/units/quantity.hpp>
  18. #include <boost/units/static_constant.hpp>
  19. #include <boost/units/unit.hpp>
  20. namespace boost {
  21. namespace units {
  22. struct length_base_dimension : boost::units::base_dimension<length_base_dimension,1> { }; ///> base dimension of length
  23. struct time_base_dimension : boost::units::base_dimension<time_base_dimension,3> { }; ///> base dimension of time
  24. typedef length_base_dimension::dimension_type length_dimension;
  25. typedef time_base_dimension::dimension_type time_dimension;
  26. struct length1_base_unit : base_unit<length1_base_unit,length_dimension,1>
  27. {
  28. static std::string name() { return "length 1"; }
  29. static std::string symbol() { return "l1"; }
  30. };
  31. struct length2_base_unit : base_unit<length2_base_unit,length_dimension,2>
  32. {
  33. static std::string name() { return "length2"; }
  34. static std::string symbol() { return "l2"; }
  35. };
  36. struct time1_base_unit : base_unit<time1_base_unit,time_dimension,3>
  37. {
  38. static std::string name() { return "time1"; }
  39. static std::string symbol() { return "t1"; }
  40. };
  41. struct time2_base_unit : base_unit<time2_base_unit,time_dimension,4>
  42. {
  43. static std::string name() { return "time2"; }
  44. static std::string symbol() { return "t2"; }
  45. };
  46. namespace s1 {
  47. typedef make_system<length1_base_unit,time1_base_unit>::type system;
  48. /// unit typedefs
  49. typedef unit<dimensionless_type,system> dimensionless;
  50. typedef unit<length_dimension,system> length;
  51. typedef unit<time_dimension,system> time;
  52. /// unit constants
  53. BOOST_UNITS_STATIC_CONSTANT(length1,length);
  54. BOOST_UNITS_STATIC_CONSTANT(time1,time);
  55. } // namespace s1
  56. namespace s2 {
  57. typedef make_system<length2_base_unit,time2_base_unit>::type system;
  58. /// unit typedefs
  59. typedef unit<dimensionless_type,system> dimensionless;
  60. typedef unit<length_dimension,system> length;
  61. typedef unit<time_dimension,system> time;
  62. /// unit constants
  63. BOOST_UNITS_STATIC_CONSTANT(length2,length);
  64. BOOST_UNITS_STATIC_CONSTANT(time2,time);
  65. } // namespace s2
  66. template<class X,class Y>
  67. struct conversion_helper< quantity<s1::length,X>,quantity<s2::length,Y> >
  68. {
  69. static quantity<s2::length,Y> convert(const quantity<s1::length,X>& source)
  70. {
  71. return quantity<s2::length,Y>::from_value(2.5*source.value());
  72. }
  73. };
  74. template<class X,class Y>
  75. struct conversion_helper< quantity<s2::length,X>,quantity<s1::length,Y> >
  76. {
  77. static quantity<s1::length,Y> convert(const quantity<s2::length,X>& source)
  78. {
  79. return quantity<s1::length,Y>::from_value((1.0/2.5)*source.value());
  80. }
  81. };
  82. template<class X,class Y>
  83. struct conversion_helper< quantity<s1::time,X>,quantity<s2::time,Y> >
  84. {
  85. static quantity<s2::time,Y> convert(const quantity<s1::time,X>& source)
  86. {
  87. return quantity<s2::time,Y>::from_value(0.5*source.value());
  88. }
  89. };
  90. } // namespace units
  91. } // namespace boost
  92. int main(void)
  93. {
  94. using namespace boost::units;
  95. quantity<s1::length,float> l1(1.0*s1::length1);
  96. quantity<s2::length,double> l2(1.5*l1);
  97. quantity<s1::length,float> l3(2.0*l2/3.0);
  98. quantity<s1::time,float> t1(1.0*s1::time1);
  99. quantity<s2::time,double> t2(1.5*t1);
  100. // quantity<s1::time,float> t3(2.0*t2/3.0);
  101. return 0;
  102. }
  103. /*
  104. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  105. // unit/quantity manipulation and conversion
  106. //
  107. // Copyright (C) 2003-2008 Matthias Christian Schabel
  108. // Copyright (C) 2008 Steven Watanabe
  109. //
  110. // Distributed under the Boost Software License, Version 1.0. (See
  111. // accompanying file LICENSE_1_0.txt or copy at
  112. // http://www.boost.org/LICENSE_1_0.txt)
  113. #include <iostream>
  114. #include <boost/units/units_fwd.hpp>
  115. #include <boost/units/base_dimension.hpp>
  116. #include <boost/units/base_unit.hpp>
  117. #include <boost/units/derived_dimension.hpp>
  118. #include <boost/units/make_system.hpp>
  119. #include <boost/units/io.hpp>
  120. #include <boost/units/quantity.hpp>
  121. #include <boost/units/static_constant.hpp>
  122. #include <boost/units/unit.hpp>
  123. namespace boost {
  124. namespace units {
  125. struct length_base_dimension : boost::units::base_dimension<length_base_dimension,1> { }; ///> base dimension of length
  126. struct mass_base_dimension : boost::units::base_dimension<mass_base_dimension,2> { }; ///> base dimension of mass
  127. struct time_base_dimension : boost::units::base_dimension<time_base_dimension,3> { }; ///> base dimension of time
  128. typedef length_base_dimension::dimension_type length_dimension;
  129. typedef mass_base_dimension::dimension_type mass_dimension;
  130. typedef time_base_dimension::dimension_type time_dimension;
  131. struct centimeter_base_unit : base_unit<centimeter_base_unit,length_dimension,1>
  132. {
  133. static std::string name() { return "centimeter"; }
  134. static std::string symbol() { return "cm"; }
  135. };
  136. struct gram_base_unit : base_unit<gram_base_unit,mass_dimension,2>
  137. {
  138. static std::string name() { return "gram"; }
  139. static std::string symbol() { return "g"; }
  140. };
  141. struct second_base_unit : base_unit<second_base_unit,time_dimension,3>
  142. {
  143. static std::string name() { return "second"; }
  144. static std::string symbol() { return "s"; }
  145. };
  146. namespace CG {
  147. typedef make_system<centimeter_base_unit,gram_base_unit>::type system;
  148. /// unit typedefs
  149. typedef unit<dimensionless_type,system> dimensionless;
  150. typedef unit<length_dimension,system> length;
  151. typedef unit<mass_dimension,system> mass;
  152. /// unit constants
  153. BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
  154. BOOST_UNITS_STATIC_CONSTANT(gram,mass);
  155. } // namespace CG
  156. namespace cgs {
  157. typedef make_system<centimeter_base_unit,gram_base_unit,second_base_unit>::type system;
  158. /// unit typedefs
  159. typedef unit<dimensionless_type,system> dimensionless;
  160. typedef unit<length_dimension,system> length;
  161. typedef unit<mass_dimension,system> mass;
  162. typedef unit<time_dimension,system> time;
  163. /// unit constants
  164. BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
  165. BOOST_UNITS_STATIC_CONSTANT(gram,mass);
  166. BOOST_UNITS_STATIC_CONSTANT(second,time);
  167. } // namespace cgs
  168. namespace esu {
  169. typedef make_system<centimeter_base_unit,
  170. gram_base_unit,
  171. second_base_unit>::type system;
  172. /// derived dimension for force in electrostatic units : L M T^-2
  173. typedef derived_dimension<length_base_dimension,1,
  174. mass_base_dimension,1,
  175. time_base_dimension,-2>::type force_dimension;
  176. /// derived dimension for charge in electrostatic units : L^3/2 M^1/2 T^-1
  177. typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<3,2> >,
  178. dim<mass_base_dimension,static_rational<1,2> >,
  179. dim<time_base_dimension,static_rational<-1> > > >::type charge_dimension;
  180. /// derived dimension for current in electrostatic units : L^3/2 M^1/2 T^-2
  181. typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<3,2> >,
  182. dim<mass_base_dimension,static_rational<1,2> >,
  183. dim<time_base_dimension,static_rational<-2> > > >::type current_dimension;
  184. /// derived dimension for electric potential in electrostatic units : L^1/2 M^1/2 T^-1
  185. typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<1,2> >,
  186. dim<mass_base_dimension,static_rational<1,2> >,
  187. dim<time_base_dimension,static_rational<-1> > > >::type electric_potential_dimension;
  188. /// derived dimension for electric field in electrostatic units : L^-1/2 M^1/2 T^-1
  189. typedef make_dimension_list< mpl::list< dim<length_base_dimension,static_rational<-1,2> >,
  190. dim<mass_base_dimension,static_rational<1,2> >,
  191. dim<time_base_dimension,static_rational<-1> > > >::type electric_field_dimension;
  192. /// unit typedefs
  193. typedef unit<dimensionless_type,system> dimensionless;
  194. typedef unit<length_dimension,system> length;
  195. typedef unit<mass_dimension,system> mass;
  196. typedef unit<time_dimension,system> time;
  197. typedef unit<force_dimension,system> force;
  198. typedef unit<charge_dimension,system> charge;
  199. typedef unit<current_dimension,system> current;
  200. typedef unit<electric_potential_dimension,system> electric_potential;
  201. typedef unit<electric_field_dimension,system> electric_field;
  202. /// unit constants
  203. BOOST_UNITS_STATIC_CONSTANT(centimeter,length);
  204. BOOST_UNITS_STATIC_CONSTANT(gram,mass);
  205. BOOST_UNITS_STATIC_CONSTANT(second,time);
  206. BOOST_UNITS_STATIC_CONSTANT(dyne,force);
  207. BOOST_UNITS_STATIC_CONSTANT(esu,charge);
  208. BOOST_UNITS_STATIC_CONSTANT(statvolt,electric_potential);
  209. } // namespace esu
  210. template<class Y>
  211. quantity<esu::force,Y> coulombLaw(const quantity<esu::charge,Y>& q1,
  212. const quantity<esu::charge,Y>& q2,
  213. const quantity<esu::length,Y>& r)
  214. {
  215. return q1*q2/(r*r);
  216. }
  217. } // namespace units
  218. } // namespace boost
  219. int main(void)
  220. {
  221. using namespace boost::units;
  222. quantity<CG::length> cg_length(1.0*CG::centimeter);
  223. quantity<cgs::length> cgs_length(1.0*cgs::centimeter);
  224. std::cout << cg_length/cgs_length << std::endl;
  225. std::cout << esu::gram*pow<2>(esu::centimeter/esu::second)/esu::esu << std::endl;
  226. std::cout << esu::statvolt/esu::centimeter << std::endl;
  227. quantity<esu::charge> q1 = 1.0*esu::esu,
  228. q2 = 2.0*esu::esu;
  229. quantity<esu::length> r = 1.0*esu::centimeter;
  230. std::cout << coulombLaw(q1,q2,r) << std::endl;
  231. std::cout << coulombLaw(q1,q2,cgs_length) << std::endl;
  232. return 0;
  233. }
  234. */