Commit af6115d312e89b28c9d0356ea4d721ea824fe321
1 parent
4c992235
replace operator() by operator[], declare interpolate() and member access method…
…s const, add print() method
Showing
3 changed files
with
126 additions
and
70 deletions
include/GNodeArray.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GNodeArray.hpp - Array of nodes class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2010 by Jurgen Knodlseder * | |
| 4 | + * copyright (C) 2008-2011 by Jurgen Knodlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software; you can redistribute it and/or modify * |
| ... | ... | @@ -12,7 +12,7 @@ |
| 12 | 12 | ***************************************************************************/ |
| 13 | 13 | /** |
| 14 | 14 | * @file GNodeArray.hpp |
| 15 | - * @brief GNodeArray class interface definition. | |
| 15 | + * @brief Node array class interface definition | |
| 16 | 16 | * @author J. Knodlseder |
| 17 | 17 | */ |
| 18 | 18 | |
| ... | ... | @@ -21,14 +21,16 @@ |
| 21 | 21 | |
| 22 | 22 | /* __ Includes ___________________________________________________________ */ |
| 23 | 23 | #include <vector> |
| 24 | +#include <string> | |
| 24 | 25 | #include <iostream> |
| 26 | +#include "GLog.hpp" | |
| 25 | 27 | #include "GVector.hpp" |
| 26 | 28 | |
| 27 | 29 | |
| 28 | 30 | /***********************************************************************//** |
| 29 | 31 | * @class GNodeArray |
| 30 | 32 | * |
| 31 | - * @brief Interface for the node array class. | |
| 33 | + * @brief Interface for the node array class | |
| 32 | 34 | * |
| 33 | 35 | * The node array class collects nodes \f$x_i\f$ that may be used to describe |
| 34 | 36 | * a functional relation \f$y_i=f(x_i)\f$. This class may be used to perform |
| ... | ... | @@ -46,7 +48,8 @@ |
| 46 | 48 | class GNodeArray { |
| 47 | 49 | |
| 48 | 50 | // I/O friends |
| 49 | - friend std::ostream& operator<< (std::ostream& os, const GNodeArray& array); | |
| 51 | + friend std::ostream& operator<<(std::ostream& os, const GNodeArray& array); | |
| 52 | + friend GLog& operator<<(GLog& log, const GNodeArray& array); | |
| 50 | 53 | |
| 51 | 54 | public: |
| 52 | 55 | // Constructors and destructors |
| ... | ... | @@ -56,23 +59,25 @@ public: |
| 56 | 59 | |
| 57 | 60 | // Operators |
| 58 | 61 | GNodeArray& operator= (const GNodeArray & array); |
| 59 | - double& operator() (int index); | |
| 60 | - const double& operator() (int index) const; | |
| 62 | + double& operator[](const int& index); | |
| 63 | + const double& operator[](const int& index) const; | |
| 61 | 64 | |
| 62 | 65 | // Methods |
| 63 | - void clear(void); | |
| 64 | - GNodeArray* clone(void) const; | |
| 65 | - int size(void) const { return m_node.size(); } | |
| 66 | - void nodes(const int& num, const double* array); | |
| 67 | - void nodes(const GVector& vector); | |
| 68 | - void nodes(const std::vector<double>& vector); | |
| 69 | - void append(const double& node); | |
| 70 | - double interpolate(const double& value, const std::vector<double>& vector); | |
| 71 | - void set_value(const double& value); | |
| 72 | - int inx_left(void) { return m_inx_left; } | |
| 73 | - int inx_right(void) { return m_inx_right; } | |
| 74 | - double wgt_left(void) { return m_wgt_left; } | |
| 75 | - double wgt_right(void) { return m_wgt_right; } | |
| 66 | + void clear(void); | |
| 67 | + GNodeArray* clone(void) const; | |
| 68 | + int size(void) const { return m_node.size(); } | |
| 69 | + void nodes(const int& num, const double* array); | |
| 70 | + void nodes(const GVector& vector); | |
| 71 | + void nodes(const std::vector<double>& vector); | |
| 72 | + void append(const double& node); | |
| 73 | + double interpolate(const double& value, | |
| 74 | + const std::vector<double>& vector) const; | |
| 75 | + void set_value(const double& value); | |
| 76 | + const int& inx_left(void) const { return m_inx_left; } | |
| 77 | + const int& inx_right(void) const { return m_inx_right; } | |
| 78 | + const double& wgt_left(void) const { return m_wgt_left; } | |
| 79 | + const double& wgt_right(void) const { return m_wgt_right; } | |
| 80 | + std::string print(void) const; | |
| 76 | 81 | |
| 77 | 82 | private: |
| 78 | 83 | // Methods | ... | ... |
pyext/GNodeArray.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GNodeArray.i - Array of nodes class SWIG definition * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright : (C) 2008-2011 by Jurgen Knodlseder * | |
| 4 | + * copyright (C) 2008-2011 by Jurgen Knodlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software; you can redistribute it and/or modify * |
| ... | ... | @@ -12,7 +12,7 @@ |
| 12 | 12 | ***************************************************************************/ |
| 13 | 13 | /** |
| 14 | 14 | * @file GNodeArray.i |
| 15 | - * @brief GNodeArray class SWIG file. | |
| 15 | + * @brief Node array class Python interface definition | |
| 16 | 16 | * @author J. Knodlseder |
| 17 | 17 | */ |
| 18 | 18 | %{ |
| ... | ... | @@ -25,7 +25,7 @@ |
| 25 | 25 | /***********************************************************************//** |
| 26 | 26 | * @class GNodeArray |
| 27 | 27 | * |
| 28 | - * @brief SWIG interface for the node array class. | |
| 28 | + * @brief Python interface for the node array class | |
| 29 | 29 | ***************************************************************************/ |
| 30 | 30 | class GNodeArray { |
| 31 | 31 | public: |
| ... | ... | @@ -35,19 +35,20 @@ public: |
| 35 | 35 | virtual ~GNodeArray(void); |
| 36 | 36 | |
| 37 | 37 | // Methods |
| 38 | - void clear(void); | |
| 39 | - GNodeArray* clone(void) const; | |
| 40 | - int size(void) { return m_node.size(); } | |
| 41 | - void nodes(const int& num, const double* array); | |
| 42 | - void nodes(const GVector& vector); | |
| 43 | - void nodes(const std::vector<double>& vector); | |
| 44 | - void append(const double& node); | |
| 45 | - double interpolate(const double& value, const std::vector<double>& vector); | |
| 46 | - void set_value(const double& value); | |
| 47 | - int inx_left(void) { return m_inx_left; } | |
| 48 | - int inx_right(void) { return m_inx_right; } | |
| 49 | - double wgt_left(void) { return m_wgt_left; } | |
| 50 | - double wgt_right(void) { return m_wgt_right; } | |
| 38 | + void clear(void); | |
| 39 | + GNodeArray* clone(void) const; | |
| 40 | + int size(void) const; | |
| 41 | + void nodes(const int& num, const double* array); | |
| 42 | + void nodes(const GVector& vector); | |
| 43 | + void nodes(const std::vector<double>& vector); | |
| 44 | + void append(const double& node); | |
| 45 | + double interpolate(const double& value, | |
| 46 | + const std::vector<double>& vector) const; | |
| 47 | + void set_value(const double& value); | |
| 48 | + const int& inx_left(void) const; | |
| 49 | + const int& inx_right(void) const; | |
| 50 | + const double& wgt_left(void) const; | |
| 51 | + const double& wgt_right(void) const; | |
| 51 | 52 | }; |
| 52 | 53 | |
| 53 | 54 | |
| ... | ... | @@ -55,11 +56,23 @@ public: |
| 55 | 56 | * @brief GNodeArray class extension |
| 56 | 57 | ***************************************************************************/ |
| 57 | 58 | %extend GNodeArray { |
| 58 | - /* | |
| 59 | 59 | char *__str__() { |
| 60 | 60 | return tochar(self->print()); |
| 61 | 61 | } |
| 62 | - */ | |
| 62 | + double __getitem__(const int& index) { | |
| 63 | + if (index >= 0 && index < self->size()) | |
| 64 | + return (*self)[index]; | |
| 65 | + else | |
| 66 | + throw GException::out_of_range("__getitem__(int)", index, self->size()); | |
| 67 | + } | |
| 68 | + void __setitem__(const int& index, const double& val) { | |
| 69 | + if (index>=0 && index < self->size()) { | |
| 70 | + (*self)[index] = val; | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + else | |
| 74 | + throw GException::out_of_range("__setitem__(int)", index, self->size()); | |
| 75 | + } | |
| 63 | 76 | GNodeArray copy() { |
| 64 | 77 | return (*self); |
| 65 | 78 | } | ... | ... |
src/support/GNodeArray.cpp
| ... | ... | @@ -12,7 +12,7 @@ |
| 12 | 12 | ***************************************************************************/ |
| 13 | 13 | /** |
| 14 | 14 | * @file GNodeArray.cpp |
| 15 | - * @brief GNodeArray class implementation. | |
| 15 | + * @brief Node array class implementation | |
| 16 | 16 | * @author J. Knodlseder |
| 17 | 17 | */ |
| 18 | 18 | |
| ... | ... | @@ -21,12 +21,12 @@ |
| 21 | 21 | #include <config.h> |
| 22 | 22 | #endif |
| 23 | 23 | #include <cmath> |
| 24 | -#include <iostream> | |
| 25 | 24 | #include "GException.hpp" |
| 26 | 25 | #include "GNodeArray.hpp" |
| 26 | +#include "GTools.hpp" | |
| 27 | 27 | |
| 28 | 28 | /* __ Method name definitions ____________________________________________ */ |
| 29 | -#define G_ACCESS "GNodeArray::operator() (int)" | |
| 29 | +#define G_ACCESS "GNodeArray::operator[](int)" | |
| 30 | 30 | #define G_INTERPOLATE "GNodeArray::interpolate(std::vector<double>&, double&" |
| 31 | 31 | |
| 32 | 32 | /* __ Macros _____________________________________________________________ */ |
| ... | ... | @@ -123,12 +123,12 @@ GNodeArray& GNodeArray::operator= (const GNodeArray& array) |
| 123 | 123 | /***********************************************************************//** |
| 124 | 124 | * @brief Node access operator |
| 125 | 125 | * |
| 126 | - * @param[in] index Node index (0,1,...). | |
| 126 | + * @param[in] index Node index [0,...,size()-1]. | |
| 127 | 127 | * |
| 128 | 128 | * @exception GException::out_of_range |
| 129 | 129 | * Node index is out of range. |
| 130 | 130 | ***************************************************************************/ |
| 131 | -double& GNodeArray::operator() (int index) | |
| 131 | +double& GNodeArray::operator[](const int& index) | |
| 132 | 132 | { |
| 133 | 133 | // Compile option: raise an exception if index is out of range |
| 134 | 134 | #if defined(G_RANGE_CHECK) |
| ... | ... | @@ -144,12 +144,12 @@ double& GNodeArray::operator() (int index) |
| 144 | 144 | /***********************************************************************//** |
| 145 | 145 | * @brief Node access operator (const version) |
| 146 | 146 | * |
| 147 | - * @param[in] index Node index (0,1,...). | |
| 147 | + * @param[in] index Node index [0,...,size()-1]. | |
| 148 | 148 | * |
| 149 | 149 | * @exception GException::out_of_range |
| 150 | 150 | * Node index is out of range. |
| 151 | 151 | ***************************************************************************/ |
| 152 | -const double& GNodeArray::operator() (int index) const | |
| 152 | +const double& GNodeArray::operator[](const int& index) const | |
| 153 | 153 | { |
| 154 | 154 | // Compile option: raise an exception if index is out of range |
| 155 | 155 | #if defined(G_RANGE_CHECK) |
| ... | ... | @@ -301,7 +301,7 @@ void GNodeArray::append(const double& node) |
| 301 | 301 | * corresponding values \f$x_i\f$ are stored in the node array. |
| 302 | 302 | ***************************************************************************/ |
| 303 | 303 | double GNodeArray::interpolate(const double& value, |
| 304 | - const std::vector<double>& vector) | |
| 304 | + const std::vector<double>& vector) const | |
| 305 | 305 | { |
| 306 | 306 | // Throw exception if there are not enough nodes |
| 307 | 307 | if (m_node.size() < 2) |
| ... | ... | @@ -312,8 +312,8 @@ double GNodeArray::interpolate(const double& value, |
| 312 | 312 | throw GException::vector_mismatch(G_INTERPOLATE, m_node.size(), |
| 313 | 313 | vector.size()); |
| 314 | 314 | |
| 315 | - // Set interpolation value | |
| 316 | - set_value(value); | |
| 315 | + // Set interpolation value (circumvent const correctness) | |
| 316 | + ((GNodeArray*)this)->set_value(value); | |
| 317 | 317 | |
| 318 | 318 | // Interpolate |
| 319 | 319 | double y = vector[inx_left()] * wgt_left() + |
| ... | ... | @@ -394,6 +394,44 @@ void GNodeArray::set_value(const double& value) |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | |
| 397 | +/***********************************************************************//** | |
| 398 | + * @brief Print nodes | |
| 399 | + ***************************************************************************/ | |
| 400 | +std::string GNodeArray::print(void) const | |
| 401 | +{ | |
| 402 | + // Initialise result string | |
| 403 | + std::string result; | |
| 404 | + | |
| 405 | + // Append header | |
| 406 | + result.append("=== GNodeArray ==="); | |
| 407 | + | |
| 408 | + // Append array type | |
| 409 | + result.append("\n"+parformat("Number of nodes in array")+str(size())); | |
| 410 | + if (m_is_linear) { | |
| 411 | + result.append("\n"+parformat("Array type")+"linear"); | |
| 412 | + result.append("\n"+parformat("Linear slope")+str(m_linear_slope)); | |
| 413 | + result.append("\n"+parformat("Linear offset")+str(m_linear_offset)); | |
| 414 | + } | |
| 415 | + else | |
| 416 | + result.append("\n"+parformat("Array type")+"nonlinear"); | |
| 417 | + | |
| 418 | + // Append indices and weights | |
| 419 | + result.append("\n"+parformat("Indices and weights")); | |
| 420 | + result.append("("+str(m_inx_left)+","+str(m_inx_right)+")="); | |
| 421 | + result.append("("+str(m_wgt_left)+","+str(m_wgt_right)+")"); | |
| 422 | + | |
| 423 | + // Append nodes | |
| 424 | + for (int i = 0; i < size(); ++i) { | |
| 425 | + result.append("\n"+parformat("Node "+str(i))); | |
| 426 | + result.append(str(m_node[i])); | |
| 427 | + result.append(" (delta="+str(m_step[i])+")"); | |
| 428 | + } | |
| 429 | + | |
| 430 | + // Return result | |
| 431 | + return result; | |
| 432 | +} | |
| 433 | + | |
| 434 | + | |
| 397 | 435 | /*========================================================================== |
| 398 | 436 | = = |
| 399 | 437 | = GNodeArray private methods = |
| ... | ... | @@ -491,34 +529,34 @@ void GNodeArray::setup(void) |
| 491 | 529 | = = |
| 492 | 530 | ==========================================================================*/ |
| 493 | 531 | |
| 532 | + | |
| 494 | 533 | /***********************************************************************//** |
| 495 | - * @brief Put object in output stream | |
| 534 | + * @brief Output operator | |
| 496 | 535 | * |
| 497 | - * @param[in] os Output stream into which the model will be dumped | |
| 498 | - * @param[in] array Node array to be dumped | |
| 536 | + * @param[in] os Output stream. | |
| 537 | + * @param[in] array Node array. | |
| 499 | 538 | ***************************************************************************/ |
| 500 | -std::ostream& operator<< (std::ostream& os,const GNodeArray& array) | |
| 539 | +std::ostream& operator<<(std::ostream& os, const GNodeArray& array) | |
| 501 | 540 | { |
| 502 | - // Put object in stream | |
| 503 | - os << "=== GNodeArray ===" << std::endl; | |
| 504 | - os << " Number of nodes in array ..: " << array.m_node.size() << std::endl; | |
| 505 | - if (array.m_is_linear) { | |
| 506 | - os << " Array type ................: linear" << std::endl; | |
| 507 | - os << " Linear slope ..............: " << array.m_linear_slope | |
| 508 | - << std::endl; | |
| 509 | - os << " Linear offset .............: " << array.m_linear_offset | |
| 510 | - << std::endl; | |
| 511 | - } | |
| 512 | - else | |
| 513 | - os << " Array type ................: nonlinear" << std::endl; | |
| 514 | - os << " Indices and weights .......: (" | |
| 515 | - << array.m_inx_left << "," | |
| 516 | - << array.m_inx_right << ")=(" | |
| 517 | - << array.m_wgt_left << "," | |
| 518 | - << array.m_wgt_right << ")" << std::endl; | |
| 519 | - for (int i = 0; i < array.m_node.size(); ++i) | |
| 520 | - os << " " << array.m_node.at(i); | |
| 541 | + // Write array in output stream | |
| 542 | + os << array.print(); | |
| 521 | 543 | |
| 522 | 544 | // Return output stream |
| 523 | 545 | return os; |
| 524 | 546 | } |
| 547 | + | |
| 548 | + | |
| 549 | +/***********************************************************************//** | |
| 550 | + * @brief Log operator | |
| 551 | + * | |
| 552 | + * @param[in] log Logger. | |
| 553 | + * @param[in] array Node array. | |
| 554 | + ***************************************************************************/ | |
| 555 | +GLog& operator<<(GLog& log, const GNodeArray& array) | |
| 556 | +{ | |
| 557 | + // Write array into logger | |
| 558 | + log << array.print(); | |
| 559 | + | |
| 560 | + // Return logger | |
| 561 | + return log; | |
| 562 | +} | ... | ... |