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,17 +48,17 @@
48 class GEnergy : public GBase { 48 class GEnergy : public GBase {
49 49
50 // Operator friends 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 public: 63 public:
64 // Constructors and destructors 64 // Constructors and destructors
@@ -71,6 +71,8 @@ public: @@ -71,6 +71,8 @@ public:
71 GEnergy& operator=(const GEnergy& eng); 71 GEnergy& operator=(const GEnergy& eng);
72 GEnergy& operator+=(const GEnergy& eng); 72 GEnergy& operator+=(const GEnergy& eng);
73 GEnergy& operator-=(const GEnergy& eng); 73 GEnergy& operator-=(const GEnergy& eng);
  74 + GEnergy& operator*=(const double& scale);
  75 + GEnergy& operator/=(const double& scale);
74 void operator()(const double& eng, const std::string& unit); 76 void operator()(const double& eng, const std::string& unit);
75 double operator()(const std::string& unit) const; 77 double operator()(const std::string& unit) const;
76 78
@@ -129,8 +131,11 @@ std::string GEnergy::classname(void) const @@ -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 inline 140 inline
136 GEnergy& GEnergy::operator+=(const GEnergy& eng) 141 GEnergy& GEnergy::operator+=(const GEnergy& eng)
@@ -139,6 +144,14 @@ GEnergy&amp; GEnergy::operator+=(const GEnergy&amp; eng) @@ -139,6 +144,14 @@ GEnergy&amp; GEnergy::operator+=(const GEnergy&amp; eng)
139 m_has_log10 = false; 144 m_has_log10 = false;
140 return *this; 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 inline 155 inline
143 GEnergy& GEnergy::operator-=(const GEnergy& eng) 156 GEnergy& GEnergy::operator-=(const GEnergy& eng)
144 { 157 {
@@ -146,6 +159,45 @@ GEnergy&amp; GEnergy::operator-=(const GEnergy&amp; eng) @@ -146,6 +159,45 @@ GEnergy&amp; GEnergy::operator-=(const GEnergy&amp; eng)
146 m_has_log10 = false; 159 m_has_log10 = false;
147 return *this; 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 inline 201 inline
150 GEnergy operator+(const GEnergy& a, const GEnergy& b) 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,6 +205,15 @@ GEnergy operator+(const GEnergy&amp; a, const GEnergy&amp; b)
153 result.m_energy = a.m_energy + b.m_energy; 205 result.m_energy = a.m_energy + b.m_energy;
154 return result; 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 inline 217 inline
157 GEnergy operator-(const GEnergy& a, const GEnergy& b) 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,6 +221,15 @@ GEnergy operator-(const GEnergy&amp; a, const GEnergy&amp; b)
160 result.m_energy = a.m_energy - b.m_energy; 221 result.m_energy = a.m_energy - b.m_energy;
161 return result; 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 inline 233 inline
164 GEnergy operator*(const double& a, const GEnergy& b) 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,6 +237,15 @@ GEnergy operator*(const double&amp; a, const GEnergy&amp; b)
167 result.m_energy = a * b.m_energy; 237 result.m_energy = a * b.m_energy;
168 return result; 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 inline 249 inline
171 GEnergy operator*(const GEnergy& a, const double& b) 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,6 +253,15 @@ GEnergy operator*(const GEnergy&amp; a, const double&amp; b)
174 result.m_energy = b * a.m_energy; 253 result.m_energy = b * a.m_energy;
175 return result; 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 inline 265 inline
178 GEnergy operator/(const GEnergy& a, const double& b) 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,31 +269,85 @@ GEnergy operator/(const GEnergy&amp; a, const double&amp; b)
181 result.m_energy = a.m_energy / b; 269 result.m_energy = a.m_energy / b;
182 return result; 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 inline 281 inline
185 bool operator==(const GEnergy &a, const GEnergy &b) 282 bool operator==(const GEnergy &a, const GEnergy &b)
186 { 283 {
187 return (a.m_energy == b.m_energy); 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 inline 295 inline
190 bool operator!=(const GEnergy &a, const GEnergy &b) 296 bool operator!=(const GEnergy &a, const GEnergy &b)
191 { 297 {
192 return (a.m_energy != b.m_energy); 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 inline 309 inline
195 bool operator<(const GEnergy &a, const GEnergy &b) 310 bool operator<(const GEnergy &a, const GEnergy &b)
196 { 311 {
197 return (a.m_energy < b.m_energy); 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 inline 323 inline
200 bool operator<=(const GEnergy &a, const GEnergy &b) 324 bool operator<=(const GEnergy &a, const GEnergy &b)
201 { 325 {
202 return (a.m_energy <= b.m_energy); 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 inline 337 inline
205 bool operator>(const GEnergy &a, const GEnergy &b) 338 bool operator>(const GEnergy &a, const GEnergy &b)
206 { 339 {
207 return (a.m_energy > b.m_energy); 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 inline 351 inline
210 bool operator>=(const GEnergy &a, const GEnergy &b) 352 bool operator>=(const GEnergy &a, const GEnergy &b)
211 { 353 {
include/GTime.hpp
@@ -74,6 +74,10 @@ public: @@ -74,6 +74,10 @@ public:
74 74
75 // Operators 75 // Operators
76 GTime& operator=(const GTime& time); 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 // Methods 82 // Methods
79 void clear(void); 83 void clear(void);
@@ -175,6 +179,62 @@ int GTime::days_in_year(const int&amp; year) const @@ -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 * @brief Time addition operator friend 238 * @brief Time addition operator friend
179 * 239 *
180 * @param[in] a First time. 240 * @param[in] a First time.
@@ -230,7 +290,7 @@ GTime operator*(const double&amp; s, const GTime&amp; time) @@ -230,7 +290,7 @@ GTime operator*(const double&amp; s, const GTime&amp; time)
230 * @return Multiplied time. 290 * @return Multiplied time.
231 ***************************************************************************/ 291 ***************************************************************************/
232 inline 292 inline
233 -GTime operator* (const GTime& time, const double& s) 293 +GTime operator*(const GTime& time, const double& s)
234 { 294 {
235 GTime result; 295 GTime result;
236 result.m_time = s * time.m_time; 296 result.m_time = s * time.m_time;
pyext/GEnergy.i
@@ -51,6 +51,7 @@ public: @@ -51,6 +51,7 @@ public:
51 // Operators 51 // Operators
52 GEnergy& operator+=(const GEnergy& eng); 52 GEnergy& operator+=(const GEnergy& eng);
53 GEnergy& operator-=(const GEnergy& eng); 53 GEnergy& operator-=(const GEnergy& eng);
  54 + GEnergy& operator*=(const double& scale);
54 void operator()(const double& eng, const std::string& unit); 55 void operator()(const double& eng, const std::string& unit);
55 double operator()(const std::string& unit) const; 56 double operator()(const std::string& unit) const;
56 57
@@ -105,6 +106,16 @@ public: @@ -105,6 +106,16 @@ public:
105 GEnergy __truediv__(const double& factor) const { 106 GEnergy __truediv__(const double& factor) const {
106 return ((*self) / factor); 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 bool __eq__(const GEnergy& eng) const { 119 bool __eq__(const GEnergy& eng) const {
109 return ((*self) == eng); 120 return ((*self) == eng);
110 } 121 }
pyext/GTime.i
@@ -44,6 +44,11 @@ public: @@ -44,6 +44,11 @@ public:
44 GTime(const double& time, const GTimeReference& ref); 44 GTime(const double& time, const GTimeReference& ref);
45 virtual ~GTime(void); 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 // Methods 52 // Methods
48 void clear(void); 53 void clear(void);
49 GTime* clone(void) const; 54 GTime* clone(void) const;
@@ -85,6 +90,16 @@ public: @@ -85,6 +90,16 @@ public:
85 GTime __truediv__(const double& factor) const { 90 GTime __truediv__(const double& factor) const {
86 return ((*self) / factor); 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 bool __eq__(const GTime& time) const { 103 bool __eq__(const GTime& time) const {
89 return ((*self) == time); 104 return ((*self) == time);
90 } 105 }
test/test_GObservation.py
@@ -17,15 +17,13 @@ @@ -17,15 +17,13 @@
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 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 # Test class for GammaLib observation module # 24 # Test class for GammaLib observation module #
27 # ========================================== # 25 # ========================================== #
28 -class Test(GPythonTestSuite): 26 +class Test(gammalib.GPythonTestSuite):
29 """ 27 """
30 Test class for GammaLib observation module. 28 Test class for GammaLib observation module.
31 """ 29 """
@@ -35,7 +33,7 @@ class Test(GPythonTestSuite): @@ -35,7 +33,7 @@ class Test(GPythonTestSuite):
35 Constructor. 33 Constructor.
36 """ 34 """
37 # Call base class constructor 35 # Call base class constructor
38 - GPythonTestSuite.__init__(self) 36 + gammalib.GPythonTestSuite.__init__(self)
39 37
40 # Return 38 # Return
41 return 39 return
@@ -61,8 +59,8 @@ class Test(GPythonTestSuite): @@ -61,8 +59,8 @@ class Test(GPythonTestSuite):
61 Test GEnergy class. 59 Test GEnergy class.
62 """ 60 """
63 # Setup energies 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 # Unary addition operator 65 # Unary addition operator
68 energy += energy_b 66 energy += energy_b
@@ -72,6 +70,14 @@ class Test(GPythonTestSuite): @@ -72,6 +70,14 @@ class Test(GPythonTestSuite):
72 energy -= energy_b 70 energy -= energy_b
73 self.test_value(energy.TeV(), 3.0); 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 # Binary addition operator 81 # Binary addition operator
76 energy = energy + energy_b 82 energy = energy + energy_b
77 self.test_value(energy.TeV(), 6.0); 83 self.test_value(energy.TeV(), 6.0);
@@ -98,9 +104,25 @@ class Test(GPythonTestSuite): @@ -98,9 +104,25 @@ class Test(GPythonTestSuite):
98 Test GTime class. 104 Test GTime class.
99 """ 105 """
100 # Setup times 106 # Setup times
101 - time = GTime(3.0) 107 + time = gammalib.GTime(3.0)
102 time_b = time.copy() 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 # Binary addition operator 126 # Binary addition operator
105 time = time + time_b 127 time = time + time_b
106 self.test_value(time.secs(), 6.0); 128 self.test_value(time.secs(), 6.0);