#pragma once #include #include #include #include #include namespace Void::Matrix { // ── Matrix ───────────────────────────────────────────────────── // Compile-time-sized matrix stored in row-major order. template class Matrix { static_assert(Rows > 0 && Cols > 0, "Matrix dimensions must be positive"); public: using value_type = T; static constexpr std::size_t rows = Rows; static constexpr std::size_t cols = Cols; constexpr Matrix() { data_.fill(T{}); } explicit constexpr Matrix(T fill_value) { data_.fill(fill_value); } // Row-major initializer: Matrix{{1,2,3,4}} constexpr Matrix(std::array d) : data_{d} {} [[nodiscard]] constexpr T& at(std::size_t r, std::size_t c) { if (r >= Rows || c >= Cols) throw std::out_of_range{"Matrix::at"}; return data_[r * Cols + c]; } [[nodiscard]] constexpr const T& at(std::size_t r, std::size_t c) const { if (r >= Rows || c >= Cols) throw std::out_of_range{"Matrix::at"}; return data_[r * Cols + c]; } [[nodiscard]] constexpr T& operator()(std::size_t r, std::size_t c) noexcept { return data_[r * Cols + c]; } [[nodiscard]] constexpr const T& operator()(std::size_t r, std::size_t c) const noexcept { return data_[r * Cols + c]; } // ── arithmetic ──────────────────────────────────────────────────────────── [[nodiscard]] constexpr Matrix operator+(const Matrix& o) const noexcept { Matrix result; for (std::size_t i = 0; i < Rows * Cols; ++i) result.data_[i] = data_[i] + o.data_[i]; return result; } [[nodiscard]] constexpr Matrix operator-(const Matrix& o) const noexcept { Matrix result; for (std::size_t i = 0; i < Rows * Cols; ++i) result.data_[i] = data_[i] - o.data_[i]; return result; } [[nodiscard]] constexpr Matrix operator*(T scalar) const noexcept { Matrix result; for (std::size_t i = 0; i < Rows * Cols; ++i) result.data_[i] = data_[i] * scalar; return result; } [[nodiscard]] constexpr Matrix operator-() const noexcept { Matrix result; for (std::size_t i = 0; i < Rows * Cols; ++i) result.data_[i] = -data_[i]; return result; } constexpr Matrix& operator+=(const Matrix& o) noexcept { for (std::size_t i = 0; i < Rows * Cols; ++i) data_[i] += o.data_[i]; return *this; } constexpr Matrix& operator-=(const Matrix& o) noexcept { for (std::size_t i = 0; i < Rows * Cols; ++i) data_[i] -= o.data_[i]; return *this; } constexpr Matrix& operator*=(T scalar) noexcept { for (std::size_t i = 0; i < Rows * Cols; ++i) data_[i] *= scalar; return *this; } // ── matrix multiplication ───────────────────────────────────────────────── template [[nodiscard]] constexpr Matrix operator*(const Matrix& rhs) const noexcept { Matrix result; for (std::size_t r = 0; r < Rows; ++r) for (std::size_t k = 0; k < Cols; ++k) for (std::size_t c = 0; c < K; ++c) result(r, c) += (*this)(r, k) * rhs(k, c); return result; } // ── transpose ───────────────────────────────────────────────────────────── [[nodiscard]] constexpr Matrix transposed() const noexcept { Matrix result; for (std::size_t r = 0; r < Rows; ++r) for (std::size_t c = 0; c < Cols; ++c) result(c, r) = (*this)(r, c); return result; } // ── comparison ──────────────────────────────────────────────────────────── [[nodiscard]] constexpr bool operator==(const Matrix& o) const noexcept { return data_ == o.data_; } [[nodiscard]] constexpr bool operator!=(const Matrix& o) const noexcept { return !(*this == o); } // ── aggregates ──────────────────────────────────────────────────────────── [[nodiscard]] constexpr T sum() const noexcept { return std::accumulate(data_.begin(), data_.end(), T{}); } [[nodiscard]] constexpr T trace() const noexcept requires (Rows == Cols) { T t{}; for (std::size_t i = 0; i < Rows; ++i) t += (*this)(i, i); return t; } [[nodiscard]] constexpr T frobenius_sq() const noexcept { T s{}; for (const auto& v : data_) s += v * v; return s; } // ── factory ─────────────────────────────────────────────────────────────── [[nodiscard]] static constexpr Matrix identity() noexcept requires (Rows == Cols) { Matrix m; for (std::size_t i = 0; i < Rows; ++i) m(i, i) = T{1}; return m; } [[nodiscard]] static constexpr Matrix zeros() noexcept { return Matrix{}; } [[nodiscard]] static constexpr Matrix ones() noexcept { return Matrix{T{1}}; } // ── element access helpers ──────────────────────────────────────────────── [[nodiscard]] constexpr const std::array& flat() const noexcept { return data_; } private: std::array data_; }; // Scalar * Matrix template [[nodiscard]] constexpr Matrix operator*(T scalar, const Matrix& m) noexcept { return m * scalar; } // ── Type aliases ────────────────────────────────────────────────────────────── template using Mat2 = Matrix; template using Mat3 = Matrix; template using Mat4 = Matrix; template using Vec2 = Matrix; template using Vec3 = Matrix; template using Vec4 = Matrix; using Mat2f = Mat2; using Mat3f = Mat3; using Mat4f = Mat4; using Mat2d = Mat2; using Mat3d = Mat3; using Mat4d = Mat4; // ── dot product for column vectors ─────────────────────────────────────────── template [[nodiscard]] constexpr T dot(const Matrix& a, const Matrix& b) noexcept { T s{}; for (std::size_t i = 0; i < N; ++i) s += a(i, 0) * b(i, 0); return s; } // ── 2×2 determinant ─────────────────────────────────────────────────────────── template [[nodiscard]] constexpr T det(const Mat2& m) noexcept { return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0); } // ── 2×2 inverse ─────────────────────────────────────────────────────────────── template [[nodiscard]] constexpr std::optional> inverse(const Mat2& m) noexcept { const T d = det(m); if (d == T{}) return std::nullopt; return Mat2{{m(1, 1) / d, -m(0, 1) / d, -m(1, 0) / d, m(0, 0) / d}}; } // ── 3×3 determinant ─────────────────────────────────────────────────────────── template [[nodiscard]] constexpr T det(const Mat3& m) noexcept { return m(0,0) * (m(1,1)*m(2,2) - m(1,2)*m(2,1)) - m(0,1) * (m(1,0)*m(2,2) - m(1,2)*m(2,0)) + m(0,2) * (m(1,0)*m(2,1) - m(1,1)*m(2,0)); } } // namespace Void::Matrix