Skip to content
Snippets Groups Projects
Commit 7af7eef4 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add transpose function to TinyMatrix

parent 18db88ed
No related branches found
No related tags found
1 merge request!113Add transpose function to TinyMatrix
...@@ -41,6 +41,18 @@ class [[nodiscard]] TinyMatrix ...@@ -41,6 +41,18 @@ class [[nodiscard]] TinyMatrix
return M == N; 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 PUGS_INLINE
constexpr size_t dimension() const constexpr size_t dimension() const
{ {
......
...@@ -248,6 +248,18 @@ TEST_CASE("TinyMatrix", "[algebra]") ...@@ -248,6 +248,18 @@ TEST_CASE("TinyMatrix", "[algebra]")
REQUIRE_FALSE(TinyMatrix<3, 4>{}.isSquare()); 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") SECTION("checking for matrices output")
{ {
REQUIRE(Catch::Detail::stringify(A) == "[(1,2,3,4)(5,6,7,8)(9,10,11,12)]"); REQUIRE(Catch::Detail::stringify(A) == "[(1,2,3,4)(5,6,7,8)(9,10,11,12)]");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment