Commit 5389343111815ca2a5eadc05168e4a9520c659af
1 parent
0796cf5e
Add clear() member functions to matrix class
Showing
5 changed files
with
44 additions
and
9 deletions
src/GMatrix.cpp
| ... | ... | @@ -495,6 +495,20 @@ GMatrix& GMatrix::operator/= (const double& d) |
| 495 | 495 | ==========================================================================*/ |
| 496 | 496 | |
| 497 | 497 | /*************************************************************************** |
| 498 | + * Set all matrix elements to zero * | |
| 499 | + ***************************************************************************/ | |
| 500 | +void GMatrix::clear() | |
| 501 | +{ | |
| 502 | + // Loop over all matrix elements | |
| 503 | + for (int i = 0; i < m_elements; ++i) | |
| 504 | + m_data[i] = 0.0; | |
| 505 | + | |
| 506 | + // Return | |
| 507 | + return; | |
| 508 | +} | |
| 509 | + | |
| 510 | + | |
| 511 | +/*************************************************************************** | |
| 498 | 512 | * Get minimum physical matrix element * |
| 499 | 513 | ***************************************************************************/ |
| 500 | 514 | double GMatrix::min() const | ... | ... |
src/GMatrix.hpp
src/GSparseMatrix.hpp
| ... | ... | @@ -84,7 +84,8 @@ public: |
| 84 | 84 | // Matrix functions |
| 85 | 85 | // USE_BASE: virtual int rows() const { return m_rows; } |
| 86 | 86 | // USE_BASE: virtual int cols() const { return m_cols; } |
| 87 | - virtual double fill() const { return (double(m_elements)/double(m_rows*m_cols)); } | |
| 87 | + virtual void clear(); | |
| 88 | + virtual double fill() const; | |
| 88 | 89 | virtual double min() const; |
| 89 | 90 | virtual double max() const; |
| 90 | 91 | virtual double sum() const; |
| ... | ... | @@ -244,6 +245,21 @@ GSparseMatrix operator/ (const GSparseMatrix& a, const double& b) |
| 244 | 245 | return result; |
| 245 | 246 | } |
| 246 | 247 | |
| 248 | +// Matrix clear function | |
| 249 | +inline | |
| 250 | +void GSparseMatrix::clear() | |
| 251 | +{ | |
| 252 | + free_elements(0, m_elements); | |
| 253 | + return; | |
| 254 | +} | |
| 255 | + | |
| 256 | +// Matrix fill function | |
| 257 | +inline | |
| 258 | +double GSparseMatrix::fill() const | |
| 259 | +{ | |
| 260 | + return (double(m_elements)/double(m_rows*m_cols)); | |
| 261 | +} | |
| 262 | + | |
| 247 | 263 | // Matrix transpose function |
| 248 | 264 | inline |
| 249 | 265 | GSparseMatrix transpose(const GSparseMatrix& m) | ... | ... |
src/GSymMatrix.hpp
| ... | ... | @@ -77,8 +77,9 @@ public: |
| 77 | 77 | virtual GSymMatrix& operator/= (const double& d); |
| 78 | 78 | |
| 79 | 79 | // Matrix functions |
| 80 | - // USE_BASE: virtual int rows() const { return m_rows; } | |
| 81 | - // USE_BASE: virtual int cols() const { return m_cols; } | |
| 80 | + // USE_BASE: virtual int rows() const { return m_rows; } | |
| 81 | + // USE_BASE: virtual int cols() const { return m_cols; } | |
| 82 | + // USE_BASE: virtual void clear(); | |
| 82 | 83 | // USE_BASE: virtual double min() const; |
| 83 | 84 | // USE_BASE: virtual double max() const; |
| 84 | 85 | virtual double sum() const; |
| ... | ... | @@ -92,12 +93,6 @@ public: |
| 92 | 93 | virtual GVector cholesky_solver(const GVector& v, int compress = 1); |
| 93 | 94 | virtual void cholesky_invert(int compress = 1); |
| 94 | 95 | |
| 95 | - // Exception: All matrix elements are zero | |
| 96 | - class all_zero : public GException { | |
| 97 | - public: | |
| 98 | - all_zero(string origin); | |
| 99 | - }; | |
| 100 | - | |
| 101 | 96 | private: |
| 102 | 97 | void set_inx(void); // Set indices of non-zero rows/columns |
| 103 | 98 | int m_num_inx; // Number of non-zero rows/columns | ... | ... |
test/test_GSparseMatrix.cpp
| ... | ... | @@ -349,6 +349,15 @@ void test_assign_values(const GSparseMatrix& m_test) |
| 349 | 349 | throw; |
| 350 | 350 | } |
| 351 | 351 | cout << "."; |
| 352 | + // | |
| 353 | + result.clear(); | |
| 354 | + if (!check_matrix(result, 0.0, 0.0) || !check_matrix(m_test, 1.0, 0.0)) { | |
| 355 | + cout << endl << "TEST ERROR: Corrupt GSparseMatrix.clear()." << endl; | |
| 356 | + cout << "result:" << endl << result << endl; | |
| 357 | + cout << "m_test:" << endl << m_test << endl; | |
| 358 | + throw; | |
| 359 | + } | |
| 360 | + cout << "."; | |
| 352 | 361 | } |
| 353 | 362 | catch (exception &e) { |
| 354 | 363 | cout << e.what() << endl; | ... | ... |