Skip to contents

Mirrors the C# Evaluate class's three polynomial evaluators (Horner's method), vectorized over x against one shared coefficients vector: variant = "standard" calls Evaluate.Polynomial (coefficients in ASCENDING order, coefficients[1] the constant term); "reverse" calls Evaluate.PolynomialRev (coefficients in DESCENDING order, coefficients[1] the highest-order term), optionally truncated to order n + 1 via n; "reverse_unit" calls Evaluate.PolynomialRev_1 (DESCENDING order with an implicit leading coefficient of 1).

Usage

polynomial_eval(
  coefficients,
  x,
  variant = c("standard", "reverse", "reverse_unit"),
  n = NULL
)

Arguments

coefficients

a numeric vector of polynomial coefficients.

x

a numeric vector, the points at which to evaluate.

variant

one of "standard" (default), "reverse", or "reverse_unit".

n

an optional integer redefining the polynomial's order to n + 1; only valid with variant = "reverse".

Value

a numeric vector the same length as x.

Examples

polynomial_eval(c(3, 5, 7), 4)
#> [1] 135
polynomial_eval(c(3, 5, 7), 4, variant = "reverse")
#> [1] 75
polynomial_eval(c(3, 5, 7), 4, variant = "reverse", n = 1)
#> [1] 17