0.2.0
Loading...
Searching...
No Matches
Polynomial.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <cmath>
17#include <iostream>
18#include "util/Eigen.hpp"
19
20namespace NAV
21{
22
24template<typename Scalar>
26{
27 public:
30 template<typename Derived>
31 Polynomial(const Eigen::DenseBase<Derived>& coefficients) // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
32 : _coefficients(coefficients)
33 {}
34
37 [[nodiscard]] Scalar f(Scalar x) const
38 {
39 Scalar value = 0;
40 auto xpow = static_cast<Scalar>(1.0);
41 for (const auto& coeff : _coefficients)
42 {
43 value += coeff * xpow;
44 xpow *= x;
45 }
46 return value;
47 }
48
50 [[nodiscard]] Polynomial<Scalar> getDerivative() const
51 {
52 int degree = _coefficients.size() - 1;
53 Eigen::VectorX<Scalar> derivative = Eigen::VectorX<Scalar>::Zero(degree);
54 for (int i = 0; i < degree; i++)
55 {
56 derivative(i) = (i + 1) * _coefficients(i + 1);
57 }
58 return Polynomial<Scalar>(derivative);
59 }
60
62 [[nodiscard]] const Eigen::VectorX<Scalar>& coeffs() const
63 {
64 return _coefficients;
65 }
66
70 [[nodiscard]] std::string toString(fmt::format_string<double> fmt = "{:.1e}", const char* var = "x") const
71 {
72 std::string out;
73 for (int i = 0; i < _coefficients.rows(); i++)
74 {
75 bool positive = _coefficients(i) >= 0;
76 std::string a = fmt::format(fmt, std::abs(_coefficients(i)));
77 if (i == 0)
78 {
79 out += fmt::format("{}{}", positive ? "" : "-", a);
80 }
81 else
82 {
83 out += fmt::format(" {} {}{}{}",
84 positive ? "+" : "-",
85 a,
86 var,
87 i > 1 ? fmt::format("^{}", i) : "");
88 }
89 }
90 return out;
91 }
92
93 private:
95 Eigen::VectorX<Scalar> _coefficients;
96};
97
98} // namespace NAV
99
100#ifndef DOXYGEN_IGNORE
101
103template<typename Scalar>
104struct fmt::formatter<NAV::Polynomial<Scalar>> : fmt::formatter<std::string>
105{
110 template<typename FormatContext>
111 auto format(const NAV::Polynomial<Scalar>& poly, FormatContext& ctx) const
112 {
113 return fmt::formatter<std::string>::format(poly.toString(), ctx);
114 }
115};
116
117#endif
118
123template<typename Scalar>
124std::ostream& operator<<(std::ostream& os, const NAV::Polynomial<Scalar>& obj)
125{
126 return os << fmt::format("{}", obj);
127}
Vector space operations.
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