h5geo 0.4.0
C++17 and python API to work with geo-data (seismic, wells, maps, other in process) based on HDF5. Aimed at geoscientists and developers.
Loading...
Searching...
No Matches
h5polyfit.h
1#ifndef H5POLYFIT_H
2#define H5POLYFIT_H
3
4#include <Eigen/Dense>
5
6namespace h5geo
7{
8
9template <typename D>
10Eigen::VectorX<typename D::Scalar> polyfit(const Eigen::DenseBase<D> &x, const Eigen::DenseBase<D> &y,int order)
11{
12 if (x.size() != y.size())
13 return Eigen::VectorX<typename D::Scalar>();
14
15 if (order < 1 || order > x.size() - 1)
16 return Eigen::VectorX<typename D::Scalar>();
17
18 Eigen::MatrixX<typename D::Scalar> A(x.size(), order + 1);
19
20 for (ptrdiff_t i = 0; i < x.size(); i++) {
21 A(i, 0) = 1.0;
22 }
23
24 for (ptrdiff_t j = 0; j < x.size(); j++) {
25 for (ptrdiff_t i = 0; i < order; i++) {
26 A(j, i + 1) = A(j, i) * x(j);
27 }
28 }
29
30 auto Q = A.householderQr();
31 return Q.solve(y.derived());
32}
33
35template <typename D>
36Eigen::MatrixX<typename D::Scalar> polyval(const Eigen::DenseBase<D> &coeffs, const Eigen::DenseBase<D> &x) {
37 Eigen::MatrixX<typename D::Scalar> m(x.rows(), x.cols());
38 for (ptrdiff_t i = 0; i < coeffs.size(); i++) {
39 m.derived().array() += coeffs(i)*x.derived().array().pow(i);
40 }
41 return m;
42}
43
44} // namespace h5geo
45
46#endif // H5POLYFIT_H
Basic namespace.
Definition h5base.h:29
Eigen::MatrixX< typename D::Scalar > polyval(const Eigen::DenseBase< D > &coeffs, const Eigen::DenseBase< D > &x)
Evaluate a polynomial.
Definition h5polyfit.h:36