Programowanie Sylwester Arabas prowadzący ćwiczenia: Magdalena Kuich, Krzysztof Piasecki, Łukasz Dulny Wydział Fizyki Uniwersytetu Warszawskiego wykład VII 8. kwietnia 2015 r.
na ostatnim wykładzie... kod programu 17 18 #include <boost/math/tools/roots.hpp 19 20 int main() 21 { 22 typedef double T; 23 using namespace boost::math::tools; 24 std::pair<t,t rng = bisect( 25 f<t, // funkcja 26 0., 100., // zakres poszukiwań 27 eps_tolerance<t(3) // tolerancja: 2^(1-3) 28 ); 29 30 std::cerr << std::setprecision(2) 31 << "T_wrzenia @ Mont Blanc to ok. " 32 << (rng.first + rng.second) / 2 << " C" << std::endl; 33 }
na ostatnim wykładzie... kod programu 17 18 #include <boost/math/tools/roots.hpp 19 20 int main() 21 { 22 typedef double T; 23 using namespace boost::math::tools; 24 std::pair<t,t rng = bisect( 25 f<t, // funkcja 26 0., 100., // zakres poszukiwań 27 eps_tolerance<t(3) // tolerancja: 2^(1-3) 28 ); 29 30 std::cerr << std::setprecision(2) 31 << "T_wrzenia @ Mont Blanc to ok. " 32 << (rng.first + rng.second) / 2 << " C" << std::endl; 33 } biblioteka Boost, f-cja boost::math::tools::bisect()
na ostatnim wykładzie... kod programu 17 18 #include <boost/math/tools/roots.hpp 19 20 int main() 21 { 22 typedef double T; 23 using namespace boost::math::tools; 24 std::pair<t,t rng = bisect( 25 f<t, // funkcja 26 0., 100., // zakres poszukiwań 27 eps_tolerance<t(3) // tolerancja: 2^(1-3) 28 ); 29 30 std::cerr << std::setprecision(2) 31 << "T_wrzenia @ Mont Blanc to ok. " 32 << (rng.first + rng.second) / 2 << " C" << std::endl; 33 } biblioteka Boost, f-cja boost::math::tools::bisect() kontener std::pair<t,t
na ostatnim wykładzie... kod programu 17 18 #include <boost/math/tools/roots.hpp 19 20 int main() 21 { 22 typedef double T; 23 using namespace boost::math::tools; 24 std::pair<t,t rng = bisect( 25 f<t, // funkcja 26 0., 100., // zakres poszukiwań 27 eps_tolerance<t(3) // tolerancja: 2^(1-3) 28 ); 29 30 std::cerr << std::setprecision(2) 31 << "T_wrzenia @ Mont Blanc to ok. " 32 << (rng.first + rng.second) / 2 << " C" << std::endl; 33 } biblioteka Boost, f-cja boost::math::tools::bisect() kontener std::pair<t,t instrukcja typedef
biblioteka Boost (http://boost.org/)...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards WELCOME TO BOOST.ORG! Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use. We aim to establish "existing practice" and provide reference implementations so that Boost libraries are suitable for eventual standardization. Ten Boost libraries are included in the C++ Standards Committee's Library Technical Report (TR1) and in the new C++11 Standard. C++11 also includes several more Boost libraries in addition to those from TR1. More Boost libraries are proposed for standardization in C++17. Since 2006 an intimate week long annual conference related to Boost called C++ Now has been held in Aspen, Colorado each May. Boost has been a participant in the annual Google Summer of Code since 2007. WELCOME Search Getting Started Download Libraries Mailing Lists Reporting and Fixing Bugs Wiki INTRODUCTION
Boost.units: Zero-overhead dimensional analysis and unit/quantity manipulation and conversion zero-overhead: brak narzutu w czasie wykonania (wszystkie funkcje biblioteki realizowane są w czasie kompilacji) dimensional analysis: analiza wymiarowa kodu (sprawdzanie zgodności jednostek fizycznych) unit/quantity manipulation and conversion: wypisywanie liczb z jednostkami, konwersja pomiędzy systemami (np. SI/CGS) katalog stałych fizycznych
Boost.units: podstawy przestrzenie nazw boost::units i boost::units::si struktura (klasa) boost::units::quantity<q, T=double 1 #include <iostream 2 3 4 int main() 5 { 6 7 8 9 double l = 100e3; 10 double t = 60*60; 11 12 std::cout 13 << l << " / " << t 14 << " = " << l/t 15 << std::endl; 16 } 1 #include <boost/units/systems/si.hpp 2 #include <boost/units/io.hpp 3 4 int main() 5 { 6 namespace bu = boost::units; 7 namespace si = boost::units::si; 8 9 bu::quantity<si::length l = 100e3 * si::metres; 10 bu::quantity<si::time t = 60*60 * si::seconds; 11 12 std::cout << bu::engineering_prefix 13 << l << " / " << t 14 << " = " << l/t 15 << std::endl; 16 } 100000 / 3600 = 27.7778 100 km / 3.6 ks = 27.7778 m s^-1
Boost.units: przykład komunikatu kompilatora kompiluje się, działa błąd przy kompilacji 1 #include <iostream 2 3 int main() 4 { 5 6 7 8 double l = 100e3; 9 double t = 60*60; 10 double v = l*t; 11 } komunikat o błędzie (kompilator clang++) 1 #include <boost/units/systems/si.hpp 2 3 int main() 4 { 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 bu::quantity<si::length l = 100e3 * si::metres; 9 bu::quantity<si::time t = 60*60 * si::seconds; 10 bu::quantity<si::velocity v = l*t; 11 } bug_units.cpp:10:30: error: no viable conversion from 'quantity<unit<list<[...], list<dim<[...], static_rational<1, [...], [...], [...], [...]' to 'quantity<unit<list<[...], list<dim<[...], static_rational<-1, [...], [...], [...], [...]' bu::quantity<si::velocity v = l*t; ^ ~~~
Napiszmy program wyznaczający gęstość powietrza (zakładając że składa się ono w 100% z N 2 )
1 #include <boost/units/systems/si.hpp 2 #include <boost/units/systems/si/codata_constants.hpp 3 #include <boost/units/io.hpp 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density rho( 10 bu::quantity<si::temperature T, 11 bu::quantity<si::pressure p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } kompilacja na tempacu $ g++ -I/work/programs/include/ -std=c++0x rho.cpp
1 #include <boost/units/systems/si.hpp 2 #include <boost/units/systems/si/codata_constants.hpp 3 #include <boost/units/io.hpp 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density rho( 10 bu::quantity<si::temperature T, 11 bu::quantity<si::pressure p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } codata::r kompilacja na tempacu $ g++ -I/work/programs/include/ -std=c++0x rho.cpp
1 #include <boost/units/systems/si.hpp 2 #include <boost/units/systems/si/codata_constants.hpp 3 #include <boost/units/io.hpp 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density rho( 10 bu::quantity<si::temperature T, 11 bu::quantity<si::pressure p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } codata::r typ auto! (C++11) kompilacja na tempacu $ g++ -I/work/programs/include/ -std=c++0x rho.cpp
1 #include <boost/units/systems/si.hpp 2 #include <boost/units/systems/si/codata_constants.hpp 3 #include <boost/units/io.hpp 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density rho( 10 bu::quantity<si::temperature T, 11 bu::quantity<si::pressure p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } codata::r typ auto! (C++11) ρ 1 kg /m 3 kompilacja na tempacu $ g++ -I/work/programs/include/ -std=c++0x rho.cpp
Dziękuję za uwagę!