-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorpoly.cpp
159 lines (152 loc) · 4.47 KB
/
factorpoly.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
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdlib>
#include "Polynomial.h"
#include "VeryLong.h"
#include "Polynomial.inl"
std::string handler(const std::string& polynomial_string)
{
Polynomial<VeryLong> poly = Polynomial<VeryLong>::read_polynomial(polynomial_string.c_str());
std::vector<Polynomial<VeryLong> > factors;
VeryLong cont;
bool factor_double_check = false;
if (std::getenv("FACTOR_DOUBLE_CHECK") && (::atoi(std::getenv("FACTOR_DOUBLE_CHECK")) & 1)) factor_double_check = true;
bool factor_use_LLL = false;
if (std::getenv("FACTOR_USE_LLL") && (::atoi(std::getenv("FACTOR_USE_LLL")) & 1)) factor_use_LLL = true;
if (factor_use_LLL)
{
Polynomial<VeryLong>::factor_LLL(poly, factors, cont);
}
else
{
Polynomial<VeryLong>::factor(poly, factors, cont);
}
if (factor_double_check)
{
// Check that poly is fully factored
Polynomial<VeryLong> remaining_part(poly);
for (auto& factor: factors)
{
remaining_part /= factor;
}
if (remaining_part.deg() > 0)
{
factors.push_back(remaining_part);
}
// Check that factors can't be factored more
bool done = false;
while (!done)
{
std::vector<Polynomial<VeryLong> > newfactors;
for (auto& factor: factors)
{
std::vector<Polynomial<VeryLong> > subfactors;
VeryLong subcont;
Polynomial<VeryLong>::factor(factor, subfactors, subcont);
newfactors.insert(std::end(newfactors), std::begin(subfactors), std::end(subfactors));
}
if (newfactors.size() == factors.size())
{
done = true;
}
else
{
factors = newfactors;
}
}
}
// Gather common factors
std::map<std::string, int> gathered_factors;
for (auto& factor: factors)
{
std::ostringstream oss;
oss << factor;
gathered_factors[oss.str()]++;
}
std::ostringstream oss;
if (cont == VeryLong(-1L))
{
oss << "-";
}
else if (cont != VeryLong(1L))
{
oss << cont;
}
for (auto& gf: gathered_factors)
{
std::string f = gf.first;
int n = gf.second;
if (n == 1)
{
oss << "(" << f << ")";
}
else
{
if (n < 10)
{
oss << "(" << f << ")^" << n;
}
else
{
oss << "(" << f << ")^" << "{" << n << "}";
}
}
}
// for (auto& factor: factors)
// {
// oss << "(" << factor << ")";
// }
return oss.str();
}
std::string mathjax_output(const std::string& p)
{
std::ostringstream oss;
oss << "<!DOCTYPE html>" << std::endl;
oss << "<html>" << std::endl;
oss << "<head>" << std::endl;
oss << " <meta charset=\"utf-8\">" << std::endl;
oss << " <meta name=\"viewport\" content=\"width=device-width\">" << std::endl;
oss << " <title>factorpoly output</title>" << std::endl;
oss << " <script type=\"text/javascript\" async" << std::endl;
oss << " src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML\">" << std::endl;
oss << "</script>" << std::endl;
oss << "</head>" << std::endl;
oss << "<body>" << std::endl;
oss << "<p>" << std::endl;
oss << "$$" << p << "$$" << std::endl;
oss << "</p>" << std::endl;
oss << "</body>" << std::endl;
oss << "</html>" << std::endl;
return oss.str();
}
int main(int argc, char* argv[])
{
std::string input;
std::getline(std::cin, input);
if (input.find("polynomial=") == 0)
{
input.replace(0, 11, "");
}
input.erase(std::remove(input.begin(), input.end(), '\n'), input.end());
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
input.erase(std::remove(input.begin(), input.end(), '\n'), input.end());
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
try
{
std::string output = handler(input);
if (std::getenv("USE_MATHJAX"))
{
output = mathjax_output(output);
}
std::cout << output << std::endl;
}
catch (const std::string& e)
{
std::cout << e << std::endl;
}
return 0;
}