buildPolynomial method

Algebraic buildPolynomial()

Creates the interpolation polynomial of degree maxDegree.

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.flatMatrix(
    equations: matrixSource,
    constants: knownValues,
  );

  final coefficients = lu.solve().reversed.toList();

  // Buildng the polynomial
  return Algebraic.fromReal(coefficients);
}