mpoly (version 1.1.1)

as.function.mpoly: Change a multivariate polynomial into a function.

Description

Transforms an mpoly object into a function which can be evaluated.

Usage

# S3 method for mpoly
as.function(x, varorder = vars(x), vector = TRUE,
  silent = FALSE, ..., plus_pad = 1L, times_pad = 1L, squeeze = TRUE)

Arguments

x

an object of class mpoly

varorder

the order of the variables

vector

whether the function should take a vector argument (TRUE) or a series of arguments (FALSE)

silent

logical; if TRUE, suppresses output

...

any additional arguments

plus_pad

number of spaces to the left and right of plus sign

times_pad

number of spaces to the left and right of times sign

squeeze

minify code in the created function

See Also

plug(), as.function.mpolyList()

Examples

Run this code
# NOT RUN {
p <- mp("(x - 1)^2")
(f <- as.function(p))
f(1)
f(seq(0, 2, .1))

p <- mp("x + 3 x y + z^2 x")
(f <- as.function(p))
f(1:3) # -> 16
f(c(1,1,1)) # -> 5

f <- as.function(p, vector = FALSE)
f(1, 2, 3) # -> 16
f(1, 1, 1) # -> 5

f <- as.function(p, varorder = c("z","y","x"), vector = FALSE)
f(3, 2, 1) # -> 16
f(1, 1, 1) # -> 5

# for univariate mpolys, as.function() returns a vectorized function
# that can even apply to arrays
p <- mp("x^2")
f <- as.function(p)
f(1:10)
(mat <- matrix(1:4, 2))
f(mat)


p <- mp("1 2 3 4")
f <- as.function(p)
f(10) # -> 24

bernstein(1, 2)
s <- seq(0, 1, .01)
as.function(bernstein(1, 2))(s)
plot(
  s,
  as.function(bernstein(1, 2))(s)
)


as.function(mp("x + xx"))
as.function(mp("x + xx"), squeeze = FALSE)


# }

Run the code above in your browser using DataLab