2020-07-24 14:54:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-24 14:54:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <initializer_list>
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
template<size_t N, typename T>
|
|
|
|
class Matrix {
|
2022-03-23 13:07:27 +00:00
|
|
|
template<size_t U, typename V>
|
|
|
|
friend class Matrix;
|
|
|
|
|
2020-07-24 14:54:36 +00:00
|
|
|
public:
|
|
|
|
static constexpr size_t Size = N;
|
|
|
|
|
2021-05-02 14:09:18 +00:00
|
|
|
constexpr Matrix() = default;
|
|
|
|
constexpr Matrix(std::initializer_list<T> elements)
|
2020-07-24 14:54:36 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(elements.size() == N * N);
|
2020-07-24 14:54:36 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (auto& element : elements) {
|
|
|
|
m_elements[i / N][i % N] = element;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
2021-05-02 14:09:18 +00:00
|
|
|
constexpr Matrix(Args... args)
|
2020-07-24 14:54:36 +00:00
|
|
|
: Matrix({ (T)args... })
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Matrix(Matrix const& other)
|
2020-07-24 14:54:36 +00:00
|
|
|
{
|
|
|
|
__builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Matrix& operator=(Matrix const& other)
|
2021-10-10 13:42:19 +00:00
|
|
|
{
|
|
|
|
__builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:09:18 +00:00
|
|
|
constexpr auto elements() const { return m_elements; }
|
|
|
|
constexpr auto elements() { return m_elements; }
|
2020-07-24 14:54:36 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr Matrix operator*(Matrix const& other) const
|
2020-07-24 14:54:36 +00:00
|
|
|
{
|
|
|
|
Matrix product;
|
2021-05-13 19:33:17 +00:00
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
2020-07-24 14:54:36 +00:00
|
|
|
auto& element = product.m_elements[i][j];
|
|
|
|
|
|
|
|
if constexpr (N == 4) {
|
2021-05-13 17:59:57 +00:00
|
|
|
element = m_elements[i][0] * other.m_elements[0][j]
|
|
|
|
+ m_elements[i][1] * other.m_elements[1][j]
|
|
|
|
+ m_elements[i][2] * other.m_elements[2][j]
|
|
|
|
+ m_elements[i][3] * other.m_elements[3][j];
|
2020-07-24 14:54:36 +00:00
|
|
|
} else if constexpr (N == 3) {
|
2021-05-13 17:59:57 +00:00
|
|
|
element = m_elements[i][0] * other.m_elements[0][j]
|
|
|
|
+ m_elements[i][1] * other.m_elements[1][j]
|
|
|
|
+ m_elements[i][2] * other.m_elements[2][j];
|
2020-07-24 14:54:36 +00:00
|
|
|
} else if constexpr (N == 2) {
|
2021-05-13 17:59:57 +00:00
|
|
|
element = m_elements[i][0] * other.m_elements[0][j]
|
|
|
|
+ m_elements[i][1] * other.m_elements[1][j];
|
2020-07-24 14:54:36 +00:00
|
|
|
} else if constexpr (N == 1) {
|
2021-05-13 17:59:57 +00:00
|
|
|
element = m_elements[i][0] * other.m_elements[0][j];
|
2020-07-24 14:54:36 +00:00
|
|
|
} else {
|
|
|
|
T value {};
|
|
|
|
for (size_t k = 0; k < N; ++k)
|
2021-05-13 17:59:57 +00:00
|
|
|
value += m_elements[i][k] * other.m_elements[k][j];
|
2020-07-24 14:54:36 +00:00
|
|
|
|
|
|
|
element = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
2022-10-01 21:35:52 +00:00
|
|
|
constexpr Matrix operator+(Matrix const& other) const
|
|
|
|
{
|
|
|
|
Matrix sum;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j)
|
|
|
|
sum.m_elements[i][j] = m_elements[i][j] + other.m_elements[i][j];
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2021-05-23 19:43:29 +00:00
|
|
|
constexpr Matrix operator/(T divisor) const
|
|
|
|
{
|
|
|
|
Matrix division;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
2022-01-10 23:50:17 +00:00
|
|
|
for (size_t j = 0; j < N; ++j)
|
2021-05-23 19:43:29 +00:00
|
|
|
division.m_elements[i][j] = m_elements[i][j] / divisor;
|
|
|
|
}
|
|
|
|
return division;
|
|
|
|
}
|
|
|
|
|
2022-10-01 21:38:06 +00:00
|
|
|
friend constexpr Matrix operator*(Matrix const& matrix, T scalar)
|
|
|
|
{
|
|
|
|
Matrix scaled;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j)
|
|
|
|
scaled.m_elements[i][j] = matrix.m_elements[i][j] * scalar;
|
|
|
|
}
|
|
|
|
return scaled;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend constexpr Matrix operator*(T scalar, Matrix const& matrix)
|
|
|
|
{
|
|
|
|
return matrix * scalar;
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:42:16 +00:00
|
|
|
[[nodiscard]] constexpr Matrix adjugate() const
|
2021-05-23 19:43:29 +00:00
|
|
|
{
|
|
|
|
if constexpr (N == 1)
|
|
|
|
return Matrix(1);
|
|
|
|
|
|
|
|
Matrix adjugate;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
|
|
|
int sign = (i + j) % 2 == 0 ? 1 : -1;
|
|
|
|
adjugate.m_elements[j][i] = sign * first_minor(i, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adjugate;
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:42:16 +00:00
|
|
|
[[nodiscard]] constexpr T determinant() const
|
2021-05-23 19:43:29 +00:00
|
|
|
{
|
|
|
|
if constexpr (N == 1) {
|
|
|
|
return m_elements[0][0];
|
|
|
|
} else {
|
|
|
|
T result = {};
|
|
|
|
int sign = 1;
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
|
|
|
result += sign * m_elements[0][j] * first_minor(0, j);
|
|
|
|
sign *= -1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:42:16 +00:00
|
|
|
[[nodiscard]] constexpr T first_minor(size_t skip_row, size_t skip_column) const
|
2021-05-23 19:43:29 +00:00
|
|
|
{
|
|
|
|
static_assert(N > 1);
|
|
|
|
VERIFY(skip_row < N);
|
|
|
|
VERIFY(skip_column < N);
|
|
|
|
|
|
|
|
Matrix<N - 1, T> first_minor;
|
|
|
|
constexpr auto new_size = N - 1;
|
|
|
|
size_t k = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
|
|
|
if (i == skip_row || j == skip_column)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
first_minor.elements()[k / new_size][k % new_size] = m_elements[i][j];
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return first_minor.determinant();
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:42:16 +00:00
|
|
|
[[nodiscard]] constexpr static Matrix identity()
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
Matrix result;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
|
|
|
if (i == j)
|
|
|
|
result.m_elements[i][j] = 1;
|
|
|
|
else
|
|
|
|
result.m_elements[i][j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:54:14 +00:00
|
|
|
[[nodiscard]] constexpr Matrix inverse() const
|
2021-05-23 19:43:29 +00:00
|
|
|
{
|
2022-01-11 23:54:14 +00:00
|
|
|
return adjugate() / determinant();
|
2021-05-23 19:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 06:42:16 +00:00
|
|
|
[[nodiscard]] constexpr Matrix transpose() const
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
Matrix result;
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
2022-01-10 23:50:17 +00:00
|
|
|
for (size_t j = 0; j < N; ++j)
|
2021-05-13 19:33:17 +00:00
|
|
|
result.m_elements[i][j] = m_elements[j][i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:07:27 +00:00
|
|
|
template<size_t U>
|
|
|
|
[[nodiscard]] constexpr Matrix<U, T> submatrix_from_topleft() const requires(U > 0 && U < N)
|
|
|
|
{
|
|
|
|
Matrix<U, T> result;
|
|
|
|
for (size_t i = 0; i < U; ++i) {
|
|
|
|
for (size_t j = 0; j < U; ++j)
|
|
|
|
result.m_elements[i][j] = m_elements[i][j];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:54:36 +00:00
|
|
|
private:
|
|
|
|
T m_elements[N][N];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using Gfx::Matrix;
|