-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrationalinteger.cpp
207 lines (148 loc) · 5.09 KB
/
rationalinteger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "rationalinteger.h"
RationalInteger::~RationalInteger() {}
RationalInteger::RationalInteger() : iVal(0) {}
RationalInteger::RationalInteger(long int iVal) : iVal(iVal) {}
RationalInteger::RationalInteger(const RationalInteger &ri) : iVal(ri.iVal) {}
AlgebraicInteger *RationalInteger::aiCopyToInteger(const int &n) const {
return new RationalInteger(n);
}
AlgebraicInteger *RationalInteger::copy() const {
return new RationalInteger(*this);
}
void RationalInteger::set(const int &n) { iVal = n; }
void RationalInteger::set(AlgebraicInteger *ai) {
RationalInteger *ri(dynamic_cast<RationalInteger *>(ai));
iVal = ri->iVal;
}
bool RationalInteger::isInvertible() const {
return (iVal == 1 || iVal == -1);
}
bool RationalInteger::isSquareOfIvertible() const { return (iVal == 1); }
void RationalInteger::gcd(const AlgebraicInteger *ai) {
const RationalInteger *ri(dynamic_cast<const RationalInteger *>(ai));
long u(iVal < 0 ? -iVal : iVal), v(ri->iVal < 0 ? -ri->iVal : ri->iVal);
while (v != 0) {
unsigned int r = u % v;
u = v;
v = r;
}
iVal = u;
}
long int RationalInteger::get_iValue() const { return iVal; }
void RationalInteger::removeSquareFactors() {
unsigned long int iDivisor(3), iDivisorSquared;
unsigned long int iResult(1);
if (iVal == 1)
return;
while (!(iVal % 4))
iVal /= 4;
if (!(iVal % 2)) {
iResult *= 2;
do {
iVal /= 2;
} while (!(iVal % 2));
}
while (iVal > 1) {
iDivisorSquared = iDivisor * iDivisor;
while (!(iVal % iDivisorSquared))
iVal /= iDivisorSquared;
if (!(iVal % iDivisor)) {
iResult *= iDivisor;
do {
iVal /= iDivisor;
} while (!(iVal % iDivisor));
}
iDivisor += 2;
}
iVal = iResult;
}
bool operator<(const RationalInteger &i1, const RationalInteger &i2) {
return (i1.iVal < i2.iVal);
}
bool RationalInteger::isLessThan(const AlgebraicInteger &ai) const {
return (iVal < dynamic_cast<const RationalInteger &>(ai).iVal);
}
bool RationalInteger::isLessOEThan(const AlgebraicInteger &ai) const {
return (iVal <= dynamic_cast<const RationalInteger &>(ai).iVal);
}
bool RationalInteger::isLessThan(const int &n) const { return (iVal < n); }
bool RationalInteger::isGreaterThan(const int &n) const { return (iVal > n); }
bool RationalInteger::isGreaterOEThan(const int &n) const {
return (iVal >= n);
}
bool RationalInteger::isEqualTo(const AlgebraicInteger &ai) const {
return (iVal == dynamic_cast<const RationalInteger &>(ai).iVal);
}
bool RationalInteger::isEqualTo(const int &n) const { return (iVal == n); }
bool RationalInteger::isDivisibleBy(const AlgebraicInteger *ai) const {
return !(iVal % dynamic_cast<const RationalInteger *>(ai)->iVal);
}
bool RationalInteger::divideByIfDivisible(const AlgebraicInteger *ai) {
long int iV(dynamic_cast<const RationalInteger *>(ai)->iVal);
if (iVal % iV)
return false;
iVal /= iV;
return true;
}
void RationalInteger::divideBy(const AlgebraicInteger *ai) {
iVal /= dynamic_cast<const RationalInteger *>(ai)->iVal;
}
void RationalInteger::add(const AlgebraicInteger *ai) {
iVal += dynamic_cast<const RationalInteger *>(ai)->iVal;
}
void RationalInteger::multiplyBy(const int &n) { iVal *= n; }
void RationalInteger::multiplyBy(const AlgebraicInteger *ai) {
iVal *= dynamic_cast<const RationalInteger *>(ai)->iVal;
}
void RationalInteger::substract(const AlgebraicInteger *ai) {
iVal -= dynamic_cast<const RationalInteger *>(ai)->iVal;
}
void RationalInteger::opp() { iVal *= -1; }
ostream &RationalInteger::print(ostream &o) const {
o << iVal;
return o;
}
string RationalInteger::to_string(const string &strFormat,
const bool &bProtect) const {
return std::to_string(iVal);
}
double RationalInteger::to_double() const { return (double)iVal; }
string RationalInteger::get_classname() const { return "RationalInteger"; }
// ------------------------------------------------------------------------
// Operators
RationalInteger &RationalInteger::operator=(const RationalInteger &ri) {
iVal = ri.iVal;
return *this;
}
RationalInteger &RationalInteger::operator/=(const RationalInteger &ri) {
iVal /= ri.iVal;
return *this;
}
RationalInteger &RationalInteger::operator*=(const RationalInteger &ri) {
iVal *= ri.iVal;
return *this;
}
RationalInteger RationalInteger::operator-() const {
return RationalInteger(-iVal);
}
RationalInteger RationalInteger::operator+(const RationalInteger &ri2) const {
return RationalInteger(iVal + ri2.iVal);
}
RationalInteger RationalInteger::operator*(const RationalInteger &ri2) const {
return RationalInteger(iVal * ri2.iVal);
}
RationalInteger RationalInteger::operator-(const RationalInteger &ri2) const {
return RationalInteger(iVal - ri2.iVal);
}
bool RationalInteger::operator==(const long int &i) const {
return (iVal == i);
}
bool RationalInteger::operator==(const RationalInteger &ri) const {
return (iVal == ri.iVal);
}
bool RationalInteger::operator>(const RationalInteger &ri) const {
return !isLessOEThan(ri);
}
bool RationalInteger::operator<(const RationalInteger &ri) const {
return isLessThan(ri);
}