-
-
Notifications
You must be signed in to change notification settings - Fork 119
Implement simple vector and tensor types #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jullija
wants to merge
22
commits into
mpusz:master
Choose a base branch
from
Jullija:feature/la-vectors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
edec240
add % operation and aliases
ab3c2e8
add header with vector and tensors
a4f0682
spli into 2 files
73d8554
add tests
Jullija fa57a3a
small changes
Jullija 7bc1e18
formatting
Jullija 90a371c
vector fixes after review
Jullija 26c88fe
fix tensor and vector after review
Jullija 00b2e74
fix tensor test
Jullija 59f9298
fix pipeline by adding export
1ecb38f
format change
40fb1ee
Merge branch 'master' into feature/la-vectors
Jullija 8cfa4c8
fixes after review
Jullija 5b8ffe3
Merge branch 'master' into feature/la-vectors
Jullija 7313258
little order fix
dedc039
fix style and cast
Jullija bfde546
add tensor
Jullija 84ebb5c
Merge branch 'master' into feature/la-vectors
Jullija b516aa7
remove tensors
Jullija e82ac7d
merge
Jullija 9ffc374
fix tests
Jullija 1f6e9f0
add concepts
Jullija File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mp-units/bits/module_macros.h> | ||
| #include <mp-units/bits/requires_hosted.h> | ||
| #include <mp-units/cartesian_vector.h> // for matvec/outer_numeric | ||
| #include <mp-units/framework/customization_points.h> | ||
| #include <mp-units/framework/representation_concepts.h> | ||
|
|
||
| #if MP_UNITS_HOSTED | ||
| #include <mp-units/bits/fmt.h> | ||
| #endif | ||
|
|
||
| #ifndef MP_UNITS_IN_MODULE_INTERFACE | ||
| #ifdef MP_UNITS_IMPORT_STD | ||
| import std; | ||
| #else | ||
| #include <cmath> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <type_traits> | ||
| #if MP_UNITS_HOSTED | ||
| #include <ostream> | ||
| #endif | ||
| #endif | ||
| #endif | ||
|
|
||
| namespace mp_units { | ||
|
|
||
| // Forward declaration (exported) | ||
| MP_UNITS_EXPORT template<detail::Scalar T, std::size_t R, std::size_t C> | ||
| class cartesian_tensor; | ||
|
|
||
| // ================================ tensor rep ================================ | ||
|
|
||
| MP_UNITS_EXPORT template<detail::Scalar T, std::size_t R, std::size_t C> | ||
| class cartesian_tensor { | ||
| static_assert(R >= 1 && R <= 3 && C >= 1 && C <= 3, "cartesian_tensor supports sizes up to 3x3"); | ||
|
|
||
| public: | ||
| using value_type = T; | ||
| static constexpr std::size_t rows_v = R; | ||
| static constexpr std::size_t cols_v = C; | ||
|
|
||
| T _data_[R * C]{}; | ||
|
|
||
| cartesian_tensor() = default; | ||
| cartesian_tensor(const cartesian_tensor&) = default; | ||
| cartesian_tensor(cartesian_tensor&&) = default; | ||
| cartesian_tensor& operator=(const cartesian_tensor&) = default; | ||
| cartesian_tensor& operator=(cartesian_tensor&&) = default; | ||
|
|
||
| // fill ctor (row-major R*C) | ||
| template<typename... Args> | ||
| requires(sizeof...(Args) == R * C) && (... && std::constructible_from<T, Args>) | ||
| constexpr explicit(!(... && std::convertible_to<Args, T>)) cartesian_tensor(Args&&... args) : | ||
| _data_{static_cast<T>(std::forward<Args>(args))...} | ||
| { | ||
| } | ||
|
|
||
| // element access | ||
| [[nodiscard]] constexpr T& operator()(std::size_t r, std::size_t c) { return _data_[r * C + c]; } | ||
| [[nodiscard]] constexpr const T& operator()(std::size_t r, std::size_t c) const { return _data_[r * C + c]; } | ||
|
|
||
| // elementwise +, - | ||
| template<typename U> | ||
| requires requires(const T& t, const U& u) { t + u; } | ||
| [[nodiscard]] friend constexpr auto operator+(const cartesian_tensor& A, const cartesian_tensor<U, R, C>& B) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| for (std::size_t i = 0; i < R * C; ++i) Rm._data_[i] = static_cast<CT>(A._data_[i]) + static_cast<CT>(B._data_[i]); | ||
| return Rm; | ||
| } | ||
|
|
||
| template<typename U> | ||
| requires requires(const T& t, const U& u) { t - u; } | ||
| [[nodiscard]] friend constexpr auto operator-(const cartesian_tensor& A, const cartesian_tensor<U, R, C>& B) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| for (std::size_t i = 0; i < R * C; ++i) Rm._data_[i] = static_cast<CT>(A._data_[i]) - static_cast<CT>(B._data_[i]); | ||
| return Rm; | ||
| } | ||
|
|
||
| // elementwise % (integral uses %, floating uses fmod) | ||
| template<typename U> | ||
| requires(requires(const T& t, const U& u) { t % u; }) || (std::floating_point<T> && std::floating_point<U>) | ||
| [[nodiscard]] friend constexpr auto operator%(const cartesian_tensor& A, const cartesian_tensor<U, R, C>& B) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| if constexpr (std::floating_point<T> || std::floating_point<U>) { | ||
| using std::fmod; | ||
| for (std::size_t i = 0; i < R * C; ++i) | ||
| Rm._data_[i] = | ||
| static_cast<CT>(fmod(static_cast<long double>(A._data_[i]), static_cast<long double>(B._data_[i]))); | ||
| } else { | ||
| for (std::size_t i = 0; i < R * C; ++i) Rm._data_[i] = static_cast<CT>(A._data_[i] % B._data_[i]); | ||
| } | ||
| return Rm; | ||
| } | ||
|
|
||
| // scalar *, / (constrained to numeric scalars to avoid recursive constraints) | ||
| template<detail::Scalar S> | ||
| [[nodiscard]] friend constexpr auto operator*(const cartesian_tensor& tensor, const S& scalar) | ||
| { | ||
| using CT = std::common_type_t<T, S>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| for (std::size_t i = 0; i < R * C; ++i) Rm._data_[i] = static_cast<CT>(tensor._data_[i]) * static_cast<CT>(scalar); | ||
| return Rm; | ||
| } | ||
|
|
||
| template<detail::Scalar S> | ||
| [[nodiscard]] friend constexpr auto operator*(const S& scalar, const cartesian_tensor& tensor) | ||
| { | ||
| return tensor * scalar; | ||
| } | ||
|
|
||
| template<detail::Scalar S> | ||
| [[nodiscard]] friend constexpr auto operator/(const cartesian_tensor& tensor, const S& scalar) | ||
| { | ||
| using CT = std::common_type_t<T, S>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| for (std::size_t i = 0; i < R * C; ++i) Rm._data_[i] = static_cast<CT>(tensor._data_[i]) / static_cast<CT>(scalar); | ||
| return Rm; | ||
| } | ||
|
|
||
| #if MP_UNITS_HOSTED | ||
| friend std::ostream& operator<<(std::ostream& os, const cartesian_tensor& A) | ||
| { | ||
| for (std::size_t r = 0; r < R; ++r) { | ||
| os << (r == 0 ? "[[" : " ["); | ||
| for (std::size_t c = 0; c < C; ++c) { | ||
| os << A(r, c); | ||
| if (c + 1 != C) os << ", "; | ||
| } | ||
| os << (r + 1 == R ? "]]" : "]\n"); | ||
| } | ||
| return os; | ||
| } | ||
| #endif | ||
| }; | ||
|
|
||
| // Register as tensor rep | ||
| template<detail::Scalar T, std::size_t R, std::size_t C> | ||
| inline constexpr bool is_tensor<cartesian_tensor<T, R, C>> = true; | ||
|
|
||
| // ======================== numeric helpers (no units) ======================== | ||
|
|
||
| // Matrix × Matrix | ||
| template<typename T, typename U, std::size_t R, std::size_t K, std::size_t C> | ||
| [[nodiscard]] constexpr auto matmul(const cartesian_tensor<T, R, K>& A, const cartesian_tensor<U, K, C>& B) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_tensor<CT, R, C> Rm{}; | ||
| for (std::size_t r = 0; r < R; ++r) | ||
| for (std::size_t c = 0; c < C; ++c) { | ||
| CT acc{}; | ||
| for (std::size_t k = 0; k < K; ++k) acc += static_cast<CT>(A(r, k)) * static_cast<CT>(B(k, c)); | ||
| Rm(r, c) = acc; | ||
| } | ||
| return Rm; | ||
| } | ||
|
|
||
| // Matrix × Vector (3×3) | ||
| template<typename T, typename U> | ||
| [[nodiscard]] constexpr auto matvec(const cartesian_tensor<T, 3, 3>& M, const cartesian_vector<U>& x) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_vector<CT> y{}; | ||
| for (std::size_t r = 0; r < 3; ++r) { | ||
| CT acc{}; | ||
| for (std::size_t c = 0; c < 3; ++c) acc += static_cast<CT>(M(r, c)) * static_cast<CT>(x[c]); | ||
| y[r] = acc; | ||
| } | ||
| return y; | ||
| } | ||
|
|
||
| // Double contraction: A : B | ||
| template<typename T, typename U, std::size_t R, std::size_t C> | ||
| [[nodiscard]] constexpr auto double_contraction(const cartesian_tensor<T, R, C>& A, const cartesian_tensor<U, R, C>& B) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| CT acc{}; | ||
| for (std::size_t i = 0; i < R * C; ++i) acc += static_cast<CT>(A._data_[i]) * static_cast<CT>(B._data_[i]); | ||
| return acc; // numeric scalar | ||
| } | ||
|
|
||
| // Outer product: vector ⊗ vector -> 3x3 matrix | ||
| template<typename T, typename U> | ||
| [[nodiscard]] constexpr auto outer_numeric(const cartesian_vector<T>& a, const cartesian_vector<U>& b) | ||
| { | ||
| using CT = std::common_type_t<T, U>; | ||
| cartesian_tensor<CT, 3, 3> Rm{}; | ||
| for (std::size_t i = 0; i < 3; ++i) | ||
| for (std::size_t j = 0; j < 3; ++j) Rm(i, j) = static_cast<CT>(a[i]) * static_cast<CT>(b[j]); | ||
| return Rm; | ||
| } | ||
|
|
||
| } // namespace mp_units | ||
|
|
||
| #if MP_UNITS_HOSTED | ||
| // fmt/format (or std::format) support | ||
| template<typename T, std::size_t R, std::size_t C, typename Char> | ||
| struct MP_UNITS_STD_FMT::formatter<mp_units::cartesian_tensor<T, R, C>, Char> : | ||
| formatter<std::basic_string_view<Char>, Char> { | ||
| template<typename Ctx> | ||
| auto format(const mp_units::cartesian_tensor<T, R, C>& A, Ctx& ctx) const | ||
| { | ||
| auto out = ctx.out(); | ||
| for (std::size_t r = 0; r < R; ++r) { | ||
| out = format_to(out, "{}", (r == 0 ? "[[" : " [")); | ||
| for (std::size_t c = 0; c < C; ++c) { | ||
| out = format_to(out, "{}", A(r, c)); | ||
| if (c + 1 != C) out = format_to(out, "{}", ", "); | ||
| } | ||
| out = format_to(out, "{}", (r + 1 == R ? "]]" : "]\n")); | ||
| } | ||
| return out; | ||
| } | ||
| }; | ||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.