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

Description Usage Arguments See Also Examples

View source: R/as.function.mpoly.R

Description

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

Usage

1
2
3
## S3 method for class '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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)

mpoly documentation built on March 26, 2020, 7:33 p.m.