Matrix.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <initializer_list>
  9. namespace Gfx {
  10. template<size_t N, typename T>
  11. class Matrix {
  12. template<size_t U, typename V>
  13. friend class Matrix;
  14. public:
  15. static constexpr size_t Size = N;
  16. constexpr Matrix() = default;
  17. constexpr Matrix(std::initializer_list<T> elements)
  18. {
  19. VERIFY(elements.size() == N * N);
  20. size_t i = 0;
  21. for (auto& element : elements) {
  22. m_elements[i / N][i % N] = element;
  23. ++i;
  24. }
  25. }
  26. template<typename... Args>
  27. constexpr Matrix(Args... args)
  28. : Matrix({ (T)args... })
  29. {
  30. }
  31. constexpr Matrix(Matrix const& other)
  32. {
  33. *this = other;
  34. }
  35. constexpr Matrix& operator=(Matrix const& other)
  36. {
  37. #ifndef __clang__
  38. if (is_constant_evaluated()) {
  39. for (size_t i = 0; i < N; i++) {
  40. for (size_t j = 0; j < N; j++) {
  41. m_elements[i][j] = other.elements()[i][j];
  42. }
  43. }
  44. return *this;
  45. }
  46. #endif
  47. __builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
  48. return *this;
  49. }
  50. constexpr auto elements() const { return m_elements; }
  51. constexpr auto elements() { return m_elements; }
  52. // FIXME: Change to multi-arg operator[] once we upgrade to C++23
  53. constexpr auto const& operator()(size_t row, size_t col) const { return m_elements[row][col]; }
  54. constexpr auto& operator()(size_t row, size_t col) { return m_elements[row][col]; }
  55. [[nodiscard]] constexpr Matrix operator*(Matrix const& other) const
  56. {
  57. Matrix product;
  58. for (size_t i = 0; i < N; ++i) {
  59. for (size_t j = 0; j < N; ++j) {
  60. auto& element = product.m_elements[i][j];
  61. if constexpr (N == 4) {
  62. element = m_elements[i][0] * other.m_elements[0][j]
  63. + m_elements[i][1] * other.m_elements[1][j]
  64. + m_elements[i][2] * other.m_elements[2][j]
  65. + m_elements[i][3] * other.m_elements[3][j];
  66. } else if constexpr (N == 3) {
  67. element = m_elements[i][0] * other.m_elements[0][j]
  68. + m_elements[i][1] * other.m_elements[1][j]
  69. + m_elements[i][2] * other.m_elements[2][j];
  70. } else if constexpr (N == 2) {
  71. element = m_elements[i][0] * other.m_elements[0][j]
  72. + m_elements[i][1] * other.m_elements[1][j];
  73. } else if constexpr (N == 1) {
  74. element = m_elements[i][0] * other.m_elements[0][j];
  75. } else {
  76. T value {};
  77. for (size_t k = 0; k < N; ++k)
  78. value += m_elements[i][k] * other.m_elements[k][j];
  79. element = value;
  80. }
  81. }
  82. }
  83. return product;
  84. }
  85. [[nodiscard]] constexpr Matrix operator+(Matrix const& other) const
  86. {
  87. Matrix sum;
  88. for (size_t i = 0; i < N; ++i) {
  89. for (size_t j = 0; j < N; ++j)
  90. sum.m_elements[i][j] = m_elements[i][j] + other.m_elements[i][j];
  91. }
  92. return sum;
  93. }
  94. [[nodiscard]] constexpr Matrix operator/(T divisor) const
  95. {
  96. Matrix division;
  97. for (size_t i = 0; i < N; ++i) {
  98. for (size_t j = 0; j < N; ++j)
  99. division.m_elements[i][j] = m_elements[i][j] / divisor;
  100. }
  101. return division;
  102. }
  103. [[nodiscard]] friend constexpr Matrix operator*(Matrix const& matrix, T scalar)
  104. {
  105. Matrix scaled;
  106. for (size_t i = 0; i < N; ++i) {
  107. for (size_t j = 0; j < N; ++j)
  108. scaled.m_elements[i][j] = matrix.m_elements[i][j] * scalar;
  109. }
  110. return scaled;
  111. }
  112. [[nodiscard]] friend constexpr Matrix operator*(T scalar, Matrix const& matrix)
  113. {
  114. return matrix * scalar;
  115. }
  116. [[nodiscard]] constexpr Matrix adjugate() const
  117. {
  118. if constexpr (N == 1)
  119. return Matrix(1);
  120. Matrix adjugate;
  121. for (size_t i = 0; i < N; ++i) {
  122. for (size_t j = 0; j < N; ++j) {
  123. int sign = (i + j) % 2 == 0 ? 1 : -1;
  124. adjugate.m_elements[j][i] = sign * first_minor(i, j);
  125. }
  126. }
  127. return adjugate;
  128. }
  129. [[nodiscard]] constexpr T determinant() const
  130. {
  131. if constexpr (N == 1) {
  132. return m_elements[0][0];
  133. } else {
  134. T result = {};
  135. int sign = 1;
  136. for (size_t j = 0; j < N; ++j) {
  137. result += sign * m_elements[0][j] * first_minor(0, j);
  138. sign *= -1;
  139. }
  140. return result;
  141. }
  142. }
  143. [[nodiscard]] constexpr T first_minor(size_t skip_row, size_t skip_column) const
  144. {
  145. static_assert(N > 1);
  146. VERIFY(skip_row < N);
  147. VERIFY(skip_column < N);
  148. Matrix<N - 1, T> first_minor;
  149. constexpr auto new_size = N - 1;
  150. size_t k = 0;
  151. for (size_t i = 0; i < N; ++i) {
  152. for (size_t j = 0; j < N; ++j) {
  153. if (i == skip_row || j == skip_column)
  154. continue;
  155. first_minor.elements()[k / new_size][k % new_size] = m_elements[i][j];
  156. ++k;
  157. }
  158. }
  159. return first_minor.determinant();
  160. }
  161. [[nodiscard]] constexpr static Matrix identity()
  162. {
  163. Matrix result;
  164. for (size_t i = 0; i < N; ++i) {
  165. for (size_t j = 0; j < N; ++j) {
  166. if (i == j)
  167. result.m_elements[i][j] = 1;
  168. else
  169. result.m_elements[i][j] = 0;
  170. }
  171. }
  172. return result;
  173. }
  174. [[nodiscard]] constexpr Matrix inverse() const
  175. {
  176. return adjugate() / determinant();
  177. }
  178. [[nodiscard]] constexpr Matrix transpose() const
  179. {
  180. Matrix result;
  181. for (size_t i = 0; i < N; ++i) {
  182. for (size_t j = 0; j < N; ++j)
  183. result.m_elements[i][j] = m_elements[j][i];
  184. }
  185. return result;
  186. }
  187. template<size_t U>
  188. [[nodiscard]] constexpr Matrix<U, T> submatrix_from_topleft() const
  189. requires(U > 0 && U < N)
  190. {
  191. Matrix<U, T> result;
  192. for (size_t i = 0; i < U; ++i) {
  193. for (size_t j = 0; j < U; ++j)
  194. result.m_elements[i][j] = m_elements[i][j];
  195. }
  196. return result;
  197. }
  198. constexpr bool is_invertible() const
  199. {
  200. return determinant() != static_cast<T>(0.0);
  201. }
  202. private:
  203. T m_elements[N][N];
  204. };
  205. }
  206. using Gfx::Matrix;