Description Usage Arguments See Also Examples
View source: R/as.function.mpolyList.R
Transforms an mpolyList object into a function which can be evaluated.
1 2 3 4 5 6 | ## 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, ...)
|
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 |
plug()
, as.function.mpolyList()
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # 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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.