Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
249 lines
8.8 KiB
C++
249 lines
8.8 KiB
C++
#pragma once
|
||
|
||
#include <array>
|
||
#include <cstddef>
|
||
#include <numeric>
|
||
#include <optional>
|
||
#include <stdexcept>
|
||
|
||
namespace Void::Matrix
|
||
{
|
||
|
||
// ── Matrix<T, Rows, Cols> ─────────────────────────────────────────────────────
|
||
// Compile-time-sized matrix stored in row-major order.
|
||
|
||
template <typename T, std::size_t Rows, std::size_t Cols>
|
||
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<int,2,2>{{1,2,3,4}}
|
||
constexpr Matrix(std::array<T, Rows * Cols> 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 <std::size_t K>
|
||
[[nodiscard]] constexpr Matrix<T, Rows, K>
|
||
operator*(const Matrix<T, Cols, K>& rhs) const noexcept
|
||
{
|
||
Matrix<T, Rows, K> 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<T, Cols, Rows> transposed() const noexcept
|
||
{
|
||
Matrix<T, Cols, Rows> 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<T, Rows * Cols>& flat() const noexcept
|
||
{
|
||
return data_;
|
||
}
|
||
|
||
private:
|
||
std::array<T, Rows * Cols> data_;
|
||
};
|
||
|
||
// Scalar * Matrix
|
||
template <typename T, std::size_t R, std::size_t C>
|
||
[[nodiscard]] constexpr Matrix<T, R, C> operator*(T scalar, const Matrix<T, R, C>& m) noexcept
|
||
{
|
||
return m * scalar;
|
||
}
|
||
|
||
// ── Type aliases ──────────────────────────────────────────────────────────────
|
||
|
||
template <typename T> using Mat2 = Matrix<T, 2, 2>;
|
||
template <typename T> using Mat3 = Matrix<T, 3, 3>;
|
||
template <typename T> using Mat4 = Matrix<T, 4, 4>;
|
||
template <typename T> using Vec2 = Matrix<T, 2, 1>;
|
||
template <typename T> using Vec3 = Matrix<T, 3, 1>;
|
||
template <typename T> using Vec4 = Matrix<T, 4, 1>;
|
||
|
||
using Mat2f = Mat2<float>;
|
||
using Mat3f = Mat3<float>;
|
||
using Mat4f = Mat4<float>;
|
||
using Mat2d = Mat2<double>;
|
||
using Mat3d = Mat3<double>;
|
||
using Mat4d = Mat4<double>;
|
||
|
||
// ── dot product for column vectors ───────────────────────────────────────────
|
||
|
||
template <typename T, std::size_t N>
|
||
[[nodiscard]] constexpr T dot(const Matrix<T, N, 1>& a, const Matrix<T, N, 1>& 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 <typename T>
|
||
[[nodiscard]] constexpr T det(const Mat2<T>& m) noexcept
|
||
{
|
||
return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
|
||
}
|
||
|
||
// ── 2×2 inverse ───────────────────────────────────────────────────────────────
|
||
|
||
template <typename T>
|
||
[[nodiscard]] constexpr std::optional<Mat2<T>> inverse(const Mat2<T>& m) noexcept
|
||
{
|
||
const T d = det(m);
|
||
if (d == T{}) return std::nullopt;
|
||
return Mat2<T>{{m(1, 1) / d, -m(0, 1) / d,
|
||
-m(1, 0) / d, m(0, 0) / d}};
|
||
}
|
||
|
||
// ── 3×3 determinant ───────────────────────────────────────────────────────────
|
||
|
||
template <typename T>
|
||
[[nodiscard]] constexpr T det(const Mat3<T>& 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
|