Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions include/LinearLib/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cassert>
#include <format>
#include <initializer_list>
#include <random>
#include <type_traits>
#include <ranges>

Expand Down Expand Up @@ -50,11 +51,43 @@ namespace LinearLib {
}

static Matrix zeros() {
return uniform(T{0});
}

static Matrix ones() {
return uniform(T{1});
}

static Matrix uniform(T const val) {
Matrix res;

for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
res.data[i][j] = T{0};
res.data[i][j] = val;
}
}

return res;
}

static Matrix random(T const min, T const max, std::size_t const seed = 0) {
Matrix res;

std::mt19937_64 rng(seed);

if constexpr (std::is_integral_v<T>) {
std::uniform_int_distribution<T> dist(min, max);
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
res.data[i][j] = dist(rng);
}
}
} else if constexpr (std::is_floating_point_v<T>) {
std::uniform_real_distribution<T> dist(min, max);
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
res.data[i][j] = dist(rng);
}
}
}

Expand Down Expand Up @@ -182,6 +215,31 @@ namespace LinearLib {
return data;
}


void forEach(const std::function<void()>& func) {
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
func();
}
}
}

void forEach(const std::function<void(T&)>& func) {
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
func(data[i][j]);
}
}
}

void forEach(const std::function<void(T&, std::size_t, std::size_t)> func) {
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
func(data[i][j], i, j);
}
}
}

bool operator==(const Matrix& other) const {
for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
Expand Down Expand Up @@ -237,7 +295,7 @@ namespace LinearLib {
Matrix<R, I, T> res;

for (std::size_t i = 0; i < R; i++) {
for (std::size_t j = 0; j < C; j++) {
for (std::size_t j = 0; j < I; j++) {
T sum = T{};
for (std::size_t k = 0; k < C; k++) {
sum += data[i][k] * other[k][j];
Expand Down Expand Up @@ -284,5 +342,6 @@ namespace LinearLib {

return res;
}

};
}
50 changes: 45 additions & 5 deletions tests/MatrixTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ TEST_CASE("Matrix operations", "[vector]") {
const Matrix<2, 2, int> m2 {{3, 6},
{7, 2}};

const Matrix<2, 2, int> result = m1 + m2;
const Matrix<2, 2, int> result = m1 - m2;

REQUIRE(result[0][0] == 8);
REQUIRE(result[0][1] == 9);
REQUIRE(result[1][0] == 9);
REQUIRE(result[1][1] == 6);
REQUIRE(result[0][0] == 2);
REQUIRE(result[0][1] == -3);
REQUIRE(result[1][0] == -5);
REQUIRE(result[1][1] == 2);
}

SECTION("Matrix dot product") {
Expand Down Expand Up @@ -197,4 +197,44 @@ TEST_CASE("Matrix operations", "[vector]") {
REQUIRE(eye2[3][2] == 0);
REQUIRE(eye2[3][3] == 0);
}

SECTION("Random") {
size_t seed = 42;
const Matrix<2, 2, int> m1 = Matrix<2, 2, int>::random(1, 10, seed);

REQUIRE(m1[0][0] == 8);
REQUIRE(m1[0][1] == 7);
REQUIRE(m1[1][0] == 8);
REQUIRE(m1[1][1] == 2);

const Matrix<2, 2, float> m2 = Matrix<2, 2, float>::random(0.0, 100.0, seed);
REQUIRE(m2[0][0] == 75.515548706f);
REQUIRE(m2[0][1] == 63.903141022f);
REQUIRE(m2[1][0] == 75.214515686f);
REQUIRE(m2[1][1] == 13.627268791f);
}

SECTION("ForEach") {
Matrix<2, 2, int> m {{1, 2}, {3, 4} };

m.forEach([](int& value) {
value *= 2;
});

REQUIRE(m[0][0] == 2);
REQUIRE(m[0][1] == 4);
REQUIRE(m[1][0] == 6);
REQUIRE(m[1][1] == 8);

Matrix<2, 2, float> m2 = Matrix<2, 2, float>::uniform(10);

m2.forEach([](float& value, std::size_t row, std::size_t col) {
value = row + col;
});

REQUIRE(m2[0][0] == 0.0f);
REQUIRE(m2[0][1] == 1.0f);
REQUIRE(m2[1][0] == 1.0f);
REQUIRE(m2[1][1] == 2.0f);
}
}