Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

horner

Horner's Rule


Description

Compute the value of a polynomial via Horner's Rule.

Usage

horner(p, x)
hornerdefl(p, x)

Arguments

p

Numeric vector representing a polynomial.

x

Numeric scalar, vector or matrix at which to evaluate the polynomial.

Details

horner utilizes the Horner scheme to evaluate the polynomial and its first derivative at the same time.

The polynomial p = p_1*x^n + p_2*x^{n-1} + ... + p_n*x + p_{n+1} is hereby represented by the vector p_1, p_2, ..., p_n, p_{n+1}, i.e. from highest to lowest coefficient.

hornerdefl uses a similar approach to return the value of p at x and a polynomial q that satisfies

p(t) = q(t) * (t - x) + r, r constant

which implies r=0 if x is a root of p. This will allow for a repeated root finding of polynomials.

Value

horner returns a list with two elements, list(y=..., dy=...) where the first list elements returns the values of the polynomial, the second the values of its derivative at the point(s) x.

hornerdefl returns a list list(y=..., dy=...) where q represents a polynomial, see above.

Note

For fast evaluation, there is no error checking for p and x, which both must be numerical vectors (x can be a matrix in horner).

References

Quarteroni, A., and Saleri, F. (2006) Scientific Computing with Matlab and Octave. Second Edition, Springer-Verlag, Berlin Heidelberg.

See Also

Examples

x <- c(-2, -1, 0, 1, 2)
p <- c(1, 0, 1)  # polynomial x^2 + x, derivative 2*x
horner(p, x)$y   #=>  5  2  1  2  5
horner(p, x)$dy  #=> -4 -2  0  2  4

p <- Poly(c(1, 2, 3))  # roots 1, 2, 3
hornerdefl(p, 3)          # q = x^2- 3 x + 2  with roots 1, 2

pracma

Practical Numerical Math Functions

v2.3.3
GPL (>= 3)
Authors
Hans W. Borchers [aut, cre]
Initial release
2021-01-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.