-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.cpp
executable file
·185 lines (137 loc) · 3.58 KB
/
polynomial.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
#include "polynomial.hpp"
#include <sstream>
#include <algorithm>
Polynomial::Polynomial(int deg){
coeffs.resize(deg+1);
}
Polynomial::Polynomial(vector<double> v){
coeffs=v;
reduce();
}
void Polynomial::reduce(){
int hnz=coeffs.size()-1;
while(hnz>=0&&abs(coeffs[hnz])<1e-9)
hnz--;
coeffs.resize(hnz+1);
}
string Polynomial::toString() const{
if(coeffs.size()==0)
return "0";
string s="";
for(int i=coeffs.size()-1; i>=0; i--){
if(coeffs[i]==0)
continue;
if(s!="")
s+=" + ";
stringstream ss;
ss<<coeffs[i];
string tmp;
ss>>tmp;
if(i==0 || coeffs[i]!=1)
s+=tmp;
if(i==0)
break;
s+="x";
if(i!=1){
s+="^";
stringstream ss2;
ss2<<i;
string exp;
ss2>>exp;
s+=exp;
}
}
return s;
}
double Polynomial::coeff(int d) const{
if(d>=coeffs.size())
return 0;
return coeffs[d];
}
int Polynomial::deg() const{
return coeffs.size()-1;
}
bool Polynomial::operator ==(Polynomial g) const{
return coeffs==g.coeffs;
}
Polynomial Polynomial::operator +(Polynomial g) const{
Polynomial ret(max(this->deg(), g.deg()));
for(int i=0; i<=max(this->deg(), g.deg()); i++)
ret.coeffs[i]=this->coeff(i) + g.coeff(i);
ret.reduce();
return ret;
}
Polynomial Polynomial::operator -(Polynomial g) const{
Polynomial ret(max(this->deg(), g.deg()));
for(int i=0; i<=max(this->deg(), g.deg()); i++)
ret.coeffs[i]=this->coeff(i) - g.coeff(i);
ret.reduce();
return ret;
}
Polynomial Polynomial::operator *(Polynomial g) const{
if(this->deg()==-1 || g.deg()==-1)
return Polynomial({0});
int d=this->deg()+g.deg();
Polynomial ret(d);
for(int i=0; i<=d; i++)
for(int e=0; e<=i; e++)
ret.coeffs[i]+=this->coeff(i-e)*g.coeff(e);
ret.reduce(); ///fucking zerodivisors...
return ret;
}
Polynomial Polynomial::operator %(Polynomial g) const{
if(g.deg()==0)
return Polynomial({0});
if(g.deg()==-1||g.deg()>this->deg())
return (*this)+Polynomial({0});
int d_h=this->deg()-g.deg();
vector<double> v_h(d_h+1), v_f=coeffs, v_g=g.coeffs;
int up=this->deg();
double lead=*v_g.rbegin();
for(int i=d_h; i>=0; i--){
double c=v_f[up]/lead;
v_h[i]=c;
for(int e=0; e<=g.deg(); e++)
v_f[up-e]-=c*v_g[g.deg()-e];
v_f[up]=0;
up--;
}
return Polynomial(v_f);
}
Polynomial Polynomial::operator /(Polynomial g) const{
if(g.deg()==0)
return Polynomial({0});
if(g.deg()==-1||g.deg()>this->deg())
return (*this)+Polynomial({0});
int d_h=this->deg()-g.deg();
vector<double> v_h(d_h+1), v_f=coeffs, v_g=g.coeffs;
int up=this->deg();
double lead=*v_g.rbegin();
for(int i=d_h; i>=0; i--){
double c=v_f[up]/lead;
v_h[i]=c;
for(int e=0; e<=g.deg(); e++)
v_f[up-e]-=c*v_g[g.deg()-e];
v_f[up]=0;
up--;
}
return Polynomial(v_h);
}
Polynomial Polynomial::derivative() const{
Polynomial ret(this->deg()-1);
for(int i=0; i<=this->deg()-1; i++)
ret.coeffs[i]=(i+1)*this->coeffs[i+1];
return ret;
}
Polynomial GCD(Polynomial f, Polynomial g){
if(f%g==Polynomial({0}))
return g;
return GCD(g,f%g);
}
bool isSeparable(Polynomial f){
return GCD(f,f.derivative()).deg()==0;
}
ostream& operator<<(ostream& os, const Polynomial& f){
os<<f.toString();
return os;
}