diff --git a/src/algebra/TinyMatrix.hpp b/src/algebra/TinyMatrix.hpp index 73c187772126a478610aa2c4ee7313cc3333b41f..0d3b9c0e5fef46f85fa576b68f940708c8ed529d 100644 --- a/src/algebra/TinyMatrix.hpp +++ b/src/algebra/TinyMatrix.hpp @@ -41,6 +41,18 @@ class [[nodiscard]] TinyMatrix return M == N; } + PUGS_INLINE + constexpr friend TinyMatrix<N, M, T> transpose(const TinyMatrix& A) + { + TinyMatrix<N, M, T> tA; + for (size_t i = 0; i < M; ++i) { + for (size_t j = 0; j < N; ++j) { + tA(j, i) = A(i, j); + } + } + return tA; + } + PUGS_INLINE constexpr size_t dimension() const { diff --git a/tests/test_TinyMatrix.cpp b/tests/test_TinyMatrix.cpp index 800f8497ece648bf81e75b3cf41b3e6adb1e76a2..c9b72d5de9ecb6b047ae8c16ad832f8b29b38157 100644 --- a/tests/test_TinyMatrix.cpp +++ b/tests/test_TinyMatrix.cpp @@ -248,6 +248,18 @@ TEST_CASE("TinyMatrix", "[algebra]") REQUIRE_FALSE(TinyMatrix<3, 4>{}.isSquare()); } + SECTION("transpose") + { + TinyMatrix tA = transpose(A); + + REQUIRE(((tA(0, 0) == 1) and (tA(1, 0) == 2) and (tA(2, 0) == 3) and (tA(3, 0) == 4) and // + (tA(0, 1) == 5) and (tA(1, 1) == 6) and (tA(2, 1) == 7) and (tA(3, 1) == 8) and // + (tA(0, 2) == 9) and (tA(1, 2) == 10) and (tA(2, 2) == 11) and (tA(3, 2) == 12))); + + TinyMatrix ttA = transpose(tA); + REQUIRE(ttA == A); + } + SECTION("checking for matrices output") { REQUIRE(Catch::Detail::stringify(A) == "[(1,2,3,4)(5,6,7,8)(9,10,11,12)]");