Takes a polynomial argument and constructs an R function to evaluate it at arbitrary points.

# S3 method for class 'polynomial'
as.function(x, ...)

Arguments

x

An object of class "polynomial".

...

further arguments to be passed to or from methods.

Details

This is a method for the generic function as.function.

The polynomial is evaluated within the function using the Horner scheme.

Note that you can use the model-oriented predict method for polynomials for purpose of evaluation (without explicit coercion to a function), see the example below.

Value

A function to evaluate the polynomial p.

Examples

pr <- (poly.calc(-1:1) - 2 * polynomial(c(1, 2, 1)))^2
pr
#> 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 
## 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6
prf <- as.function(pr)
prf
#> function (x) 
#> {
#>     w <- 0
#>     w <- 1 + x * w
#>     w <- -4 + x * w
#>     w <- -6 + x * w
#>     w <- 16 + x * w
#>     w <- 33 + x * w
#>     w <- 20 + x * w
#>     w <- 4 + x * w
#>     w
#> }
#> <environment: 0x5827782e3b10>
## function (x) 
## 4 + x * (20 + x * (33 + x * (16 + x * (-6 + x * (-4 + x * (1))))))
## <environment: 0x402440f0>
prf(-3:3)
#> [1] 1024   64    0    4   64  144   64
##  1024 64  0 4 64 144 64
predict(pr, -3:3)
#> [1] 1024   64    0    4   64  144   64
##  1024 64  0 4 64 144 64