-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcmeta.cpp
More file actions
333 lines (281 loc) · 11.2 KB
/
calcmeta.cpp
File metadata and controls
333 lines (281 loc) · 11.2 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <cstring>
#include <utility>
#include "boost/mp11/list.hpp"
#include "boost/mp11/algorithm.hpp"
#include "boost/type_index.hpp"
#include "boost/mp11/utility.hpp"
#include "boost/mp11.hpp"
#include "Digits.hpp"
#include "Sum.hpp"
#include "Carry.hpp"
#include "Print.hpp"
#include "Miscellaenous.hpp"
#include "TypeList.hpp"
#include "core/Add.hpp"
#include "core/Subtract.hpp"
#include "core/Divide.hpp"
#include "core/Multiply.hpp"
#include "core/Binary.hpp"
#include "core/Exponent.hpp"
#include "notation/Scientific.hpp"
constexpr char ops = OPERATION[0];
using namespace boost::mp11;
template <typename PrintOperation, typename Notation>
struct Print : public PrintOperation, public Notation {
using Notation::printResult;
using PrintOperation::print;
static void result() {
print();
printResult();
}
};
template<typename Number1, typename Number2>
struct PrintBinaryOperation {
static void print() {
mp_for_each<mp_reverse<Number1>>([] (auto d) {
printDigit(d);
});
std::cout << " " << '*' << " ";
mp_for_each<mp_reverse<Number2>>([] (auto d) {
printDigit(d);
});
std::cout << " = ";
}
};
template<typename Number1>
struct PrintSingletonOperation {
static void print() {
mp_for_each<mp_reverse<Number1>>([] (auto d) {
printDigit(d);
});
std::cout << " = ";
}
};
template <typename Final>
struct Plain {
static bool found;
static int count;
static void printResult() {
mp_for_each<mp_reverse<Final>>([&](auto I) {
if (ops == '/') {
if (count == Final::decimal) {
std::cout << '.';
}
print(I);
}
else {
if constexpr (!std::is_same_v<decltype(I), Zero>) {
found = true;
}
if (found)
print(I);
}
count++;
});
}
};
template <typename Final>
bool Plain<Final>::found = false;
template <typename Final>
int Plain<Final>::count = 0;
template <typename... Ts>
struct Polynomial {
using type = std::tuple<Ts...>;
};
template <typename T, typename U, size_t N>
struct Monomial {
constexpr static size_t degree = N;
using sign = T;
using value = U;
};
template <typename... Ts>
struct concat_index_sequence {};
template <size_t... Ts>
struct concat_index_sequence<std::index_sequence<Ts...>> {
using value = std::index_sequence<Ts...>;
};
template <size_t... Ts, size_t... Us, typename... Ps>
struct concat_index_sequence<std::index_sequence<Ts...>, std::index_sequence<Us...>, Ps...> {
using value = typename concat_index_sequence<std::index_sequence<Ts..., Us...>, Ps...>::value;
};
template <fixed_string S, size_t I>
consteval char char_at() {
return S.value[I];
}
template <fixed_string S, typename T>
struct CharToMPList {};
template <fixed_string S, size_t... Is>
struct CharToMPList<S, std::index_sequence<Is...>> {
using polylist = mp_list<typename convertIntDigit<S.value[Is] - '0'>::D...>;
};
template <char ch>
struct IsBinaryOperation : std::bool_constant<
ch == '+' || ch == '-'> {};
//SFINAE for IsPolyChar does NOT work :(
template <char ch>
struct IsPolyChar : std::bool_constant<
ch == 'x' || ch == '+' || ch == '-' || (ch - '0' <= 9 && ch - '0' >= 0) ||
ch == '^'>{};
template <template <char> class Cond, typename Seq, fixed_string S>
struct filter_indices;
template <template <char> class Cond, fixed_string S, size_t... Is>
struct filter_indices<Cond, std::index_sequence<Is...>, S> {
using type = typename concat_index_sequence<
std::conditional_t<Cond<S.value[Is]>::value,
std::index_sequence<Is>, std::index_sequence<>>...
>::value;
};
template <fixed_string S>
struct CharToPoly {
using indices = std::make_index_sequence<sizeof(S.value) - 1>;
using filtered = typename filter_indices<IsPolyChar, indices, S>::type;
using value = typename CharToMPList<S, filtered>::polylist;
};
template <fixed_string, typename... >
struct getindex {};
template <size_t... Idx, fixed_string S>
struct getindex<S, std::index_sequence<Idx...>> {
static void printindex() {
((std::cout << S.value[Idx]), ...);
}
};
// takes in mp_list of already filtered char's , filtered chars are
// chars that only have polynomial valid characters
// probably need to filter once more to separate out monomials
template <typename T, typename U>
struct MonomialIndices {};
// this contains list of start of monomials
template <typename T, typename... Ts, size_t... Idx>
struct MonomialIndices<mp_list<T, Ts...>, std::index_sequence<Idx...>> {
using head = typename concat_index_sequence<std::conditional_t<std::is_same_v<T, Neg> || std::is_same_v<T, Pos>,
std::index_sequence<Idx>, std::index_sequence<>>...>::value;
using value = typename concat_index_sequence<head, std::index_sequence<1 + sizeof...(Ts)>>::value;
};
template <typename T>
struct AdjacentDistance {};
template <size_t Idx1, size_t Idx2, size_t... Idx>
struct AdjacentDistance<std::index_sequence<Idx1, Idx2, Idx...>> {
using value = typename concat_index_sequence<std::index_sequence<Idx2 - Idx1 - 1>,
typename AdjacentDistance<std::index_sequence<Idx2, Idx...>>::value>::value;
};
template <size_t Idx>
struct AdjacentDistance<std::index_sequence<Idx>> {
using value = std::index_sequence<>;
};
template <typename T>
struct CharToMonoPairList {};
template <typename... Ts>
struct CharToMonoPairList<mp_list<Ts...>> {
using indices = typename MonomialIndices<mp_list<Ts...>, std::make_index_sequence<sizeof...(Ts)>>::value;
using dist = typename AdjacentDistance<indices>::value;
};
template <typename T, typename U>
struct MonomialIndicesToMonomial {};
template <typename T, typename... Ts, size_t Idx, size_t... Idx>
struct MonomialIndicesToMonomial<mp_list<T, Ts...>, std::index_sequence<Idxs...>> {
using mono = MonomialConstructor<mp_list<Ts...>, Idx>;
};
struct MonomialList {
using value = mp_list<PartialMPList<
}
template <typename... Ts, size Idx>
struct PartialMPList<mp_list<Ts...>, Idx> {
using value = mp_list<std::conditional_t<Idx >= 0, Ts, mp_list<>>...>;
}
// used to construct Monomial struct
// need the sign, coefficient, and power
struct MonomialConstructor {
using sign = T;
using coef = MonoCoefficient<mp_list<Ts...>>::value;
using power = MonoPower<mp_list<Ts...>>::value;
}
template <typename T>
struct MonoPower {};
template <typename... Ts>
struct MonoPower<mp_list<Ts...>> {
using value = std::conditional_t<std::is_same_v<Variable, T1> && std::is_same_v<Power, T2>,
mp_list<Ts...>, typename MonoPowerMPList<T2, Ts...>::value;
};
template <typename D>
struct isDigit : std::bool_constant<D::val >= 0 && D::val <= 9> {};
template <typename T, typename... Ts>
struct MonomialCoefficient<mp_list<T, Ts...> {
using value = flatten<mp_list<std::conditional_t<isDigit<T>::value,
mp_list<T, typename MonomialCoefficient<mp_list<Ts...>>::value>, mp_list<>>>::res;
};
template <fixed_string S>
struct CharToMono {
using indices = std::make_index_sequence<sizeof(S.value) - 1>;
using filtered = typename filter_indices<IsPolyChar, indices, S>::type;
using value = typename CharToMPList<S, filtered>::polylist;
};
int main() {
fixed_string s(FUNCTION);
using p = CharToPoly<FUNCTION>;
getindex<FUNCTION, typename p::filtered>::printindex();
#if defined(MULTIPLY)
using Number1 = mp_reverse<typename CharToDigit<NUMBER1>::digits>;
using Number2 = mp_reverse<typename CharToDigit<NUMBER2>::digits>;
using ans = typename Multiply<Number1, Number2, 0>::ans;
using Final = typename RecursiveAdd<ans>::ans;
Print<PrintBinaryOperation<Number1, Number2>, Scientific<PRECISION, Final>>::result();
//Print<PrintBinaryOperation<Number1, Number2>, Plain<Final>>::result();
#elif defined(ADD)
constexpr size_t n1 = mp_size<number1>::value;
constexpr size_t n2 = mp_size<number2>::value;
// Need to swap since the implementation requires the same number of digits
using num1 = typename Swap<number1, number2, (n1 > n2)>::num1;
using num2 = typename Swap<number1, number2, (n1 > n2)>::num2;
using zeros = mp_repeat_c<mp_list<Zero>, (mp_size<num1>::value - mp_size<num2>::value)>;
using alter = mp_append<num2, zeros>;
using final = typename flatten<typename Add<num1, alter, Zero>::ans>::res;
printAll<number1, number2, final, true, true, true, -1>(ops);
#elif defined(SUBTRACT)
// This requires two steps:
// 1. Perform padding to fill the two numbers with equal number of digits
// 2. Compare the two digits to see which one is bigger
// This require two steps because to compare which digit is bigger,
// Need to swap based on # of digits, and padd the smaller lesser digits with 0's
// Need to do this to compare the numbers digit by digit.
// Sure, we can compare just which number has more digits, and it's the greater one,
// But, what happens if the # of digits are the same. We still have to compare digit.
// So to consider both cases (same digits, different digits), need both step
constexpr size_t n1 = mp_size<number1>::value;
constexpr size_t n2 = mp_size<number2>::value;
using num1 = typename Swap<number1, number2, (n1 >= n2)>::num1;
using num2 = typename Swap<number1, number2, (n1 >= n2)>::num2;
using zeros = mp_repeat_c<mp_list<Zero>, (mp_size<num1>::value - mp_size<num2>::value)>;
using alter = mp_append<num2, zeros>;
// Next, we compare the digits which one is bigger, obviously starting from
// the most significant digit
using val = typename Compare<mp_reverse<num1>, mp_reverse<alter>>::ans;
using sub1 = typename Swap<num1, alter, val::value>::num1;
using sub2 = typename Swap<num1, alter, val::value>::num2;
using final = mp_flatten<typename Subtract<sub1, sub2, Zero>::ans>;
//printAll<number1, number2, final, true, true, val::value && (n1 >= n2), -1>(ops);
#elif defined(DIVIDE)
constexpr size_t n11 = mp_size<number1>::value;
constexpr size_t n22 = mp_size<number2>::value;
constexpr size_t offset = (n11 > n22) ? n11 - n22 : 0;
using C = Divide<mp_reverse<number1>, mp_append<mp_reverse<number2>,
mp_repeat_c<mp_list<Zero>, offset>>, PRECISION>;
using final = C::ans;
printAll<number1, number2, mp_reverse<final>, true, true, true, offset + C::decimal>(ops);
#elif defined(BINARY)
using p = typename Binary<mp_reverse<number1>, false>::ans;
using ans = ReconstructBinary<p>::ans;
std::cout << "Binary of " << NUMBER1 << ": ";
printSingle<ans>();
#elif defined(EXPONENT)
using f = typename Binary<mp_reverse<number2>, false>::ans;
using g = mp_reverse<typename ReconstructBinary<f>::ans>;
using d = typename Exponent<g, number1>::ans;
using pl = CleanBinary<d>::ans;
using final = RecursiveMultiply<pl>::ans;
std::cout << NUMBER1 << "^" << NUMBER2 << " = ";
printSingle<mp_reverse<final>>();
#endif
}