// 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 // linear_algebra.hpp has to be included first otherwise the header will fail to compile! #include #include #include #include #include #include #include #include namespace STD_LA { template std::ostream& operator<<(std::ostream& os, const vector& v) { os << "|"; for (auto i = 0U; i < v.size(); ++i) { os << STD_FMT::format(" {:>9}", v(i)); } os << " |"; return os; } template std::ostream& operator<<(std::ostream& os, const matrix& v) { for (auto i = 0U; i < v.rows(); ++i) { os << "|"; for (auto j = 0U; j < v.columns(); ++j) { os << STD_FMT::format(" {:>9}", v(i, j)); } os << (i != v.rows() - 1U ? " |\n" : " |"); } return os; } } // namespace STD_LA using namespace mp_units; using namespace mp_units::si::unit_symbols; template using vector = std::math::fs_vector; template inline constexpr bool mp_units::is_vector> = true; namespace { template [[nodiscard]] auto get_magnitude(const vector& v) { return std::hypot(v[0], v[1], v[2]); } template [[nodiscard]] 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]] quantity_of auto cross_product(const Q1& q1, const Q2& q2) { return (Q1::reference * Q2::reference)(cross_product(q1.number(), q2.number())); } } // namespace TEST_CASE("vector quantity", "[la]") { SECTION("cast of unit") { SECTION("non-truncating") { const quantity> v{vector{3, 2, 1}}; CHECK(v[m].number() == vector{3000, 2000, 1000}); } SECTION("truncating") { const quantity> v{vector{1001, 1002, 1003}}; CHECK(value_cast(v).number() == vector{1, 1, 1}); } } SECTION("to scalar magnitude") { const quantity> v{vector{2, 3, 6}}; const auto speed = get_magnitude(v.number()) * isq::speed[v.unit]; // TODO can we do better here? CHECK(speed.number() == 7); } SECTION("multiply by scalar value") { const quantity> v{vector{1, 2, 3}}; SECTION("integral") { SECTION("scalar on LHS") { CHECK((2 * v).number() == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK((v * 2).number() == vector{2, 4, 6}); } } SECTION("floating-point") { SECTION("scalar on LHS") { CHECK((0.5 * v).number() == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK((v * 0.5).number() == vector{0.5, 1., 1.5}); } } } SECTION("divide by scalar value") { const quantity> v{vector{2, 4, 6}}; SECTION("integral") { CHECK((v / 2).number() == vector{1, 2, 3}); } SECTION("floating-point") { CHECK((v / 0.5).number() == vector{4., 8., 12.}); } } SECTION("add") { const quantity> v{vector{1, 2, 3}}; SECTION("same unit") { const quantity> u{vector{3, 2, 1}}; CHECK((v + u).number() == vector{4, 4, 4}); } SECTION("different units") { const quantity> u{vector{3, 2, 1}}; CHECK((v + u).number() == vector{3001, 2002, 1003}); } } SECTION("subtract") { const quantity> v{vector{1, 2, 3}}; SECTION("same unit") { const quantity> u{vector{3, 2, 1}}; CHECK((v - u).number() == vector{-2, 0, 2}); } SECTION("different units") { const quantity> u{vector{3, 2, 1}}; CHECK((v - u).number() == vector{-2999, -1998, -997}); } } SECTION("multiply by scalar quantity") { const quantity> v{vector{1, 2, 3}}; SECTION("integral") { const auto mass = 2 * isq::mass[kg]; SECTION("derived_quantity_spec") { SECTION("scalar on LHS") { CHECK((mass * v).number() == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK((v * mass).number() == vector{2, 4, 6}); } } SECTION("quantity_cast to momentum") { SECTION("scalar on LHS") { CHECK(quantity_cast(mass * v).number() == vector{2, 4, 6}); } SECTION("scalar on RHS") { CHECK(quantity_cast(v * mass).number() == vector{2, 4, 6}); } } SECTION("quantity of momentum") { SECTION("scalar on LHS") { const quantity> momentum = mass * v; CHECK(momentum.number() == vector{2, 4, 6}); } SECTION("scalar on RHS") { const quantity> momentum = v * mass; CHECK(momentum.number() == 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).number() == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK((v * mass).number() == vector{0.5, 1., 1.5}); } } SECTION("quantity_cast to momentum") { SECTION("scalar on LHS") { CHECK(quantity_cast(mass * v).number() == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { CHECK(quantity_cast(v * mass).number() == vector{0.5, 1., 1.5}); } } SECTION("quantity of momentum") { SECTION("scalar on LHS") { const quantity> momentum = mass * v; CHECK(momentum.number() == vector{0.5, 1., 1.5}); } SECTION("scalar on RHS") { const quantity> momentum = v * mass; CHECK(momentum.number() == vector{0.5, 1., 1.5}); } } } } SECTION("divide by scalar quantity") { const quantity> pos{vector{30, 20, 10}}; SECTION("integral") { const auto dur = 2 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK((pos / dur).number() == vector{15, 10, 5}); } SECTION("quantity_cast to velocity") { CHECK(quantity_cast(pos / dur).number() == vector{15, 10, 5}); } SECTION("quantity of velocity") { const quantity> v = pos / dur; CHECK(v.number() == vector{15, 10, 5}); } } SECTION("floating-point") { const auto dur = 0.5 * isq::duration[h]; SECTION("derived_quantity_spec") { CHECK((pos / dur).number() == vector{60, 40, 20}); } SECTION("quantity_cast to velocity") { CHECK(quantity_cast(pos / dur).number() == vector{60, 40, 20}); } SECTION("quantity of velocity") { const quantity> v = pos / dur; CHECK(v.number() == vector{60, 40, 20}); } } } SECTION("cross product with a vector quantity") { const quantity> r{vector{3, 0, 0}}; const quantity> f{vector{0, 10, 0}}; CHECK(cross_product(r, f) == isq::moment_of_force[N * m](vector{0, 0, 30})); } }