Laguerre polynomials
Loading...
Searching...
No Matches
Polynomial.h
Go to the documentation of this file.
1// Александр, Дмитрий
2
3#ifndef POLYNOMIAL_H
4#define POLYNOMIAL_H
5
6#include <vector>
7#include <complex>
8#include <iostream>
9
10#include "Polynomial.h"
11#include "BaseSolver.h"
12#include "ExtendedFunctions.h"
13
14namespace Laguerre{
15template<typename T>
17protected:
18 std::vector<std::complex<T>> roots;
19 std::vector<T> coeffs;
20
21 // Pointer to a base solver class
22 BaseSolver<T>* Solver;
23public:
24
30 Polynomial(std::vector<T> _coeffs, std::vector<std::complex<T>> _roots){
31 setCoeffs(_coeffs);
32 setRoots(_roots);
33 }
34
39 Polynomial(std::vector<T> args){
40 setCoeffs(args);
41 }
42
47 template<typename... Args>
48 Polynomial(Args...args){
49 setCoeffs(args...);
50 }
51
56 int degree() const {
57 int size = coeffs.size();
58 return size > 0 ? size - 1 : 0;
59 }
60
65 void setRoots(std::vector<std::complex<T>> args){
66 size_t count = args.size();
67 if(count != degree())
68 throw std::invalid_argument("Count of given roots is different from given polynomial degree. ("
69 + std::to_string(count) + " vs " + std::to_string(degree()) + ")\n");
70 roots = args;
71 }
72
77 template<typename... Args>
78 void setRoots(Args...args){
79 setRoots(std::vector<std::complex<T>>{args...});
80 }
81
86 void setCoeffs(std::vector<T> args){
87 coeffs = args;
88 roots.clear();
89 }
90
95 template<typename... Args>
96 void setCoeffs(Args...args){
97 setCoeffs(std::vector<T>{args...});
98 }
99
104 void setSolver(BaseSolver<T>* solver) {
105 Solver = solver;
106 }
107
114 void solve(std::vector<std::complex<T>>& roots, std::vector<int>& conv, int maxiter=80){
115 if(Solver) {
116 (*Solver)(coeffs, roots, conv, 80);
117 // std::cout << "solved, exiting";
118 }
119 else{
120 throw std::invalid_argument("Solver wasnt set!\n");
121 }
122 }
123
125 std::vector<T> result_coeffs(std::max(this->degree(), rhs.degree()) + 1, 0);
126 for (int i = 0; i <= this->degree(); ++i) {
127 result_coeffs[i] += this->coeffs[i];
128 }
129
130 for (int i = 0; i <= rhs.degree(); ++i) {
131 result_coeffs[i] += rhs.coeffs[i];
132 }
133
134 return Polynomial<T>(result_coeffs);
135 }
136
137 Polynomial operator-(const Polynomial& rhs) const {
138 std::vector<T> result_coeffs(std::max(this->degree(), rhs.degree()) + 1, 0);
139 for (int i = 0; i <= this->degree(); ++i) {
140 result_coeffs[i] += this->coeffs[i];
141 }
142 for (int i = 0; i <= rhs.degree(); ++i) {
143 result_coeffs[i] -= rhs.coeffs[i];
144 }
145 return Polynomial<T>(result_coeffs);
146 }
147
148
149
151 int n = coeffs.size();
152 int m = other.coeffs.size();
153 std::vector<T> result(n + m - 1, 0);
154
155 for (int i = 0; i < n; ++i) {
156 for (int j = 0; j < m; ++j) {
157 result[i + j] += coeffs[i] * other.coeffs[j];
158 }
159 }
160 return Polynomial(result);
161 }
162
164 if (divisor.degree() > this->degree()) {
165 throw std::invalid_argument("The degree of the divisor is greater than the dividend");
166 }
167
168 Polynomial<T> dividend = *this;
169 std::vector<T> quotient_coeffs(this->degree() - divisor.degree() + 1, 0);
170 Polynomial<T> tmp(std::vector<T>(this->degree() + 1, 0));
171
172 while (dividend.degree() >= divisor.degree()) {
173 int degree_diff = dividend.degree() - divisor.degree();
174 T coeff = dividend.coeffs.back() / divisor.coeffs.back();
175
176 // Update the temporary polynomial for subtraction
177 tmp.coeffs[degree_diff] = coeff;
178
179 // Subtract and update the dividend
180 for (int i = 0; i <= divisor.degree(); ++i) {
181 dividend.coeffs[i + degree_diff] -= coeff * divisor.coeffs[i];
182 }
183 dividend.coeffs.pop_back();
184
185 // Update the quotient
186 quotient_coeffs[degree_diff] = coeff;
187 }
188
189 return Polynomial<T>(quotient_coeffs);
190 }
191
192
203 void divide(Polynomial& divisor, Polynomial& quotient, Polynomial& remainder) {
204 if (divisor.degree() > this->degree()) {
205 throw std::invalid_argument("The degree of the divisor is greater than the dividend");
206 }
207
208 Polynomial<T> dividend = *this;
209 std::vector<T> quotient_coeffs(this->degree() - divisor.degree() + 1, 0);
210 Polynomial<T> tmp;
211
212 while (dividend.degree() >= divisor.degree()) {
213 int degree_diff = dividend.degree() - divisor.degree();
214 T coeff = dividend.coeffs.back() / divisor.coeffs.back();
215
216 // Update the temporary polynomial for subtraction
217 tmp.coeffs = std::vector<T>(degree_diff + 1, 0);
218 tmp.coeffs.back() = coeff;
219
220 // Subtract and update the dividend
221 for (int i = 0; i <= divisor.degree(); ++i) {
222 dividend.coeffs[i + degree_diff] -= coeff * divisor.coeffs[i];
223 }
224 dividend.coeffs.pop_back();
225
226 // Update the quotient
227 quotient_coeffs[degree_diff] = coeff;
228 }
229
230 quotient = Polynomial<T>(quotient_coeffs);
231 remainder = dividend;
232 }
233
235 *this = *this * other;
236 this->roots.clear();
237 return *this;
238 }
239
241 if (this != &other) {
242 this->setCoeffs(other.coeffs);
243 }
244 return *this;
245 }
246
247 T operator[](int i) const{
248 return coeffs[i];
249 }
250
251 T& operator [](int i){
252 return coeffs[i];
253 }
254
267 std::vector<T> diff(int deg=1){
268 std::vector ret = coeffs;
269 for(int j = 0; j < deg; ++j){
270 for (int i = 1; i < ret.size(); ++i) {
271 ret[i] *= static_cast<T>(i);
272 }
273 if (!ret.size()) {
274 ret.erase(ret.begin());
275 }
276 else break;
277 }
278 return ret;
279 }
280
281 void print(){
282 int _degree = degree();
283 if(!_degree){
284 std::cout << "This polynomial object doesnt contain any coefficients.\n";
285 }
286 else{
287 for(int i = _degree; i >= 0; --i){
288 std::cout << "(" << coeffs[i] << ")";
289 std::cout << "x^" << i;
290 if(i != 0) {
291 std::cout << " + ";
292 }
293 }
294 std::cout << ". Degree: " << _degree << '\n';
295 std::cout << "Roots: " << roots.size() << "; ";
297 }
298 }
299};
300};
301
302#endif
p2 de j *z j
Definition Laguerre15m_or.h:23
cohn_schur ra local deg
Definition Laguerre15m_or.h:3
Definition Polynomial.h:16
Polynomial(std::vector< T > args)
Create polynomial object from given vector of coefficients.
Definition Polynomial.h:39
void setRoots(Args...args)
Set roots for this polynomial.
Definition Polynomial.h:78
Polynomial operator*(const Polynomial &other)
Definition Polynomial.h:150
void print()
Definition Polynomial.h:281
void setRoots(std::vector< std::complex< T > > args)
Set roots for this polynomial.
Definition Polynomial.h:65
void setCoeffs(std::vector< T > args)
Set coefficients for this polynomial.
Definition Polynomial.h:86
BaseSolver< T > * Solver
Definition Polynomial.h:22
void divide(Polynomial &divisor, Polynomial &quotient, Polynomial &remainder)
Divides one polynomial by another.
Definition Polynomial.h:203
std::vector< T > coeffs
Definition Polynomial.h:19
void setSolver(BaseSolver< T > *solver)
Set solver for this polynomial.
Definition Polynomial.h:104
Polynomial operator-(const Polynomial &rhs) const
Definition Polynomial.h:137
void solve(std::vector< std::complex< T > > &roots, std::vector< int > &conv, int maxiter=80)
Find the roots of this polynomial.
Definition Polynomial.h:114
T operator[](int i) const
Definition Polynomial.h:247
Polynomial & operator=(const Polynomial &other)
Definition Polynomial.h:240
Polynomial(std::vector< T > _coeffs, std::vector< std::complex< T > > _roots)
Create polynomial object from given vectors of coefficients and roots.
Definition Polynomial.h:30
Polynomial(Args...args)
Create polynomial object from given coefficients.
Definition Polynomial.h:48
Polynomial & operator*=(const Polynomial &other)
Definition Polynomial.h:234
int degree() const
Get polynomials degree.
Definition Polynomial.h:56
Polynomial operator+(Polynomial &rhs)
Definition Polynomial.h:124
std::vector< std::complex< T > > roots
Definition Polynomial.h:18
void setCoeffs(Args...args)
Set coefficients for this polynomial.
Definition Polynomial.h:96
Polynomial operator/(Polynomial &divisor)
Definition Polynomial.h:163
std::vector< T > diff(int deg=1)
Calculates the derivative of a Polynomial object.
Definition Polynomial.h:267
Definition ExtendedFunctions.h:14
void printVec(vector< number > vec)
Print out vector.
Definition ExtendedFunctions.h:124