Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
218 lines
7.0 KiB
C++
218 lines
7.0 KiB
C++
#include <Void/Matrix/Matrix.hpp>
|
||
#include <Void/Testing/Assert.hpp>
|
||
#include <Void/Testing/Test.hpp>
|
||
|
||
using namespace Void::Matrix;
|
||
|
||
// ── construction ──────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_default_zeros()
|
||
{
|
||
const Mat2<int> m;
|
||
Void::Testing::expect_eq(m(0, 0), 0);
|
||
Void::Testing::expect_eq(m(1, 1), 0);
|
||
}
|
||
|
||
Void::Testing::Test matrix_fill_constructor()
|
||
{
|
||
const Matrix<int, 3, 3> m{5};
|
||
Void::Testing::expect_eq(m(0, 0), 5);
|
||
Void::Testing::expect_eq(m(2, 2), 5);
|
||
}
|
||
|
||
Void::Testing::Test matrix_array_init()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
Void::Testing::expect_eq(m(0, 0), 1);
|
||
Void::Testing::expect_eq(m(0, 1), 2);
|
||
Void::Testing::expect_eq(m(1, 0), 3);
|
||
Void::Testing::expect_eq(m(1, 1), 4);
|
||
}
|
||
|
||
Void::Testing::Test matrix_identity_factory()
|
||
{
|
||
const auto id = Mat3<int>::identity();
|
||
Void::Testing::expect_eq(id(0, 0), 1);
|
||
Void::Testing::expect_eq(id(1, 1), 1);
|
||
Void::Testing::expect_eq(id(2, 2), 1);
|
||
Void::Testing::expect_eq(id(0, 1), 0);
|
||
Void::Testing::expect_eq(id(1, 0), 0);
|
||
}
|
||
|
||
Void::Testing::Test matrix_at_out_of_range()
|
||
{
|
||
const Mat2<int> m;
|
||
bool threw = false;
|
||
try { [[maybe_unused]] auto v = m.at(5, 0); } catch (...) { threw = true; }
|
||
Void::Testing::expect(threw);
|
||
}
|
||
|
||
// ── arithmetic ────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_add()
|
||
{
|
||
const Mat2<int> a{{1, 2, 3, 4}};
|
||
const Mat2<int> b{{5, 6, 7, 8}};
|
||
const auto c = a + b;
|
||
Void::Testing::expect_eq(c(0, 0), 6);
|
||
Void::Testing::expect_eq(c(1, 1), 12);
|
||
}
|
||
|
||
Void::Testing::Test matrix_subtract()
|
||
{
|
||
const Mat2<int> a{{10, 8, 6, 4}};
|
||
const Mat2<int> b{{1, 2, 3, 4}};
|
||
const auto c = a - b;
|
||
Void::Testing::expect_eq(c(0, 0), 9);
|
||
Void::Testing::expect_eq(c(1, 0), 3);
|
||
}
|
||
|
||
Void::Testing::Test matrix_scalar_mul()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
const auto r = m * 3;
|
||
Void::Testing::expect_eq(r(0, 0), 3);
|
||
Void::Testing::expect_eq(r(1, 1), 12);
|
||
}
|
||
|
||
Void::Testing::Test matrix_scalar_mul_left()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
const auto r = 2 * m;
|
||
Void::Testing::expect_eq(r(0, 1), 4);
|
||
}
|
||
|
||
Void::Testing::Test matrix_negate()
|
||
{
|
||
const Mat2<int> m{{1, -2, 3, -4}};
|
||
const auto n = -m;
|
||
Void::Testing::expect_eq(n(0, 0), -1);
|
||
Void::Testing::expect_eq(n(0, 1), 2);
|
||
}
|
||
|
||
Void::Testing::Test matrix_mul_identity()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
const auto id = Mat2<int>::identity();
|
||
const auto result = m * id;
|
||
Void::Testing::expect_eq(result, m);
|
||
}
|
||
|
||
Void::Testing::Test matrix_mul_2x2()
|
||
{
|
||
// [1 2] * [5 6] = [1*5+2*7 1*6+2*8] = [19 22]
|
||
// [3 4] [7 8] [3*5+4*7 3*6+4*8] [43 50]
|
||
const Mat2<int> a{{1, 2, 3, 4}};
|
||
const Mat2<int> b{{5, 6, 7, 8}};
|
||
const auto c = a * b;
|
||
Void::Testing::expect_eq(c(0, 0), 19);
|
||
Void::Testing::expect_eq(c(0, 1), 22);
|
||
Void::Testing::expect_eq(c(1, 0), 43);
|
||
Void::Testing::expect_eq(c(1, 1), 50);
|
||
}
|
||
|
||
Void::Testing::Test matrix_mul_non_square()
|
||
{
|
||
// [1 2 3] (1×3) * [1] (3×1) = [1+4+9] = [14] (1×1)
|
||
// [2]
|
||
// [3]
|
||
Matrix<int, 1, 3> row{{1, 2, 3}};
|
||
Matrix<int, 3, 1> col{{1, 2, 3}};
|
||
const auto r = row * col;
|
||
Void::Testing::expect_eq(r(0, 0), 14);
|
||
}
|
||
|
||
// ── transpose ─────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_transpose()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
const auto t = m.transposed();
|
||
Void::Testing::expect_eq(t(0, 0), 1);
|
||
Void::Testing::expect_eq(t(0, 1), 3);
|
||
Void::Testing::expect_eq(t(1, 0), 2);
|
||
Void::Testing::expect_eq(t(1, 1), 4);
|
||
}
|
||
|
||
Void::Testing::Test matrix_transpose_non_square()
|
||
{
|
||
Matrix<int, 2, 3> m{{1, 2, 3, 4, 5, 6}};
|
||
const auto t = m.transposed();
|
||
static_assert(decltype(t)::rows == 3 && decltype(t)::cols == 2);
|
||
Void::Testing::expect_eq(t(0, 0), 1);
|
||
Void::Testing::expect_eq(t(2, 0), 3);
|
||
Void::Testing::expect_eq(t(0, 1), 4);
|
||
}
|
||
|
||
// ── aggregates ────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_sum()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
Void::Testing::expect_eq(m.sum(), 10);
|
||
}
|
||
|
||
Void::Testing::Test matrix_trace()
|
||
{
|
||
const Mat3<int> m{{1, 0, 0,
|
||
0, 2, 0,
|
||
0, 0, 3}};
|
||
Void::Testing::expect_eq(m.trace(), 6);
|
||
}
|
||
|
||
// ── determinant ───────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_det_2x2()
|
||
{
|
||
const Mat2<int> m{{1, 2, 3, 4}};
|
||
Void::Testing::expect_eq(det(m), -2); // 1*4 - 2*3
|
||
}
|
||
|
||
Void::Testing::Test matrix_det_3x3()
|
||
{
|
||
const Mat3<int> m{{1, 2, 3,
|
||
4, 5, 6,
|
||
7, 8, 9}};
|
||
Void::Testing::expect_eq(det(m), 0); // singular
|
||
}
|
||
|
||
// ── inverse ───────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_inverse_2x2()
|
||
{
|
||
const Mat2<double> m{{1.0, 2.0, 3.0, 4.0}};
|
||
const auto inv = inverse(m);
|
||
Void::Testing::expect(inv.has_value());
|
||
// m * inv should equal identity
|
||
const auto prod = m * *inv;
|
||
Void::Testing::expect(prod(0, 0) > 0.999 && prod(0, 0) < 1.001);
|
||
Void::Testing::expect(prod(1, 1) > 0.999 && prod(1, 1) < 1.001);
|
||
Void::Testing::expect(prod(0, 1) > -0.001 && prod(0, 1) < 0.001);
|
||
}
|
||
|
||
Void::Testing::Test matrix_inverse_singular_returns_nullopt()
|
||
{
|
||
const Mat2<double> m{{1.0, 2.0, 2.0, 4.0}}; // det = 0
|
||
Void::Testing::expect(!inverse(m).has_value());
|
||
}
|
||
|
||
// ── dot product ───────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_dot_product()
|
||
{
|
||
Vec3<int> a{{1, 2, 3}};
|
||
Vec3<int> b{{4, 5, 6}};
|
||
Void::Testing::expect_eq(dot(a, b), 32); // 4+10+18
|
||
}
|
||
|
||
// ── equality ──────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test matrix_equality()
|
||
{
|
||
const Mat2<int> a{{1, 2, 3, 4}};
|
||
const Mat2<int> b{{1, 2, 3, 4}};
|
||
const Mat2<int> c{{1, 2, 3, 5}};
|
||
Void::Testing::expect(a == b);
|
||
Void::Testing::expect(a != c);
|
||
}
|