Repository navigation
Expand file tree
/
Copy pathPolyNomial.cc
More file actions
108 lines (98 loc) · 2.75 KB
/
Copy pathPolyNomial.cc
File metadata and controls
108 lines (98 loc) · 2.75 KB
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
/******************************************************************************
(c) 2005-2016 Scientific Computation Research Center,
Rensselaer Polytechnic Institute. All rights reserved.
This work is open source software, licensed under the terms of the
BSD license as described in the LICENSE file in the top-level directory.
*******************************************************************************/
#include "BSpline.h"
#include <assert.h>
#include <cmath>
#include <iostream>
using std::cout;
using std::endl;
using std::vector;
using namespace Spline;
PolyNomial::PolyNomial(int degree_p, vector<double> &coffs_p) {
degree = degree_p;
coffs = coffs_p;
assert(degree > 0);
assert(coffs.size() == degree);
for (int i = 0; i < degree - 1; i++) {
firstDerivCoffs.push_back(coffs[i] * (degree - 1 - i));
}
for (int i = 0; i < degree - 2; i++) {
secondDerivCoffs.push_back(firstDerivCoffs[i] * (degree - 2 - i));
}
}
void PolyNomial::getcoeffs(vector<double> &coffs_p) const { coffs_p = coffs; }
void PolyNomial::print() {
cout << " print PolyNomial: degree " << degree << endl;
cout << " expression: ";
for (int i = 0; i < degree; i++) {
if (i > 0)
cout << " + ";
cout << coffs[i] << "x^" << degree - 1 - i;
}
cout << endl;
cout << " first Deriv: ";
if (degree - 1 <= 0)
cout << "0";
for (int i = 0; i < degree - 1; i++) {
if (i > 0)
cout << " + ";
cout << firstDerivCoffs[i] << "x^" << degree - 2 - i;
}
cout << endl;
cout << " second Deriv coffs: ";
if (degree - 2 <= 0)
cout << "0";
for (int i = 0; i < degree - 2; i++) {
if (i > 0)
cout << " + ";
cout << secondDerivCoffs[i] << "x^" << degree - 3 - i;
}
cout << endl;
}
double PolyNomial::eval(double x, bool debug) const {
double result = 0;
vector<double> x_n(degree, 1.0);
for (int i = degree - 2; i >= 0; i--) {
x_n[i] = x_n[i + 1] * x;
}
for (int i = 0; i < degree; i++) {
result += coffs[i] * x_n[i];
}
return result;
}
double PolyNomial::evalFirstDeriv(double x) const {
double result = 0;
if (degree == 1)
result = 0.0;
else {
int thedegree = degree - 1;
vector<double> x_n(thedegree, 1.0);
for (int i = thedegree - 2; i >= 0; i--) {
x_n[i] = x_n[i + 1] * x;
}
for (int i = 0; i < thedegree; i++) {
result += firstDerivCoffs[i] * x_n[i];
}
}
return result;
}
double PolyNomial::evalSecondDeriv(double x) const {
double result = 0;
if (degree <= 2)
result = 0.0;
else {
int thedegree = degree - 2;
vector<double> x_n(thedegree, 1.0);
for (int i = thedegree - 2; i >= 0; i--) {
x_n[i] = x_n[i + 1] * x;
}
for (int i = 0; i < thedegree; i++) {
result += secondDerivCoffs[i] * x_n[i];
}
}
return result;
}