// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include #include #include #ifdef MP_UNITS_IMPORT_STD import std; #else #include #include #endif #ifdef MP_UNITS_MODULES import mp_units; #else #include #include #include // IWYU pragma: keep #include #include #include #endif template using vector = STD_LA::fixed_size_column_vector; template constexpr bool mp_units::treat_as_floating_point> = mp_units::treat_as_floating_point; template constexpr bool mp_units::is_vector> = true; template std::ostream& operator<<(std::ostream& os, const vector& v) { os << "|"; for (auto i = 0U; i < v.size(); ++i) { os << MP_UNITS_STD_FMT::format(" {:>9}", v(i)); } os << " |"; return os; } namespace { using namespace mp_units; using namespace mp_units::si::unit_symbols; template Q> requires(Q::quantity_spec.character == quantity_character::vector) && (QS.character == quantity_character::scalar) [[nodiscard]] constexpr QuantityOf auto get_magnitude(const Q& q) { const auto& v = q.numerical_value_ref_in(q.unit); return hypot(v(0) * QS[Q::unit], v(1) * QS[Q::unit], v(2) * QS[Q::unit]); } template T> requires(T::quantity_spec.character == quantity_character::vector) && (QS.character == quantity_character::scalar) [[nodiscard]] constexpr QuantityOf auto get_magnitude(const vector& v) { return hypot(QS(v(0)), QS(v(1)), QS(v(2))); } template [[nodiscard]] constexpr vector cross_product(const vector& a, const vector& b) { return {a(1) * b(2) - a(2) * b(1), a(2) * b(0) - a(0) * b(2), a(0) * b(1) - a(1) * b(0)}; } template requires is_vector && is_vector && requires(typename Q1::rep v1, typename Q2::rep v2) { cross_product(v1, v2); } [[nodiscard]] constexpr QuantityOf auto cross_product(const Q1& q1, const Q2& q2) { return cross_product(q1.numerical_value_ref_in(q1.unit), q2.numerical_value_ref_in(q2.unit)) * (Q1::reference * Q2::reference); } } // namespace TEST_CASE("vector quantity", "[la]") { SECTION("cast of unit") { SECTION("non-truncating") { const auto v = vector{3, 2, 1} * isq::position_vector[km]; CHECK(v.numerical_value_in(m) == vector{3000, 2000, 1000}); } SECTION("truncating") { const auto v = vector{1001, 1002, 1003} * isq::position_vector[m]; CHECK(v.force_numerical_value_in(km) == vector{1, 1, 1}); } } SECTION("to scalar magnitude") { const auto v = vector{2, 3, 6} * isq::velocity[km / h]; const auto speed = get_magnitude(v); CHECK(speed.numerical_value_ref_in(km / h) == 7); } SECTION("multiply by scalar value") { const auto v = vector{1, 2, 3} * isq::position_vector[m]; SECTION("integral") { SECTION("scalar on LHS") { CHECK((2 * v).numerical_value_in(m) == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK((v * 2).numerical_value_in(m) == vector{2, 4, 6}); } } SECTION("floating-point") { SECTION("scalar on LHS") { CHECK((0.5 * v).numerical_value_in(m) == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK((v * 0.5).numerical_value_in(m) == vector{0.5, 1., 1.5}); } } } SECTION("divide by scalar value") { const auto v = vector{2, 4, 6} * isq::position_vector[m]; SECTION("integral") { CHECK((v / 2).numerical_value_in(m) == vector{1, 2, 3}); } SECTION("floating-point") { CHECK((v / 0.5).numerical_value_in(m) == vector{4., 8., 12.}); } } SECTION("add") { const auto v = vector{1, 2, 3} * isq::position_vector[m]; SECTION("same unit") { const auto u = vector{3, 2, 1} * isq::position_vector[m]; CHECK((v + u).numerical_value_in(m) == vector{4, 4, 4}); } SECTION("different units") { const auto u = vector{3, 2, 1} * isq::position_vector[km]; CHECK((v + u).numerical_value_in(m) == vector{3001, 2002, 1003}); } } SECTION("subtract") { const auto v = vector{1, 2, 3} * isq::position_vector[m]; SECTION("same unit") { const auto u = vector{3, 2, 1} * isq::position_vector[m]; CHECK((v - u).numerical_value_in(m) == vector{-2, 0, 2}); } SECTION("different units") { const auto u = vector{3, 2, 1} * isq::position_vector[km]; CHECK((v - u).numerical_value_in(m) == vector{-2999, -1998, -997}); } } SECTION("multiply by scalar quantity") { const auto v = vector{1, 2, 3} * isq::velocity[m / s]; SECTION("integral") { const auto mass = 2 * isq::mass[kg]; SECTION("derived_quantity_spec") { SECTION("scalar on LHS") { CHECK((mass * v).numerical_value_in(kg * m / s) == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK((v * mass).numerical_value_in(kg * m / s) == vector{2, 4, 6}); } } SECTION("quantity_cast to momentum") { SECTION("scalar on LHS") { CHECK(quantity_cast(mass * v).numerical_value_in(N * s) == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK(quantity_cast(v * mass).numerical_value_in(N * s) == vector{2, 4, 6}); } } SECTION("quantity of momentum") { SECTION("scalar on LHS") { const quantity> momentum = mass * v; CHECK(momentum.numerical_value_ref_in(N * s) == vector{2, 4, 6}); } SECTION("scalar on RHS") { const quantity> momentum = v * mass; CHECK(momentum.numerical_value_ref_in(N * s) == vector{2, 4, 6}); } } } SECTION("floating-point") { const auto mass = 0.5 * isq::mass[kg]; SECTION("derived_quantity_spec") { SECTION("scalar on LHS") { CHECK((mass * v).numerical_value_in(kg * m / s) == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK((v * mass).numerical_value_in(kg * m / s) == vector{0.5, 1., 1.5}); } } SECTION("quantity_cast to momentum") { SECTION("scalar on LHS") { CHECK(quantity_cast(mass * v).numerical_value_in(N * s) == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK(quantity_cast(v * mass).numerical_value_in(N * s) == vector{0.5, 1., 1.5}); } } SECTION("quantity of momentum") { SECTION("scalar on LHS") { const quantity> momentum = mass * v; CHECK(momentum.numerical_value_ref_in(N * s) == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { const quantity> momentum = v * mass; CHECK(momentum.numerical_value_ref_in(N * s) == vector{0.5, 1., 1.5}); } } } } SECTION("divide by scalar quantity") { const auto pos = vector{30, 20, 10} * isq::position_vector[km]; SECTION("integral") { const auto dur = 2 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK((pos / dur).numerical_value_in(km / h) == vector{15, 10, 5}); } SECTION("quantity_cast to velocity") { CHECK(quantity_cast(pos / dur).numerical_value_in(km / h) == vector{15, 10, 5}); } SECTION("quantity of velocity") { const quantity> v = pos / dur; CHECK(v.numerical_value_ref_in(km / h) == vector{15, 10, 5}); } } SECTION("floating-point") { const auto dur = 0.5 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK((pos / dur).numerical_value_in(km / h) == vector{60, 40, 20}); } SECTION("quantity_cast to velocity") { CHECK(quantity_cast(pos / dur).numerical_value_in(km / h) == vector{60, 40, 20}); } SECTION("quantity of velocity") { const quantity> v = pos / dur; CHECK(v.numerical_value_ref_in(km / h) == vector{60, 40, 20}); } } } SECTION("cross product with a vector quantity") { const auto r = vector{3, 0, 0} * isq::position_vector[m]; const auto f = vector{0, 10, 0} * isq::force[N]; CHECK(cross_product(r, f) == vector{0, 0, 30} * isq::moment_of_force[N * m]); } } template requires mp_units::is_scalar constexpr bool mp_units::is_vector = true; TEST_CASE("vector of quantities", "[la]") { SECTION("cast of unit") { SECTION("non-truncating") { const vector> v = {3 * km, 2 * km, 1 * km}; CHECK(vector>(v) == vector>{3000 * m, 2000 * m, 1000 * m}); } // truncating not possible (no way to apply quantity_cast to sub-components of a vector) } SECTION("to scalar magnitude") { const vector> v = {2 * km / h, 3 * km / h, 6 * km / h}; const auto speed = get_magnitude(v); CHECK(speed.numerical_value_ref_in(km / h) == 7); } SECTION("multiply by scalar value") { const vector> v = {1 * m, 2 * m, 3 * m}; SECTION("integral") { const vector> result = {2 * m, 4 * m, 6 * m}; SECTION("scalar on LHS") { CHECK(2 * v == result); } SECTION("scalar on RHS") { CHECK(v * 2 == result); } } SECTION("floating-point") { const vector> result = {0.5 * m, 1. * m, 1.5 * m}; SECTION("scalar on LHS") { CHECK(0.5 * v == result); } SECTION("scalar on RHS") { CHECK(v * 0.5 == result); } } } SECTION("divide by scalar value") { const vector> v = {2 * m, 4 * m, 6 * m}; SECTION("integral") { CHECK(v / 2 == vector>{1 * m, 2 * m, 3 * m}); } SECTION("floating-point") { CHECK(v / 0.5 == vector>{4. * m, 8. * m, 12. * m}); } } SECTION("add") { const vector> v = {1 * m, 2 * m, 3 * m}; SECTION("same unit") { const vector> u = {3 * m, 2 * m, 1 * m}; CHECK(v + u == vector>{4 * m, 4 * m, 4 * m}); } SECTION("different units") { const vector> u = {3 * km, 2 * km, 1 * km}; CHECK(v + u == vector>{3001 * m, 2002 * m, 1003 * m}); } } SECTION("subtract") { const vector> v = {1 * m, 2 * m, 3 * m}; SECTION("same unit") { const vector> u = {3 * m, 2 * m, 1 * m}; CHECK(v - u == vector>{-2 * m, 0 * m, 2 * m}); } SECTION("different units") { const vector> u = {3 * km, 2 * km, 1 * km}; CHECK(v - u == vector>{-2999 * m, -1998 * m, -997 * m}); } } SECTION("multiply by scalar quantity") { const vector> v = {1 * m / s, 2 * m / s, 3 * m / s}; SECTION("integral") { const auto mass = 2 * isq::mass[kg]; const auto result = vector>{2 * N * s, 4 * N * s, 6 * N * s}; SECTION("derived_quantity_spec") { SECTION("scalar on LHS") { CHECK(mass * v == result); } SECTION("scalar on RHS") { CHECK(v * mass == result); } } // no way to apply quantity_cast to sub-components SECTION("quantity of momentum") { SECTION("scalar on LHS") { const vector> momentum = mass * v; CHECK(momentum == result); } SECTION("scalar on RHS") { const vector> momentum = v * mass; CHECK(momentum == result); } } } SECTION("floating-point") { const auto mass = 0.5 * isq::mass[kg]; const auto result = vector>{0.5 * N * s, 1. * N * s, 1.5 * N * s}; SECTION("derived_quantity_spec") { SECTION("scalar on LHS") { CHECK(mass * v == result); } SECTION("scalar on RHS") { CHECK(v * mass == result); } } // no way to apply quantity_cast to sub-components SECTION("quantity of momentum") { SECTION("scalar on LHS") { const vector> momentum = mass * v; CHECK(momentum == result); } SECTION("scalar on RHS") { const vector> momentum = v * mass; CHECK(momentum == result); } } } } SECTION("divide by scalar quantity") { const vector> pos = {30 * km, 20 * km, 10 * km}; SECTION("integral") { const auto dur = 2 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK(pos / dur == vector>{15 * km / h, 10 * km / h, 5 * km / h}); } // no way to apply quantity_cast to sub-components SECTION("quantity of velocity") { const vector> v = pos / dur; CHECK(v == vector>{15 * km / h, 10 * km / h, 5 * km / h}); } } SECTION("floating-point") { const auto dur = 0.5 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK(pos / dur == vector>{60. * km / h, 40. * km / h, 20. * km / h}); } // no way to apply quantity_cast to sub-components SECTION("quantity of velocity") { const vector> v = pos / dur; CHECK(v == vector>{60. * km / h, 40. * km / h, 20. * km / h}); } } } SECTION("cross product with a vector of quantities") { const vector> r = {3 * m, 0 * m, 0 * m}; const vector> f = {0 * N, 10 * N, 0 * N}; CHECK(cross_product(r, f) == vector>{0 * N * m, 0 * N * m, 30 * N * m}); } }