mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-02 20:04:27 +02:00
refactor: all examples refactored to use a simplified quantity creation
This commit is contained in:
@@ -31,15 +31,14 @@
|
||||
namespace {
|
||||
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
constexpr quantity<isq::speed[m / s], int> fixed_int_si_avg_speed(quantity<isq::length[m], int> d,
|
||||
quantity<isq::time[s], int> t)
|
||||
constexpr quantity<si::metre / si::second, int> fixed_int_si_avg_speed(quantity<si::metre, int> d,
|
||||
quantity<si::second, int> t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
constexpr quantity<isq::speed[m / s]> fixed_double_si_avg_speed(quantity<isq::length[m]> d, quantity<isq::time[s]> t)
|
||||
constexpr quantity<si::metre / si::second> fixed_double_si_avg_speed(quantity<si::metre> d, quantity<si::second> t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
@@ -52,6 +51,7 @@ constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,
|
||||
template<QuantityOf<isq::length> D, QuantityOf<isq::time> T, QuantityOf<isq::speed> V>
|
||||
void print_result(D distance, T duration, V speed)
|
||||
{
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
const auto result_in_kmph = value_cast<km / h>(speed);
|
||||
std::cout << "Average speed of a car that makes " << distance << " in " << duration << " is " << result_in_kmph
|
||||
<< ".\n";
|
||||
@@ -59,10 +59,12 @@ void print_result(D distance, T duration, V speed)
|
||||
|
||||
void example()
|
||||
{
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
// SI (int)
|
||||
{
|
||||
constexpr auto distance = isq::length(220, km);
|
||||
constexpr auto duration = isq::time(2, h);
|
||||
constexpr auto distance = 220 * km;
|
||||
constexpr auto duration = 2 * h;
|
||||
|
||||
std::cout << "SI units with 'int' as representation\n";
|
||||
|
||||
@@ -73,8 +75,8 @@ void example()
|
||||
|
||||
// SI (double)
|
||||
{
|
||||
constexpr auto distance = isq::length(220., km);
|
||||
constexpr auto duration = isq::time(2., h);
|
||||
constexpr auto distance = 220. * km;
|
||||
constexpr auto duration = 2. * h;
|
||||
|
||||
std::cout << "\nSI units with 'double' as representation\n";
|
||||
|
||||
@@ -88,8 +90,8 @@ void example()
|
||||
{
|
||||
using namespace mp_units::international::unit_symbols;
|
||||
|
||||
constexpr auto distance = isq::length(140, mi);
|
||||
constexpr auto duration = isq::time(2, h);
|
||||
constexpr auto distance = 140 * mi;
|
||||
constexpr auto duration = 2 * h;
|
||||
|
||||
std::cout << "\nUS Customary Units with 'int' as representation\n";
|
||||
|
||||
@@ -104,8 +106,8 @@ void example()
|
||||
{
|
||||
using namespace mp_units::international::unit_symbols;
|
||||
|
||||
constexpr auto distance = isq::length(140., mi);
|
||||
constexpr auto duration = isq::time(2., h);
|
||||
constexpr auto distance = 140. * mi;
|
||||
constexpr auto duration = 2. * h;
|
||||
|
||||
std::cout << "\nUS Customary Units with 'double' as representation\n";
|
||||
|
||||
@@ -120,8 +122,8 @@ void example()
|
||||
|
||||
// CGS (int)
|
||||
{
|
||||
constexpr auto distance = isq::length(22'000'000, cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200, cgs::second);
|
||||
constexpr auto distance = 22'000'000 * cgs::centimetre;
|
||||
constexpr auto duration = 7200 * cgs::second;
|
||||
|
||||
std::cout << "\nCGS units with 'int' as representation\n";
|
||||
|
||||
@@ -134,8 +136,8 @@ void example()
|
||||
|
||||
// CGS (double)
|
||||
{
|
||||
constexpr auto distance = isq::length(22'000'000., cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200., cgs::second);
|
||||
constexpr auto distance = 22'000'000. * cgs::centimetre;
|
||||
constexpr auto duration = 7200. * cgs::second;
|
||||
|
||||
std::cout << "\nCGS units with 'double' as representation\n";
|
||||
|
||||
|
@@ -39,24 +39,24 @@ namespace {
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
inline constexpr auto g = si::standard_gravity(1);
|
||||
inline constexpr auto air_density = isq::mass_density(1.225, kg / m3);
|
||||
inline constexpr auto g = 1 * si::standard_gravity;
|
||||
inline constexpr auto air_density = isq::mass_density(1.225 * (kg / m3));
|
||||
|
||||
class Box {
|
||||
quantity<isq::area[m2]> base_;
|
||||
quantity<isq::height[m]> height_;
|
||||
quantity<isq::mass_density[kg / m3]> density_ = air_density;
|
||||
public:
|
||||
constexpr Box(const quantity<isq::length[m]>& length, const quantity<isq::length[m]>& width,
|
||||
quantity<isq::length[m]> height) :
|
||||
constexpr Box(const quantity<isq::length[m]>& length, const quantity<isq::width[m]>& width,
|
||||
quantity<isq::height[m]> height) :
|
||||
base_(length * width), height_(std::move(height))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr QuantityOf<isq::weight> auto filled_weight() const
|
||||
{
|
||||
const WeakQuantityOf<isq::volume> auto volume = base_ * height_;
|
||||
const WeakQuantityOf<isq::mass> auto mass = density_ * volume;
|
||||
const QuantityOf<isq::volume> auto volume = isq::volume(base_ * height_);
|
||||
const QuantityOf<isq::mass> auto mass = density_ * volume;
|
||||
return quantity_cast<isq::weight>(mass * g);
|
||||
}
|
||||
|
||||
@@ -82,22 +82,21 @@ public:
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace mp_units::si;
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
constexpr auto mm = isq::length[unit_symbols::mm]; // helper reference object
|
||||
const quantity<isq::height[m]> height = 200. * mm;
|
||||
auto box = Box(isq::length(1000. * mm), isq::width(500. * mm), height);
|
||||
box.set_contents_density(1000.0 * isq::mass_density[kg / m3]);
|
||||
|
||||
const auto height = mm(200.0)[metre];
|
||||
auto box = Box(mm(1000.0), mm(500.0), height);
|
||||
box.set_contents_density(isq::mass_density(1000.0, kg / m3));
|
||||
|
||||
const auto fill_time = isq::time(200.0, s); // time since starting fill
|
||||
const auto measured_mass = isq::mass(20.0, kg); // measured mass at fill_time
|
||||
const auto fill_time = 200. * s; // time since starting fill
|
||||
const auto measured_mass = 20.0 * kg; // measured mass at fill_time
|
||||
const auto fill_level = box.fill_level(measured_mass);
|
||||
const auto spare_capacity = box.spare_capacity(measured_mass);
|
||||
const auto filled_weight = box.filled_weight();
|
||||
|
||||
const WeakQuantityOf<isq::mass_change_rate> auto input_flow_rate = measured_mass / fill_time;
|
||||
const WeakQuantityOf<isq::speed> auto float_rise_rate = fill_level / fill_time;
|
||||
const QuantityOf<isq::mass_change_rate> auto input_flow_rate = measured_mass / fill_time;
|
||||
const QuantityOf<isq::speed> auto float_rise_rate = fill_level / fill_time;
|
||||
const QuantityOf<isq::time> auto fill_time_left = (height / fill_level - 1) * fill_time;
|
||||
|
||||
const auto fill_percent = (fill_level / height)[percent];
|
||||
|
@@ -20,8 +20,8 @@
|
||||
physical_quantities
|
||||
*/
|
||||
|
||||
#include <mp_units/math.h> // IWYU pragma: keep
|
||||
#include <mp_units/iostream.h>
|
||||
#include <mp_units/math.h> // IWYU pragma: keep
|
||||
#include <mp_units/systems/isq/electromagnetism.h>
|
||||
#include <mp_units/systems/si/si.h>
|
||||
#include <iostream>
|
||||
@@ -35,22 +35,22 @@ int main()
|
||||
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
|
||||
std::cout.precision(3);
|
||||
|
||||
constexpr auto C = isq::capacitance(0.47, uF);
|
||||
constexpr auto V0 = isq::voltage(5.0, V);
|
||||
constexpr auto R = isq::resistance(4.7, si::kilo<si::ohm>);
|
||||
constexpr auto C = isq::capacitance(0.47 * uF);
|
||||
constexpr auto V0 = isq::voltage(5.0 * V);
|
||||
constexpr auto R = isq::resistance(4.7 * si::kilo<si::ohm>);
|
||||
|
||||
for (auto t = isq::time(0, ms); t <= isq::time(50, ms); ++t) {
|
||||
const WeakQuantityOf<isq::voltage> auto Vt = V0 * mp_units::exp(-t / (R * C));
|
||||
for (auto t = 0 * ms; t <= 50 * ms; ++t) {
|
||||
const QuantityOf<isq::voltage> auto Vt = V0 * mp_units::exp(-t / (R * C));
|
||||
|
||||
std::cout << "at " << t << " voltage is ";
|
||||
|
||||
if (Vt >= isq::voltage(1, V))
|
||||
if (Vt >= 1 * V)
|
||||
std::cout << Vt[V];
|
||||
else if (Vt >= isq::voltage(1, mV))
|
||||
else if (Vt >= 1 * mV)
|
||||
std::cout << Vt[mV];
|
||||
else if (Vt >= isq::voltage(1, uV))
|
||||
else if (Vt >= 1 * uV)
|
||||
std::cout << Vt[uV];
|
||||
else if (Vt >= isq::voltage(1, nV))
|
||||
else if (Vt >= 1 * nV)
|
||||
std::cout << Vt[nV];
|
||||
else
|
||||
std::cout << Vt[pV];
|
||||
|
@@ -37,12 +37,12 @@ void simple_quantities()
|
||||
using distance = quantity<isq::distance[kilo<metre>]>;
|
||||
using duration = quantity<isq::duration[second]>;
|
||||
|
||||
constexpr distance km = isq::distance[kilo<metre>](1.0);
|
||||
constexpr distance miles = isq::distance[mile](1.0);
|
||||
constexpr distance km = 1. * kilo<metre>;
|
||||
constexpr distance miles = 1. * mile;
|
||||
|
||||
constexpr duration sec = isq::duration[second](1);
|
||||
constexpr duration min = isq::duration[minute](1);
|
||||
constexpr duration hr = isq::duration[hour](1);
|
||||
constexpr duration sec = 1 * second;
|
||||
constexpr duration min = 1 * minute;
|
||||
constexpr duration hr = 1 * hour;
|
||||
|
||||
std::cout << "A physical quantities library can choose the simple\n";
|
||||
std::cout << "option to provide output using a single type for each base unit:\n\n";
|
||||
@@ -59,14 +59,14 @@ void quantities_with_typed_units()
|
||||
using namespace mp_units::si;
|
||||
using namespace mp_units::international;
|
||||
|
||||
constexpr auto km = isq::distance[kilo<metre>](1.0);
|
||||
constexpr auto miles = isq::distance[mile](1.0);
|
||||
constexpr auto km = 1.0 * kilo<metre>;
|
||||
constexpr auto miles = 1.0 * mile;
|
||||
|
||||
std::cout.precision(6);
|
||||
|
||||
constexpr auto sec = isq::duration[second](1);
|
||||
constexpr auto min = isq::duration[minute](1);
|
||||
constexpr auto hr = isq::duration[hour](1);
|
||||
constexpr auto sec = 1 * second;
|
||||
constexpr auto min = 1 * minute;
|
||||
constexpr auto hr = 1 * hour;
|
||||
|
||||
std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
|
||||
std::cout << km << '\n';
|
||||
@@ -75,7 +75,7 @@ void quantities_with_typed_units()
|
||||
std::cout << min << '\n';
|
||||
std::cout << hr << "\n\n";
|
||||
|
||||
constexpr quantity<isq::length[metre]> meter{1};
|
||||
constexpr auto meter = 1. * metre;
|
||||
std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
|
||||
" for consistency and repeatability across applications:\n\n";
|
||||
|
||||
@@ -110,8 +110,8 @@ void calcs_comparison()
|
||||
"when adding two values of the same very big\n"
|
||||
"or very small type:\n\n";
|
||||
|
||||
const auto L1A = isq::length[fm](2.f);
|
||||
const auto L2A = isq::length[fm](3.f);
|
||||
const auto L1A = 2.f * fm;
|
||||
const auto L2A = 3.f * fm;
|
||||
const auto LrA = L1A + L2A;
|
||||
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
|
||||
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <mp_units/format.h>
|
||||
#include <mp_units/systems/isq/space_and_time.h>
|
||||
#include <mp_units/systems/si/unit_symbols.h>
|
||||
#include <mp_units/systems/si/units.h>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
@@ -40,11 +41,12 @@ inline constexpr double conversion_factor(Target, Source)
|
||||
int main()
|
||||
{
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
std::cout << "conversion factor in mp-units...\n\n";
|
||||
|
||||
constexpr auto lengthA = isq::length(2.0, si::metre);
|
||||
constexpr auto lengthB = lengthA[si::milli<si::metre>];
|
||||
constexpr auto lengthA = 2.0 * m;
|
||||
constexpr auto lengthB = lengthA[mm];
|
||||
|
||||
std::cout << STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
|
||||
<< "represent the same length in different units.\n\n";
|
||||
|
@@ -58,7 +58,7 @@ auto fmt_line(const Q& q)
|
||||
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
|
||||
void print_details(std::string_view description, const Ship& ship)
|
||||
{
|
||||
const auto waterDensity = isq::density[lb / cubic<ft>](62.4);
|
||||
const auto waterDensity = 62.4 * isq::density[lb / cubic<ft>];
|
||||
std::cout << STD_FMT::format("{}\n", description);
|
||||
std::cout << STD_FMT::format("{:20} : {}\n", "length", fmt_line<yd, m>(ship.length))
|
||||
<< STD_FMT::format("{:20} : {}\n", "draft", fmt_line<yd, m>(ship.draft))
|
||||
@@ -77,37 +77,37 @@ int main()
|
||||
using mp_units::international::unit_symbols::ft; // collides with si::femto<si::tonne>
|
||||
|
||||
// KMS Bismark, using the units the Germans would use, taken from Wiki
|
||||
auto bismark = Ship{.length{isq::length[m](251.)},
|
||||
.draft{isq::length[m](9.3)},
|
||||
.beam{isq::length[m](36)},
|
||||
.speed{isq::speed[km / h](56)},
|
||||
.mass{isq::mass[t](50'300)},
|
||||
.mainGuns{isq::length[mm](380)},
|
||||
.shellMass{isq::mass[kg](800)},
|
||||
.shellSpeed{isq::speed[m / s](820.)},
|
||||
.power{isq::power[kW](110.45)}};
|
||||
auto bismark = Ship{.length{251. * m},
|
||||
.draft{9.3 * m},
|
||||
.beam{36 * m},
|
||||
.speed{56 * (km / h)},
|
||||
.mass{50'300 * t},
|
||||
.mainGuns{380 * mm},
|
||||
.shellMass{800 * kg},
|
||||
.shellSpeed{820. * (m / s)},
|
||||
.power{110.45 * kW}};
|
||||
|
||||
// USS Iowa, using units from the foot-pound-second system
|
||||
auto iowa = Ship{.length{isq::length[ft](860.)},
|
||||
.draft{isq::length[ft](37.) + isq::length[in](2.)},
|
||||
.beam{isq::length[ft](108.) + isq::length[in](2.)},
|
||||
.speed{isq::speed[kt](33)},
|
||||
.mass{isq::mass[imperial::long_ton](57'540)},
|
||||
.mainGuns{isq::length[in](16)},
|
||||
.shellMass{isq::mass[lb](2700)},
|
||||
.shellSpeed{isq::speed[ft / s](2690.)},
|
||||
.power{isq::power[hp](212'000)}};
|
||||
auto iowa = Ship{.length{860. * ft},
|
||||
.draft{37. * ft + 2. * in},
|
||||
.beam{108. * ft + 2. * in},
|
||||
.speed{33 * kt},
|
||||
.mass{57'540 * imperial::long_ton},
|
||||
.mainGuns{16 * in},
|
||||
.shellMass{2700 * lb},
|
||||
.shellSpeed{2690. * (ft / s)},
|
||||
.power{212'000 * hp}};
|
||||
|
||||
// HMS King George V, using units from the foot-pound-second system
|
||||
auto kgv = Ship{.length{isq::length[ft](745.1)},
|
||||
.draft{isq::length[ft](33.) + isq::length[in](7.5)},
|
||||
.beam{isq::length[ft](103.2) + isq::length[in](2.5)},
|
||||
.speed{isq::speed[kt](28.3)},
|
||||
.mass{isq::mass[imperial::long_ton](42'245)},
|
||||
.mainGuns{isq::length[in](14)},
|
||||
.shellMass{isq::mass[lb](1'590)},
|
||||
.shellSpeed{isq::speed[ft / s](2483.)},
|
||||
.power{isq::power[hp](110'000)}};
|
||||
auto kgv = Ship{.length{745.1 * ft},
|
||||
.draft{33. * ft + 7.5 * in},
|
||||
.beam{103.2 * ft + 2.5 * in},
|
||||
.speed{28.3 * kt},
|
||||
.mass{42'245 * imperial::long_ton},
|
||||
.mainGuns{14 * in},
|
||||
.shellMass{1'590 * lb},
|
||||
.shellSpeed{2483. * (ft / s)},
|
||||
.power{110'000 * hp}};
|
||||
|
||||
print_details("KMS Bismark, defined in appropriate units from the SI system", bismark);
|
||||
std::cout << "\n\n";
|
||||
|
@@ -105,7 +105,7 @@ struct glider {
|
||||
std::array<polar_point, 1> polar;
|
||||
};
|
||||
|
||||
constexpr mp_units::WeakQuantityOf<mp_units::dimensionless> auto glide_ratio(const glider::polar_point& polar)
|
||||
constexpr mp_units::QuantityOf<mp_units::dimensionless> auto glide_ratio(const glider::polar_point& polar)
|
||||
{
|
||||
return polar.v / -polar.climb;
|
||||
}
|
||||
|
@@ -45,11 +45,10 @@ auto get_gliders()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
UNITS_DIAGNOSTIC_PUSH
|
||||
UNITS_DIAGNOSTIC_IGNORE_MISSING_BRACES
|
||||
static const std::array gliders = {
|
||||
glider{"SZD-30 Pirat", {83 * isq::speed[km / h], -0.7389 * rate_of_climb_speed[m / s]}},
|
||||
glider{"SZD-51 Junior", {80 * isq::speed[km / h], -0.6349 * rate_of_climb_speed[m / s]}},
|
||||
glider{"SZD-48 Jantar Std 3", {110 * isq::speed[km / h], -0.77355 * rate_of_climb_speed[m / s]}},
|
||||
glider{"SZD-56 Diana", {110 * isq::speed[km / h], -0.63657 * rate_of_climb_speed[m / s]}}};
|
||||
static const std::array gliders = {glider{"SZD-30 Pirat", {83 * (km / h), -0.7389 * (m / s)}},
|
||||
glider{"SZD-51 Junior", {80 * (km / h), -0.6349 * (m / s)}},
|
||||
glider{"SZD-48 Jantar Std 3", {110 * (km / h), -0.77355 * (m / s)}},
|
||||
glider{"SZD-56 Diana", {110 * (km / h), -0.63657 * (m / s)}}};
|
||||
UNITS_DIAGNOSTIC_POP
|
||||
return gliders;
|
||||
}
|
||||
@@ -57,10 +56,9 @@ auto get_gliders()
|
||||
auto get_weather_conditions()
|
||||
{
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
static const std::array weather_conditions = {
|
||||
std::pair{"Good", weather{1900 * isq::height[m], 4.3 * rate_of_climb_speed[m / s]}},
|
||||
std::pair{"Medium", weather{1550 * isq::height[m], 2.8 * rate_of_climb_speed[m / s]}},
|
||||
std::pair{"Bad", weather{850 * isq::height[m], 1.8 * rate_of_climb_speed[m / s]}}};
|
||||
static const std::array weather_conditions = {std::pair{"Good", weather{1900 * m, 4.3 * (m / s)}},
|
||||
std::pair{"Medium", weather{1550 * m, 2.8 * (m / s)}},
|
||||
std::pair{"Bad", weather{850 * m, 1.8 * (m / s)}}};
|
||||
return weather_conditions;
|
||||
}
|
||||
|
||||
@@ -69,8 +67,8 @@ auto get_waypoints()
|
||||
using namespace geographic::literals;
|
||||
using namespace mp_units::international::unit_symbols;
|
||||
static const std::array waypoints = {
|
||||
waypoint{"EPPR", {54.24772_N, 18.6745_E}, altitude{16. * isq::altitude[ft]}}, // N54°14'51.8" E18°40'28.2"
|
||||
waypoint{"EPGI", {53.52442_N, 18.84947_E}, altitude{115. * isq::altitude[ft]}} // N53°31'27.9" E18°50'58.1"
|
||||
waypoint{"EPPR", {54.24772_N, 18.6745_E}, altitude{16. * ft}}, // N54°14'51.8" E18°40'28.2"
|
||||
waypoint{"EPGI", {53.52442_N, 18.84947_E}, altitude{115. * ft}} // N53°31'27.9" E18°50'58.1"
|
||||
};
|
||||
return waypoints;
|
||||
}
|
||||
@@ -159,12 +157,12 @@ void example()
|
||||
{
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
const safety sfty = {300 * isq::height[m]};
|
||||
const safety sfty = {300 * m};
|
||||
const auto gliders = get_gliders();
|
||||
const auto waypoints = get_waypoints();
|
||||
const auto weather_conditions = get_weather_conditions();
|
||||
const task t = {waypoints[0], waypoints[1], waypoints[0]};
|
||||
const aircraft_tow tow = {400 * isq::height[m], 1.6 * rate_of_climb_speed[m / s]};
|
||||
const aircraft_tow tow = {400 * m, 1.6 * (m / s)};
|
||||
// TODO use C++20 date library when available
|
||||
// set `start_time` to 11:00 am today
|
||||
const timestamp start_time(std::chrono::system_clock::now());
|
||||
|
@@ -39,10 +39,10 @@ int main()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
using namespace mp_units::international::unit_symbols;
|
||||
|
||||
constexpr auto v1 = isq::speed(110, km / h);
|
||||
constexpr auto v2 = isq::speed(70., mph);
|
||||
constexpr auto v3 = avg_speed(isq::distance(220, km), isq::duration(2, h));
|
||||
constexpr auto v4 = avg_speed(quantity<isq::distance[mi]>{140}, quantity<isq::duration[h]>{2});
|
||||
constexpr auto v1 = 110 * (km / h);
|
||||
constexpr auto v2 = 70. * mph;
|
||||
constexpr auto v3 = avg_speed(220 * km, 2 * h);
|
||||
constexpr auto v4 = avg_speed(140 * mi, 2 * h);
|
||||
constexpr auto v5 = value_cast<m / s>(v3);
|
||||
constexpr auto v6 = value_cast<m / s>(v4);
|
||||
constexpr auto v7 = value_cast<int>(v6);
|
||||
|
@@ -49,16 +49,15 @@ int main()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
using state = kalman::state<quantity<isq::mass[g]>>;
|
||||
|
||||
const state initial = {1 * isq::mass[kg]};
|
||||
const std::array measurements = {1030 * isq::mass[g], 989 * isq::mass[g], 1017 * isq::mass[g], 1009 * isq::mass[g],
|
||||
1013 * isq::mass[g], 979 * isq::mass[g], 1008 * isq::mass[g], 1042 * isq::mass[g],
|
||||
1012 * isq::mass[g], 1011 * isq::mass[g]};
|
||||
const state initial = {1 * kg};
|
||||
const std::array measurements = {1030 * g, 989 * g, 1017 * g, 1009 * g, 1013 * g,
|
||||
979 * g, 1008 * g, 1042 * g, 1012 * g, 1011 * g};
|
||||
|
||||
print_header(initial);
|
||||
state next = initial;
|
||||
for (int index = 1; const auto& m : measurements) {
|
||||
const auto& previous = next;
|
||||
const quantity gain = 1. / index;
|
||||
const auto gain = 1. / index * one;
|
||||
const auto current = state_update(previous, m, gain);
|
||||
next = current;
|
||||
print(index++, gain, m, current, next);
|
||||
|
@@ -51,14 +51,11 @@ int main()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
using state = kalman::state<quantity<isq::position_vector[m]>, quantity<isq::velocity[m / s]>>;
|
||||
|
||||
const auto interval = 5 * isq::period_duration[s];
|
||||
const state initial = {30 * isq::position_vector[km], 40 * isq::velocity[m / s]};
|
||||
const std::array measurements = {30110 * isq::position_vector[m], 30265 * isq::position_vector[m],
|
||||
30740 * isq::position_vector[m], 30750 * isq::position_vector[m],
|
||||
31135 * isq::position_vector[m], 31015 * isq::position_vector[m],
|
||||
31180 * isq::position_vector[m], 31610 * isq::position_vector[m],
|
||||
31960 * isq::position_vector[m], 31865 * isq::position_vector[m]};
|
||||
std::array gain = {quantity{0.2}, quantity{0.1}};
|
||||
const auto interval = isq::period_duration(5 * s);
|
||||
const state initial = {30 * km, 40 * (m / s)};
|
||||
const quantity<isq::position_vector[m], int> measurements[] = {30110 * m, 30265 * m, 30740 * m, 30750 * m, 31135 * m,
|
||||
31015 * m, 31180 * m, 31610 * m, 31960 * m, 31865 * m};
|
||||
std::array gain = {0.2 * one, 0.1 * one};
|
||||
|
||||
print_header(initial);
|
||||
state next = state_extrapolation(initial, interval);
|
||||
|
@@ -51,14 +51,11 @@ int main()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
using state = kalman::state<quantity<isq::position_vector[m]>, quantity<isq::velocity[m / s]>>;
|
||||
|
||||
const auto interval = 5 * isq::period_duration[s];
|
||||
const state initial = {30 * isq::position_vector[km], 50 * isq::velocity[m / s]};
|
||||
const std::array measurements = {30160 * isq::position_vector[m], 30365 * isq::position_vector[m],
|
||||
30890 * isq::position_vector[m], 31050 * isq::position_vector[m],
|
||||
31785 * isq::position_vector[m], 32215 * isq::position_vector[m],
|
||||
33130 * isq::position_vector[m], 34510 * isq::position_vector[m],
|
||||
36010 * isq::position_vector[m], 37265 * isq::position_vector[m]};
|
||||
std::array gain = {quantity{0.2}, quantity{0.1}};
|
||||
const auto interval = isq::period_duration(5 * s);
|
||||
const state initial = {30 * km, 50 * (m / s)};
|
||||
const quantity<isq::position_vector[m], int> measurements[] = {30160 * m, 30365 * m, 30890 * m, 31050 * m, 31785 * m,
|
||||
32215 * m, 33130 * m, 34510 * m, 36010 * m, 37265 * m};
|
||||
std::array gain = {0.2 * one, 0.1 * one};
|
||||
|
||||
print_header(initial);
|
||||
state next = state_extrapolation(initial, interval);
|
||||
|
@@ -51,15 +51,12 @@ int main()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
using state = kalman::state<quantity<isq::position_vector[m]>, quantity<isq::velocity[m / s]>,
|
||||
quantity<isq::acceleration[m / s2]>>;
|
||||
const auto interval = 5. * isq::period_duration[s];
|
||||
const state initial = {30 * isq::position_vector[km], 50 * isq::velocity[m / s], 0 * isq::acceleration[m / s2]};
|
||||
const auto interval = isq::period_duration(5. * s);
|
||||
const state initial = {30 * km, 50 * (m / s), 0 * (m / s2)};
|
||||
|
||||
const std::array measurements = {30160 * isq::position_vector[m], 30365 * isq::position_vector[m],
|
||||
30890 * isq::position_vector[m], 31050 * isq::position_vector[m],
|
||||
31785 * isq::position_vector[m], 32215 * isq::position_vector[m],
|
||||
33130 * isq::position_vector[m], 34510 * isq::position_vector[m],
|
||||
36010 * isq::position_vector[m], 37265 * isq::position_vector[m]};
|
||||
std::array gain = {quantity{0.5}, quantity{0.4}, quantity{0.1}};
|
||||
const quantity<isq::position_vector[m], int> measurements[] = {30160 * m, 30365 * m, 30890 * m, 31050 * m, 31785 * m,
|
||||
32215 * m, 33130 * m, 34510 * m, 36010 * m, 37265 * m};
|
||||
std::array gain = {0.5 * one, 0.4 * one, 0.1 * one};
|
||||
|
||||
print_header(initial);
|
||||
state next = state_extrapolation(initial, interval);
|
||||
|
@@ -52,12 +52,10 @@ int main()
|
||||
using namespace kalman;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
const estimation initial = {state{60. * isq::height[m]}, pow<2>(15. * isq::height[m])};
|
||||
const std::array measurements = {48.54 * isq::height[m], 47.11 * isq::height[m], 55.01 * isq::height[m],
|
||||
55.15 * isq::height[m], 49.89 * isq::height[m], 40.85 * isq::height[m],
|
||||
46.72 * isq::height[m], 50.05 * isq::height[m], 51.27 * isq::height[m],
|
||||
49.95 * isq::height[m]};
|
||||
const auto measurement_uncertainty = pow<2>(5. * isq::height[m]);
|
||||
const estimation initial = {state{isq::height(60. * m)}, pow<2>(isq::height(15. * m))};
|
||||
const quantity<isq::height[m]> measurements[] = {48.54 * m, 47.11 * m, 55.01 * m, 55.15 * m, 49.89 * m,
|
||||
40.85 * m, 46.72 * m, 50.05 * m, 51.27 * m, 49.95 * m};
|
||||
const auto measurement_uncertainty = pow<2>(isq::height(5. * m));
|
||||
|
||||
auto update = [=]<Quantity Q>(const estimation<Q>& previous, const Q& measurement,
|
||||
QuantityOf<dimensionless> auto gain) {
|
||||
|
@@ -131,13 +131,13 @@ void example()
|
||||
using namespace mp_units;
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
|
||||
const auto a = isq::acceleration(measurement{9.8, 0.1}, m / s2);
|
||||
const auto t = isq::time(measurement{1.2, 0.1}, s);
|
||||
const auto a = isq::acceleration(measurement{9.8, 0.1} * (m / s2));
|
||||
const auto t = isq::time(measurement{1.2, 0.1} * s);
|
||||
|
||||
const WeakQuantityOf<isq::velocity> auto v = a * t;
|
||||
const QuantityOf<isq::velocity> auto v = a * t;
|
||||
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n';
|
||||
|
||||
const auto length = isq::length(measurement{123., 1.}, si::metre);
|
||||
const auto length = isq::length(measurement{123., 1.} * m);
|
||||
std::cout << "10 * " << length << " = " << 10 * length << '\n';
|
||||
}
|
||||
|
||||
|
@@ -36,18 +36,18 @@ int main()
|
||||
|
||||
std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
|
||||
std::cout << STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n",
|
||||
si2019::hyperfine_structure_transition_frequency_of_cs(1.),
|
||||
si2019::hyperfine_structure_transition_frequency_of_cs(1.)[Hz]);
|
||||
1. * si2019::hyperfine_structure_transition_frequency_of_cs,
|
||||
(1. * si2019::hyperfine_structure_transition_frequency_of_cs)[Hz]);
|
||||
std::cout << STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n",
|
||||
si2019::speed_of_light_in_vacuum(1.), si2019::speed_of_light_in_vacuum(1.)[m / s]);
|
||||
1. * si2019::speed_of_light_in_vacuum, (1. * si2019::speed_of_light_in_vacuum)[m / s]);
|
||||
std::cout << STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n",
|
||||
si2019::planck_constant(1.), si2019::planck_constant(1.)[J * s]);
|
||||
1. * si2019::planck_constant, (1. * si2019::planck_constant)[J * s]);
|
||||
std::cout << STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n",
|
||||
si2019::elementary_charge(1.), si2019::elementary_charge(1.)[C]);
|
||||
1. * si2019::elementary_charge, (1. * si2019::elementary_charge)[C]);
|
||||
std::cout << STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n",
|
||||
si2019::boltzmann_constant(1.), si2019::boltzmann_constant(1.)[J / K]);
|
||||
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant)[J / K]);
|
||||
std::cout << STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n",
|
||||
si2019::avogadro_constant(1.), si2019::avogadro_constant(1.)[1 / mol]);
|
||||
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant)[1 / mol]);
|
||||
// TODO uncomment the below when ISQ is done
|
||||
// std::cout << STD_FMT::format("- luminous efficacy: {} = {}\n", si2019::luminous_efficacy(1.),
|
||||
// si2019::luminous_efficacy(1.)[lm / W]);
|
||||
|
@@ -20,8 +20,8 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <mp_units/math.h>
|
||||
#include <mp_units/iostream.h>
|
||||
#include <mp_units/math.h>
|
||||
#include <mp_units/systems/isq_angle/isq_angle.h>
|
||||
#include <mp_units/systems/si/unit_symbols.h>
|
||||
#include <iostream>
|
||||
@@ -38,11 +38,11 @@ int main()
|
||||
using mp_units::angular::unit_symbols::deg;
|
||||
using mp_units::angular::unit_symbols::rad;
|
||||
|
||||
const auto lever = isq_angle::position_vector[cm](20);
|
||||
const auto force = isq_angle::force[N](500);
|
||||
const auto angle = isq_angle::angular_measure[deg](90.);
|
||||
const WeakQuantityOf<isq_angle::torque> auto torque =
|
||||
lever * force * angular::sin(angle) / isq_angle::cotes_angle(1);
|
||||
const auto lever = isq_angle::position_vector(20 * cm);
|
||||
const auto force = isq_angle::force(500 * N);
|
||||
const auto angle = isq_angle::angular_measure(90. * deg);
|
||||
const auto torque =
|
||||
quantity_cast<isq_angle::torque>(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle));
|
||||
|
||||
std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in "
|
||||
<< torque[N * m / rad] << " of torque.\n";
|
||||
|
@@ -37,9 +37,8 @@ namespace {
|
||||
|
||||
using namespace mp_units;
|
||||
|
||||
QuantityOf<isq::mechanical_energy> auto total_energy(WeakQuantityOf<isq::momentum> auto p,
|
||||
WeakQuantityOf<isq::mass> auto m,
|
||||
WeakQuantityOf<isq::speed> auto c)
|
||||
QuantityOf<isq::mechanical_energy> auto total_energy(QuantityOf<isq::momentum> auto p, QuantityOf<isq::mass> auto m,
|
||||
QuantityOf<isq::speed> auto c)
|
||||
{
|
||||
return quantity_cast<isq::mechanical_energy>(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c))));
|
||||
}
|
||||
@@ -49,9 +48,9 @@ void si_example()
|
||||
using namespace mp_units::si::unit_symbols;
|
||||
constexpr auto GeV = si::giga<si::electronvolt>;
|
||||
|
||||
constexpr QuantityOf<isq::speed> auto c = si::si2019::speed_of_light_in_vacuum(1.);
|
||||
const WeakQuantityOf<isq::momentum> auto p1 = isq::mechanical_energy(4., GeV) / c;
|
||||
const WeakQuantityOf<isq::mass> auto m1 = isq::mechanical_energy(3., GeV) / pow<2>(c);
|
||||
constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;
|
||||
const QuantityOf<isq::momentum> auto p1 = isq::mechanical_energy(4. * GeV) / c;
|
||||
const QuantityOf<isq::mass> auto m1 = isq::mechanical_energy(3. * GeV) / pow<2>(c);
|
||||
const auto E = total_energy(p1, m1, c);
|
||||
|
||||
std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n";
|
||||
@@ -74,7 +73,7 @@ void si_example()
|
||||
const auto m3 = m1[kg];
|
||||
const auto E3 = total_energy(p3, m3, c)[J];
|
||||
|
||||
std::cout << "\n[in SI units]\n"
|
||||
std::cout << "\n[in SI base units]\n"
|
||||
<< "p = " << p3 << "\n"
|
||||
<< "m = " << m3 << "\n"
|
||||
<< "E = " << E3 << "\n";
|
||||
|
Reference in New Issue
Block a user