From aa74b76815b437bad4002a5e43bf1ad863d36208 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Thu, 6 Jul 2023 17:13:14 -0600 Subject: [PATCH] common: add qmat::transpose() --- include/common/qvec.hh | 11 +++++++++++ tests/test_common.cc | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/common/qvec.hh b/include/common/qvec.hh index b073463a..207be1b4 100644 --- a/include/common/qvec.hh +++ b/include/common/qvec.hh @@ -984,6 +984,17 @@ public: } return res; } + + [[nodiscard]] constexpr qmat transpose() const + { + qmat res; + for (size_t i = 0; i < NRow; i++) { + for (size_t j = 0; j < NCol; j++) { + res.at(j, i) = at(i, j); + } + } + return res; + } }; // Fmt support diff --git a/tests/test_common.cc b/tests/test_common.cc index f5c8ab09..bfefe840 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -294,3 +294,21 @@ TEST_SUITE("common") CHECK(texture->height_scale == 1); } } + +TEST_SUITE("qmat") +{ + TEST_CASE("transpose") + { + // clang-format off + auto in = qmat::row_major( + {1.0, 2.0, 3.0, + 4.0, 5.0, 6.0}); + auto exp = qmat::row_major( + {1.0, 4.0, + 2.0, 5.0, + 3.0, 6.0}); + // clang-format on + + CHECK(in.transpose() == exp); + } +}