From 272e6a63df7369e5afcb16c5a6c14f7dd6a75893 Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 20 Nov 2023 23:50:25 +0100 Subject: Replace `Vector::map_indexed` with `zip` and add vector division --- src/Math/Vector.hpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/Math') diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index a7c3782..a31afec 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -46,10 +46,10 @@ struct Vector { } template - Vector map_indexed(F f) const { + Vector zip(const Vector other, F f) const { Vector result{}; for (Int i = 0; i < S; i++) { - result[i] = f(i, elements[i]); + result[i] = f(elements[i], other[i]); } return result; } @@ -108,7 +108,7 @@ struct Vector { } Vector operator+(const Vector other) const { - return map_indexed([&](auto i, auto x) { return x + other[i]; }); + return zip(other, [](auto a, auto b) { return a + b; }); } Vector operator+(T scalar) const { @@ -120,11 +120,11 @@ struct Vector { } T operator*(const Vector other) const { - return map_indexed([&](auto i, auto x) { return x * other[i]; }).sum(); + return zip(other, [](auto a, auto b) { return a * b; }).sum(); } Vector operator-(const Vector other) const { - return map_indexed([&](auto i, auto x) { return x - other[i]; }); + return zip(other, [](auto a, auto b) { return a - b; }); } Vector operator-() const { @@ -135,6 +135,10 @@ struct Vector { return map([=](auto x) { return x / scalar; }); } + Vector operator/(const Vector other) const { + return zip(other, [](auto a, auto b) { return a / b; }); + } + Bool operator==(const Vector& other) { for (Int i = 0; i < S; i++) { if (elements[i] != other[i]) { -- cgit 1.4.1