18 std::vector<std::complex<T>>
roots;
30 Polynomial(std::vector<T> _coeffs, std::vector<std::complex<T>> _roots){
47 template<
typename... Args>
58 return size > 0 ? size - 1 : 0;
65 void setRoots(std::vector<std::complex<T>> args){
66 size_t count = args.size();
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");
77 template<
typename... Args>
79 setRoots(std::vector<std::complex<T>>{args...});
95 template<
typename... Args>
114 void solve(std::vector<std::complex<T>>&
roots, std::vector<int>& conv,
int maxiter=80){
120 throw std::invalid_argument(
"Solver wasnt set!\n");
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];
130 for (
int i = 0; i <= rhs.degree(); ++i) {
131 result_coeffs[i] += rhs.coeffs[i];
134 return Polynomial<T>(result_coeffs);
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];
142 for (
int i = 0; i <= rhs.degree(); ++i) {
143 result_coeffs[i] -= rhs.coeffs[i];
145 return Polynomial<T>(result_coeffs);
152 int m = other.
coeffs.size();
153 std::vector<T> result(n + m - 1, 0);
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];
164 if (divisor.degree() > this->degree()) {
165 throw std::invalid_argument(
"The degree of the divisor is greater than the dividend");
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));
172 while (dividend.degree() >= divisor.degree()) {
173 int degree_diff = dividend.degree() - divisor.degree();
174 T coeff = dividend.coeffs.back() / divisor.coeffs.back();
177 tmp.coeffs[degree_diff] = coeff;
180 for (
int i = 0; i <= divisor.degree(); ++i) {
181 dividend.coeffs[i + degree_diff] -= coeff * divisor.coeffs[i];
183 dividend.coeffs.pop_back();
186 quotient_coeffs[degree_diff] = coeff;
189 return Polynomial<T>(quotient_coeffs);
204 if (divisor.degree() > this->degree()) {
205 throw std::invalid_argument(
"The degree of the divisor is greater than the dividend");
208 Polynomial<T> dividend = *
this;
209 std::vector<T> quotient_coeffs(this->
degree() - divisor.
degree() + 1, 0);
212 while (dividend.degree() >= divisor.degree()) {
213 int degree_diff = dividend.degree() - divisor.degree();
214 T coeff = dividend.coeffs.back() / divisor.coeffs.back();
217 tmp.coeffs = std::vector<T>(degree_diff + 1, 0);
218 tmp.coeffs.back() = coeff;
221 for (
int i = 0; i <= divisor.degree(); ++i) {
222 dividend.coeffs[i + degree_diff] -= coeff * divisor.coeffs[i];
224 dividend.coeffs.pop_back();
227 quotient_coeffs[degree_diff] = coeff;
230 quotient = Polynomial<T>(quotient_coeffs);
231 remainder = dividend;
235 *
this = *
this * other;
241 if (
this != &other) {
269 for(
int j = 0;
j <
deg; ++
j){
270 for (
int i = 1; i < ret.size(); ++i) {
271 ret[i] *=
static_cast<T
>(i);
274 ret.erase(ret.begin());
284 std::cout <<
"This polynomial object doesnt contain any coefficients.\n";
287 for(
int i = _degree; i >= 0; --i){
288 std::cout <<
"(" <<
coeffs[i] <<
")";
289 std::cout <<
"x^" << i;
294 std::cout <<
". Degree: " << _degree <<
'\n';
295 std::cout <<
"Roots: " <<
roots.size() <<
"; ";
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 "ient, 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