Commit 23d56fb3d00684daf6ec846f6fd268d57ba5b099
1 parent
adc6fa6c
Added matrix scalar addition/subtraction (binary and unary) operators.
Showing
2 changed files
with
80 additions
and
0 deletions
include/GMatrix.hpp
| ... | ... | @@ -96,11 +96,15 @@ public: |
| 96 | 96 | virtual GMatrix& operator=(const GMatrix& matrix); |
| 97 | 97 | virtual GMatrix& operator=(const double& value); |
| 98 | 98 | virtual GMatrix operator+(const GMatrix& matrix) const; |
| 99 | + virtual GMatrix operator+(const double& scalar) const; | |
| 99 | 100 | virtual GMatrix operator-(const GMatrix& matrix) const; |
| 101 | + virtual GMatrix operator-(const double& scalar) const; | |
| 100 | 102 | virtual GMatrix operator*(const GMatrix& matrix) const; |
| 101 | 103 | virtual GMatrix operator-(void) const; |
| 102 | 104 | virtual GMatrix& operator+=(const GMatrix& matrix); |
| 105 | + virtual GMatrix& operator+=(const double& scalar); | |
| 103 | 106 | virtual GMatrix& operator-=(const GMatrix& matrix); |
| 107 | + virtual GMatrix& operator-=(const double& scalar); | |
| 104 | 108 | virtual GMatrix& operator*=(const GMatrix& matrix); |
| 105 | 109 | virtual GMatrix& operator*=(const double& scalar); |
| 106 | 110 | virtual GMatrix& operator/=(const double& scalar); |
| ... | ... | @@ -207,6 +211,24 @@ GMatrix GMatrix::operator+(const GMatrix& matrix) const |
| 207 | 211 | |
| 208 | 212 | |
| 209 | 213 | /***********************************************************************//** |
| 214 | + * @brief Binary matrix scalar addition | |
| 215 | + * | |
| 216 | + * @param[in] double Scalar. | |
| 217 | + * @return Result of matrix addition. | |
| 218 | + * | |
| 219 | + * Returns the sum of the matrix with another matrix were all entries have | |
| 220 | + * the value of scalar. The method makes use of the unary addition operator. | |
| 221 | + ***************************************************************************/ | |
| 222 | +inline | |
| 223 | +GMatrix GMatrix::operator+(const double& scalar) const | |
| 224 | +{ | |
| 225 | + GMatrix result = *this; | |
| 226 | + result += scalar; | |
| 227 | + return result; | |
| 228 | +} | |
| 229 | + | |
| 230 | + | |
| 231 | +/***********************************************************************//** | |
| 210 | 232 | * @brief Binary matrix subtraction |
| 211 | 233 | * |
| 212 | 234 | * @param[in] matrix Matrix. |
| ... | ... | @@ -225,6 +247,24 @@ GMatrix GMatrix::operator-(const GMatrix& matrix) const |
| 225 | 247 | |
| 226 | 248 | |
| 227 | 249 | /***********************************************************************//** |
| 250 | + * @brief Binary matrix scalar subtraction | |
| 251 | + * | |
| 252 | + * @param[in] double Scalar. | |
| 253 | + * @return Result of matrix subtraction. | |
| 254 | + * | |
| 255 | + * Returns the difference of the matrix with another matrix were all entries have | |
| 256 | + * the value of scalar. The method makes use of the unary subtraction operator. | |
| 257 | + ***************************************************************************/ | |
| 258 | +inline | |
| 259 | +GMatrix GMatrix::operator+(const double& scalar) const | |
| 260 | +{ | |
| 261 | + GMatrix result = *this; | |
| 262 | + result -= scalar; | |
| 263 | + return result; | |
| 264 | +} | |
| 265 | + | |
| 266 | + | |
| 267 | +/***********************************************************************//** | |
| 228 | 268 | * @brief Binary matrix multiplication |
| 229 | 269 | * |
| 230 | 270 | * @param[in] matrix Matrix. | ... | ... |
src/linalg/GMatrix.cpp
| ... | ... | @@ -362,6 +362,26 @@ GMatrix& GMatrix::operator+=(const GMatrix& matrix) |
| 362 | 362 | |
| 363 | 363 | |
| 364 | 364 | /***********************************************************************//** |
| 365 | + * @brief Unary matrix scalar addition operator | |
| 366 | + * | |
| 367 | + * @param[in] double Scalar. | |
| 368 | + * | |
| 369 | + * This method adds a given scalar to each matrix element. | |
| 370 | + ***************************************************************************/ | |
| 371 | +GMatrix& GMatrix::operator+=(const double& scalar) | |
| 372 | +{ | |
| 373 | + // Add all matrix elements | |
| 374 | + double* dst = m_data; | |
| 375 | + for (int i = 0; i < m_elements; ++i) { | |
| 376 | + *dst++ += scalar; | |
| 377 | + } | |
| 378 | + | |
| 379 | + // Return result | |
| 380 | + return *this; | |
| 381 | +} | |
| 382 | + | |
| 383 | + | |
| 384 | +/***********************************************************************//** | |
| 365 | 385 | * @brief Unary matrix subtraction operator |
| 366 | 386 | * |
| 367 | 387 | * @param[in] matrix Matrix. |
| ... | ... | @@ -394,6 +414,26 @@ GMatrix& GMatrix::operator-=(const GMatrix& matrix) |
| 394 | 414 | |
| 395 | 415 | |
| 396 | 416 | /***********************************************************************//** |
| 417 | + * @brief Unary matrix scalar subtraction operator | |
| 418 | + * | |
| 419 | + * @param[in] double Scalar. | |
| 420 | + * | |
| 421 | + * This method subtracts a given scalar from each matrix element. | |
| 422 | + ***************************************************************************/ | |
| 423 | +GMatrix& GMatrix::operator+=(const double& scalar) | |
| 424 | +{ | |
| 425 | + // Add all matrix elements | |
| 426 | + double* dst = m_data; | |
| 427 | + for (int i = 0; i < m_elements; ++i) { | |
| 428 | + *dst++ -= scalar; | |
| 429 | + } | |
| 430 | + | |
| 431 | + // Return result | |
| 432 | + return *this; | |
| 433 | +} | |
| 434 | + | |
| 435 | + | |
| 436 | +/***********************************************************************//** | |
| 397 | 437 | * @brief Unary matrix multiplication operator |
| 398 | 438 | * |
| 399 | 439 | * @param[in] matrix Matrix. | ... | ... |