7#include "cantera/numerics/eigen_dense.h"
14double polyfit(
size_t deg, span<const double> x, span<const double> y,
15 span<const double> w, span<double> p)
25 throw CanteraError(
"polyfit",
"Polynomial degree ({}) must be less "
26 "than number of input data points ({})", deg, n);
29 ConstMappedVector xx(x.data(), n);
30 Eigen::VectorXd yy = ConstMappedVector(y.data(), n);
31 MappedVector pp(p.data(), deg + 1);
35 Eigen::MatrixXd A(n, deg+1);
36 A.col(0).setConstant(1.0);
41 for (
size_t i = 1; i < deg; i++) {
42 A.col(i+1) = A.col(i).array() * xx.array();
45 if (!w.empty() && w[0] > 0) {
48 Eigen::VectorXd ww = ConstMappedVector(w.data(), n).cwiseSqrt().eval();
51 A = ww.asDiagonal() * A;
52 yy.array() *= ww.array();
56 pp = A.colPivHouseholderQr().solve(yy);
60 return (A * pp - yy).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 and logging,...
double polyfit(size_t deg, span< const double > x, span< const double > y, span< const double > w, span< double > p)
Fits a polynomial function to a set of data points.
Namespace for the Cantera kernel.
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.