From 9afcf9e695437b21901b13fcadd311b7d7c9e40a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 25 Mar 2020 15:57:53 +0100 Subject: [PATCH] Linear Algebra support added --- conanfile.py | 1 + docs/CHANGELOG.md | 1 + docs/CMakeLists.txt | 1 + docs/examples.rst | 1 + docs/examples/linear_algebra.rst | 7 ++ example/CMakeLists.txt | 4 + example/linear_algebra.cpp | 181 +++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+) create mode 100644 docs/examples/linear_algebra.rst create mode 100644 example/linear_algebra.cpp diff --git a/conanfile.py b/conanfile.py index f296cdc0..b78c0480 100644 --- a/conanfile.py +++ b/conanfile.py @@ -89,6 +89,7 @@ class UnitsConan(ConanFile): self.build_requires("Catch2/2.11.0@catchorg/stable") # TODO update doxygen to the latest version when available self.build_requires("doxygen_installer/1.8.17@bincrafters/stable") + self.build_requires("linear_algebra/0.6.0@public-conan/testing") def build(self): cmake = self._configure_cmake() diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 74b87eda..a4d756c9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -15,6 +15,7 @@ - `unknown_unit` renamed to `unknown_coherent_unit` - Project documentation extended and switched to Sphinx - A few more usage examples added + - Linear algebra from `std::experimental::math` support added - ... Many thanks to GitHub users @oschonrock and @kwikius for their support in drafting diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 0e7b80c7..da42686c 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -74,6 +74,7 @@ add_custom_command(OUTPUT "${SPHINX_INDEX_FILE}" "${CMAKE_CURRENT_SOURCE_DIR}/examples.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/hello_units.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/avg_speed.rst" + "${CMAKE_CURRENT_SOURCE_DIR}/examples/linear_algebra.rst" "${CMAKE_CURRENT_SOURCE_DIR}/examples/measurement.rst" "${CMAKE_CURRENT_SOURCE_DIR}/faq.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework.rst" diff --git a/docs/examples.rst b/docs/examples.rst index 6e0f8a17..b42f5819 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -7,3 +7,4 @@ Examples examples/hello_units examples/avg_speed examples/measurement + examples/linear_algebra diff --git a/docs/examples/linear_algebra.rst b/docs/examples/linear_algebra.rst new file mode 100644 index 00000000..21bef28b --- /dev/null +++ b/docs/examples/linear_algebra.rst @@ -0,0 +1,7 @@ +linear_algebra +============== + +.. literalinclude:: ../../example/linear_algebra.cpp + :caption: linear_algebra.cpp + :start-at: #include + :linenos: diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 350c8eb0..93f23776 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -36,4 +36,8 @@ add_example(clcpp_response) add_example(conversion_factor) add_example(kalman_filter-alpha_beta_filter_example2) +conan_check_testing(linear_algebra) +add_example(linear_algebra) +target_link_libraries(linear_algebra PRIVATE CONAN_PKG::linear_algebra) + add_subdirectory(alternative_namespaces) diff --git a/example/linear_algebra.cpp b/example/linear_algebra.cpp new file mode 100644 index 00000000..a5f57738 --- /dev/null +++ b/example/linear_algebra.cpp @@ -0,0 +1,181 @@ +// 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 +#include +#include +#include + +namespace { + +template +std::ostream& operator<<(std::ostream& os, const std::experimental::math::vector& v) +{ + os << "|"; + for (size_t i = 0; i < v.size(); ++i) { + os << fmt::format(" {:>9}", v(i)); + } + os << " |"; + return os; +} + +template +std::ostream& operator<<(std::ostream& os, const std::experimental::math::matrix& v) +{ + for (size_t i = 0; i < v.rows(); ++i) { + os << "|"; + for (size_t j = 0; j < v.columns(); ++j) { + os << fmt::format(" {:>9}", v(i, j)); + } + os << (i != v.rows() - 1 ? " |\n" : " |"); + } + return os; +} + + +using namespace std::experimental::math; +using namespace units; +using namespace si::literals; + +void vector_of_quantity_add() +{ + std::cout << "\nvector_of_quantity_add:\n"; + + fs_vector, 3> v = { 1q_m, 2q_m, 3q_m }; + fs_vector, 3> u = { 3q_m, 2q_m, 1q_m }; + fs_vector, 3> t = { 3q_km, 2q_km, 1q_km }; + + auto x = v + u; + std::cout << "x = " << x << "\n"; + + auto y = v + t; + std::cout << "y = " << y << "\n"; + + fs_vector, 3> z = t; + std::cout << "z = " << z << "\n"; +} + +void vector_of_quantity_multiply_same() +{ + std::cout << "\nvector_of_quantity_multiply_same:\n"; + + fs_vector, 3> v = { 1q_m, 2q_m, 3q_m }; + fs_vector, 3> u = { 3q_m, 2q_m, 1q_m }; + + auto x = v * u; + std::cout << "x = " << x << "\n"; + + auto y = 2q_m * v; + std::cout << "y = " << y << "\n"; +} + +void vector_of_quantity_multiply_different() +{ + std::cout << "\nvector_of_quantity_multiply_different:\n"; + + fs_vector, 3> v = { 1q_N, 2q_N, 3q_N }; + fs_vector, 3> u = { 3q_m, 2q_m, 1q_m }; + + auto x = v * u; + std::cout << "x = " << x << "\n"; + + auto y = 2q_N * u; + std::cout << "y = " << y << "\n"; + + auto z = 2 * u; + std::cout << "z = " << z << "\n"; +} + +void vector_of_quantity_tests() +{ + vector_of_quantity_add(); + vector_of_quantity_multiply_same(); + vector_of_quantity_multiply_different(); +} + +void matrix_of_quantity_add() +{ + std::cout << "\nmatrix_of_quantity_add:\n"; + + fs_matrix, 3, 3> v = {{ 1q_m, 2q_m, 3q_m }, { 4q_m, 5q_m, 6q_m }, { 7q_m, 8q_m, 9q_m }}; + fs_matrix, 3, 3> u = {{ 3q_m, 2q_m, 1q_m }, { 3q_m, 2q_m, 1q_m }, { 3q_m, 2q_m, 1q_m }}; + fs_matrix, 3, 3> t = {{ 3q_mm, 2q_mm, 1q_mm }, { 3q_mm, 2q_mm, 1q_mm }, { 3q_mm, 2q_mm, 1q_mm }}; + + auto x = v + u; + std::cout << "x =\n" << x << "\n"; + + auto y = v + t; + std::cout << "y =\n" << y << "\n"; + + // TODO Uncomment when fixed in the LA lib + // fs_matrix, 3, 3> z = v; + // std::cout << "z =\n" << z << "\n"; +} + +void matrix_of_quantity_multiply_same() +{ + std::cout << "\nmatrix_of_quantity_multiply_same:\n"; + + fs_matrix, 3, 3> v = {{ 1q_m, 2q_m, 3q_m }, { 4q_m, 5q_m, 6q_m }, { 7q_m, 8q_m, 9q_m }}; + fs_vector, 3> u = { 3q_m, 2q_m, 1q_m }; + + auto x = v * u; + std::cout << "x =\n" << x << "\n"; + + auto y = 2q_m * u; + std::cout << "y =\n" << y << "\n"; + +} + +void matrix_of_quantity_multiply_different() +{ + std::cout << "\nmatrix_of_quantity_multiply_different:\n"; + + fs_vector, 3> v = { 1q_N, 2q_N, 3q_N }; + fs_matrix, 3, 3> u = {{ 1q_m, 2q_m, 3q_m }, { 4q_m, 5q_m, 6q_m }, { 7q_m, 8q_m, 9q_m }}; + + auto x = v * u; + std::cout << "x =\n" << x << "\n"; + + auto y = 2q_N * u; + std::cout << "y =\n" << y << "\n"; + + auto z = 2 * u; + std::cout << "z =\n" << z << "\n"; +} + +void matrix_of_quantity_tests() +{ + matrix_of_quantity_add(); + matrix_of_quantity_multiply_same(); + matrix_of_quantity_multiply_different(); +} + +} + +int main() +{ + vector_of_quantity_tests(); + matrix_of_quantity_tests(); +}