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

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

# S3 method for bezier
as.function(x, ...)

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

# basic examples (mpolyList <- mp(c("2 x + 1", "x - z^2")))
#> 2 x + 1 #> x - z^2
(f <- as.function(mpolyList))
#> f(.) with . = (x, z)
#> function (.) #> { #> c(2 * .[1] + 1, .[1] - .[2]^2) #> } #> <environment: 0x7fae3e5b1a00>
f(c(1,2)) # -> (2*1 + 1, 1-2^2) = 3 -3
#> [1] 3 -3
f <- as.function(mpolyList, varorder = c("x","y","z"))
#> f(.) with . = (x, y, z)
f(c(1,0,2)) # -> 3 -3
#> [1] 3 -3
f(c(1,4,2)) # -> 3 -3
#> [1] 3 -3
f <- as.function(mpolyList, varorder = c("x","y","z"), vector = FALSE) f(1, 0, 2) # -> 3 -3
#> [1] 3 -3
f(1, 4, 2) # -> 3 -3
#> [1] 3 -3
# making a gradient function (useful for optim) mpoly <- mp("x + y^2 + y z") mpolyList <- gradient(mpoly) f <- as.function(mpolyList, varorder = vars(mpoly))
#> f(.) with . = (x, y, z)
f(c(0,2,3)) # -> 1 7 2
#> [1] 1 7 2
# a univariate mpolyList creates a vectorized function ps <- mp(c("x", "x^2", "x^3")) f <- as.function(ps) f
#> function (.) #> { #> if (length(.) > 1) #> return(t(sapply(., f))) #> c(., .^2, .^3) #> } #> <environment: 0x7fae3f233058>
s <- seq(-1, 1, length.out = 11) f(s)
#> [,1] [,2] [,3] #> [1,] -1.0 1.00 -1.000 #> [2,] -0.8 0.64 -0.512 #> [3,] -0.6 0.36 -0.216 #> [4,] -0.4 0.16 -0.064 #> [5,] -0.2 0.04 -0.008 #> [6,] 0.0 0.00 0.000 #> [7,] 0.2 0.04 0.008 #> [8,] 0.4 0.16 0.064 #> [9,] 0.6 0.36 0.216 #> [10,] 0.8 0.64 0.512 #> [11,] 1.0 1.00 1.000
# another example ps <- chebyshev(1:3)
#> Loading required package: polynom
#> #> Attaching package: ‘polynom’
#> The following object is masked from ‘package:mpoly’: #> #> LCM
f <- as.function(ps) f(s)
#> [,1] [,2] [,3] #> [1,] -1.0 1.00 -1.000 #> [2,] -0.8 0.28 0.352 #> [3,] -0.6 -0.28 0.936 #> [4,] -0.4 -0.68 0.944 #> [5,] -0.2 -0.92 0.568 #> [6,] 0.0 -1.00 0.000 #> [7,] 0.2 -0.92 -0.568 #> [8,] 0.4 -0.68 -0.944 #> [9,] 0.6 -0.28 -0.936 #> [10,] 0.8 0.28 -0.352 #> [11,] 1.0 1.00 1.000
# the binomial pmf as an algebraic (polynomial) map # from [0,1] to [0,1]^size # p |-> {choose(size, x) p^x (1-p)^(size-x)}_{x = 0, ..., size} abinom <- function(size, indet = "p"){ chars4mp <- vapply(as.list(0:size), function(x){ sprintf("%d %s^%d (1-%s)^%d", choose(size, x), indet, x, indet, size-x) }, character(1)) mp(chars4mp) } (ps <- abinom(2, "p")) # = mp(c("(1-p)^2", "2 p (1-p)", "p^2"))
#> p^2 - 2 p + 1 #> -2 p^2 + 2 p #> p^2
f <- as.function(ps) f(.5) # P[X = 0], P[X = 1], and P[X = 2] for X ~ Bin(2, .5)
#> [1] 0.25 0.50 0.25
dbinom(0:2, 2, .5)
#> [1] 0.25 0.50 0.25
f(.75) # P[X = 0], P[X = 1], and P[X = 2] for X ~ Bin(2, .75)
#> [1] 0.0625 0.3750 0.5625
dbinom(0:2, 2, .75)
#> [1] 0.0625 0.3750 0.5625
# as the degree gets larger, you'll need to be careful when evaluating # the polynomial. as.function() is not currently optimized for # stable numerical evaluation of polynomials; it evaluates them in # the naive way all.equal( as.function(abinom(10))(.5), dbinom(0:10, 10, .5) )
#> [1] TRUE
all.equal( as.function(abinom(30))(.5), dbinom(0:30, 20, .5) )
#> [1] "Mean relative difference: 1.375229"
# the function produced is vectorized: number_of_probs <- 11 probs <- seq(0, 1, length.out = number_of_probs) (mat <- f(probs))
#> [,1] [,2] [,3] #> [1,] 1.00 0.00 0.00 #> [2,] 0.81 0.18 0.01 #> [3,] 0.64 0.32 0.04 #> [4,] 0.49 0.42 0.09 #> [5,] 0.36 0.48 0.16 #> [6,] 0.25 0.50 0.25 #> [7,] 0.16 0.48 0.36 #> [8,] 0.09 0.42 0.49 #> [9,] 0.04 0.32 0.64 #> [10,] 0.01 0.18 0.81 #> [11,] 0.00 0.00 1.00
colnames(mat) <- sprintf("P[X = %d]", 0:2) rownames(mat) <- sprintf("p = %.2f", s) mat
#> P[X = 0] P[X = 1] P[X = 2] #> p = -1.00 1.00 0.00 0.00 #> p = -0.80 0.81 0.18 0.01 #> p = -0.60 0.64 0.32 0.04 #> p = -0.40 0.49 0.42 0.09 #> p = -0.20 0.36 0.48 0.16 #> p = 0.00 0.25 0.50 0.25 #> p = 0.20 0.16 0.48 0.36 #> p = 0.40 0.09 0.42 0.49 #> p = 0.60 0.04 0.32 0.64 #> p = 0.80 0.01 0.18 0.81 #> p = 1.00 0.00 0.00 1.00