From f3cf6150239ee6d6af471b3fc3d49e3b9b834e78 Mon Sep 17 00:00:00 2001 From: Julian Kent Date: Tue, 30 Jun 2020 15:47:23 +0200 Subject: [PATCH] Do += -= and scalar *= /= in place --- matrix/Matrix.hpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index 524ecee33a..a910d4fb04 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -238,13 +238,21 @@ public: void operator+=(const Matrix &other) { Matrix &self = *this; - self = self + other; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + self(i, j) += other(i, j); + } + } } void operator-=(const Matrix &other) { Matrix &self = *this; - self = self - other; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + self(i, j) -= other(i, j); + } + } } template @@ -302,7 +310,7 @@ public: for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { - self(i, j) = self(i, j) * scalar; + self(i, j) *= scalar; } } } @@ -310,17 +318,23 @@ public: void operator/=(Type scalar) { Matrix &self = *this; - self = self * (Type(1) / scalar); + self *= (Type(1) / scalar); } inline void operator+=(Type scalar) { - *this = (*this) + scalar; + Matrix &self = *this; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + self(i, j) += scalar; + } + } } inline void operator-=(Type scalar) { - *this = (*this) - scalar; + Matrix &self = *this; + self += (-scalar); } bool operator==(const Matrix &other) const