horner: Horner's rule

Description Usage Arguments Details Value See Also Examples

View source: R/horner.R

Description

Use Horner's rule to evaluate a polynomial

Usage

1
2
3
4
5
6
7
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

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.

Value

the value of the function at x

See Also

Other algebra: bilinear(), cubicspline(), division, fibonacci(), isPrime(), linterp(), nthroot(), polyinterp(), pwiselinterp(), quadratic()

Examples

1
2
3
4
5
6
7
b <- c(2, 10, 11)
x <- 5
horner(x, b)
b <- c(-1, 0, 1)
x <- c(1, 2, 3, 4)
horner(x, b)
rhorner(x, b)

Example output

[1] 327
[1]  0  3  8 15
[1]  0  3  8 15

cmna documentation built on July 14, 2021, 5:11 p.m.