# basic usage. m = # polys/eqns, n = # vars
# m = 1, n = 1, `as.function.mpoly()`
p <- mp("x^2 + 1")
(f <- as.function(p))
f(2)
f(1:3) # vectorized
# m = 1, n = 2 , `as.function.mpoly()`
p <- mp("x y")
(f <- as.function(p))
f(1:2)
(mat <- matrix(1:6, ncol = 2))
f(mat) # vectorized across rows of input matrix
# m = 2, n = 1, `as.function.mpolyList()`
p <- mp(c("x", "x^2"))
(f <- as.function(p))
f(2)
f(1:3) # vectorized
(f <- as.function(p, name = TRUE))
f(2)
f(1:3) # vectorized
# m = 3, n = 2, `as.function.mpolyList()`
p <- mp("(x + y)^2")
(p <- monomials(p))
(f <- as.function(p))
f(1:2)
(mat <- cbind(x = 1:3, y = 4:6))
f(mat) # vectorized across rows of input matrix
(f <- as.function(p, name = TRUE))
f(1:2)
f(mat)
# setting vector = FALSE changes the function to a sequence of arguments
# this is only of interest if n = # of vars > 1
# m = 1, n = 2, `as.function.mpoly()`
p <- mp("x y")
(f <- as.function(p, vector = FALSE))
f(1, 2)
(mat <- matrix(1:6, ncol = 2))
f(mat[,1], mat[,2]) # vectorized across input vectors
# m = 3, n = 2, `as.function.mpolyList()`
p <- mp(c("x", "y", "x y"))
(f <- as.function(p, vector = FALSE))
f(1, 2)
(mat <- matrix(1:4, ncol = 2))
f(mat[,1], mat[,2]) # vectorized across rows of input matrix
(f <- as.function(p, vector = FALSE, name = TRUE))
f(1, 2)
(mat <- matrix(1:4, ncol = 2))
f(mat[,1], mat[,2]) # vectorized across rows of input matrix
# it's almost always a good idea to use the varorder argument,
# otherwise, mpoly has to guess at the order of the arguments
invisible( as.function(mp("y + x")) )
invisible( as.function(mp("x + y")) )
invisible( as.function(mp("y + x"), varorder = c("x","y")) )
# constant polynomials have some special rules
f <- as.function(mp("1"))
f(2)
f(1:10)
f(matrix(1:6, nrow = 2))
# you can use this to create a gradient function, useful for optim()
p <- mp("x + y^2 + y z")
(ps <- gradient(p))
(f <- as.function(ps, varorder = c("x","y","z")))
f(c(0,2,3)) # -> [1, 7, 2]
# a m = 1, n = 2+ mpolyList creates a vectorized function
# whose rows are the evaluated quantities
(ps <- basis_monomials("x", 3))
(f <- as.function(ps))
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(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(.50) # 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)
f(c(.50, .75)) # the above two as rows
# 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", probs)
mat
Run the code above in your browser using DataLab