-
Notifications
You must be signed in to change notification settings - Fork 17
/
Polynom.cpp
99 lines (87 loc) · 1.7 KB
/
Polynom.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
#include "Polynom.hpp"
namespace Leph {
Polynom::Polynom() :
_coefs()
{
}
Polynom::Polynom(unsigned int degree) :
_coefs()
{
for (size_t i=0;i<degree+1;i++) {
_coefs.push_back(0.0);
}
}
const std::vector<double>& Polynom::getCoefs() const
{
return _coefs;
}
std::vector<double>& Polynom::getCoefs()
{
return _coefs;
}
const double& Polynom::operator()(size_t index) const
{
return _coefs.at(index);
}
double& Polynom::operator()(size_t index)
{
return _coefs.at(index);
}
size_t Polynom::degree() const
{
return _coefs.size()-1;
}
double Polynom::pos(double x) const
{
double xx = 1.0;
double val = 0.0;
for (size_t i=0;i<_coefs.size();i++) {
val += xx*_coefs[i];
xx *= x;
}
return val;
}
double Polynom::vel(double x) const
{
double xx = 1.0;
double val = 0.0;
for (size_t i=1;i<_coefs.size();i++) {
val += i*xx*_coefs[i];
xx *= x;
}
return val;
}
double Polynom::acc(double x) const
{
double xx = 1.0;
double val = 0.0;
for (size_t i=2;i<_coefs.size();i++) {
val += (i-1)*i*xx*_coefs[i];
xx *= x;
}
return val;
}
void Polynom::operator*=(double coef)
{
for (size_t i=0;i<_coefs.size();i++) {
_coefs[i] *= coef;
}
}
void Polynom::operator+=(const Polynom& p)
{
while (p._coefs.size() > _coefs.size()) {
_coefs.push_back(0.0);
}
for (size_t i=0;i<p._coefs.size();i++) {
_coefs[i] += p._coefs[i];
}
}
std::ostream& operator<<(std::ostream& os, const Polynom& p)
{
os << "degree=" << p.degree() << " ";
for (size_t i=0;i<p.degree()+1;i++) {
os << p(i) << " ";
}
return os;
}
}