-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.hh
185 lines (162 loc) · 7.94 KB
/
Polynomial.hh
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
//===================================================================//
//===================================================================//
/* The Polynomial class stores a polynomial and performs many
mathematical operations with it.
A basic polynomial is of the following form: p(x) = a0 + a1x + a2x^2
+ ... + adx^d
where /a/'s are the coefficients of the polynomial and /d/ is the degree
of the polynomial.
Note that the /a/'s subindex /i/ corresponds to the exponent of the /x/
that is multiplying that coefficient.
The Polynomial class is aimed to do the following functions which will
be all implemented in \methods/:
- Create a polynomial.
- Consult a polynomial.
- Modify any term (monomial).
- Evaluate it on a certain x.
- Basic arithmetic operations: addition, subtraction, multiplication
and division.
- Get the maximum common divisor of two polynomials.
- Represent a polynomial as (1) a string, (2) a vector of coefficients.
*/
//===================================================================//
//===================================================================//
#ifndef Polynomial_hh
#define Polynomial_hh
//--------------------//
#include <complex> //
#include <vector> //
#include <string> //
//--------------------//
using namespace std;
typedef vector<double> Coefficients; // Coefficients of a Polynomial.
typedef vector<complex<double>> iCoefficients; // Complex coefficients.
class Polycomplex; // Complex polynomial...
//===================================================================//
//===================================================================//
class Polynomial { // Real polynomial...
public:
//===================================================================//
// INICIALIZATION FUNCTIONS ://
/*---------------------------------------------------------------*/
// Trivial empty constructor. To be able to clear the map.
Polynomial () {}
// Constructor from vector.
Polynomial (const vector<double>& coeff);
//===================================================================//
// VISUALIZATION FUNCTIONS ://
/*---------------------------------------------------------------*/
// Write the elements of the vector of coefficients.
void write () const;
/*---------------------------------------------------------------*/
// Get the degree of the polynomial.
int get_degree () const;
/*---------------------------------------------------------------*/
// Get the size of the vector of coefficients.
int get_size () const;
/*---------------------------------------------------------------*/
// Get the coefficient of a given degree.
double get_coeff (int deg) const;
/*---------------------------------------------------------------*/
// Transforms a Polynomial to a poly-string.
void toString () const; // with cout's.
/*---------------------------------------------------------------*/
// Transforms a Polynomial to a poly-string.
string toString2 () const; // returning a string.
//===================================================================//
// MODIFICATION FUNCTIONS ://
/*---------------------------------------------------------------*/
// Modify the coefficient of a given degree.
Polynomial& mod_coeff (int deg, double value);
//===================================================================//
// ARITHMETIC FUNCTIONS ://
/*---------------------------------------------------------------*/
// Perform the addition of two polynomials.
Polynomial operator+ (const Polynomial& poly) const;
/*---------------------------------------------------------------*/
// Perform the substraction of two polynomials.
Polynomial operator- (const Polynomial& poly) const;
/*---------------------------------------------------------------*/
// Perform the multiplication of two polynomials.
Polynomial operator* (const Polynomial& poly) const;
/*---------------------------------------------------------------*/
// Perform the division of two polynomials.
void divide (const Polynomial& dividend, const Polynomial& divisor,
Polynomial& quotient, Polynomial& remainder);
/*---------------------------------------------------------------*/
// Return the quotient of a division.
Polynomial operator/ (const Polynomial& divisor);
/*---------------------------------------------------------------*/
// Perform the multiplication of two polynomials. FOURIER.
Polynomial mul_fft (const Polynomial& poly);
//===================================================================//
// OTHER UTILITIES ://
/*---------------------------------------------------------------*/
// Evaluate the polynomial on a certain x.
double operator() (double x) const;
/*---------------------------------------------------------------*/
// Get the maximum common divisor of two polynomials.
Polynomial gcd (const Polynomial& poly) const;
/*---------------------------------------------------------------*/
// Convert to complex Polynomial.
Polycomplex toComplex ();
/*---------------------------------------------------------------*/
// Updates the polynomial's degree so that the leading
// coefficient is not zero.
Polynomial& normalize ();
private:
//===================================================================//
// INICIALIZATION OF PRIVATE FIELDS ://
vector<double> coeff; // this vector stores each coeff. per degree.
// string spoly; // this is a poly-string.
//===================================================================//
// PRIVATE METHODS ://
/*---------------------------------------------------------------*/
// Get the maximum common divisor of two polynomials.
Polynomial operator% (const Polynomial& divisor);
/*---------------------------------------------------------------*/
// Convert to monic Polynomial.
Polynomial mono ();
/*---------------------------------------------------------------*/
// Calculate an appropriate power for /omega/ to execute FFT.
int power_2 (int n);
/*---------------------------------------------------------------*/
// Clean the vector of coefficients of floating-point errors.
Polynomial& clean();
};
//===================================================================//
//===================================================================//
class Polycomplex { // The class of the Complex Polynomial...
public:
// INICIALIZATION FUNCTIONS ://
/*---------------------------------------------------------------*/
// Trivial empty constructor. To be able to clear the map.
Polycomplex () {}
// Constructor from vector.
Polycomplex (const iCoefficients& icoeff);
//===================================================================//
// Get the size of the vector of coefficients.
int get_size () const;
/*---------------------------------------------------------------*/
// Get the coefficient of a given degree.
complex<double> get_coeff (int deg) const;
/*---------------------------------------------------------------*/
// Perform the FFT, obtaining the point-value representation. FOURIER.
Polycomplex FFT ();
/*---------------------------------------------------------------*/
// Perform the iFFT. INVERSE FOURIER.
Polycomplex iFFT ();
/*---------------------------------------------------------------*/
// Convert a Polycomplex into Polynomial.
Polynomial toReal ();
/*---------------------------------------------------------------*/
// Perform the multiplication of two polycomplexes.
Polycomplex complex_mul (const Polycomplex& pcomplex) const;
private:
//===================================================================//
// INICIALIZATION OF PRIVATE FIELDS ://
// Coefficients with Real and Complex (Imaginary) parts.
iCoefficients icoeff;
};
//===================================================================//
#endif