是否可以編寫靈活的函數表達式? 我想使用輸入參數來控制函數的表達式。如何在R中定義靈活的'函數表達式'
例如
input arg -> function
c(1,1) -> func1 = function(x) x+1
c(1,3,2) -> func2 = function(x) x^2+3*x+2
c(6,8,-1) -> func3 = function(x) 6*x^2+8*x-1
是否可以編寫靈活的函數表達式? 我想使用輸入參數來控制函數的表達式。如何在R中定義靈活的'函數表達式'
例如
input arg -> function
c(1,1) -> func1 = function(x) x+1
c(1,3,2) -> func2 = function(x) x^2+3*x+2
c(6,8,-1) -> func3 = function(x) 6*x^2+8*x-1
您可以使用polynom
library(polynom)
as.polynomial(c(2,3,1))
2 + 3*x + x^2
as.polynomial(c(6,8,1)
1 + 8*x + 6*x^2
編輯你當然可以強迫的結果,使用the generic
as.function.polynomial . better here you can use ,
as.polylist`的功能根據係數列表創建許多多項式。例如:
lapply(as.polylist(list(c(2,3,1),c(6,8,1),c(6,8,-1))),
as.function)
[[1]]
function (x)
{
w <- 0
w <- 1 + x * w
w <- 3 + x * w
w <- 2 + x * w
w
}
<environment: 0x00000000113bd778>
[[2]]
function (x)
{
w <- 0
w <- 1 + x * w
w <- 8 + x * w
w <- 6 + x * w
w
}
<environment: 0x0000000011524168>
[[3]]
function (x)
{
w <- 0
w <- -1 + x * w
w <- 8 + x * w
w <- 6 + x * w
w
}
<environment: 0x0000000011527f28>
makepoly <- function(b)
{
p <- rev(seq_along(b) - 1)
function(x)
{
xp <- outer(x, p, '^')
rowSums(xp * rep(b, each=length(x)))
}
}
# x^2 + 2x + 3
f <- makepoly(1:3)
f(0:4)
[1] 3 6 11 18 27
目前尚不清楚你想怎麼一般是從OP。對於多項式的特定情況下,你可以這樣做:
f = function(x, coeffs) {
sum(outer(x, seq_along(coeffs) - 1, `^`) * coeffs)
}
f(2, c(1,2,3)) # 1 + 2*x + 3*x^2, with x = 2
#[1] 17
我看這是使職能的願望,我認爲agstudy/EDDI反應可能會做到這一點,但我想從頭開始嘗試它可能是有益的:
poly.maker <- function(coefs) { func <- function(x){} #empty func in x
body(func) <- parse(text= paste(seq_along(coefs),"*x^",
(length(coefs)-1):0,collapse="+"))
return(func) }
func2 <- poly.maker(c(1,2,3)) # return a function
func2(3) # now test it out
#[1] 18
注意,我需要交換爲了與OP的要求,我只獲得比@dickoa不同的結果後發現同意。這似乎不太笨重:
poly.make2 <- function(coefs) { func <- function(x){}
body(func) <- bquote(sum(.(coefs)*x^.((length(coefs)-1):0)))
return(func) }
func <- poly.make2(c(1,2,5))
func
#function (x)
#sum(c(1, 2, 5) * x^c(2L, 1L, 0L))
#<environment: 0x29023d508>
func(3)
#[1] 20
這是我擔此重任
create_poly <- function(coef)
paste(rev(coef),
paste("x", seq_along(coef) - 1, sep = "^"),
sep = "*", collapse = " + ")
make_polyfun <- function(input) {
myfun <- paste("function(x)", create_poly(input))
eval(parse(text = myfun))
}
隨着例如OP給了我們:
make_polyfun(c(1, 1))
## function(x) 1*x^0 + 1*x^1
## <environment: 0x243a540>
make_polyfun(c(1, 3, 2))
## function(x) 2*x^0 + 3*x^1 + 1*x^2
## <environment: 0x1bd46e0>
make_polyfun(c(6, 8, 1))
## function(x) 1*x^0 + 8*x^1 + 6*x^2
## <environment: 0x22a59c0>
+1;這實際上對問題反應更快,因爲你注意到hte係數的順序是從高到低,而我沒有。 –
@DWin謝謝,但這個級別並不是什麼大事,只是一個約定。順便使用'身體' – dickoa
謝謝你dickoa!這就是我要的。對不起,我的問題在第一天並不清楚。我需要的是一個靈活的「函數表達式」,我可以在其他地方進一步使用,而不是函數。 – user2548246
一個內膽:
polymaker2 <- function(coefs)
{
eval(parse(text=paste0("function(x) sum(x^(",length(coefs)-1,":0) * ",capture.output(dput(coefs)),")")))
}
矢量化形式:
polymaker3 <- function(coefs)
{
eval(parse(text=paste0("function(x) colSums(t(outer(x, ",length(coefs)-1,":0, `^`))*",capture.output(dput(coefs)),")")))
}
似乎相當巴洛克風格。 –
@DWin根據Merrian-Webster的說法:「巴洛克式的:與17世紀流行的藝術表現風格的特徵有關,或具有這種風格的特徵,通常用複雜的形式,大膽的裝飾和對比元素的並置常常表達戲劇,動作和緊張感; 2.以怪誕,奢侈,複雜或華麗爲特徵; 3.不規則形狀「。我喜歡第一個意思:-) –
特別值得一提的是:......,捕獲。輸出(dput(coefs))' –
我想你最後一個例子應該是6x^2 + 8x + 1。 –
是的,只是更正了 – user2548246