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.