-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.h
184 lines (148 loc) · 3.79 KB
/
Polynomial.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef __POLYNOMIAL_H__
#define __POLYNOMIAL_H__
#include <vector>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <assert.h>
class Polynomial
{
public:
double& operator[](int power)
{
// Relies on the std::vector zero'ing the values
if(power+1 > (int)m_coeffs.size()) {
m_coeffs.resize(power+1);
}
return m_coeffs[power];
}
double operator[](int power) const
{
return m_coeffs[power];
}
double Eval(double x)
{
double ret = 0.0;
double tmp_x = 1.0;
for(size_t i=0; i < m_coeffs.size(); i++) {
ret += m_coeffs[i]*tmp_x;
tmp_x *= x;
}
return ret;
}
Polynomial operator*(const double val) const
{
Polynomial ans;
for(size_t i=0; i < m_coeffs.size(); i++) {
ans[i] = m_coeffs[i]*val;
}
return ans;
}
Polynomial& operator*=(const double val)
{
*this = *this * val;
return *this;
}
Polynomial operator*(const Polynomial &rhs) const
{
Polynomial ans;
for(size_t i=0; i < m_coeffs.size(); i++) {
for(size_t j=0; j < rhs.m_coeffs.size(); j++) {
double c = m_coeffs[i]*rhs.m_coeffs[j];
ans[i+j] += c;
}
}
return ans;
}
Polynomial& operator*=(const Polynomial &rhs)
{
*this = *this * rhs;
return *this;
}
Polynomial operator+(const Polynomial &rhs) const
{
Polynomial ans;
if(m_coeffs.size() > rhs.m_coeffs.size()) {
for(size_t i=0; i < m_coeffs.size(); i++) {
ans[i] = m_coeffs[i] + rhs[i];
}
}
else {
for(size_t i=0; i < rhs.m_coeffs.size(); i++) {
ans[i] = operator[](i) + rhs.m_coeffs[i];
}
}
return ans;
}
Polynomial& operator+=(const Polynomial &rhs)
{
*this = *this + rhs;
return *this;
}
Polynomial operator-(const Polynomial &rhs) const
{
Polynomial ans;
if(m_coeffs.size() > rhs.m_coeffs.size()) {
for(size_t i=0; i < m_coeffs.size(); i++) {
ans[i] = m_coeffs[i] - rhs[i];
}
}
else {
for(size_t i=0; i < rhs.m_coeffs.size(); i++) {
ans[i] = operator[](i) - rhs.m_coeffs[i];
}
}
return ans;
}
Polynomial& operator-=(const Polynomial &rhs)
{
*this = *this - rhs;
return *this;
}
friend std::ostream& operator<<(std::ostream &os, const Polynomial &p)
{
for(size_t i=0; i < p.m_coeffs.size(); i++) {
os << i << ": " << p.m_coeffs[i] << std::endl;
}
return os;
}
std::vector <double> m_coeffs;
};
class PolyMatrix
{
public:
PolyMatrix(int rows, int cols)
{
m_rows = rows;
m_cols = cols;
m_data.resize(rows);
for(int i=0; i < rows; i++) {
m_data[i].resize(cols);
}
}
Polynomial& operator()(int row, int col)
{
return m_data[row][col];
}
void Eval(double x, double *ret)
{
for(int i=0; i < m_rows; i++) {
for(int j=0; j < m_cols; j++) {
ret[i*m_cols + j] = m_data[i][j].Eval(x);
}
}
}
friend std::ostream& operator<<(std::ostream &os, const PolyMatrix &p)
{
for(size_t i=0; i < p.m_data.size(); i++) {
for(size_t j=0; j < p.m_data[i].size(); j++) {
os << std::fixed << std::setprecision(4) << p.m_data[i][j][0] << " ";
}
os << std::endl;
}
return os;
}
int m_rows, m_cols;
std::vector < std::vector<Polynomial> > m_data;
};
#endif