33 coefficients[0] += increment;
41 void PolynomialRegression::computeCoeffs()
43 std::sort(data.begin(), data.end(), Point2DDouble::Comparer(
Direction::LEFT));
47 for (
size_t tmp = 0; tmp < data.size(); ++tmp)
50 for (
size_t d = 1; d <= dimension; ++d)
52 X[tmp][d] = X[tmp][d - 1] * data[tmp].X;
54 Y[tmp][0] = data[tmp].Y;
58 tmpMat *= (double)data.size();
59 tmpMat = tmpMat.MakeGaussJordanInverse();
64 for (
size_t d = 0; d <= dimension; ++d)
66 coefficients[d] = res[d][0];
74 if (x < data.front().X)
76 double x0 = data[0].X;
77 double x1 = x0 + (data[1].X - x0) / 5;
78 double y0 = (*this)[x0];
79 double y1 = (*this)[x1];
80 double slope = (y1 - y0) / (x1 - x0);
81 return y0 + slope * (x - x0);
83 if (x > data.back().X)
85 double xn = data.back().X;
86 double xn1 = xn - (xn - data[data.size() - 2].X) / 5;
87 double yn = (*this)[xn];
88 double yn1 = (*this)[xn1];
89 double slope = (yn - yn1) / (xn - xn1);
90 return yn + slope * (x - xn);
93 double y = coefficients[0];
95 for (
size_t d = 1; d <= dimension; ++d)
98 y += coefficients[d] * xx;
void TranslateY(int increment)
Translates the polynomial.
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
virtual double operator[](double x) const override
Gets ordinate at x (double)
Square double matrix class.
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.