From 674bd99f3b9284e1088d100d765b3a09cf0c192f Mon Sep 17 00:00:00 2001 From: kamilritz Date: Thu, 11 Jun 2020 19:44:29 +0200 Subject: [PATCH] Add operator* and operator/ for slice with type --- matrix/Slice.hpp | 18 ++++++++++++++++++ test/slice.cpp | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/matrix/Slice.hpp b/matrix/Slice.hpp index 91cd7f7aea..5157da20e6 100644 --- a/matrix/Slice.hpp +++ b/matrix/Slice.hpp @@ -184,6 +184,24 @@ public: return operator*=(Type(1) / other); } + Matrix operator*(const Type& other) + { + Slice& self = *this; + Matrix res; + for (size_t i = 0; i < P; i++) { + for (size_t j = 0; j < Q; j++) { + res(i, j) = self(i, j) * other; + } + } + return res; + } + + Matrix operator/(const Type& other) + { + Slice& self = *this; + return self * (Type(1) / other); + } + template const Slice slice(size_t x0, size_t y0) const { diff --git a/test/slice.cpp b/test/slice.cpp index 2987ba3ee4..7e4667b56a 100644 --- a/test/slice.cpp +++ b/test/slice.cpp @@ -208,6 +208,16 @@ int main() float O_check_data_10 [9] = {0, 2, 3, 4, 2.5, 6, 7, 4, 10}; TEST(isEqual(O, SquareMatrix3f(O_check_data_10))); + // Different operations + O = SquareMatrix3f(data); + SquareMatrix res_11(O.slice<2,2>(1,1) * 2.f); + float O_check_data_11 [4] = {10, 12, 16, 20}; + TEST(isEqual(res_11, SquareMatrix(O_check_data_11))); + + O = SquareMatrix3f(data); + SquareMatrix res_12(O.slice<2,2>(1,1) / 2.f); + float O_check_data_12 [4] = {2.5, 3, 4, 5}; + TEST(isEqual(res_12, SquareMatrix(O_check_data_12))); return 0; }