/usr/include/SurgSim/Math/Polynomial.h is in libopensurgsim-dev 0.7.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | // This file is a part of the OpenSurgSim project.
// Copyright 2013-2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_MATH_POLYNOMIAL_H
#define SURGSIM_MATH_POLYNOMIAL_H
#include <iostream>
namespace SurgSim
{
namespace Math
{
namespace
{
double polynomial_epsilon = 1.0e-09;
}
/// Define an utility function for comparing individual coefficients to 0.
///
/// \tparam T underlying data type used as either the scalar or as the data type
/// for the interval
/// \param value the value to compare
/// \param epsilon the tolerance
/// \return true if the value is within epsilon of 0
template <typename T>
bool isNearZero(const T& value, const T& epsilon = static_cast<T>(polynomial_epsilon));
/// Polynomial<T, N> defines the concept of an N degree polynomial with type T
/// coefficients and provides operations on them including arithmetic operations,
/// construction, and IO.
///
/// \tparam T underlying data type over which the interval is defined.
/// \tparam N degree of the Polynomial
template <typename T, int N> class Polynomial
{
static_assert(N >= 0, "Polynomials must have degree >= 0.");
static_assert(N <= 3, "Polynomials of degree > 3 are not yet supported.");
};
/// Polynomial<T, 0> specializes the Polynomial class for degree 0 (constant polynomials)
/// \sa Polynomial<T, N>
template <typename T>
class Polynomial<T, 0>
{
public:
/// Constructor
Polynomial();
/// Constructor
/// \param a0 coefficient of the 0 degree term
explicit Polynomial(const T& a0);
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T evaluate(const T& x) const;
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T operator()(const T& x) const;
/// @{
/// Standard arithmetic operators extended to intervals
T& operator[](const size_t i);
const T& operator[](const size_t i) const;
Polynomial<T, 0> operator- () const;
Polynomial<T, 0> operator+ (const Polynomial<T, 0>& rhs) const;
Polynomial<T, 0>& operator+= (const Polynomial<T, 0>& rhs);
Polynomial<T, 0> operator- (const Polynomial<T, 0>& rhs) const;
Polynomial<T, 0>& operator-= (const Polynomial<T, 0>& rhs);
/// @}
/// \param epsilon the closeness parameter
/// \return true if all coefficients of the polynomial are within an epsilon of 0
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
/// \param p the test polynomial
/// \param epsilon the closeness parameter
/// \return true if all the coefficients of the current polynomial are within an epsilon of
/// the corresponding coefficient in p
bool isApprox(const Polynomial<T, 0>& p, const T& epsilon) const;
/// \param i index of the desired coefficient
/// \return the value of coefficient i
T getCoefficient(size_t i) const;
/// Set a specified coefficient to a desired value
/// \param i index of the desired coefficient
/// \param value the value to be set in the coefficient
/// \exception if i is greater than the polynomial degree
void setCoefficient(size_t i, const T& value);
private:
/// @{
/// Coefficient of the polynomial
T m_a0;
/// @}
};
/// Polynomial<T, 1> specializes the Polynomial class for degree 1 (linear polynomials)
/// \sa Polynomial<T, N>
template <typename T>
class Polynomial<T, 1>
{
public:
/// Constructor
Polynomial();
/// Constructor
/// \param a0 coefficient of the 0 degree term
/// \param a1 coefficient of the 1 degree term
Polynomial(const T& a0, const T& a1);
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T evaluate(const T& x) const;
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T operator()(const T& x) const;
/// @{
/// Standard arithmetic operators extended to intervals
T& operator[](const size_t i);
const T& operator[](const size_t i) const;
Polynomial<T, 1> operator- () const;
Polynomial<T, 1> operator+ (const Polynomial<T, 1>& rhs) const;
Polynomial<T, 1>& operator+= (const Polynomial<T, 1>& rhs);
Polynomial<T, 1> operator- (const Polynomial<T, 1>& rhs) const;
Polynomial<T, 1>& operator-= (const Polynomial<T, 1>& rhs);
/// @}
/// \return the derivative of the polynomial
Polynomial<T, 0> derivative() const;
/// \param epsilon the closeness parameter
/// \return true if all coefficients of the polynomial are within an epsilon of 0
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
/// \param p the test polynomial
/// \param epsilon the closeness parameter
/// \return true if all the coefficients of the current polynomial are within an epsilon of
/// the corresponding coefficient in p
bool isApprox(const Polynomial<T, 1>& p, const T& epsilon) const;
/// \param i index of the desired coefficient
/// \return the value of coefficient i
T getCoefficient(size_t i) const;
/// Set a specified coefficient to a desired value
/// \param i index of the desired coefficient
/// \param value the value to be set in the coefficient
/// \exception if i is greater than the polynomial degree
void setCoefficient(size_t i, const T& value);
private:
/// @{
/// Coefficient of the polynomial
T m_a0;
T m_a1;
/// @}
};
/// Polynomial<T, 2> specializes the Polynomial class for degree 2 (quadratic polynomials)
/// \sa Polynomial<T, N>
template <typename T>
class Polynomial<T, 2>
{
public:
/// Constructor
Polynomial();
/// Constructor
/// \param a0 coefficient of the 0 degree term
/// \param a1 coefficient of the 1 degree term
/// \param a2 coefficient of the 2 degree term
Polynomial(const T& a0, const T& a1, const T& a2);
/// Evaluate the discriminant of a quadratic polynomial
/// \return the discriminant (b^2 - a4c)
T discriminant() const;
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T evaluate(const T& x) const;
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T operator()(const T& x) const;
/// @{
/// Standard arithmetic operators extended to intervals
T& operator[](const size_t i);
const T& operator[](const size_t i) const;
Polynomial<T, 2> operator- () const;
Polynomial<T, 2> operator+ (const Polynomial<T, 2>& rhs) const;
Polynomial<T, 2>& operator+= (const Polynomial<T, 2>& rhs);
Polynomial<T, 2> operator- (const Polynomial<T, 2>& rhs) const;
Polynomial<T, 2>& operator-= (const Polynomial<T, 2>& rhs);
/// @}
/// \return the derivative of the polynomial
Polynomial<T, 1> derivative() const;
/// \param epsilon the closeness parameter
/// \return true if all coefficients of the polynomial are within an epsilon of 0
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
/// \param p the test polynomial
/// \param epsilon the closeness parameter
/// \return true if all the coefficients of the current polynomial are within an epsilon of
/// the corresponding coefficient in p
bool isApprox(const Polynomial<T, 2>& p, const T& epsilon) const;
/// \param i index of the desired coefficient
/// \return the value of coefficient i
T getCoefficient(size_t i) const;
/// Set a specified coefficient to a desired value
/// \param i index of the desired coefficient
/// \param value the value to be set in the coefficient
/// \exception if i is greater than the polynomial degree
void setCoefficient(size_t i, const T& value);
private:
/// @{
/// Coefficient of the polynomial
T m_a0;
T m_a1;
T m_a2;
/// @}
};
/// Polynomial<T, 3> specializes the Polynomial class for degree 3 (cubic polynomials)
/// \sa Polynomial<T, N>
template <typename T>
class Polynomial<T, 3>
{
public:
/// Constructor
Polynomial();
/// Constructor
/// \param a0 coefficient of the 0 degree term
/// \param a1 coefficient of the 1 degree term
/// \param a2 coefficient of the 2 degree term
/// \param a3 coefficient of the 3 degree term
Polynomial(const T& a0, const T& a1, const T& a2, const T& a3);
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T evaluate(const T& x) const;
/// Evaluate the polynomial at a point
/// \param x point at which to evaluate the polynomial
/// \return the value of the polynomial at x
T operator()(const T& x) const;
/// @{
/// Standard arithmetic operators extended to intervals
T& operator[](const size_t i);
const T& operator[](const size_t i) const;
Polynomial<T, 3> operator- () const;
Polynomial<T, 3> operator+ (const Polynomial<T, 3>& rhs) const;
Polynomial<T, 3>& operator+= (const Polynomial<T, 3>& rhs);
Polynomial<T, 3> operator- (const Polynomial<T, 3>& rhs) const;
Polynomial<T, 3>& operator-= (const Polynomial<T, 3>& rhs);
/// @}
/// \return the derivative of the polynomial
Polynomial<T, 2> derivative() const;
/// \param epsilon the closeness parameter
/// \return true if all coefficients of the polynomial are within an epsilon of 0
bool isNearZero(const T& epsilon = static_cast<T>(polynomial_epsilon)) const;
/// \param p the test polynomial
/// \param epsilon the closeness parameter
/// \return true if all the coefficients of the current polynomial are within an epsilon of
/// the corresponding coefficient in p
bool isApprox(const Polynomial<T, 3>& p, const T& epsilon) const;
/// \param i index of the desired coefficient
/// \return the value of coefficient i
T getCoefficient(size_t i) const;
/// Set a specified coefficient to a desired value
/// \param i index of the desired coefficient
/// \param value the value to be set in the coefficient
/// \exception if i is greater than the polynomial degree
void setCoefficient(size_t i, const T& value);
private:
/// @{
/// Coefficient of the polynomial
T m_a0;
T m_a1;
T m_a2;
T m_a3;
/// @}
};
// Operators
/// Multiply two polynomials of arbitrary degree. This current implementation is limited to
/// a resulting polynomial of no more than degree 3 (i.e. N + M <= 3) because of limits in the
/// underlying polynomial representations.
/// \tparam N degree of the first polynomial
/// \tparam M degree of the second polynomial
/// \tparam T underlying data type over which the interval is defined.
/// \param p first polynomial of degree N
/// \param q second polynomial of degree M
/// \return p * q as a polynomial of degree N + M
template <typename T, int N, int M>
Polynomial < T, N + M > operator*(const Polynomial<T, N>& p, const Polynomial<T, M>& q);
/// Multiply two polynomials of degree 1.
/// \tparam T underlying data type over which the interval is defined.
/// \param p first polynomial of degree 1
/// \param q second polynomial of degree 1
/// \return p * q as a polynomial of degree 2
template <typename T>
Polynomial<T, 2> operator*(const Polynomial<T, 1>& p, const Polynomial<T, 1>& q);
/// Multiply two polynomials of degree 2 and 1 respectively.
/// \tparam T underlying data type over which the interval is defined.
/// \param p first polynomial of degree 2
/// \param q second polynomial of degree 1
/// \return p * q as a polynomial of degree 3
template <typename T>
Polynomial<T, 3> operator*(const Polynomial<T, 2>& p, const Polynomial<T, 1>& q);
/// Multiply two polynomials of degree 1 and 2 respectively.
/// \tparam T underlying data type over which the interval is defined.
/// \param p first polynomial of degree 1
/// \param q second polynomial of degree 2
/// \return p * q as a polynomial of degree 3
template <typename T>
Polynomial<T, 3> operator*(const Polynomial<T, 1>& p, const Polynomial<T, 2>& q);
/// Square a degree 0 polynomial
/// \tparam T underlying data type over which the interval is defined.
/// \param p polynomial of degree 0
/// \return p^2 as a polynomial of degree 0
template <typename T>
Polynomial<T, 0> square(const Polynomial<T, 0>& p);
/// Square a degree 1 polynomial
/// \tparam T underlying data type over which the interval is defined.
/// \param p polynomial of degree 1
/// \return p^2 as a polynomial of degree 2
template <typename T>
Polynomial<T, 2> square(const Polynomial<T, 1>& p);
/// Write a textual version of a Polynomial to an output stream
/// \tparam T underlying type of the polynomial coefficients
/// \tparam N degree of the polynomial
/// \param stream the ostream to be written to
/// \param p the polynomial to write
/// \return the active stream
template <typename T, int N>
std::ostream& operator<<(std::ostream& stream, const Polynomial<T, N>& p);
}; // Math
}; // SurgSim
#include "SurgSim/Math/Polynomial-inl.h"
#endif // SURGSIM_MATH_POLYNOMIAL_H
|