-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.cpp
More file actions
215 lines (182 loc) · 6.52 KB
/
Math.cpp
File metadata and controls
215 lines (182 loc) · 6.52 KB
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
208
209
210
211
212
213
214
#include "Math.h"
Real Math::abs(const Real &a) {
if (a < 0) return -1 * a;
return a;
}
Integer Math::sign(const Real &a) {
if (a < 0) return -1;
if (a == 0) return 0;
return 1;
}
Integer Math::gcd(const Integer &a, const Integer &b) {
if (a == 0) return b;
if (b == 0) return a;
return gcd(b, a % b);
}
Integer Math::lcm(const Integer &a, const Integer &b) {
return static_cast<Integer>(abs(a * b)) / gcd(a, b);
}
Rational::Rational() {
this->numerator = 0;
this->denominator = 1;
}
Rational::Rational(const Integer &numerator, const Natural &denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
Rational::Rational(const std::pair<Integer, Natural> &frac) {
this->numerator = frac.first;
this->denominator = frac.second;
}
Rational::Rational(const Rational &rat) {
this->numerator = rat.numerator;
this->denominator = rat.denominator;
}
void Rational::reduce() {
const Integer gcd = Math::gcd(this->numerator, static_cast<Integer>(this->denominator));
this->numerator /= gcd;
this->denominator /= gcd;
}
void Rational::inverse() {
Integer new_numerator;
Natural new_denominator;
switch (Math::sign(this->numerator)) {
case -1: {
new_numerator = -1 * static_cast<Integer>(this->denominator);
new_denominator = static_cast<Natural>(Math::abs(this->numerator));
break;
}
case 0: {
std::cerr << "Division by zero!";
new_numerator = INF;
new_denominator = 1;
break;
}
case 1: {
new_numerator = static_cast<Integer>(this->denominator);
new_denominator = static_cast<Natural>(Math::abs(this->numerator));
break;
}
default: {
return;
}
}
this->numerator = new_numerator;
this->denominator = new_denominator;
}
double Rational::cast() const {
return static_cast<Real>(this->numerator) / this->denominator;
}
Rational & Rational::operator=(const std::pair<Integer, Natural> &frac) {
this->numerator = frac.first;
this->denominator = frac.second;
return *this;
}
Rational & Rational::operator+= (const Rational &rhs) {
const Natural common_denominator = Math::lcm(static_cast<Integer>(this->denominator), static_cast<Integer>(rhs.denominator));
this->numerator = static_cast<Integer>(common_denominator / this->denominator) * this->numerator;
const Integer rhs_numerator = static_cast<Integer>(common_denominator / rhs.denominator) * rhs.numerator;
this->numerator += rhs_numerator;
this->denominator = common_denominator;
this->reduce();
return *this;
}
Rational & Rational::operator-= (const Rational &rhs) {
const Natural common_denominator = Math::lcm(static_cast<Integer>(this->denominator), static_cast<Integer>(rhs.denominator));
this->numerator = static_cast<Integer>(common_denominator / this->denominator) * this->numerator;
const Integer rhs_numerator = static_cast<Integer>(common_denominator / rhs.denominator) * rhs.numerator;
this->numerator -= rhs_numerator;
this->denominator = common_denominator;
this->reduce();
return *this;
}
Rational & Rational::operator*= (const Rational &rhs) {
this->numerator *= rhs.numerator;
this->denominator *= rhs.denominator;
this->reduce();
return *this;
}
Rational & Rational::operator/= (Rational rhs) {
rhs.inverse();
this->operator*=(rhs);
return *this;
}
Rational operator* (const Rational &lhs, const Rational &rhs) {
Rational res(lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator);
res.reduce();
return res;
}
Rational operator/ (const Rational &lhs, Rational rhs) {
rhs.inverse();
Rational res = lhs * rhs;
res.reduce();
return res;
}
std::ostream & operator<< (std::ostream &os, const Rational &rat) {
os << rat.numerator << "/" << rat.denominator;
return os;
}
Rational operator+ (const Rational &lhs, const Rational &rhs) {
const Natural common_denominator = Math::lcm(static_cast<Integer>(lhs.denominator), static_cast<Integer>(rhs.denominator));
const Integer lhs_numerator = static_cast<Integer>(common_denominator / lhs.denominator) * lhs.numerator;
const Integer rhs_numerator = static_cast<Integer>(common_denominator / rhs.denominator) * rhs.numerator;
Rational res(lhs_numerator + rhs_numerator, common_denominator);
res.reduce();
return res;
}
Rational operator- (const Rational &lhs, const Rational &rhs) {
const Natural common_denominator = Math::lcm(static_cast<Integer>(lhs.denominator), static_cast<Integer>(rhs.denominator));
const Integer lhs_numerator = static_cast<Integer>(common_denominator / lhs.denominator) * lhs.numerator;
const Integer rhs_numerator = static_cast<Integer>(common_denominator / rhs.denominator) * rhs.numerator;
Rational res(lhs_numerator - rhs_numerator, common_denominator);
res.reduce();
return res;
}
Matrix::Matrix(): Matrix(1, 1) {}
Matrix::Matrix(const Natural &m, const Natural &n) : dimension(m, n) {
this->data = new Real* [this->dimension.m];
for (Natural i = 0; i < this->dimension.m; ++i) {
this->data[i] = new Real [this->dimension.n];
}
}
Matrix::Matrix(const Dimension &dim) : Matrix(dim.m, dim.n) {}
Matrix::Matrix(const Matrix &matrix) : Matrix(matrix.dimension.m, matrix.dimension.n) {
for (Natural i = 0; i < this->dimension.m; ++i) {
for (Natural j = 0; j < this->dimension.n; ++j) {
this->data[i][j] = matrix.data[i][j];
}
}
}
void Matrix::transpose() {
Matrix T(this->dimension.n, this->dimension.m);
for (Natural i = 0; i < T.dimension.m; ++i) {
for (Natural j = 0; j < T.dimension.n; ++j) {
T[{i, j}] = this->data[j][i];
}
}
*this = T;
}
Matrix Matrix::T() const {
Matrix T(this->dimension.n, this->dimension.m);
for (Natural i = 0; i < T.dimension.m; ++i) {
for (Natural j = 0; j < T.dimension.n; ++j) {
T[{i, j}] = this->data[j][i];
}
}
return T;
}
Matrix::Dimension Matrix::dim() {
return this->dimension;
}
Real & Matrix::operator[](const std::pair<Natural, Natural> &pos) {
return this->data[pos.first][pos.second];
}
Real Matrix::operator()(const Natural &i, const Natural &j) const {
return this->data[i][j];
}
Function linear(Real k, Real b) {
return [k, b] (const Real& x) -> Real { return k * x + b; };
}
Function quadratic(Real a, Real b, Real c) {
return [a, b, c] (const Real& x) -> Real { return a * x * x + b * x + c; };
}