buildPolynomial method
Computes the interpolation polynomial and returns it as an Algebraic object.
Implementation
Algebraic buildPolynomial() {
final length = nodes.length * nodes.length;
final matrixSource = List<double>.generate(length, (_) => 0);
// Creating the Vandermonde matrix
for (var i = 0; i < nodes.length; ++i) {
for (var j = 0; j < nodes.length; ++j) {
matrixSource[nodes.length * i + j] = pow(nodes[i].x, j) * 1.0;
}
}
// Creating the known values vector
final knownValues = nodes.map((node) => node.y).toList(growable: false);
// Finding the coefficients by solving the system
final lu = LUSolver(
matrix: RealMatrix.fromFlattenedData(
rows: nodes.length,
columns: nodes.length,
data: matrixSource,
),
knownValues: knownValues,
);
final coefficients = lu.solve().reversed.toList(growable: false);
return Algebraic.fromReal(coefficients);
}