7#ifndef __SABLIB_POLYFIT_H__
8#define __SABLIB_POLYFIT_H__
23template <
typename Derived>
24const Eigen::MatrixX<typename Derived::PlainObject::Scalar>
25Vandermonde(
const Eigen::MatrixBase<Derived> & x,
const unsigned int polyorder)
29 static_assert(Derived::IsVectorAtCompileTime,
"Error: x is not vector.");
32 throw std::invalid_argument(
"Vandermonde(): the length of x is zero.");
36 throw std::invalid_argument(
"Vandermonde(): polyorder is zero.");
39 using Scalar =
typename Derived::PlainObject::Scalar;
40 unsigned int n = x.size();
41 unsigned int m = polyorder + 1;
42 Eigen::MatrixX<Scalar> V(n, m);
44 for(
unsigned int i = 0; i < n; ++i) {
47 for(
unsigned int j = 0; j < m; ++j) {
64template <
typename Derived>
65const typename Derived::PlainObject
67 const Eigen::MatrixX<typename Derived::PlainObject::Scalar> & V,
68 const Eigen::MatrixBase<Derived> & y
73 static_assert(Derived::IsVectorAtCompileTime,
"Error: y is not vector.");
75 if(V.rows() != y.size()) {
76 throw std::invalid_argument(
"PolyFit(): the sizes of y and V do not match.");
79 return (V.transpose() * V).ldlt().solve(V.transpose() * y);
93template <
typename Derived>
94const typename Derived::PlainObject
95PolyFit(
const Eigen::MatrixBase<Derived> & x,
const Eigen::MatrixBase<Derived> & y,
const unsigned int polyorder)
99 static_assert(Derived::IsVectorAtCompileTime,
"Error: x or y is not vector.");
101 if(x.size() != y.size()) {
102 throw std::invalid_argument(
"PolyFit(): x and y must have the same size.");
105 if(polyorder + 1 > x.size()) {
106 throw std::invalid_argument(
"PolyFit(): polyorder is too high for the number of points.");
120template <
typename Derived>
121const typename Derived::PlainObject
123 const Eigen::MatrixBase<Derived> & coeff,
124 const Eigen::MatrixX<typename Derived::PlainObject::Scalar> & V
129 static_assert(Derived::IsVectorAtCompileTime,
"Error: coeff is not vector.");
131 if(coeff.size() == 0) {
132 throw std::invalid_argument(
"PolyVal(): the length of coeff is zero.");
135 if(V.cols() != coeff.size()) {
136 throw std::invalid_argument(
"PolyVal(): the sizes of V and coeff do not match.");
149template <
typename Derived>
150const typename Derived::PlainObject
151PolyVal(
const Eigen::MatrixBase<Derived> & coeff,
const Eigen::MatrixBase<Derived> & x)
155 static_assert(Derived::IsVectorAtCompileTime,
"Error: coeff or x is not vector.");
168template <
typename Derived>
169const typename Derived::PlainObject::Scalar
170PolyVal(
const Eigen::MatrixBase<Derived> & coeff,
const typename Derived::PlainObject::Scalar x)
174 static_assert(Derived::IsVectorAtCompileTime,
"Error: coeff is not vector.");
176 if(coeff.size() == 0) {
177 throw std::invalid_argument(
"PolyVal(): the length of coeff is zero.");
180 typename Derived::PlainObject::Scalar val = 0, xx = 1.0;
182 for(
int i = 0; i < coeff.size(); i++) {
183 val += coeff(i) * xx;
const Derived::PlainObject PolyFit(const Eigen::MatrixX< typename Derived::PlainObject::Scalar > &V, const Eigen::MatrixBase< Derived > &y)
Solves the polynomial least squares fitting problem using a pre-calculated Vandermonde matrix.
const Derived::PlainObject PolyVal(const Eigen::MatrixBase< Derived > &coeff, const Eigen::MatrixX< typename Derived::PlainObject::Scalar > &V)
Evaluates the polynomial at specified points using a pre-calculated Vandermonde matrix.
const Eigen::MatrixX< typename Derived::PlainObject::Scalar > Vandermonde(const Eigen::MatrixBase< Derived > &x, const unsigned int polyorder)
Generates a Vandermonde matrix for a given vector and polynomial order.