-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyn.h
144 lines (134 loc) · 4.63 KB
/
polyn.h
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
#pragma once
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <cmath>
#include "advcmath.h"
using namespace std;
template <class T> class polyn{
private:
vector<T>poly;
string poly_out = "";
void buildpoly(){
poly_out = "";
for(auto i = 0;i < poly.size();i++){
poly_out += to_string(poly.at(i));
if(i != poly.size() - 1){
if(i == poly.size() - 2)
poly_out += " X + ";
else{
poly_out += " X(";
poly_out += to_string(poly.size() - 1 - i);
poly_out += ") + ";
}
}
}
}
public:
polyn(vector<T> poly):poly(poly){buildpoly();}
~polyn(){string poly_out = ""; poly.clear();}
void upload_poly(vector<T> in){string poly_out = "";poly.clear();poly.insert(poly.end(),in.begin(),in.end());buildpoly();}
string getout_poly(){return poly_out;}
polyn<T>& operator+(polyn<T>& cw);
polyn<T>& operator-(polyn<T>& kd);
polyn<T>& operator*(polyn<T>& pr);
polyn<T>& operator*(const T& konst);
polyn<T>& operator/(const T& konst);
inline T apply_value(const T& val){
T result = 0.0;
int n = poly.size();
for (int i = 0; i != n; i++) {
result += poly.at(i) * pow(val, n - i - 1);
}
return result;
}
// Derivada del polinomio
inline T deriv(const T& x) {
T result = 0.0;
int n = poly.size();
for (int i = 0; i != n - 1; i++) {
result += (n - i - 1) * poly.at(i) * pow(x, n - i - 2);
}
return result;
}
// Segunda derivada del polinomio
inline T deriv_2nd(const T& x) {
T result = 0.0;
int n = poly.size();
for (int i = 0; i != n - 2; i++) {
result += (n - i - 1) * (n - i - 2) * poly.at(i) * pow(x, n - i - 3);
}
return result;
}
};
typedef polyn<int> pli; typedef polyn<double> pld;
template<typename T>
ostream& operator << (ostream& out,polyn<T>& msg){return out << "P(x) = " << msg.getout_poly() << " " << endl;}
template<typename T>
polyn<T>& polyn<T>::operator+(polyn<T>& cw){
int pmax_size,pmin_size;
vector<T>poly_max; vector<T>poly_min;
pmax_size = max((this->poly).size(),(cw.poly).size()); pmin_size = min((this->poly).size(),(cw.poly).size());
if((this->poly).size() >= (cw.poly).size()){
poly_max.insert(poly_max.end(),(this->poly).begin(),(this->poly).end());
poly_min.insert(poly_min.end(),(cw.poly).begin(),(cw.poly).end());
}
else{
poly_max.insert(poly_max.end(),(cw.poly).begin(),(cw.poly).end());
poly_min.insert(poly_min.end(),(this->poly).begin(),(this->poly).end());
}
vector<T> res(pmax_size,0);(this->poly).clear();
for(int u = 0;u < pmax_size;u++){
res.at(pmax_size-u-1) = (u >= pmin_size)?poly_max.at(pmax_size-u-1):poly_max.at(pmax_size-u-1)+poly_min.at(pmin_size-u-1);
}
this->upload_poly(res);res.clear();
return *this;
}
template<typename T>
polyn<T>& polyn<T>::operator-(polyn<T>& kd){
int pmax_size,pmin_size;
vector<T>poly_max; vector<T>poly_min;
pmax_size = max((this->poly).size(),(kd.poly).size()); pmin_size = min((this->poly).size(),(kd.poly).size());
vector<T> res(pmax_size,0);(this->poly).clear();
for(int u = 0;u < pmax_size;u++){
res.at(pmax_size-u-1) = (u >= pmin_size)?poly_max.at(pmax_size-u-1):poly_max.at(pmax_size-u-1)-poly_min.at(pmin_size-u-1);
}
this->upload_poly(res);res.clear();
return *this;
}
template<typename T>
polyn<T>& polyn<T>::operator*(polyn<T>& pr){
int pprod_size;
pprod_size = (this->poly).size() + (pr.poly).size() - 1;
vector<T> res(pprod_size,0);
for(int u = 0;u < this->poly.size();u++){
for(int v = 0;v < (pr.poly).size();v++){
res.at(u+v) += (pr.poly).at(v) * this->poly.at(u);
}
}
(this->poly).clear();
this->upload_poly(res);res.clear();
return *this;
}
template<typename T>
polyn<T>& polyn<T>::operator*(const T& konst){
vector<T> res((this->poly).size(),0);
for(int i = 0;i < (this->poly).size();i++){
res.at(i) = (this->poly).at(i) * konst;
}
(this->poly).clear();
this->upload_poly(res);res.clear();
return *this;
}
template<typename T>
polyn<T>& polyn<T>::operator/(const T& konst){
vector<T> res((this->poly).size(),0);
for(int i = 0;i < (this->poly).size();i++){
res.at(i) = (this->poly).at(i) / konst;
}
(this->poly).clear();
this->upload_poly(res);res.clear();
return *this;
}