R/as.function.mpoly.R
as.function.mpoly.Rd
Transforms an mpoly object into a function which can be evaluated.
# S3 method for mpoly as.function(x, varorder = vars(x), vector = TRUE, silent = FALSE, ..., plus_pad = 1L, times_pad = 1L, squeeze = TRUE)
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 |
#>#> function (.) #> { #> if (length(.) > 1) { #> .[] <- sapply(., f) #> return(.) #> } #> .^2 - 2 * . + 1 #> } #> <environment: 0x7fae3e919a10>f(1)#> [1] 0#> [1] 1.00 0.81 0.64 0.49 0.36 0.25 0.16 0.09 0.04 0.01 0.00 0.01 0.04 0.09 0.16 #> [16] 0.25 0.36 0.49 0.64 0.81 1.00#>#> function (.) #> { #> .[1] + 3 * .[1] * .[2] + .[1] * .[3]^2 #> } #> <environment: 0x7fae3d8fc040>f(1:3) # -> 16#> [1] 16#> [1] 5#>f(1, 2, 3) # -> 16#> [1] 16f(1, 1, 1) # -> 5#> [1] 5#>f(3, 2, 1) # -> 16#> [1] 16f(1, 1, 1) # -> 5#> [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)#> [1] 1 4 9 16 25 36 49 64 81 100#> [,1] [,2] #> [1,] 1 3 #> [2,] 2 4f(mat)#> [,1] [,2] #> [1,] 1 9 #> [2,] 4 16#> [1] 24#> 2 x - 2 x^2#>#> [1] 0.0000 0.0198 0.0392 0.0582 0.0768 0.0950 0.1128 0.1302 0.1472 0.1638 #> [11] 0.1800 0.1958 0.2112 0.2262 0.2408 0.2550 0.2688 0.2822 0.2952 0.3078 #> [21] 0.3200 0.3318 0.3432 0.3542 0.3648 0.3750 0.3848 0.3942 0.4032 0.4118 #> [31] 0.4200 0.4278 0.4352 0.4422 0.4488 0.4550 0.4608 0.4662 0.4712 0.4758 #> [41] 0.4800 0.4838 0.4872 0.4902 0.4928 0.4950 0.4968 0.4982 0.4992 0.4998 #> [51] 0.5000 0.4998 0.4992 0.4982 0.4968 0.4950 0.4928 0.4902 0.4872 0.4838 #> [61] 0.4800 0.4758 0.4712 0.4662 0.4608 0.4550 0.4488 0.4422 0.4352 0.4278 #> [71] 0.4200 0.4118 0.4032 0.3942 0.3848 0.3750 0.3648 0.3542 0.3432 0.3318 #> [81] 0.3200 0.3078 0.2952 0.2822 0.2688 0.2550 0.2408 0.2262 0.2112 0.1958 #> [91] 0.1800 0.1638 0.1472 0.1302 0.1128 0.0950 0.0768 0.0582 0.0392 0.0198 #> [101] 0.0000#>#>#> function (.) #> { #> .[1] + .[2] #> } #> <environment: 0x7fae3f531830>#>#> function (.) #> { #> .[1] + .[2] #> } #> <environment: 0x7fae3e41d5e8>