7 #include "cantera/numerics/eigen_dense.h"
14 double polyfit(
size_t n,
size_t deg,
const double* xp,
const double* yp,
15 const double* wp,
double* pp)
17 ConstMappedVector x(xp, n);
18 Eigen::VectorXd y = ConstMappedVector(yp, n);
19 MappedVector p(pp, deg+1);
22 throw CanteraError(
"polyfit",
"Polynomial degree ({}) must be less "
23 "than number of input data points ({})", deg, n);
28 Eigen::MatrixXd A(n, deg+1);
29 A.col(0).setConstant(1.0);
34 for (
size_t i = 1; i < deg; i++) {
35 A.col(i+1) = A.col(i).array() * x.array();
38 if (wp !=
nullptr && wp[0] > 0) {
41 Eigen::VectorXd w = ConstMappedVector(wp, n).cwiseSqrt().eval();
44 A = w.asDiagonal() * A;
45 y.array() *= w.array();
49 p = A.colPivHouseholderQr().solve(y);
53 return (A*p - y).eval().norm() / sqrt(n);
Base class for exceptions thrown by Cantera classes.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles,...
Namespace for the Cantera kernel.
double polyfit(size_t n, size_t deg, const double *xp, const double *yp, const double *wp, double *pp)
Fits a polynomial function to a set of data points.