as.function.mpolyList {mpoly} | R Documentation |
Change a vector of multivariate polynomials into a function.
Description
Transforms an mpolyList object into a function which can be evaluated.
Usage
## S3 method for class 'mpolyList'
as.function(x, varorder = vars(x), vector = TRUE,
silent = FALSE, ..., plus_pad = 1L, times_pad = 1L, squeeze = TRUE)
## S3 method for class '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")))
(f <- as.function(mpolyList))
f(c(1,2)) # -> (2*1 + 1, 1-2^2) = 3 -3
f <- as.function(mpolyList, varorder = c("x","y","z"))
f(c(1,0,2)) # -> 3 -3
f(c(1,4,2)) # -> 3 -3
f <- as.function(mpolyList, varorder = c("x","y","z"), vector = FALSE)
f(1, 0, 2) # -> 3 -3
f(1, 4, 2) # -> 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(c(0,2,3)) # -> 1 7 2
# a univariate mpolyList creates a vectorized function
ps <- mp(c("x", "x^2", "x^3"))
f <- as.function(ps)
f
s <- seq(-1, 1, length.out = 11)
f(s)
# another example
ps <- chebyshev(1:3)
f <- as.function(ps)
f(s)
# 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"))
f <- as.function(ps)
f(.5) # P[X = 0], P[X = 1], and P[X = 2] for X ~ Bin(2, .5)
dbinom(0:2, 2, .5)
f(.75) # P[X = 0], P[X = 1], and P[X = 2] for X ~ Bin(2, .75)
dbinom(0:2, 2, .75)
# 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)
)
all.equal(
as.function(abinom(30))(.5),
dbinom(0:30, 20, .5)
)
# the function produced is vectorized:
number_of_probs <- 11
probs <- seq(0, 1, length.out = number_of_probs)
(mat <- f(probs))
colnames(mat) <- sprintf("P[X = %d]", 0:2)
rownames(mat) <- sprintf("p = %.2f", s)
mat
[Package mpoly version 1.1.1 Index]