SurgSim
Math
Polynomial.h
Go to the documentation of this file.
1
// This file is a part of the OpenSurgSim project.
2
// Copyright 2013-2016, SimQuest Solutions Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
// http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
16
#ifndef SURGSIM_MATH_POLYNOMIAL_H
17
#define SURGSIM_MATH_POLYNOMIAL_H
18
19
#include <iostream>
20
21
namespace
SurgSim
22
{
23
namespace
Math
24
{
25
26
namespace
27
{
28
double
polynomial_epsilon = 1.0e-09;
29
}
30
38
template
<
typename
T>
39
bool
isNearZero
(
const
T& value,
const
T& epsilon =
static_cast<
T
>
(polynomial_epsilon));
40
47
template
<
typename
T,
int
N>
class
Polynomial
48
{
49
static_assert(N >= 0,
"Polynomials must have degree >= 0."
);
50
static_assert(N <= 3, "Polynomials of degree > 3 are not yet supported.
");
51
};
52
55
template <typename T>
56
class Polynomial<T, 0>
57
{
58
public:
60
Polynomial();
61
64
explicit Polynomial(const T& a0);
65
69
T evaluate(const T& x) const;
70
74
T operator()(const T& x) const;
75
78
T& operator[](const size_t i);
79
const T& operator[](const size_t i) const;
80
Polynomial<T, 0> operator- () const;
81
Polynomial<T, 0> operator+ (const Polynomial<T, 0>& rhs) const;
82
Polynomial<T, 0>& operator+= (const Polynomial<T, 0>& rhs);
83
Polynomial<T, 0> operator- (const Polynomial<T, 0>& rhs) const;
84
Polynomial<T, 0>& operator-= (const Polynomial<T, 0>& rhs);
86
89
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
90
95
bool isApprox(const Polynomial<T, 0>& p, const T& epsilon) const;
96
99
T getCoefficient(size_t i) const;
100
105
void setCoefficient(size_t i, const T& value);
106
107
private:
110
T m_a0;
112
};
113
116
template <typename T>
117
class Polynomial<T, 1>
118
{
119
public:
121
Polynomial();
122
126
Polynomial(const T& a0, const T& a1);
127
131
T evaluate(const T& x) const;
132
136
T operator()(const T& x) const;
137
140
T& operator[](const size_t i);
141
const T& operator[](const size_t i) const;
142
Polynomial<T, 1> operator- () const;
143
Polynomial<T, 1> operator+ (const Polynomial<T, 1>& rhs) const;
144
Polynomial<T, 1>& operator+= (const Polynomial<T, 1>& rhs);
145
Polynomial<T, 1> operator- (const Polynomial<T, 1>& rhs) const;
146
Polynomial<T, 1>& operator-= (const Polynomial<T, 1>& rhs);
148
150
Polynomial<T, 0> derivative() const;
151
154
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
155
160
bool isApprox(const Polynomial<T, 1>& p, const T& epsilon) const;
161
164
T getCoefficient(size_t i) const;
165
170
void setCoefficient(size_t i, const T& value);
171
172
private:
175
T m_a0;
176
T m_a1;
178
};
179
182
template <typename T>
183
class Polynomial<T, 2>
184
{
185
public:
187
Polynomial();
188
193
Polynomial(const T& a0, const T& a1, const T& a2);
194
197
T discriminant() const;
198
202
T evaluate(const T& x) const;
203
207
T operator()(const T& x) const;
208
211
T& operator[](const size_t i);
212
const T& operator[](const size_t i) const;
213
Polynomial<T, 2> operator- () const;
214
Polynomial<T, 2> operator+ (const Polynomial<T, 2>& rhs) const;
215
Polynomial<T, 2>& operator+= (const Polynomial<T, 2>& rhs);
216
Polynomial<T, 2> operator- (const Polynomial<T, 2>& rhs) const;
217
Polynomial<T, 2>& operator-= (const Polynomial<T, 2>& rhs);
219
221
Polynomial<T, 1> derivative() const;
222
225
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
226
231
bool isApprox(const Polynomial<T, 2>& p, const T& epsilon) const;
232
235
T getCoefficient(size_t i) const;
236
241
void setCoefficient(size_t i, const T& value);
242
243
private:
246
T m_a0;
247
T m_a1;
248
T m_a2;
250
};
251
254
template <typename T>
255
class Polynomial<T, 3>
256
{
257
public:
259
Polynomial();
260
266
Polynomial(const T& a0, const T& a1, const T& a2, const T& a3);
267
271
T evaluate(const T& x) const;
272
276
T operator()(const T& x) const;
277
280
T& operator[](const size_t i);
281
const T& operator[](const size_t i) const;
282
Polynomial<T, 3> operator- () const;
283
Polynomial<T, 3> operator+ (const Polynomial<T, 3>& rhs) const;
284
Polynomial<T, 3>& operator+= (const Polynomial<T, 3>& rhs);
285
Polynomial<T, 3> operator- (const Polynomial<T, 3>& rhs) const;
286
Polynomial<T, 3>& operator-= (const Polynomial<T, 3>& rhs);
288
290
Polynomial<T, 2> derivative() const;
291
294
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
295
300
bool isApprox(const Polynomial<T, 3>& p, const T& epsilon) const;
301
304
T getCoefficient(size_t i) const;
305
310
void setCoefficient(size_t i, const T& value);
311
312
private:
315
T m_a0;
316
T m_a1;
317
T m_a2;
318
T m_a3;
320
};
321
322
// Operators
323
333
template <typename T, int N, int M>
334
Polynomial < T, N + M > operator*(const Polynomial<T, N>& p, const Polynomial<T, M>& q);
335
341
template <typename T>
342
Polynomial<T, 2> operator*(const Polynomial<T, 1>& p, const Polynomial<T, 1>& q);
343
349
template <typename T>
350
Polynomial<T, 3> operator*(const Polynomial<T, 2>& p, const Polynomial<T, 1>& q);
351
357
template <typename T>
358
Polynomial<T, 3> operator*(const Polynomial<T, 1>& p, const Polynomial<T, 2>& q);
359
364
template <typename T>
365
Polynomial<T, 0> square(const Polynomial<T, 0>& p);
366
371
template <typename T>
372
Polynomial<T, 2> square(const Polynomial<T, 1>& p);
373
380
template <typename T, int N>
381
std::ostream& operator<<(std::ostream& stream, const Polynomial<T, N>& p);
382
383
}; // Math
384
}; // SurgSim
385
386
#include "
SurgSim/Math/
Polynomial
-inl.h
"
387
388
#endif // SURGSIM_MATH_POLYNOMIAL_H
SurgSim::Math::isNearZero
bool isNearZero(const T &value, const T &epsilon)
Define an utility function for comparing individual coefficients to 0.
Definition:
Polynomial-inl.h:30
SurgSim
Definition:
CompoundShapeToGraphics.cpp:29
SurgSim::Math::Polynomial
Polynomial<T, N> defines the concept of an N degree polynomial with type T coefficients and provides ...
Definition:
Polynomial.h:47
Generated on Fri May 29 2020 12:22:57 for OpenSurgSim by
1.8.17