LOGO

RestFrames  v1.0.1
RestFrames HEP Event Analysis Software Library
RFCharge.cc
Go to the documentation of this file.
1 // RestFrames: particle physics event analysis library
3 // --------------------------------------------------------------------
4 // Copyright (c) 2014-2016, Christopher Rogan
14 // This file is part of RestFrames.
15 //
16 // RestFrames is free software; you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation; either version 2 of the License, or
19 // (at your option) any later version.
20 //
21 // RestFrames is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with RestFrames. If not, see <http://www.gnu.org/licenses/>.
29 
30 #include "RestFrames/RFCharge.hh"
31 
32 namespace RestFrames {
33 
34  RFCharge::RFCharge(const RFCharge& charge){
35  int num = charge.GetNumerator();
36  int den = charge.GetDenominator();
37 
38  if(den == 0){
39  m_ChargeNum = 0;
40  m_ChargeDen = 1;
41  m_Sign = true;
42  return;
43  }
44 
45  if(num*den < 0)
46  m_Sign = false;
47  else
48  m_Sign = true;
49 
50  m_ChargeNum = std::abs(num);
51  m_ChargeDen = std::abs(den);
52 
53  if(m_ChargeDen > 1)
54  Simplify();
55  }
56 
57  RFCharge::RFCharge(int charge){
58  if(charge < 0)
59  m_Sign = false;
60  else
61  m_Sign = true;
62 
63  m_ChargeNum = std::abs(charge);
64  m_ChargeDen = 1;
65  }
66 
67  RFCharge::RFCharge(int num, int den){
68  if(den == 0){
69  m_ChargeNum = 0;
70  m_ChargeDen = 1;
71  m_Sign = true;
72  return;
73  }
74 
75  if(num*den < 0)
76  m_Sign = false;
77  else
78  m_Sign = true;
79 
80  m_ChargeNum = std::abs(num);
81  m_ChargeDen = std::abs(den);
82 
83  if(m_ChargeDen > 1)
84  Simplify();
85  }
86 
87  RFCharge::~RFCharge() {}
88 
89  int RFCharge::GetNumerator() const {
90  if(m_Sign)
91  return m_ChargeNum;
92  else
93  return -m_ChargeNum;
94  }
95 
97  return m_ChargeDen;
98  }
99 
100  void RFCharge::Simplify(){
101  int f = gcd(m_ChargeNum, m_ChargeDen);
102  if(f > 1){
103  m_ChargeNum /= f;
104  m_ChargeDen /= f;
105  }
106  }
107 
108  bool RFCharge::operator==(const RFCharge& val) const {
109  if(m_Sign)
110  return (m_ChargeNum == val.GetNumerator())
111  && (m_ChargeDen == val.GetDenominator());
112  else
113  return (-m_ChargeNum == val.GetNumerator())
114  && (m_ChargeDen == val.GetDenominator());
115  }
116 
117  bool RFCharge::operator==(int val) const {
118  if(m_ChargeDen != 1)
119  return false;
120 
121  if(m_Sign)
122  return (m_ChargeNum == val);
123  else
124  return (-m_ChargeNum == val);
125  }
126 
127  bool RFCharge::operator!=(const RFCharge& val) const {
128  if(m_Sign)
129  return (m_ChargeNum != val.GetNumerator())
130  || (m_ChargeDen != val.GetDenominator());
131  else
132  return (-m_ChargeNum != val.GetNumerator())
133  || (m_ChargeDen != val.GetDenominator());
134  }
135 
136  bool RFCharge::operator!=(int val) const{
137  if(m_ChargeDen != 1)
138  return true;
139 
140  if(m_Sign)
141  return (m_ChargeNum != val);
142  else
143  return (-m_ChargeNum != val);
144  }
145 
146  void RFCharge::operator=(const RFCharge& val){
147  int num = val.GetNumerator();
148 
149  if(num < 0)
150  m_Sign = false;
151  else
152  m_Sign = true;
153 
154  m_ChargeNum = std::abs(num);
155  m_ChargeDen = val.GetDenominator();
156  }
157 
158  void RFCharge::operator=(int val){
159  if(val < 0)
160  m_Sign = false;
161  else
162  m_Sign = true;
163 
164  m_ChargeNum = std::abs(val);
165  m_ChargeDen = 1;
166  }
167 
168  RFCharge RFCharge::operator+(const RFCharge& val) const {
169  int den = val.GetDenominator();
170 
171  return RFCharge(GetNumerator()*den+val.GetNumerator()*m_ChargeDen,
172  den*m_ChargeDen);
173  }
174 
175  RFCharge RFCharge::operator+(int val) const {
176  return RFCharge(GetNumerator()+val*m_ChargeDen, m_ChargeDen);
177  }
178 
179  RFCharge RFCharge::operator-(const RFCharge& val) const {
180  int den = val.GetDenominator();
181 
182  return RFCharge(GetNumerator()*den-val.GetNumerator()*m_ChargeDen,
183  den*m_ChargeDen);
184  }
185 
186  RFCharge RFCharge::operator-(int val) const {
187  return RFCharge(GetNumerator()-val*m_ChargeDen, m_ChargeDen);
188  }
189 
190  RFCharge RFCharge::operator*(const RFCharge& val) const {
191  return RFCharge(GetNumerator()*val.GetNumerator(),
192  val.GetDenominator()*m_ChargeDen);
193  }
194 
195  RFCharge RFCharge::operator*(int val) const {
196  return RFCharge(GetNumerator()*val, m_ChargeDen);
197  }
198 
199  RFCharge RFCharge::operator/(const RFCharge& val) const {
200  return RFCharge(GetNumerator()*val.GetDenominator(),
201  val.GetNumerator()*m_ChargeDen);
202  }
203 
204  RFCharge RFCharge::operator/(int val) const {
205  return RFCharge(GetNumerator(), m_ChargeDen*val);
206  }
207 
208  RFCharge& RFCharge::operator+=(const RFCharge& val){
209  int den = val.GetDenominator();
210  int mnum = GetNumerator()*den + val.GetNumerator()*m_ChargeDen;
211 
212  if(mnum < 0)
213  m_Sign = false;
214  else
215  m_Sign = true;
216 
217  m_ChargeNum = std::abs(mnum);
218  m_ChargeDen *= den;
219 
220  if(m_ChargeDen > 1)
221  Simplify();
222 
223  return *this;
224  }
225 
226  RFCharge& RFCharge::operator+=(int val){
227  int mnum = GetNumerator() + val*m_ChargeDen;
228 
229  if(mnum < 0)
230  m_Sign = false;
231  else
232  m_Sign = true;
233 
234  m_ChargeNum = std::abs(mnum);
235 
236  if(m_ChargeDen > 1)
237  Simplify();
238 
239  return *this;
240  }
241 
242  RFCharge& RFCharge::operator-=(const RFCharge& val){
243  int den = val.GetDenominator();
244  int mnum = GetNumerator()*den - val.GetNumerator()*m_ChargeDen;
245 
246  if(mnum < 0)
247  m_Sign = false;
248  else
249  m_Sign = true;
250 
251  m_ChargeNum = std::abs(mnum);
252  m_ChargeDen *= den;
253 
254  if(m_ChargeDen > 1)
255  Simplify();
256 
257  return *this;
258  }
259 
260  RFCharge& RFCharge::operator-=(int val){
261  int mnum = GetNumerator() - val*m_ChargeDen;
262 
263  if(mnum < 0)
264  m_Sign = false;
265  else
266  m_Sign = true;
267 
268  m_ChargeNum = std::abs(mnum);
269 
270  if(m_ChargeDen > 1)
271  Simplify();
272 
273  return *this;
274  }
275 
276  RFCharge& RFCharge::operator*=(const RFCharge& val){
277  int mnum = GetNumerator()*val.GetNumerator();
278 
279  if(mnum == 0){
280  m_ChargeNum = 0;
281  m_ChargeDen = 1;
282  m_Sign = true;
283  return *this;
284  }
285 
286  if(mnum < 0)
287  m_Sign = false;
288  else
289  m_Sign = true;
290 
291  m_ChargeNum = std::abs(mnum);
292  m_ChargeDen *= val.GetDenominator();
293 
294  if(m_ChargeDen > 1)
295  Simplify();
296 
297  return *this;
298  }
299 
300  RFCharge& RFCharge::operator*=(int val){
301  int mnum = GetNumerator()*val;
302 
303  if(mnum == 0){
304  m_ChargeNum = 0;
305  m_ChargeDen = 1;
306  m_Sign = true;
307  return *this;
308  }
309 
310  if(mnum < 0)
311  m_Sign = false;
312  else
313  m_Sign = true;
314 
315  m_ChargeNum = std::abs(mnum);
316 
317  if(m_ChargeDen > 1)
318  Simplify();
319 
320  return *this;
321  }
322 
323  RFCharge& RFCharge::operator/=(const RFCharge& val){
324  int num = val.GetNumerator();
325 
326  if(num == 0){
327  m_ChargeNum = 0;
328  m_ChargeDen = 1;
329  m_Sign = true;
330  return *this;
331  }
332 
333  if(num < 0)
334  m_Sign = !m_Sign;
335 
336  m_ChargeDen *= std::abs(num);
337  m_ChargeNum *= val.GetDenominator();
338 
339  if(m_ChargeDen > 1)
340  Simplify();
341 
342  return *this;
343  }
344 
345  RFCharge& RFCharge::operator/=(int val){
346  if(val == 0){
347  m_ChargeNum = 0;
348  m_ChargeDen = 1;
349  m_Sign = true;
350  return *this;
351  }
352 
353  if(val < 0)
354  m_Sign = !m_Sign;
355 
356  m_ChargeDen *= std::abs(val);
357 
358  if(m_ChargeDen > 1)
359  Simplify();
360 
361  return *this;
362  }
363 
364  RFCharge RFCharge::operator-() {
365  return (*this)*(-1);
366  }
367 
368  RFCharge::operator double() const {
369  return double(GetNumerator())/
370  double(m_ChargeDen);
371  }
372 
373  RFCharge operator*(int val1, const RFCharge& val2){
374  return val2*val1;
375  }
376 
377  RFCharge operator/(int val1, const RFCharge& val2){
378  return RFCharge(val1)/val2;
379  }
380 
381  int gcd(int x, int y){
382  if(x < y)
383  return gcd( y, x );
384 
385  int f = x % y;
386  if(f == 0)
387  return y;
388  else
389  return gcd( y, f );
390  }
391 
392 }
RestFrames::RFCharge::GetDenominator
int GetDenominator() const
Returns denominator.
Definition: RFCharge.cc:96
RFCharge.hh
RestFrames::RFCharge
Definition: RFCharge.hh:40
RestFrames::RFCharge::GetNumerator
int GetNumerator() const
Returns numerator.
Definition: RFCharge.cc:89
RestFrames::RFCharge::operator=
void operator=(const RFCharge &val)
Tests whether val is the same as this object's charge.
Definition: RFCharge.cc:146
RestFrames::RFCharge::operator!=
bool operator!=(const RFCharge &val) const
Tests whether val is not the same as this object's charge.
Definition: RFCharge.cc:127
RestFrames::RFCharge::operator==
bool operator==(const RFCharge &val) const
Tests whether val is the same as this object's charge.
Definition: RFCharge.cc:108
RestFrames::RFCharge::RFCharge
RFCharge(const RFCharge &charge)
Standard constructor.
Definition: RFCharge.cc:34