Commit b898b00e8d49c5aa1156abee0283d6bfde08bcf1
Merge branch 'pull-request-1990-gmatrix-add-constant' into integration
Showing
15 changed files
with
357 additions
and
13 deletions
ChangeLog
| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | * Version 1.3.0 released |
| 4 | 4 | ========================= |
| 5 | 5 | |
| 6 | + Add scalar addition and subtraction operators to matrix classes (#1990) | |
| 6 | 7 | Allow fitting of scale parameter in composite models (#1992) |
| 7 | 8 | Add GCTAAeff::ebounds() method (#1989) |
| 8 | 9 | Add GEbounds::emin() and GEbounds::emax() setter methods (#1989) | ... | ... |
NEWS
| ... | ... | @@ -28,6 +28,18 @@ The following methods have been added: |
| 28 | 28 | - GEbounds::emax(int&, GEnergy&) |
| 29 | 29 | - GEnergies::set(GEbounds&) |
| 30 | 30 | - GFilename::type() |
| 31 | +- GMatrix::operator+(double&) | |
| 32 | +- GMatrix::operator-(double&) | |
| 33 | +- GMatrix::operator+=(double&) | |
| 34 | +- GMatrix::operator-=(double&) | |
| 35 | +- GMatrixSymmetric::operator+(double&) | |
| 36 | +- GMatrixSymmetric::operator-(double&) | |
| 37 | +- GMatrixSymmetric::operator+=(double&) | |
| 38 | +- GMatrixSymmetric::operator-=(double&) | |
| 39 | +- GMatrixSparse::operator+(double&) | |
| 40 | +- GMatrixSparse::operator-(double&) | |
| 41 | +- GMatrixSparse::operator+=(double&) | |
| 42 | +- GMatrixSparse::operator-=(double&) | |
| 31 | 43 | - GCTAResponseIrf::rspname() |
| 32 | 44 | - GCTARoi::contains(GCTAInstDir&) |
| 33 | 45 | - GCTAEdisp2D::fetch() |
| ... | ... | @@ -84,7 +96,8 @@ image of any type into a specific storage class (#1986). |
| 84 | 96 | |
| 85 | 97 | 7. Linear algebra module |
| 86 | 98 | ------------------------ |
| 87 | -None | |
| 99 | +Add operators for addition and subtraction of scalars to all matrix classes | |
| 100 | +(#1990). | |
| 88 | 101 | |
| 89 | 102 | |
| 90 | 103 | 8. Model module | ... | ... |
include/GMatrix.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrix.hpp - General matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -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,23 @@ GMatrix GMatrix::operator+(const GMatrix& matrix) const |
| 207 | 211 | |
| 208 | 212 | |
| 209 | 213 | /***********************************************************************//** |
| 214 | + * @brief Binary matrix scalar addition | |
| 215 | + * | |
| 216 | + * @param[in] scalar Scalar. | |
| 217 | + * @return Matrix with @p scalar added. | |
| 218 | + * | |
| 219 | + * Returns a matrix where a @p scalar has been added to each matrix element. | |
| 220 | + ***************************************************************************/ | |
| 221 | +inline | |
| 222 | +GMatrix GMatrix::operator+(const double& scalar) const | |
| 223 | +{ | |
| 224 | + GMatrix result = *this; | |
| 225 | + result += scalar; | |
| 226 | + return result; | |
| 227 | +} | |
| 228 | + | |
| 229 | + | |
| 230 | +/***********************************************************************//** | |
| 210 | 231 | * @brief Binary matrix subtraction |
| 211 | 232 | * |
| 212 | 233 | * @param[in] matrix Matrix. |
| ... | ... | @@ -225,6 +246,24 @@ GMatrix GMatrix::operator-(const GMatrix& matrix) const |
| 225 | 246 | |
| 226 | 247 | |
| 227 | 248 | /***********************************************************************//** |
| 249 | + * @brief Binary matrix scalar subtraction | |
| 250 | + * | |
| 251 | + * @param[in] scalar Scalar. | |
| 252 | + * @return Matrix with @p scalar subtracted. | |
| 253 | + * | |
| 254 | + * Returns a matrix where a @p scalar has been subtracted from each matrix | |
| 255 | + * element. | |
| 256 | + ***************************************************************************/ | |
| 257 | +inline | |
| 258 | +GMatrix GMatrix::operator-(const double& scalar) const | |
| 259 | +{ | |
| 260 | + GMatrix result = *this; | |
| 261 | + result -= scalar; | |
| 262 | + return result; | |
| 263 | +} | |
| 264 | + | |
| 265 | + | |
| 266 | +/***********************************************************************//** | |
| 228 | 267 | * @brief Binary matrix multiplication |
| 229 | 268 | * |
| 230 | 269 | * @param[in] matrix Matrix. | ... | ... |
include/GMatrixSparse.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSparse.hpp - Sparse matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -221,11 +221,15 @@ public: |
| 221 | 221 | virtual GMatrixSparse& operator=(const GMatrixSparse& matrix); |
| 222 | 222 | virtual GMatrixSparse& operator=(const double& value); |
| 223 | 223 | virtual GMatrixSparse operator+(const GMatrixSparse& matrix) const; |
| 224 | + virtual GMatrixSparse operator+(const double& scalar) const; | |
| 224 | 225 | virtual GMatrixSparse operator-(const GMatrixSparse& matrix) const; |
| 226 | + virtual GMatrixSparse operator-(const double& scalar) const; | |
| 225 | 227 | virtual GMatrixSparse operator*(const GMatrixSparse& matrix) const; |
| 226 | 228 | virtual GMatrixSparse operator-(void) const; |
| 227 | 229 | virtual GMatrixSparse& operator+=(const GMatrixSparse& matrix); |
| 230 | + virtual GMatrixSparse& operator+=(const double& scalar); | |
| 228 | 231 | virtual GMatrixSparse& operator-=(const GMatrixSparse& matrix); |
| 232 | + virtual GMatrixSparse& operator-=(const double& scalar); | |
| 229 | 233 | virtual GMatrixSparse& operator*=(const GMatrixSparse& matrix); |
| 230 | 234 | virtual GMatrixSparse& operator*=(const double& scalar); |
| 231 | 235 | virtual GMatrixSparse& operator/=(const double& scalar); |
| ... | ... | @@ -359,6 +363,23 @@ GMatrixSparse GMatrixSparse::operator+(const GMatrixSparse& matrix) const |
| 359 | 363 | |
| 360 | 364 | |
| 361 | 365 | /***********************************************************************//** |
| 366 | + * @brief Binary matrix scalar addition | |
| 367 | + * | |
| 368 | + * @param[in] scalar Scalar. | |
| 369 | + * @return Matrix with @p scalar added. | |
| 370 | + * | |
| 371 | + * Returns a matrix where a @p scalar has been added to each matrix element. | |
| 372 | + ***************************************************************************/ | |
| 373 | +inline | |
| 374 | +GMatrixSparse GMatrixSparse::operator+(const double& scalar) const | |
| 375 | +{ | |
| 376 | + GMatrixSparse result = *this; | |
| 377 | + result += scalar; | |
| 378 | + return result; | |
| 379 | +} | |
| 380 | + | |
| 381 | + | |
| 382 | +/***********************************************************************//** | |
| 362 | 383 | * @brief Binary matrix subtraction |
| 363 | 384 | * |
| 364 | 385 | * @param[in] matrix Matrix. |
| ... | ... | @@ -377,6 +398,24 @@ GMatrixSparse GMatrixSparse::operator-(const GMatrixSparse& matrix) const |
| 377 | 398 | |
| 378 | 399 | |
| 379 | 400 | /***********************************************************************//** |
| 401 | + * @brief Binary matrix scalar subtraction | |
| 402 | + * | |
| 403 | + * @param[in] scalar Scalar. | |
| 404 | + * @return Matrix with @p scalar subtracted. | |
| 405 | + * | |
| 406 | + * Returns a matrix where a @p scalar has been subtracted from each matrix | |
| 407 | + * element. | |
| 408 | + ***************************************************************************/ | |
| 409 | +inline | |
| 410 | +GMatrixSparse GMatrixSparse::operator-(const double& scalar) const | |
| 411 | +{ | |
| 412 | + GMatrixSparse result = *this; | |
| 413 | + result -= scalar; | |
| 414 | + return result; | |
| 415 | +} | |
| 416 | + | |
| 417 | + | |
| 418 | +/***********************************************************************//** | |
| 380 | 419 | * @brief Binary matrix multiplication |
| 381 | 420 | * |
| 382 | 421 | * @param[in] matrix Matrix. | ... | ... |
include/GMatrixSymmetric.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSymmetric.hpp - Symmetric matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -88,11 +88,15 @@ public: |
| 88 | 88 | virtual GMatrixSymmetric& operator=(const GMatrixSymmetric& matrix); |
| 89 | 89 | virtual GMatrixSymmetric& operator=(const double& value); |
| 90 | 90 | virtual GMatrixSymmetric operator+(const GMatrixSymmetric& matrix) const; |
| 91 | + virtual GMatrixSymmetric operator+(const double& scalar) const; | |
| 91 | 92 | virtual GMatrixSymmetric operator-(const GMatrixSymmetric& matrix) const; |
| 93 | + virtual GMatrixSymmetric operator-(const double& scalar) const; | |
| 92 | 94 | virtual GMatrix operator*(const GMatrixSymmetric& matrix) const; |
| 93 | 95 | virtual GMatrixSymmetric operator-(void) const; |
| 94 | 96 | virtual GMatrixSymmetric& operator+=(const GMatrixSymmetric& matrix); |
| 97 | + virtual GMatrixSymmetric& operator+=(const double& scalar); | |
| 95 | 98 | virtual GMatrixSymmetric& operator-=(const GMatrixSymmetric& matrix); |
| 99 | + virtual GMatrixSymmetric& operator-=(const double& scalar); | |
| 96 | 100 | virtual GMatrixSymmetric& operator*=(const double& scaler); |
| 97 | 101 | virtual GMatrixSymmetric& operator/=(const double& scalar); |
| 98 | 102 | |
| ... | ... | @@ -214,6 +218,23 @@ GMatrixSymmetric GMatrixSymmetric::operator+(const GMatrixSymmetric& matrix) con |
| 214 | 218 | |
| 215 | 219 | |
| 216 | 220 | /***********************************************************************//** |
| 221 | + * @brief Binary matrix scalar addition | |
| 222 | + * | |
| 223 | + * @param[in] scalar Scalar. | |
| 224 | + * @return Matrix with @p scalar added. | |
| 225 | + * | |
| 226 | + * Returns a matrix where a @p scalar has been added to each matrix element. | |
| 227 | + ***************************************************************************/ | |
| 228 | +inline | |
| 229 | +GMatrixSymmetric GMatrixSymmetric::operator+(const double& scalar) const | |
| 230 | +{ | |
| 231 | + GMatrixSymmetric result = *this; | |
| 232 | + result += scalar; | |
| 233 | + return result; | |
| 234 | +} | |
| 235 | + | |
| 236 | + | |
| 237 | +/***********************************************************************//** | |
| 217 | 238 | * @brief Binary matrix subtraction |
| 218 | 239 | * |
| 219 | 240 | * @param[in] matrix Matrix. |
| ... | ... | @@ -232,6 +253,24 @@ GMatrixSymmetric GMatrixSymmetric::operator-(const GMatrixSymmetric& matrix) con |
| 232 | 253 | |
| 233 | 254 | |
| 234 | 255 | /***********************************************************************//** |
| 256 | + * @brief Binary matrix scalar subtraction | |
| 257 | + * | |
| 258 | + * @param[in] scalar Scalar. | |
| 259 | + * @return Matrix with @p scalar subtracted. | |
| 260 | + * | |
| 261 | + * Returns a matrix where a @p scalar has been subtracted from each matrix | |
| 262 | + * element. | |
| 263 | + ***************************************************************************/ | |
| 264 | +inline | |
| 265 | +GMatrixSymmetric GMatrixSymmetric::operator-(const double& scalar) const | |
| 266 | +{ | |
| 267 | + GMatrixSymmetric result = *this; | |
| 268 | + result -= scalar; | |
| 269 | + return result; | |
| 270 | +} | |
| 271 | + | |
| 272 | + | |
| 273 | +/***********************************************************************//** | |
| 235 | 274 | * @brief Scale matrix elements |
| 236 | 275 | * |
| 237 | 276 | * @param[in] scalar Scale factor. | ... | ... |
pyext/GMatrix.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrix.i - General Matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -53,8 +53,12 @@ public: |
| 53 | 53 | virtual GMatrix operator+(const GMatrix& matrix) const; |
| 54 | 54 | virtual GMatrix operator-(const GMatrix& matrix) const; |
| 55 | 55 | virtual GMatrix operator-(void) const; |
| 56 | + virtual GMatrix operator+(const double& scalar) const; | |
| 57 | + virtual GMatrix operator-(const double& scalar) const; | |
| 56 | 58 | virtual GMatrix& operator+=(const GMatrix& matrix); |
| 59 | + virtual GMatrix& operator+=(const double& scalar); | |
| 57 | 60 | virtual GMatrix& operator-=(const GMatrix& matrix); |
| 61 | + virtual GMatrix& operator-=(const double& scalar); | |
| 58 | 62 | virtual GMatrix& operator*=(const GMatrix& matrix); |
| 59 | 63 | virtual GMatrix& operator*=(const double& scalar); |
| 60 | 64 | ... | ... |
pyext/GMatrixSparse.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSparse.i - Sparse matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -55,10 +55,14 @@ public: |
| 55 | 55 | |
| 56 | 56 | // Other operators |
| 57 | 57 | virtual GMatrixSparse operator+(const GMatrixSparse& matrix) const; |
| 58 | + virtual GMatrixSparse operator+(const double& scalar) const; | |
| 58 | 59 | virtual GMatrixSparse operator-(const GMatrixSparse& matrix) const; |
| 60 | + virtual GMatrixSparse operator-(const double& scalar) const; | |
| 59 | 61 | virtual GMatrixSparse operator-(void) const; |
| 60 | 62 | virtual GMatrixSparse& operator+=(const GMatrixSparse& matrix); |
| 63 | + virtual GMatrixSparse& operator+=(const double& scalar); | |
| 61 | 64 | virtual GMatrixSparse& operator-=(const GMatrixSparse& matrix); |
| 65 | + virtual GMatrixSparse& operator-=(const double& scalar); | |
| 62 | 66 | virtual GMatrixSparse& operator*=(const GMatrixSparse& matrix); |
| 63 | 67 | virtual GMatrixSparse& operator*=(const double& scalar); |
| 64 | 68 | ... | ... |
pyext/GMatrixSymmetric.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSymmetric.i - Symmetric matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -50,10 +50,14 @@ public: |
| 50 | 50 | |
| 51 | 51 | // Other operators |
| 52 | 52 | virtual GMatrixSymmetric operator+(const GMatrixSymmetric& matrix) const; |
| 53 | + virtual GMatrixSymmetric operator+(const double& scalar) const; | |
| 53 | 54 | virtual GMatrixSymmetric operator-(const GMatrixSymmetric& matrix) const; |
| 55 | + virtual GMatrixSymmetric operator-(const double& scalar) const; | |
| 54 | 56 | virtual GMatrixSymmetric operator-(void) const; |
| 55 | 57 | virtual GMatrixSymmetric& operator+=(const GMatrixSymmetric& matrix); |
| 58 | + virtual GMatrixSymmetric& operator+=(const double& scalar); | |
| 56 | 59 | virtual GMatrixSymmetric& operator-=(const GMatrixSymmetric& matrix); |
| 60 | + virtual GMatrixSymmetric& operator-=(const double& scalar); | |
| 57 | 61 | virtual GMatrixSymmetric& operator*=(const double& scaler); |
| 58 | 62 | |
| 59 | 63 | // Implemented pure virtual base class methods | ... | ... |
src/linalg/GMatrix.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrix.cpp - General matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -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] scaler Scalar. | |
| 368 | + * | |
| 369 | + * Adds a @p 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] scalar Scalar. | |
| 420 | + * | |
| 421 | + * Subtracts a @p 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. | ... | ... |
src/linalg/GMatrixSparse.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSparse.cpp - Sparse matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -572,6 +572,27 @@ GMatrixSparse& GMatrixSparse::operator+=(const GMatrixSparse& matrix) |
| 572 | 572 | |
| 573 | 573 | |
| 574 | 574 | /***********************************************************************//** |
| 575 | + * @brief Unary matrix scalar addition operator | |
| 576 | + * | |
| 577 | + * @param[in] scaler Scalar. | |
| 578 | + * | |
| 579 | + * Adds a @p scalar to each matrix element. | |
| 580 | + ***************************************************************************/ | |
| 581 | +GMatrixSparse& GMatrixSparse::operator+=(const double& scalar) | |
| 582 | +{ | |
| 583 | + // Perform inplace matrix addition using vectors | |
| 584 | + for (int col = 0; col < m_cols; ++col) { | |
| 585 | + GVector vector = column(col); | |
| 586 | + vector += scalar; | |
| 587 | + column(col, vector); | |
| 588 | + } | |
| 589 | + | |
| 590 | + // Return result | |
| 591 | + return *this; | |
| 592 | +} | |
| 593 | + | |
| 594 | + | |
| 595 | +/***********************************************************************//** | |
| 575 | 596 | * @brief Unary matrix subtraction operator |
| 576 | 597 | * |
| 577 | 598 | * @param[in] matrix Matrix. |
| ... | ... | @@ -607,6 +628,27 @@ GMatrixSparse& GMatrixSparse::operator-=(const GMatrixSparse& matrix) |
| 607 | 628 | |
| 608 | 629 | |
| 609 | 630 | /***********************************************************************//** |
| 631 | + * @brief Unary matrix scalar subtraction operator | |
| 632 | + * | |
| 633 | + * @param[in] scalar Scalar. | |
| 634 | + * | |
| 635 | + * Subtracts a @p scalar from each matrix element. | |
| 636 | + ***************************************************************************/ | |
| 637 | +GMatrixSparse& GMatrixSparse::operator-=(const double& scalar) | |
| 638 | +{ | |
| 639 | + // Perform inplace matrix subtraction | |
| 640 | + for (int col = 0; col < m_cols; ++col) { | |
| 641 | + GVector vector = column(col); | |
| 642 | + vector -= scalar; | |
| 643 | + column(col, vector); | |
| 644 | + } | |
| 645 | + | |
| 646 | + // Return result | |
| 647 | + return *this; | |
| 648 | +} | |
| 649 | + | |
| 650 | + | |
| 651 | +/***********************************************************************//** | |
| 610 | 652 | * @brief Unary matrix multiplication operator |
| 611 | 653 | * |
| 612 | 654 | * @param[in] matrix Matrix. | ... | ... |
src/linalg/GMatrixSymmetric.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMatrixSymmetric.cpp - Symmetric matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -412,6 +412,26 @@ GMatrixSymmetric& GMatrixSymmetric::operator+=(const GMatrixSymmetric& matrix) |
| 412 | 412 | |
| 413 | 413 | |
| 414 | 414 | /***********************************************************************//** |
| 415 | + * @brief Unary matrix scalar addition operator | |
| 416 | + * | |
| 417 | + * @param[in] scaler Scalar. | |
| 418 | + * | |
| 419 | + * Adds a @p scalar to each matrix element. | |
| 420 | + ***************************************************************************/ | |
| 421 | +GMatrixSymmetric& GMatrixSymmetric::operator+=(const double& scalar) | |
| 422 | +{ | |
| 423 | + // Add all matrix elements | |
| 424 | + double* dst = m_data; | |
| 425 | + for (int i = 0; i < m_elements; ++i) { | |
| 426 | + *dst++ += scalar; | |
| 427 | + } | |
| 428 | + | |
| 429 | + // Return result | |
| 430 | + return *this; | |
| 431 | +} | |
| 432 | + | |
| 433 | + | |
| 434 | +/***********************************************************************//** | |
| 415 | 435 | * @brief Unary matrix subtraction operator |
| 416 | 436 | * |
| 417 | 437 | * @param[in] matrix Matrix. |
| ... | ... | @@ -443,6 +463,26 @@ GMatrixSymmetric& GMatrixSymmetric::operator-=(const GMatrixSymmetric& matrix) |
| 443 | 463 | } |
| 444 | 464 | |
| 445 | 465 | |
| 466 | +/***********************************************************************//** | |
| 467 | + * @brief Unary matrix scalar subtraction operator | |
| 468 | + * | |
| 469 | + * @param[in] scalar Scalar. | |
| 470 | + * | |
| 471 | + * Subtracts a @p scalar from each matrix element. | |
| 472 | + ***************************************************************************/ | |
| 473 | +GMatrixSymmetric& GMatrixSymmetric::operator-=(const double& scalar) | |
| 474 | +{ | |
| 475 | + // Add all matrix elements | |
| 476 | + double* dst = m_data; | |
| 477 | + for (int i = 0; i < m_elements; ++i) { | |
| 478 | + *dst++ -= scalar; | |
| 479 | + } | |
| 480 | + | |
| 481 | + // Return result | |
| 482 | + return *this; | |
| 483 | +} | |
| 484 | + | |
| 485 | + | |
| 446 | 486 | /*========================================================================== |
| 447 | 487 | = = |
| 448 | 488 | = Public methods = | ... | ... |
test/Makefile.am
| ... | ... | @@ -205,6 +205,7 @@ all-local: |
| 205 | 205 | # Files to be removed in build directory |
| 206 | 206 | clean-local: |
| 207 | 207 | rm -rf $(top_builddir)/test/*.fits |
| 208 | + rm -rf $(top_builddir)/test/*.csv | |
| 208 | 209 | rm -rf $(top_builddir)/test/*.log |
| 209 | 210 | rm -rf $(top_builddir)/test/*.html |
| 210 | 211 | rm -rf $(top_builddir)/test/*.xml | ... | ... |
test/test_GMatrix.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GMatrix.cpp - Test generic matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -532,6 +532,32 @@ void TestGMatrix::matrix_arithmetics(void) |
| 532 | 532 | test_assert(check_matrix(m_test), "Test source matrix"); |
| 533 | 533 | test_assert(check_matrix(test, 1.0/3.0, 0.0), "Test GMatrix / 3.0", |
| 534 | 534 | "Unexpected result matrix:\n"+test.print()); |
| 535 | + | |
| 536 | + // GMatrix + 3.0 | |
| 537 | + test = m_test + 3.0; | |
| 538 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 539 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrix + 3.0", | |
| 540 | + "Unexpected result matrix:\n"+test.print()); | |
| 541 | + | |
| 542 | + // GMatrix += 3.0 | |
| 543 | + test = m_test; | |
| 544 | + test += 3.0; | |
| 545 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 546 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrix + 3.0", | |
| 547 | + "Unexpected result matrix:\n"+test.print()); | |
| 548 | + | |
| 549 | + // GMatrix - 5.0 | |
| 550 | + test = m_test - 5.0; | |
| 551 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 552 | + test_assert(check_matrix(test, 1.0, -5.0), "Test GMatrix -= 5.0", | |
| 553 | + "Unexpected result matrix:\n"+test.print()); | |
| 554 | + | |
| 555 | + // GMatrix -= 3.0 | |
| 556 | + test = m_test; | |
| 557 | + test -= 3.0; | |
| 558 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 559 | + test_assert(check_matrix(test, 1.0, -3.0), "Test GMatrix -= 3.0", | |
| 560 | + "Unexpected result matrix:\n"+test.print()); | |
| 535 | 561 | |
| 536 | 562 | // Test invalid matrix addition |
| 537 | 563 | test_try("Test invalid matrix addition"); | ... | ... |
test/test_GMatrixSparse.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GMatrixSparse.cpp - Test sparse matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -861,6 +861,32 @@ void TestGMatrixSparse::matrix_arithmetics(void) |
| 861 | 861 | test_assert(check_matrix(test, 1.0/3.0, 0.0), "Test GMatrixSparse / 3.0", |
| 862 | 862 | "Unexpected result matrix:\n"+test.print()); |
| 863 | 863 | |
| 864 | + // GMatrixSparse + 3.0 | |
| 865 | + test = m_test + 3.0; | |
| 866 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 867 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrixSparse + 3.0", | |
| 868 | + "Unexpected result matrix:\n"+test.print()); | |
| 869 | + | |
| 870 | + // GMatrixSparse += 3.0 | |
| 871 | + test = m_test; | |
| 872 | + test += 3.0; | |
| 873 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 874 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrixSparse + 3.0", | |
| 875 | + "Unexpected result matrix:\n"+test.print()); | |
| 876 | + | |
| 877 | + // GMatrixSparse - 5.0 | |
| 878 | + test = m_test - 5.0; | |
| 879 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 880 | + test_assert(check_matrix(test, 1.0, -5.0), "Test GMatrixSparse -= 5.0", | |
| 881 | + "Unexpected result matrix:\n"+test.print()); | |
| 882 | + | |
| 883 | + // GMatrixSparse -= 3.0 | |
| 884 | + test = m_test; | |
| 885 | + test -= 3.0; | |
| 886 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 887 | + test_assert(check_matrix(test, 1.0, -3.0), "Test GMatrixSparse -= 3.0", | |
| 888 | + "Unexpected result matrix:\n"+test.print()); | |
| 889 | + | |
| 864 | 890 | // Test invalid matrix addition |
| 865 | 891 | test_try("Test invalid matrix addition"); |
| 866 | 892 | try { | ... | ... |
test/test_GMatrixSymmetric.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GMatrixSymmetric.cpp - Test symmetric matrix class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -537,6 +537,32 @@ void TestGMatrixSymmetric::matrix_arithmetics(void) |
| 537 | 537 | test_assert(check_matrix(test, 1.0/3.0, 0.0), "Test GMatrixSymmetric / 3.0", |
| 538 | 538 | test.print()); |
| 539 | 539 | |
| 540 | + // GMatrixSymmetric + 3.0 | |
| 541 | + test = m_test + 3.0; | |
| 542 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 543 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrixSymmetric + 3.0", | |
| 544 | + "Unexpected result matrix:\n"+test.print()); | |
| 545 | + | |
| 546 | + // GMatrixSymmetric += 3.0 | |
| 547 | + test = m_test; | |
| 548 | + test += 3.0; | |
| 549 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 550 | + test_assert(check_matrix(test, 1.0, 3.0), "Test GMatrixSymmetric + 3.0", | |
| 551 | + "Unexpected result matrix:\n"+test.print()); | |
| 552 | + | |
| 553 | + // GMatrixSymmetric - 5.0 | |
| 554 | + test = m_test - 5.0; | |
| 555 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 556 | + test_assert(check_matrix(test, 1.0, -5.0), "Test GMatrixSymmetric -= 5.0", | |
| 557 | + "Unexpected result matrix:\n"+test.print()); | |
| 558 | + | |
| 559 | + // GMatrixSymmetric -= 3.0 | |
| 560 | + test = m_test; | |
| 561 | + test -= 3.0; | |
| 562 | + test_assert(check_matrix(m_test), "Test source matrix"); | |
| 563 | + test_assert(check_matrix(test, 1.0, -3.0), "Test GMatrixSymmetric -= 3.0", | |
| 564 | + "Unexpected result matrix:\n"+test.print()); | |
| 565 | + | |
| 540 | 566 | // Test invalid matrix addition |
| 541 | 567 | test_try("Test invalid matrix addition"); |
| 542 | 568 | try { | ... | ... |