14#include "../../Matrix.h"
15#include "../lin_alg/gaussSeidel.h"
57 ostr << base.
Function() << std::flush;
83 if(x.columns() > x.rows()) { x = x.
Transpose(); }
85 for(
size_t i = 0; i < out.
rows(); ++i) {
86 for(
size_t j = 0; j < out.
rows(); ++j) { out(i, j) =
pow(x(i, 0), j); }
94 std::cerr <<
"Error computing Vandermonde-Matrix! Most likely passed equal support values" << std::endl;
106 for(
size_t i = 0; i <
A.
rows(); ++i) {
107 Y +=
A(i, 0) * X.
Apply([i](
const double& in) {
return pow(in, i); });
117 for(
size_t row = 0; row <
A.
rows(); ++row) {
118 if(row != 0) { out +=
" + "; }
119 out +=
format(
"%d",
int(
A(row, 0)));
120 if(row > 0) { out +=
format(
"x^%d", row); }
160 for(
size_t j = 0; j <
x.
rows(); ++j) {
163 out *= (xk -
x(j, 0)) / (
x(i, 0) -
x(j, 0));
175 for(
size_t k = 0; k < in.
rows(); ++k) {
185 [[nodiscard]] std::string
buildLx(
size_t i)
const {
188 bool needsOperator =
false;
189 for(
size_t j = 0; j <
x.
rows(); ++j) {
191 needsOperator =
false;
196 if(
x(i, 0) -
x(j, 0) == 0)
return "";
198 if(needsOperator) { Lx +=
" * "; }
199 if(
x(i, 0) -
x(j, 0) < 0) { Lx +=
"-"; }
204 Lx +=
format(
"(x + %.3f)", std::abs(
x(j, 0)));
206 Lx +=
format(
"(x - %.3f)",
x(j, 0));
213 Lx +=
format(
"/%.3f", std::abs(
x(i, 0) -
x(j, 0)));
214 needsOperator =
true;
224 for(
size_t i = 0; i <
y.
rows(); ++i) {
225 if(
y(i, 0) == 0)
continue;
227 if(i > 0) { out +=
" + "; }
228 out +=
format(
"%d * (",
int(
y(i, 0)));
278 for(
size_t j = 1; j < m; ++j) {
279 for(
size_t i = j; i < m; ++i) { F(i, j) = (F(i, j - 1) - F(i - 1, j - 1)) / (x(i, 0) - x(i - j, 0)); }
289 for(
size_t i = 0; i <
b.
rows(); ++i) {
290 if(i > 0) { out +=
" + "; }
291 out +=
format(
"%.3f",
b(i, 0));
293 for(
size_t j = 0; j < i; ++j) {
295 if(
X(j, 0) > 0) out +=
format(
"* (x - %.3f)",
X(j, 0));
297 out +=
format(
"* (x + %.3f)", std::abs(
X(j, 0)));
314 for(
size_t i = 0; i < index - 1; ++i) { out *= (x -
X(i, 0)); }
324 for(
int j =
b.
rows() - 2; j >= 0; --j) {
328 .
Apply([xAct](
const double& in) {
return in - xAct; })
330 .Apply([bAct](
const double& in) {
return bAct + in; });
double pow(double x, int exponent)
Definition: SupportValues.h:133
double GetCoefficient(const double &xk, const size_t i) const
Definition: SupportValues.h:158
virtual std::string Function() const
Definition: SupportValues.h:222
Matrix< double > x
$$x_i$$ Support values
Definition: SupportValues.h:135
Matrix< double > y
$$y_i$$ Support values evaluated with the function
Definition: SupportValues.h:137
std::string buildLx(size_t i) const
Definition: SupportValues.h:185
virtual Matrix< double > Evaluate(const Matrix< double > &in) const
Definition: SupportValues.h:173
LagrangeBase(const Matrix< double > &X, const Matrix< double > &Y)
Definition: SupportValues.h:145
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
size_t rows() const
Definition: Matrix.h:193
T Determinant() const
Definition: Matrix.h:216
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
void SetColumn(size_t index, const Matrix< T > &other)
Definition: Matrix.h:524
Definition: SupportValues.h:69
MonomBase(const Matrix< double > &X, const Matrix< double > &Y)
Definition: SupportValues.h:79
virtual std::string Function() const
Definition: SupportValues.h:115
Matrix< double > A
Coefficient matrix.
Definition: SupportValues.h:72
virtual Matrix< double > Evaluate(const Matrix< double > &X) const
Definition: SupportValues.h:104
Definition: SupportValues.h:259
Matrix< double > X
support values $$x_i$$
Definition: SupportValues.h:261
double GetCoefficient(size_t index, const double &x) const
Definition: SupportValues.h:312
NewtonBase(const Matrix< double > &x, const Matrix< double > &y)
Definition: SupportValues.h:272
Matrix< double > b
newton base coefficients $$b_i$$
Definition: SupportValues.h:265
virtual Matrix< double > Evaluate(const Matrix< double > &xIn) const
Definition: SupportValues.h:322
virtual std::string Function() const
Definition: SupportValues.h:287
Definition: SupportValues.h:25
friend std::ostream & operator<<(std::ostream &ostr, const PolynomialBase &base)
Definition: SupportValues.h:56
PolynomialBase(const Matrix< double > &X, const Matrix< double > &Y)
Definition: SupportValues.h:32
virtual Matrix< double > Evaluate(const Matrix< double > &) const =0
virtual std::string Function() const =0
Matrix< double > gaussSeidel(const Matrix< double > &A, const Matrix< double > &b)
Definition: gaussSeidel.h:29
Matrix< T > diag_elements(const Matrix< T > &in)
Definition: matrix_utils.h:451
Matrix< T > HadamardMulti(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:17