|
Robotics Library
0.7.0
|
Go to the documentation of this file.
27 #ifndef RL_MATH_POLYNOMIAL_H
28 #define RL_MATH_POLYNOMIAL_H
30 #define EIGEN_MATRIXBASE_PLUGIN <rl/math/MatrixBaseAddons.h>
31 #define EIGEN_QUATERNIONBASE_PLUGIN <rl/math/QuaternionBaseAddons.h>
32 #define EIGEN_TRANSFORM_PLUGIN <rl/math/TransformAddons.h>
38 #ifdef HAVE_EIGEN_UNSUPPORTED
39 #include <unsupported/Eigen/Polynomials>
40 #else // HAVE_EIGEN_UNSUPPORTED
41 #include <Eigen/Eigenvalues>
42 #endif // HAVE_EIGEN_UNSUPPORTED
79 f.
c[2] = -(2 *
x1 * yd0 + 3 * y0 - 3 * y1 +
x1 * yd1) / ::
std::pow(
x1, 2);
93 f.
c[3] = -(ydd0 - ydd1) /
x1 / 6;
104 f.
c[1] = (y1 - y0) /
x1;
159 f.
c[4] = -(
::std::pow(
x1, 3) * (yddd1 + 4 * yddd0) + ::
std::pow(
x1, 2) * (30 * ydd0 - 15 * ydd1) +
x1 * (90 * yd1 + 120 * yd0) - 210 * y1 + 210 * y0 ) / (6 * ::
std::pow(
x1, 4));
160 f.
c[5] = (
::std::pow(
x1, 3) * (yddd1 + 2 * yddd0) + ::
std::pow(
x1, 2) * (20 * ydd0 - 14 * ydd1) +
x1 * (78 * yd1 + 90 * yd0) - 168 * y1 + 168 * y0) / (2 * ::
std::pow(
x1, 5));
161 f.
c[6] = -(
::std::pow(
x1, 3) * (3 * yddd1 + 4 * yddd0) + ::
std::pow(
x1, 2) * (45 * ydd0 - 39 * ydd1) +
x1 * (204 * yd1 + 216 * yd0) - 420 * y1 + 420 * y0) / (6 * ::
std::pow(
x1, 6));
201 return this->
c.size() - 1;
216 for (::std::size_t i = 0; i < this->
degree(); ++i)
218 f.
c[i] =
static_cast<Real>(i + 1) * this->
c[i + 1];
241 for (::std::ptrdiff_t row = 0; row < maximum.size(); ++row)
243 maximum[row] = ::std::max(maximum[row], y1[row]);
251 ::std::vector<Real> extrema = this->
realRoots(coefficients);
253 for (::std::vector<Real>::const_iterator x = extrema.begin(); x < extrema.end(); ++x)
255 if (this->
x0 < *x && *x < this->
x1)
257 Real y = ::std::abs((*
this)(*x)[row]);
259 if (y > maximum[row])
278 for (::std::size_t i = 0; i < this->
degree() + 1; ++i)
280 f.
c[i + 1] = this->
c[i] /
static_cast<Real>(i + 1);
292 for (::std::size_t i = 1; i <
derivative; ++i)
306 for (::std::size_t i = 1; i < this->
degree() + 1; ++i)
309 y += this->
c[this->
degree() - i];
333 if (this->
c.size() < 1)
348 ::std::size_t min = ::std::min(this->
c.size(), rhs.
c.size());
350 for (::std::size_t i = 0; i < min; ++i)
352 this->
c[i] += rhs.
c[i];
355 this->
c.resize(::std::max(this->
c.size(), rhs.
c.size()));
357 for (::std::size_t i = min; i < rhs.
c.size(); ++i)
359 this->
c[i] = rhs.
c[i];
387 if (this->
c.size() < 1)
402 ::std::size_t min = ::std::min(this->
c.size(), rhs.
c.size());
404 for (::std::size_t i = 0; i < min; ++i)
406 this->
c[i] -= rhs.
c[i];
409 this->
c.resize(::std::max(this->
c.size(), rhs.
c.size()));
411 for (::std::size_t i = min; i < rhs.
c.size(); ++i)
413 this->
c[i] = rhs.
c[i];
431 for (::std::size_t i = 0; i < this->
c.size(); ++i)
451 return *
this *= 1 / rhs;
459 static ::std::vector<Real>
realRoots(const ::std::vector<Real>&
c)
461 ::std::size_t
degree =
c.empty() ? 0 :
c.size() - 1;
463 for (::std::size_t i = 0; i <
c.size(); ++i)
465 if (::std::abs(
c[
c.size() - 1 - i]) <= ::std::numeric_limits<Real>::epsilon())
477 return ::std::vector<Real>(0);
481 ::std::vector<Real> roots(1);
482 roots[0] = -
c[0] /
c[1];
487 Real det =
c[1] *
c[1] - 4 *
c[0] *
c[2];
491 Real sqrtDet = ::std::sqrt(det);
492 ::std::vector<Real> roots(2);
493 roots[0] = (-
c[1] + sqrtDet) / (2 *
c[2]);
494 roots[1] = (-
c[1] - sqrtDet) / (2 *
c[2]);
497 else if (::std::abs(det) <= ::std::numeric_limits<Real>::epsilon())
499 ::std::vector<Real> roots(1);
500 roots[0] = -
c[1] / (2 *
c[2]);
505 return ::std::vector<Real>(0);
513 Real back =
c[2] / (3 *
c[3]);
515 if (::std::abs(p) <= ::std::numeric_limits<Real>::epsilon() && ::std::abs(q) <= ::std::numeric_limits<Real>::epsilon())
517 ::std::vector<Real> roots(1);
521 else if (::std::abs(p) <= ::std::numeric_limits<Real>::epsilon() && ::std::abs(q) > ::std::numeric_limits<Real>::epsilon())
523 ::std::vector<Real> roots(1);
524 roots[0] =
cbrt(-q) - back;
527 else if (::std::abs(p) > ::std::numeric_limits<Real>::epsilon() && ::std::abs(q) <= ::std::numeric_limits<Real>::epsilon())
529 Real sqrtP = ::std::sqrt(-p);
530 ::std::vector<Real> roots(3);
532 roots[1] = sqrtP - back;
533 roots[2] = -sqrtP - back;
536 else if (::std::abs(4 * ::
std::pow(p, 3) + 27 * ::
std::pow(q, 2)) <= ::std::numeric_limits<Real>::epsilon() && ::std::abs(p) > ::std::numeric_limits<Real>::epsilon())
538 ::std::vector<Real> roots(2);
539 roots[0] = -(3 * q) / (2 * p) - back;
540 roots[1] = (3 * q) / p - back;
549 Real tmp1 = 2 * ::std::sqrt(-p / 3);
550 Real tmp2 = ::std::acos((3 * q) / (2 * p) * ::std::sqrt(-3 / p)) / 3;
551 ::std::vector<Real> roots(3);
552 roots[0] = tmp1 * ::std::cos(tmp2) - back;
553 roots[1] = tmp1 * ::std::cos(tmp2 - 2 * M_PI / 3) - back;
554 roots[2] = tmp1 * ::std::cos(tmp2 - 4 * M_PI / 3) - back;
559 Real sqrtDet = ::std::sqrt(det);
562 ::std::vector<Real> roots(1);
563 roots[0] = u + v - back;
570 #ifdef HAVE_EIGEN_UNSUPPORTED
571 ::std::vector<Real> roots;
572 ::Eigen::PolynomialSolver<Real, ::Eigen::Dynamic> solver(Vector::Map(
c.data(),
degree));
573 solver.realRoots(roots);
577 companion.topLeftCorner(1,
degree).setZero();
578 companion.bottomLeftCorner(
degree - 1,
degree).setIdentity();
579 companion.rightCols(1) = -1 /
c[
degree] * Vector::Map(
c.data(),
degree);
581 ::Eigen::EigenSolver<Matrix> solver(companion);
583 ::std::vector<Real> roots;
585 for (::std::ptrdiff_t i = 0; i < solver.eigenvalues().size(); ++i)
587 if (::std::abs(solver.eigenvalues()[i].imag()) < ::Eigen::NumTraits<Real>::dummy_precision())
589 roots.push_back(solver.eigenvalues()[i].real());
607 assert(this->
lower() == 0);
612 for (::std::size_t i = 0; i < this->
degree() + 1; ++i)
630 translated.
lower() = this->
lower() - translation;
631 translated.
upper() = this->
upper() - translation;
633 for (::std::size_t n = 0; n < this->
degree() + 1; ++n)
638 for (::std::size_t n = 0; n < this->
degree() + 1; ++n)
643 for (::std::size_t k = 0; k <= n; ++k)
649 b = b * (n - k) / (k + 1);
665 #endif // RL_MATH_POLYNOMIAL_H
T getAbsoluteMaximum() const
Returns the array of the maximum function values of each dimension within the definition range,...
Definition: Polynomial.h:234
::Eigen::Matrix< Real, ::Eigen::Dynamic, ::Eigen::Dynamic > Matrix
Definition: Matrix.h:42
Quaternion< Scalar > pow(const Scalar &t) const
Definition: QuaternionBaseAddons.h:128
Polynomial(const ::std::size_t °ree)
Definition: Polynomial.h:63
friend Polynomial operator-(const T &lhs, const Polynomial &rhs)
Definition: Polynomial.h:370
A mathematical mapping from Real -> ArrayX.
Definition: Function.h:45
Real x1
Definition: Function.h:104
Polynomial & operator/=(const Real &rhs)
Definition: Polynomial.h:449
static Polynomial CubicFirst(const T &y0, const T &y1, const T &yd0, const T &yd1, const Real &x1=1)
Definition: Polynomial.h:73
static Polynomial SepticFirstSecondThird(const T &y0, const T &y1, const T &yd0, const T &yd1, const T &ydd0, const T &ydd1, const T &yddd0, const T &yddd1, const Real &x1=1)
Definition: Polynomial.h:151
friend Polynomial operator-(const Polynomial &lhs, const T &rhs)
Definition: Polynomial.h:375
static Polynomial SexticFirstSecondThird(const T &y0, const T &y1, const T &yd0, const T &yd1, const T &ydd0, const T &ydd1, const T &yddd0, const Real &x1=1)
Definition: Polynomial.h:168
static T Zero(const ::std::size_t &i)
Definition: TypeTraits.h:50
Polynomial & operator-=(const Polynomial &rhs)
Definition: Polynomial.h:400
static Polynomial QuinticFirstSecond(const T &y0, const T &y1, const T &yd0, const T &yd1, const T &ydd0, const T &ydd1, const Real &x1=1)
Definition: Polynomial.h:136
::std::size_t degree() const
Definition: Polynomial.h:199
Polynomial & operator+=(const T &rhs)
Definition: Polynomial.h:331
Polynomial & operator+=(const Polynomial &rhs)
Definition: Polynomial.h:346
static Polynomial Quadratic(const T &y0, const T &y1, const T &yd0, const Real &x1=1)
Definition: Polynomial.h:110
Polynomial & operator*=(const Real &rhs)
Definition: Polynomial.h:429
T cbrt(const T &arg)
Definition: Real.h:45
A vector-valued polynomial function from Real -> T.
Definition: Polynomial.h:61
Polynomial integral() const
Definition: Polynomial.h:270
virtual ~Polynomial()
Definition: Polynomial.h:69
static Polynomial Linear(const T &y0, const T &y1, const Real &x1=1)
Definition: Polynomial.h:99
Polynomial operator-(const Polynomial &rhs) const
Definition: Polynomial.h:380
static ::std::vector< Real > realRoots(const ::std::vector< Real > &c)
Calculates the real roots (or, zeros) for given polynomial coefficients.
Definition: Polynomial.h:459
Polynomial scaledX(const Real &factor) const
Stretches the x-axis of a polynomial by a given factor.
Definition: Polynomial.h:605
Real & upper()
Definition: Function.h:74
Polynomial derivative() const
Definition: Polynomial.h:204
Polynomial operator-() const
Definition: Polynomial.h:365
static Polynomial CubicSecond(const T &y0, const T &y1, const T &ydd0, const T &ydd1, const Real &x1=1)
Definition: Polynomial.h:86
Polynomial translatedX(const Real &translation) const
Translates a polynomial along the x-axis by a given parameter translation.
Definition: Polynomial.h:627
friend Polynomial operator*(const Polynomial &lhs, const Real &rhs)
Definition: Polynomial.h:424
T operator()(const Real &x, const ::std::size_t &derivative=0) const
Evaluates the function or a derivative thereof for a given value x.
Definition: Polynomial.h:286
static T abs(const T &t)
Definition: TypeTraits.h:55
Real x0
Definition: Function.h:102
Polynomial * clone() const
Definition: Polynomial.h:184
friend Polynomial operator+(const T &lhs, const Polynomial &rhs)
Definition: Polynomial.h:316
friend Polynomial operator+(const Polynomial &lhs, const T &rhs)
Definition: Polynomial.h:321
::std::vector< T > c
Definition: Polynomial.h:657
Definition: TypeTraits.h:43
static Polynomial QuarticFirstSecond(const T &y0, const T &y1, const T &yd0, const T &yd1, const T &ydd0, const Real &x1=1)
Definition: Polynomial.h:122
friend Polynomial operator/(const Polynomial &lhs, const Real &rhs)
Definition: Polynomial.h:444
const T & coefficient(const ::std::size_t &i) const
Definition: Polynomial.h:194
static const Real FUNCTION_BOUNDARY
Definition: Function.h:110
T & coefficient(const ::std::size_t &i)
Definition: Polynomial.h:189
Polynomial & operator-=(const T &rhs)
Definition: Polynomial.h:385
Polynomial operator+(const Polynomial &rhs) const
Definition: Polynomial.h:326
Real & lower()
Definition: Function.h:64
friend Polynomial operator/(const Real &lhs, const Polynomial &rhs)
Definition: Polynomial.h:439
double Real
Definition: Real.h:42
friend Polynomial operator*(const Real &lhs, const Polynomial &rhs)
Definition: Polynomial.h:419
Robotics Library.
Definition: AnalogInput.cpp:30