Commit 668e2cd5f70c606d32cc81a5e552b8b180bdb461

Authored by Jürgen Knödlseder
1 parent e4936932

Add missing operators and add corresponding Python unit tests.

include/GEnergy.hpp
... ... @@ -48,17 +48,17 @@
48 48 class GEnergy : public GBase {
49 49  
50 50 // Operator friends
51   - friend GEnergy operator+ (const GEnergy &a, const GEnergy &b);
52   - friend GEnergy operator- (const GEnergy &a, const GEnergy &b);
53   - friend GEnergy operator* (const double &a, const GEnergy &b);
54   - friend GEnergy operator* (const GEnergy &a, const double &b);
55   - friend GEnergy operator/ (const GEnergy &a, const double &b);
56   - friend bool operator== (const GEnergy &a, const GEnergy &b);
57   - friend bool operator!= (const GEnergy &a, const GEnergy &b);
58   - friend bool operator< (const GEnergy &a, const GEnergy &b);
59   - friend bool operator<= (const GEnergy &a, const GEnergy &b);
60   - friend bool operator> (const GEnergy &a, const GEnergy &b);
61   - friend bool operator>= (const GEnergy &a, const GEnergy &b);
  51 + friend GEnergy operator+(const GEnergy &a, const GEnergy &b);
  52 + friend GEnergy operator-(const GEnergy &a, const GEnergy &b);
  53 + friend GEnergy operator*(const double &a, const GEnergy &b);
  54 + friend GEnergy operator*(const GEnergy &a, const double &b);
  55 + friend GEnergy operator/(const GEnergy &a, const double &b);
  56 + friend bool operator==(const GEnergy &a, const GEnergy &b);
  57 + friend bool operator!=(const GEnergy &a, const GEnergy &b);
  58 + friend bool operator<(const GEnergy &a, const GEnergy &b);
  59 + friend bool operator<=(const GEnergy &a, const GEnergy &b);
  60 + friend bool operator>(const GEnergy &a, const GEnergy &b);
  61 + friend bool operator>=(const GEnergy &a, const GEnergy &b);
62 62  
63 63 public:
64 64 // Constructors and destructors
... ... @@ -71,6 +71,8 @@ public:
71 71 GEnergy& operator=(const GEnergy& eng);
72 72 GEnergy& operator+=(const GEnergy& eng);
73 73 GEnergy& operator-=(const GEnergy& eng);
  74 + GEnergy& operator*=(const double& scale);
  75 + GEnergy& operator/=(const double& scale);
74 76 void operator()(const double& eng, const std::string& unit);
75 77 double operator()(const std::string& unit) const;
76 78  
... ... @@ -129,8 +131,11 @@ std::string GEnergy::classname(void) const
129 131 }
130 132  
131 133  
132   -/***************************************************************************
133   - * Inline friends *
  134 +/***********************************************************************//**
  135 + * @brief Energy unary addition operator
  136 + *
  137 + * @param[in] eng Energy.
  138 + * @return Sum of energies.
134 139 ***************************************************************************/
135 140 inline
136 141 GEnergy& GEnergy::operator+=(const GEnergy& eng)
... ... @@ -139,6 +144,14 @@ GEnergy&amp; GEnergy::operator+=(const GEnergy&amp; eng)
139 144 m_has_log10 = false;
140 145 return *this;
141 146 }
  147 +
  148 +
  149 +/***********************************************************************//**
  150 + * @brief Energy unary differnce operator
  151 + *
  152 + * @param[in] eng Energy.
  153 + * @return Difference of energies.
  154 + ***************************************************************************/
142 155 inline
143 156 GEnergy& GEnergy::operator-=(const GEnergy& eng)
144 157 {
... ... @@ -146,6 +159,45 @@ GEnergy&amp; GEnergy::operator-=(const GEnergy&amp; eng)
146 159 m_has_log10 = false;
147 160 return *this;
148 161 }
  162 +
  163 +
  164 +/***********************************************************************//**
  165 + * @brief Energy unary multiplication operator
  166 + *
  167 + * @param[in] scale Scale.
  168 + * @return Energy multiplied with scale.
  169 + ***************************************************************************/
  170 +inline
  171 +GEnergy& GEnergy::operator*=(const double& scale)
  172 +{
  173 + m_energy *= scale;
  174 + m_has_log10 = false;
  175 + return *this;
  176 +}
  177 +
  178 +
  179 +/***********************************************************************//**
  180 + * @brief Energy unary division operator
  181 + *
  182 + * @param[in] scale Scale.
  183 + * @return Energy divided by scale.
  184 + ***************************************************************************/
  185 +inline
  186 +GEnergy& GEnergy::operator/=(const double& scale)
  187 +{
  188 + m_energy /= scale;
  189 + m_has_log10 = false;
  190 + return *this;
  191 +}
  192 +
  193 +
  194 +/***********************************************************************//**
  195 + * @brief Energy addition operator friend
  196 + *
  197 + * @param[in] a First energy.
  198 + * @param[in] b Second energy.
  199 + * @return Sum of energies.
  200 + ***************************************************************************/
149 201 inline
150 202 GEnergy operator+(const GEnergy& a, const GEnergy& b)
151 203 {
... ... @@ -153,6 +205,15 @@ GEnergy operator+(const GEnergy&amp; a, const GEnergy&amp; b)
153 205 result.m_energy = a.m_energy + b.m_energy;
154 206 return result;
155 207 }
  208 +
  209 +
  210 +/***********************************************************************//**
  211 + * @brief Energy subtraction operator friend
  212 + *
  213 + * @param[in] a First energy.
  214 + * @param[in] b Second energy.
  215 + * @return Difference of energies.
  216 + ***************************************************************************/
156 217 inline
157 218 GEnergy operator-(const GEnergy& a, const GEnergy& b)
158 219 {
... ... @@ -160,6 +221,15 @@ GEnergy operator-(const GEnergy&amp; a, const GEnergy&amp; b)
160 221 result.m_energy = a.m_energy - b.m_energy;
161 222 return result;
162 223 }
  224 +
  225 +
  226 +/***********************************************************************//**
  227 + * @brief Energy multiplication operator friend
  228 + *
  229 + * @param[in] a Scale.
  230 + * @param[in] b Energy.
  231 + * @return Energy multiplied by scale.
  232 + ***************************************************************************/
163 233 inline
164 234 GEnergy operator*(const double& a, const GEnergy& b)
165 235 {
... ... @@ -167,6 +237,15 @@ GEnergy operator*(const double&amp; a, const GEnergy&amp; b)
167 237 result.m_energy = a * b.m_energy;
168 238 return result;
169 239 }
  240 +
  241 +
  242 +/***********************************************************************//**
  243 + * @brief Energy multiplication operator friend
  244 + *
  245 + * @param[in] a Energy.
  246 + * @param[in] b Scale.
  247 + * @return Energy multiplied by scale.
  248 + ***************************************************************************/
170 249 inline
171 250 GEnergy operator*(const GEnergy& a, const double& b)
172 251 {
... ... @@ -174,6 +253,15 @@ GEnergy operator*(const GEnergy&amp; a, const double&amp; b)
174 253 result.m_energy = b * a.m_energy;
175 254 return result;
176 255 }
  256 +
  257 +
  258 +/***********************************************************************//**
  259 + * @brief Energy division operator friend
  260 + *
  261 + * @param[in] a Energy.
  262 + * @param[in] b Scale.
  263 + * @return Energy divided by scale.
  264 + ***************************************************************************/
177 265 inline
178 266 GEnergy operator/(const GEnergy& a, const double& b)
179 267 {
... ... @@ -181,31 +269,85 @@ GEnergy operator/(const GEnergy&amp; a, const double&amp; b)
181 269 result.m_energy = a.m_energy / b;
182 270 return result;
183 271 }
  272 +
  273 +
  274 +/***********************************************************************//**
  275 + * @brief Energy equality operator friend
  276 + *
  277 + * @param[in] a First energy.
  278 + * @param[in] b Second energy.
  279 + * @return True if both energies are equal.
  280 + ***************************************************************************/
184 281 inline
185 282 bool operator==(const GEnergy &a, const GEnergy &b)
186 283 {
187 284 return (a.m_energy == b.m_energy);
188 285 }
  286 +
  287 +
  288 +/***********************************************************************//**
  289 + * @brief Energy non-equality operator friend
  290 + *
  291 + * @param[in] a First energy.
  292 + * @param[in] b Second energy.
  293 + * @return True if both energies are not equal.
  294 + ***************************************************************************/
189 295 inline
190 296 bool operator!=(const GEnergy &a, const GEnergy &b)
191 297 {
192 298 return (a.m_energy != b.m_energy);
193 299 }
  300 +
  301 +
  302 +/***********************************************************************//**
  303 + * @brief Energy smaller than operator friend
  304 + *
  305 + * @param[in] a First energy.
  306 + * @param[in] b Second energy.
  307 + * @return True if first energy is smaller than second energy.
  308 + ***************************************************************************/
194 309 inline
195 310 bool operator<(const GEnergy &a, const GEnergy &b)
196 311 {
197 312 return (a.m_energy < b.m_energy);
198 313 }
  314 +
  315 +
  316 +/***********************************************************************//**
  317 + * @brief Energy smaller than or equal to operator friend
  318 + *
  319 + * @param[in] a First energy.
  320 + * @param[in] b Second energy.
  321 + * @return True if first energy is smaller than or equal to second energy.
  322 + ***************************************************************************/
199 323 inline
200 324 bool operator<=(const GEnergy &a, const GEnergy &b)
201 325 {
202 326 return (a.m_energy <= b.m_energy);
203 327 }
  328 +
  329 +
  330 +/***********************************************************************//**
  331 + * @brief Energy larger than operator friend
  332 + *
  333 + * @param[in] a First energy.
  334 + * @param[in] b Second energy.
  335 + * @return True if first energy is larger than second energy.
  336 + ***************************************************************************/
204 337 inline
205 338 bool operator>(const GEnergy &a, const GEnergy &b)
206 339 {
207 340 return (a.m_energy > b.m_energy);
208 341 }
  342 +
  343 +
  344 +/***********************************************************************//**
  345 + * @brief Energy larger than or equal to operator friend
  346 + *
  347 + * @param[in] a First energy.
  348 + * @param[in] b Second energy.
  349 + * @return True if first energy is larger than or equal to second energy.
  350 + ***************************************************************************/
209 351 inline
210 352 bool operator>=(const GEnergy &a, const GEnergy &b)
211 353 {
... ...
include/GTime.hpp
... ... @@ -74,6 +74,10 @@ public:
74 74  
75 75 // Operators
76 76 GTime& operator=(const GTime& time);
  77 + GTime& operator+=(const GTime& time);
  78 + GTime& operator-=(const GTime& time);
  79 + GTime& operator*=(const double& scale);
  80 + GTime& operator/=(const double& scale);
77 81  
78 82 // Methods
79 83 void clear(void);
... ... @@ -175,6 +179,62 @@ int GTime::days_in_year(const int&amp; year) const
175 179  
176 180  
177 181 /***********************************************************************//**
  182 + * @brief Time unary addition operator
  183 + *
  184 + * @param[in] time Time.
  185 + * @return Sum of times.
  186 + ***************************************************************************/
  187 +inline
  188 +GTime& GTime::operator+=(const GTime& time)
  189 +{
  190 + m_time += time.m_time;
  191 + return *this;
  192 +}
  193 +
  194 +
  195 +/***********************************************************************//**
  196 + * @brief Time unary subtraction operator
  197 + *
  198 + * @param[in] time Time.
  199 + * @return Difference of times.
  200 + ***************************************************************************/
  201 +inline
  202 +GTime& GTime::operator-=(const GTime& time)
  203 +{
  204 + m_time -= time.m_time;
  205 + return *this;
  206 +}
  207 +
  208 +
  209 +/***********************************************************************//**
  210 + * @brief Time unary multiplication operator
  211 + *
  212 + * @param[in] scale Scale.
  213 + * @return Time multiplied by scale.
  214 + ***************************************************************************/
  215 +inline
  216 +GTime& GTime::operator*=(const double& scale)
  217 +{
  218 + m_time *= scale;
  219 + return *this;
  220 +}
  221 +
  222 +
  223 +/***********************************************************************//**
  224 + * @brief Time unary division operator
  225 + *
  226 + * @param[in] scale Scale.
  227 + * @return Time divided by scale.
  228 + ***************************************************************************/
  229 +inline
  230 +GTime& GTime::operator/=(const double& scale)
  231 +{
  232 + m_time /= scale;
  233 + return *this;
  234 +}
  235 +
  236 +
  237 +/***********************************************************************//**
178 238 * @brief Time addition operator friend
179 239 *
180 240 * @param[in] a First time.
... ... @@ -230,7 +290,7 @@ GTime operator*(const double&amp; s, const GTime&amp; time)
230 290 * @return Multiplied time.
231 291 ***************************************************************************/
232 292 inline
233   -GTime operator* (const GTime& time, const double& s)
  293 +GTime operator*(const GTime& time, const double& s)
234 294 {
235 295 GTime result;
236 296 result.m_time = s * time.m_time;
... ...
pyext/GEnergy.i
... ... @@ -51,6 +51,7 @@ public:
51 51 // Operators
52 52 GEnergy& operator+=(const GEnergy& eng);
53 53 GEnergy& operator-=(const GEnergy& eng);
  54 + GEnergy& operator*=(const double& scale);
54 55 void operator()(const double& eng, const std::string& unit);
55 56 double operator()(const std::string& unit) const;
56 57  
... ... @@ -105,6 +106,16 @@ public:
105 106 GEnergy __truediv__(const double& factor) const {
106 107 return ((*self) / factor);
107 108 }
  109 + // Python 2.x operator/=
  110 + GEnergy __idiv__(const double& scale) {
  111 + self->operator/=(scale);
  112 + return (*self);
  113 + }
  114 + // Python 3.x operator/=
  115 + GEnergy __itruediv__(const double& scale) {
  116 + self->operator/=(scale);
  117 + return (*self);
  118 + }
108 119 bool __eq__(const GEnergy& eng) const {
109 120 return ((*self) == eng);
110 121 }
... ...
pyext/GTime.i
... ... @@ -44,6 +44,11 @@ public:
44 44 GTime(const double& time, const GTimeReference& ref);
45 45 virtual ~GTime(void);
46 46  
  47 + // Operators
  48 + GTime& operator+=(const GTime& time);
  49 + GTime& operator-=(const GTime& time);
  50 + GTime& operator*=(const double& scale);
  51 +
47 52 // Methods
48 53 void clear(void);
49 54 GTime* clone(void) const;
... ... @@ -85,6 +90,16 @@ public:
85 90 GTime __truediv__(const double& factor) const {
86 91 return ((*self) / factor);
87 92 }
  93 + // Python 2.x operator/=
  94 + GTime __idiv__(const double& scale) {
  95 + self->operator/=(scale);
  96 + return (*self);
  97 + }
  98 + // Python 3.x operator/=
  99 + GTime __itruediv__(const double& scale) {
  100 + self->operator/=(scale);
  101 + return (*self);
  102 + }
88 103 bool __eq__(const GTime& time) const {
89 104 return ((*self) == time);
90 105 }
... ...
test/test_GObservation.py
... ... @@ -17,15 +17,13 @@
17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 #
19 19 # ==========================================================================
20   -from gammalib import *
21   -from math import *
22   -import os
  20 +import gammalib
23 21  
24 22  
25 23 # ========================================== #
26 24 # Test class for GammaLib observation module #
27 25 # ========================================== #
28   -class Test(GPythonTestSuite):
  26 +class Test(gammalib.GPythonTestSuite):
29 27 """
30 28 Test class for GammaLib observation module.
31 29 """
... ... @@ -35,7 +33,7 @@ class Test(GPythonTestSuite):
35 33 Constructor.
36 34 """
37 35 # Call base class constructor
38   - GPythonTestSuite.__init__(self)
  36 + gammalib.GPythonTestSuite.__init__(self)
39 37  
40 38 # Return
41 39 return
... ... @@ -61,8 +59,8 @@ class Test(GPythonTestSuite):
61 59 Test GEnergy class.
62 60 """
63 61 # Setup energies
64   - energy = GEnergy(3.0, "TeV")
65   - energy_b = energy.copy()
  62 + energy = gammalib.GEnergy(3.0, "TeV")
  63 + energy_b = energy.copy()
66 64  
67 65 # Unary addition operator
68 66 energy += energy_b
... ... @@ -72,6 +70,14 @@ class Test(GPythonTestSuite):
72 70 energy -= energy_b
73 71 self.test_value(energy.TeV(), 3.0);
74 72  
  73 + # Unary multiplication operator
  74 + energy *= 2.0
  75 + self.test_value(energy.TeV(), 6.0);
  76 +
  77 + # Unary division operator
  78 + energy /= 2.0
  79 + self.test_value(energy.TeV(), 3.0);
  80 +
75 81 # Binary addition operator
76 82 energy = energy + energy_b
77 83 self.test_value(energy.TeV(), 6.0);
... ... @@ -98,9 +104,25 @@ class Test(GPythonTestSuite):
98 104 Test GTime class.
99 105 """
100 106 # Setup times
101   - time = GTime(3.0)
  107 + time = gammalib.GTime(3.0)
102 108 time_b = time.copy()
103 109  
  110 + # Unary addition operator
  111 + time += time_b
  112 + self.test_value(time.secs(), 6.0);
  113 +
  114 + # Unary subtraction operator
  115 + time -= time_b
  116 + self.test_value(time.secs(), 3.0);
  117 +
  118 + # Unary multiplication operator
  119 + time *= 2.0
  120 + self.test_value(time.secs(), 6.0);
  121 +
  122 + # Unary division operator
  123 + time /= 2.0
  124 + self.test_value(time.secs(), 3.0);
  125 +
104 126 # Binary addition operator
105 127 time = time + time_b
106 128 self.test_value(time.secs(), 6.0);
... ...