ARTS  2.2.66
rational.h
Go to the documentation of this file.
1 /* Copyright (C) 2012
2 Richard Larsson <ric.larsson@gmail.com>
3 
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18 
26 #ifndef rational_h
27 #define rational_h
28 
29 #include <cassert>
30 #include <ostream>
31 #include "matpack.h"
32 #include "array.h"
33 
34 class Rational
35 {
36 public:
37 
38  // Defining an object
39  Rational() : mnom(0), mdenom(1) {}
40  Rational(const Index& n1) : mnom(n1), mdenom(1) {}
41  Rational(const int& n1) : mnom((Index)n1), mdenom(1) {}
42  Rational(const Index& n1, const Index& n2) : mnom(n1), mdenom(n2) {Simplify();}
43 
44  // Reading values of object
45  Index Nom() const {return mnom;}
46  Index Denom() const {return mdenom;}
47 
48  bool isUndefined() const { return (mnom == 0 && mdenom == 0); }
49 
50  // Converting object
51  Index toIndex() const;
52  Numeric toNumeric() const { return (Numeric)mnom/(Numeric)mdenom; }
53 
54  // Useful. Keep this around and/or improve.
55  void Simplify();
56 
57  // Assignment operators
58  Rational& operator+=(const Rational& a) {mnom = mnom*a.Denom() + a.Nom()*mdenom;mdenom *= a.Denom();Simplify(); return *this;}
59  Rational& operator-=(const Rational& a) {mnom = mnom*a.Denom() - a.Nom()*mdenom;mdenom *= a.Denom();Simplify(); return *this;}
60  Rational& operator/=(const Rational& a) {mnom*=a.Denom();mdenom*=a.Nom();Simplify(); return *this;}
61  Rational& operator*=(const Rational& a) {mnom*=a.Nom();mdenom*=a.Denom();Simplify(); return *this;}
62 
63  // Iterative operators
64  Rational operator++(int) {mnom += mdenom; return *this;}
65  Rational operator--(int) {mnom -= mdenom; return *this;}
66  Rational& operator++() {mnom += mdenom; return *this;}
67  Rational& operator--() {mnom -= mdenom; return *this;}
68 
69 private:
70  // Rational is supposed to be used rationally ( mnom / mdenom )
73 };
74 
75 #define RATIONAL_UNDEFINED Rational(0, 0)
76 
77 // Arithmetic operations
78 inline Rational operator-(const Rational& a) {return Rational(-a.Nom(), a.Denom());}
79 inline Rational operator+(const Rational& a) {return a;}
80 inline Rational operator+(const Rational& a, const Rational& b) {return Rational(a.Nom()*b.Denom() + b.Nom()*a.Denom(),a.Denom() * b.Denom());}
81 inline Rational operator-(const Rational& a, const Rational& b) {return Rational(a.Nom()*b.Denom() - b.Nom()*a.Denom(),a.Denom() * b.Denom());}
82 inline Rational operator/(const Rational& a, const Rational& b) {return Rational(a.Nom()*b.Denom(),a.Denom() * b.Nom());}
83 inline Rational operator*(const Rational& a, const Rational& b) {return Rational(a.Nom()*b.Nom(),a.Denom() * b.Denom());}
84 inline Rational operator%(const Rational& a, const Rational& b) {return Rational((a.Nom()*b.Denom())%(a.Denom()*b.Nom()),a.Denom()*b.Denom());}
85 
86 // Boolean operations
87 inline bool operator==(const Rational& a, const Rational& b) {return a.Denom()!=0 && b.Denom()!=0 && a.Nom()*b.Denom()==a.Denom()*b.Nom();}
88 inline bool operator!=(const Rational& a, const Rational& b) {return !operator==(a, b);}
89 inline bool operator<(const Rational& a, const Rational& b) {return a.Denom()!=0 && b.Denom()!=0 && a.Nom()*b.Denom()<a.Denom()*b.Nom();}
90 inline bool operator>(const Rational& a, const Rational& b) {return operator<(b, a);}
91 inline bool operator<=(const Rational& a, const Rational& b) {return !operator>(a, b);}
92 inline bool operator>=(const Rational& a, const Rational& b) {return !operator<(a, b);}
93 
94 inline Numeric fac(const Rational& r) { return (::fac(r.toIndex())); }
95 
96 std::ostream& operator<<(std::ostream& os, const Rational& a);
97 
98 std::istream& operator>>(std::istream& os, Rational& a);
99 
100 Rational abs(const Rational& a);
101 
103 
104 #endif // rational_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index Nom() const
Definition: rational.h:45
Rational operator-(const Rational &a)
Definition: rational.h:78
Rational abs(const Rational &a)
Definition: rational.cc:107
std::ostream & operator<<(std::ostream &os, const Rational &a)
Definition: rational.cc:65
Rational()
Definition: rational.h:39
std::istream & operator>>(std::istream &os, Rational &a)
Definition: rational.cc:71
bool operator<=(const Rational &a, const Rational &b)
Definition: rational.h:91
Rational operator%(const Rational &a, const Rational &b)
Definition: rational.h:84
Rational & operator*=(const Rational &a)
Definition: rational.h:61
Rational operator*(const Rational &a, const Rational &b)
Definition: rational.h:83
Rational operator+(const Rational &a)
Definition: rational.h:79
Rational(const int &n1)
Definition: rational.h:41
Index mnom
Definition: rational.h:71
Rational & operator/=(const Rational &a)
Definition: rational.h:60
Rational operator++(int)
Definition: rational.h:64
Rational & operator++()
Definition: rational.h:66
Rational & operator+=(const Rational &a)
Definition: rational.h:58
Numeric fac(const Rational &r)
Definition: rational.h:94
Index mdenom
Definition: rational.h:72
bool isUndefined() const
Definition: rational.h:48
Rational & operator--()
Definition: rational.h:67
bool operator==(const Rational &a, const Rational &b)
Definition: rational.h:87
bool operator>(const Rational &a, const Rational &b)
Definition: rational.h:90
void Simplify()
Definition: rational.cc:44
bool operator!=(const Rational &a, const Rational &b)
Definition: rational.h:88
This file contains the definition of Array.
bool operator>=(const Rational &a, const Rational &b)
Definition: rational.h:92
Rational operator--(int)
Definition: rational.h:65
Numeric toNumeric() const
Definition: rational.h:52
Array< Rational > ArrayOfRational
Definition: rational.h:102
Rational(const Index &n1, const Index &n2)
Definition: rational.h:42
Index toIndex() const
Definition: rational.cc:32
Rational operator/(const Rational &a, const Rational &b)
Definition: rational.h:82
Rational & operator-=(const Rational &a)
Definition: rational.h:59
bool operator<(const Rational &a, const Rational &b)
Definition: rational.h:89
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
Index Denom() const
Definition: rational.h:46
Rational(const Index &n1)
Definition: rational.h:40
This can be used to make arrays out of anything.
Definition: array.h:40