-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolynomial.h
More file actions
76 lines (66 loc) · 1.68 KB
/
Copy pathpolynomial.h
File metadata and controls
76 lines (66 loc) · 1.68 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
template<typename T>
class Polynomial : protected std::vector<T>
{
public:
Polynomial(unsigned n) : std::vector<T>(n)
{}
Polynomial(const initializer_list<T> &l) : std::vector<T>(l)
{}
static Polynomial pow(T base, unsigned n)
{
Polynomial p(n);
T a = 1;
for (unsigned i = 0; i < n; i++)
p[i] = a, a *= base;
return p;
}
static Polynomial generate(unsigned n, unsigned k = 1)
{
Polynomial p(n * k + 1);
for (unsigned i = 0; i <= n * k; i += n)
p[i] = 1;
return p;
}
Polynomial conv(const Polynomial &g) const
{
Polynomial c(this->size() + g.size() - 1);
for (unsigned i = 0; i < this->size(); i++)
for (unsigned j = 0; j < g.size(); j++)
c[i + j] += this->at(i) * g.at(j);
return c;
}
Polynomial invo(unsigned n) const
{
Polynomial p = *this;
for (unsigned i = 1; i < n; i++)
p = p.conv(*this);
return p;
}
T &operator [](unsigned pos)
{
return this->vector<T>::operator [](pos);
}
Polynomial operator *(const Polynomial &g) const
{
return this->conv(g);
}
Polynomial operator ^(unsigned n) const
{
return this->invo(n);
}
#ifdef _GLIBCXX_STRING
std::string toString() const
{
std::string s;
s.push_back('[');
for (typename std::vector<T>::const_iterator i = this->cbegin(); i != this->cend(); i++)
{
if (i != this->cbegin())
s.push_back(',');
s.append(to_string(*i));
}
s.push_back(']');
return s;
}
#endif
};