24template<
typename Scalar>
30 template<
typename Derived>
31 Polynomial(
const Eigen::DenseBase<Derived>& coefficients)
32 : _coefficients(coefficients)
37 [[nodiscard]] Scalar
f(Scalar x)
const
40 auto xpow =
static_cast<Scalar
>(1.0);
41 for (
const auto& coeff : _coefficients)
43 value += coeff * xpow;
52 int degree = _coefficients.size() - 1;
53 Eigen::VectorX<Scalar> derivative = Eigen::VectorX<Scalar>::Zero(degree);
54 for (
int i = 0; i < degree; i++)
56 derivative(i) = (i + 1) * _coefficients(i + 1);
62 [[nodiscard]]
const Eigen::VectorX<Scalar>&
coeffs()
const
70 [[nodiscard]] std::string
toString(fmt::format_string<double> fmt =
"{:.1e}",
const char* var =
"x")
const
73 for (
int i = 0; i < _coefficients.rows(); i++)
75 bool positive = _coefficients(i) >= 0;
76 std::string a = fmt::format(fmt, std::abs(_coefficients(i)));
79 out += fmt::format(
"{}{}", positive ?
"" :
"-", a);
83 out += fmt::format(
" {} {}{}{}",
87 i > 1 ? fmt::format(
"^{}", i) :
"");
95 Eigen::VectorX<Scalar> _coefficients;
100#ifndef DOXYGEN_IGNORE
103template<
typename Scalar>
104struct fmt::formatter<NAV::Polynomial<Scalar>> : fmt::formatter<std::string>
110 template<
typename FormatContext>
113 return fmt::formatter<std::string>::format(poly.
toString(), ctx);
123template<
typename Scalar>
126 return os << fmt::format(
"{}", obj);
std::ostream & operator<<(std::ostream &os, const NAV::Polynomial< Scalar > &obj)
Stream insertion operator overload.
Definition Polynomial.hpp:124
Polynomial.
Definition Polynomial.hpp:26
const Eigen::VectorX< Scalar > & coeffs() const
Returns the coefficients.
Definition Polynomial.hpp:62
Polynomial< Scalar > getDerivative() const
Calculates the derivative of the polynomial.
Definition Polynomial.hpp:50
std::string toString(fmt::format_string< double > fmt="{:.1e}", const char *var="x") const
Format the polynomial as a string.
Definition Polynomial.hpp:70
Scalar f(Scalar x) const
Calculates the polynomial value at given x.
Definition Polynomial.hpp:37
Polynomial(const Eigen::DenseBase< Derived > &coefficients)
Constructor.
Definition Polynomial.hpp:31