Do += -= and scalar *= /= in place

This commit is contained in:
Julian Kent
2020-06-30 15:47:23 +02:00
committed by Daniel Agar
parent f529358e9a
commit f3cf615023
+20 -6
View File
@@ -238,13 +238,21 @@ public:
void operator+=(const Matrix<Type, M, N> &other) void operator+=(const Matrix<Type, M, N> &other)
{ {
Matrix<Type, M, N> &self = *this; Matrix<Type, M, N> &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<Type, M, N> &other) void operator-=(const Matrix<Type, M, N> &other)
{ {
Matrix<Type, M, N> &self = *this; Matrix<Type, M, N> &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<size_t P> template<size_t P>
@@ -302,7 +310,7 @@ public:
for (size_t i = 0; i < M; i++) { for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) { 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) void operator/=(Type scalar)
{ {
Matrix<Type, M, N> &self = *this; Matrix<Type, M, N> &self = *this;
self = self * (Type(1) / scalar); self *= (Type(1) / scalar);
} }
inline void operator+=(Type scalar) inline void operator+=(Type scalar)
{ {
*this = (*this) + scalar; Matrix<Type, M, N> &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) inline void operator-=(Type scalar)
{ {
*this = (*this) - scalar; Matrix<Type, M, N> &self = *this;
self += (-scalar);
} }
bool operator==(const Matrix<Type, M, N> &other) const bool operator==(const Matrix<Type, M, N> &other) const