polynomial function

num polynomial(
  1. num x,
  2. Iterable<num> a
)

Returns the polynominal defined by the coefficients a: a[0] + a[1] * x + ... + a[n-1] * pow(x, n - 1).

Recursive function based on Horner's rule.

Implementation

num polynomial(num x, Iterable<num> a) {
  if (a.isEmpty) return 0;
  return a.first + x * polynomial(x, a.skip(1));
}