Matrix.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. [[nodiscard]] constexpr Matrix operator*(Matrix const& other) const
  53. {
  54. Matrix product;
  55. for (size_t i = 0; i < N; ++i) {
  56. for (size_t j = 0; j < N; ++j) {
  57. auto& element = product.m_elements[i][j];
  58. if constexpr (N == 4) {
  59. element = m_elements[i][0] * other.m_elements[0][j]
  60. + m_elements[i][1] * other.m_elements[1][j]
  61. + m_elements[i][2] * other.m_elements[2][j]
  62. + m_elements[i][3] * other.m_elements[3][j];
  63. } else if constexpr (N == 3) {
  64. element = m_elements[i][0] * other.m_elements[0][j]
  65. + m_elements[i][1] * other.m_elements[1][j]
  66. + m_elements[i][2] * other.m_elements[2][j];
  67. } else if constexpr (N == 2) {
  68. element = m_elements[i][0] * other.m_elements[0][j]
  69. + m_elements[i][1] * other.m_elements[1][j];
  70. } else if constexpr (N == 1) {
  71. element = m_elements[i][0] * other.m_elements[0][j];
  72. } else {
  73. T value {};
  74. for (size_t k = 0; k < N; ++k)
  75. value += m_elements[i][k] * other.m_elements[k][j];
  76. element = value;
  77. }
  78. }
  79. }
  80. return product;
  81. }
  82. [[nodiscard]] constexpr Matrix operator+(Matrix const& other) const
  83. {
  84. Matrix sum;
  85. for (size_t i = 0; i < N; ++i) {
  86. for (size_t j = 0; j < N; ++j)
  87. sum.m_elements[i][j] = m_elements[i][j] + other.m_elements[i][j];
  88. }
  89. return sum;
  90. }
  91. [[nodiscard]] constexpr Matrix operator/(T divisor) const
  92. {
  93. Matrix division;
  94. for (size_t i = 0; i < N; ++i) {
  95. for (size_t j = 0; j < N; ++j)
  96. division.m_elements[i][j] = m_elements[i][j] / divisor;
  97. }
  98. return division;
  99. }
  100. [[nodiscard]] friend constexpr Matrix operator*(Matrix const& matrix, T scalar)
  101. {
  102. Matrix scaled;
  103. for (size_t i = 0; i < N; ++i) {
  104. for (size_t j = 0; j < N; ++j)
  105. scaled.m_elements[i][j] = matrix.m_elements[i][j] * scalar;
  106. }
  107. return scaled;
  108. }
  109. [[nodiscard]] friend constexpr Matrix operator*(T scalar, Matrix const& matrix)
  110. {
  111. return matrix * scalar;
  112. }
  113. [[nodiscard]] constexpr Matrix adjugate() const
  114. {
  115. if constexpr (N == 1)
  116. return Matrix(1);
  117. Matrix adjugate;
  118. for (size_t i = 0; i < N; ++i) {
  119. for (size_t j = 0; j < N; ++j) {
  120. int sign = (i + j) % 2 == 0 ? 1 : -1;
  121. adjugate.m_elements[j][i] = sign * first_minor(i, j);
  122. }
  123. }
  124. return adjugate;
  125. }
  126. [[nodiscard]] constexpr T determinant() const
  127. {
  128. if constexpr (N == 1) {
  129. return m_elements[0][0];
  130. } else {
  131. T result = {};
  132. int sign = 1;
  133. for (size_t j = 0; j < N; ++j) {
  134. result += sign * m_elements[0][j] * first_minor(0, j);
  135. sign *= -1;
  136. }
  137. return result;
  138. }
  139. }
  140. [[nodiscard]] constexpr T first_minor(size_t skip_row, size_t skip_column) const
  141. {
  142. static_assert(N > 1);
  143. VERIFY(skip_row < N);
  144. VERIFY(skip_column < N);
  145. Matrix<N - 1, T> first_minor;
  146. constexpr auto new_size = N - 1;
  147. size_t k = 0;
  148. for (size_t i = 0; i < N; ++i) {
  149. for (size_t j = 0; j < N; ++j) {
  150. if (i == skip_row || j == skip_column)
  151. continue;
  152. first_minor.elements()[k / new_size][k % new_size] = m_elements[i][j];
  153. ++k;
  154. }
  155. }
  156. return first_minor.determinant();
  157. }
  158. [[nodiscard]] constexpr static Matrix identity()
  159. {
  160. Matrix result;
  161. for (size_t i = 0; i < N; ++i) {
  162. for (size_t j = 0; j < N; ++j) {
  163. if (i == j)
  164. result.m_elements[i][j] = 1;
  165. else
  166. result.m_elements[i][j] = 0;
  167. }
  168. }
  169. return result;
  170. }
  171. [[nodiscard]] constexpr Matrix inverse() const
  172. {
  173. return adjugate() / determinant();
  174. }
  175. [[nodiscard]] constexpr Matrix transpose() const
  176. {
  177. Matrix result;
  178. for (size_t i = 0; i < N; ++i) {
  179. for (size_t j = 0; j < N; ++j)
  180. result.m_elements[i][j] = m_elements[j][i];
  181. }
  182. return result;
  183. }
  184. template<size_t U>
  185. [[nodiscard]] constexpr Matrix<U, T> submatrix_from_topleft() const
  186. requires(U > 0 && U < N)
  187. {
  188. Matrix<U, T> result;
  189. for (size_t i = 0; i < U; ++i) {
  190. for (size_t j = 0; j < U; ++j)
  191. result.m_elements[i][j] = m_elements[i][j];
  192. }
  193. return result;
  194. }
  195. constexpr bool is_invertible() const
  196. {
  197. return determinant() != static_cast<T>(0.0);
  198. }
  199. private:
  200. T m_elements[N][N];
  201. };
  202. }
  203. using Gfx::Matrix;