Use Horner's rule to evaluate a polynomial

horner(x, coefs)

rhorner(x, coefs)

naivepoly(x, coefs)

betterpoly(x, coefs)

Arguments

x

a vector of x values to evaluate the polynomial

coefs

vector of coefficients of x

Value

the value of the function at x

Details

This function implements Horner's rule for fast polynomial evaluation. The implementation expects x to be a vector of x values at which to evaluate the polynomial. The parameter coefs is a vector of coefficients of x. The vector order is such that the first element is the constant term, the second element is the coefficient of x, the so forth to the highest degreed term. Terms with a 0 coefficient should have a 0 element in the vector.

The function rhorner implements the the Horner algorithm recursively.

The function naivepoly implements a polynomial evaluator using the straightforward algebraic approach.

The function betterpoly implements a polynomial evaluator using the straightforward algebraic approach with cached x terms.

See also

Examples

b <- c(2, 10, 11)
x <- 5
horner(x, b)
#> [1] 327
b <- c(-1, 0, 1)
x <- c(1, 2, 3, 4)
horner(x, b)
#> [1]  0  3  8 15
rhorner(x, b)
#> [1]  0  3  8 15