R:深拷贝函数参数 [英] R: deep copy a function argument
问题描述
考虑以下code
I = 3
J =
我= 4#J 1 = I
不过,我要的是
I = 3
F&下; - 功能(t,J =ⅰ)
X *Ĵ
I = 4
F(4)#16,但我希望它是12
如果你想知道为什么我想这样做,你可以考虑这个code - 应用程序是一个多递减模型。一个转移矩阵的对角线是该行中的其他递减的总和。我想定义我需要比计算使用这些递减的其他功能递减。在这种情况下,我只需要uxt01和uxt10并从这些欲产生uxt00和uxt11的功能。我想要的东西,扩展到更高的层面。
QXT< - 矩阵(C(uxt00 =功能(T = 0,X = 0)0,
uxt01 =函数(t = 0时,X = 0)0.05,
uxt10 =函数(t = 0时,X = 0)0.07
uxt11 =函数(t = 0时,X = 0)0),2,2,byrow = TRUE)Qxt.diag< - 功能(QXT){
ndecrements< - 长度(QXT [1])
对(序列(1索引,N,N + 1)){#1,4
QXT [指数]< - 功能(T = 0,X = 0,I =指数,N = ndecrements){
行< - 上限(索引/ ndecr)
row.decrements&下; - 序列((行 - 1)* N + 1,(行)* N)
other.decrements< - row.decrements [!这(row.decrements = I]
-sum(不公开(lapply(Qxt.fns [other.decrements],
功能(F)F(T,X))))
}
}
Qxt.fns
}
这可以通过为正式参数分配默认前pression做Ĵ
手动,在创建功能之后:
I< - 3;
F< - 函数(X,J)X *焦耳;
F;
##功能(X,j)的X *Ĵ
甲醛(F);
## $ X
##
##
##附加$ J
##
##
甲醛(F)$ J< - 我;
F;
##功能(t,J = 3)
## X *Ĵ
甲醛(F);
## $ X
##
##
##附加$ J
## [1] 3
##
I< - 4;
F(4);
## [1] 12
这是唯一可能的,因为R是一个真棒语言,为您提供的功能,所有这三个特殊的性质,这是完整的读/写访问:
- 的语法树,它包括身体:<一href=\"https://stat.ethz.ch/R-manual/R-devel/library/base/html/body.html\"><$c$c>body()$c$c>.
- 形式参数和它们的默认值(这本身解析树):<一href=\"https://stat.ethz.ch/R-manual/R-devel/library/base/html/formals.html\"><$c$c>formals()$c$c>.
- 的封闭环境(这是用来实现闭包):<一href=\"https://stat.ethz.ch/R-manual/R-devel/library/base/html/environment.html\"><$c$c>environment()$c$c>.
Consider the following code
i = 3
j = i
i = 4 # j != i
However, what I want is
i = 3
f <- function(x, j=i)
x * j
i = 4
f(4) # 16, but i want it to be 12
In case you are wondering why I want to do this you could consider this code - the application is a multiple decrements model. The diagonals of a transition matrix are the sum of the other decrements in that row. I would like to define the decrements I need than calculate the other functions using those decrements. In this case, I only need uxt01 and uxt10 and from these I want to produce the functions uxt00 and uxt11. I wanted something that scales to higher dimensions.
Qxt <- matrix(c(uxt00=function(t=0,x=0) 0,
uxt01=function(t=0,x=0) 0.05,
uxt10=function(t=0,x=0) 0.07
uxt11=function(t=0,x=0) 0), 2, 2, byrow=TRUE)
Qxt.diag <- function(Qxt) {
ndecrements <- length(Qxt[1,])
for(index in seq(1, N, N+1)) { # 1, 4
Qxt[[index]] <- function(t=0, x=0, i=index, N=ndecrements) {
row <- ceiling(index/ndecr)
row.decrements <- seq( (row - 1)*N + 1, (row)*N)
other.decrements <- row.decrements[which(row.decrements != i]
-sum(unlist(lapply(Qxt.fns[[other.decrements]],
function(f) f(t,x))))
}
}
Qxt.fns
}
This can be done by assigning the default expression for the formal parameter j
manually, after creating the function:
i <- 3;
f <- function(x,j) x*j;
f;
## function(x,j) x*j
formals(f);
## $x
##
##
## $j
##
##
formals(f)$j <- i;
f;
## function (x, j = 3)
## x * j
formals(f);
## $x
##
##
## $j
## [1] 3
##
i <- 4;
f(4);
## [1] 12
This is only possible because R is an awesome language and provides you complete read/write access to all three special properties of functions, which are:
- The parse tree that comprises the body:
body()
. - The formal parameters and their default values (which are themselves parse trees):
formals()
. - The enclosing environment (which is used to implement closures):
environment()
.
这篇关于R:深拷贝函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!