From 23d56fb3d00684daf6ec846f6fd268d57ba5b099 Mon Sep 17 00:00:00 2001 From: Stefan Eschbach Date: Thu, 6 Apr 2017 15:51:13 +0200 Subject: [PATCH] Added matrix scalar addition/subtraction (binary and unary) operators. --- include/GMatrix.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/linalg/GMatrix.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 0 deletions(-) diff --git a/include/GMatrix.hpp b/include/GMatrix.hpp index 3939799..9bfbfc8 100644 --- a/include/GMatrix.hpp +++ b/include/GMatrix.hpp @@ -96,11 +96,15 @@ public: virtual GMatrix& operator=(const GMatrix& matrix); virtual GMatrix& operator=(const double& value); virtual GMatrix operator+(const GMatrix& matrix) const; + virtual GMatrix operator+(const double& scalar) const; virtual GMatrix operator-(const GMatrix& matrix) const; + virtual GMatrix operator-(const double& scalar) const; virtual GMatrix operator*(const GMatrix& matrix) const; virtual GMatrix operator-(void) const; virtual GMatrix& operator+=(const GMatrix& matrix); + virtual GMatrix& operator+=(const double& scalar); virtual GMatrix& operator-=(const GMatrix& matrix); + virtual GMatrix& operator-=(const double& scalar); virtual GMatrix& operator*=(const GMatrix& matrix); virtual GMatrix& operator*=(const double& scalar); virtual GMatrix& operator/=(const double& scalar); @@ -207,6 +211,24 @@ GMatrix GMatrix::operator+(const GMatrix& matrix) const /***********************************************************************//** + * @brief Binary matrix scalar addition + * + * @param[in] double Scalar. + * @return Result of matrix addition. + * + * Returns the sum of the matrix with another matrix were all entries have + * the value of scalar. The method makes use of the unary addition operator. + ***************************************************************************/ +inline +GMatrix GMatrix::operator+(const double& scalar) const +{ + GMatrix result = *this; + result += scalar; + return result; +} + + +/***********************************************************************//** * @brief Binary matrix subtraction * * @param[in] matrix Matrix. @@ -225,6 +247,24 @@ GMatrix GMatrix::operator-(const GMatrix& matrix) const /***********************************************************************//** + * @brief Binary matrix scalar subtraction + * + * @param[in] double Scalar. + * @return Result of matrix subtraction. + * + * Returns the difference of the matrix with another matrix were all entries have + * the value of scalar. The method makes use of the unary subtraction operator. + ***************************************************************************/ +inline +GMatrix GMatrix::operator+(const double& scalar) const +{ + GMatrix result = *this; + result -= scalar; + return result; +} + + +/***********************************************************************//** * @brief Binary matrix multiplication * * @param[in] matrix Matrix. diff --git a/src/linalg/GMatrix.cpp b/src/linalg/GMatrix.cpp index 7ccc856..b7a199c 100644 --- a/src/linalg/GMatrix.cpp +++ b/src/linalg/GMatrix.cpp @@ -362,6 +362,26 @@ GMatrix& GMatrix::operator+=(const GMatrix& matrix) /***********************************************************************//** + * @brief Unary matrix scalar addition operator + * + * @param[in] double Scalar. + * + * This method adds a given scalar to each matrix element. + ***************************************************************************/ +GMatrix& GMatrix::operator+=(const double& scalar) +{ + // Add all matrix elements + double* dst = m_data; + for (int i = 0; i < m_elements; ++i) { + *dst++ += scalar; + } + + // Return result + return *this; +} + + +/***********************************************************************//** * @brief Unary matrix subtraction operator * * @param[in] matrix Matrix. @@ -394,6 +414,26 @@ GMatrix& GMatrix::operator-=(const GMatrix& matrix) /***********************************************************************//** + * @brief Unary matrix scalar subtraction operator + * + * @param[in] double Scalar. + * + * This method subtracts a given scalar from each matrix element. + ***************************************************************************/ +GMatrix& GMatrix::operator+=(const double& scalar) +{ + // Add all matrix elements + double* dst = m_data; + for (int i = 0; i < m_elements; ++i) { + *dst++ -= scalar; + } + + // Return result + return *this; +} + + +/***********************************************************************//** * @brief Unary matrix multiplication operator * * @param[in] matrix Matrix. -- libgit2 0.21.4