R,while (TRUE) 是如何工作的? [英] R, How does while (TRUE) work?
问题描述
我必须写一个如下方法的函数:
I have to write a function of the following method :
拒收方式(统一信封):
假设 fx 仅在 [a, b] 上为非零,并且 fx ≤ k.
Suppose that fx is non-zero only on [a, b], and fx ≤ k.
生成与 X 无关的 X ∼ U(a, b) 和 Y ∼ U(0, k)(所以 P =(X, Y ) 均匀分布在矩形 [a, b] × [0, k] 上.
Generate X ∼ U(a, b) and Y ∼ U(0, k) independent of X (so P = (X, Y ) is uniformly distributed over the rectangle [a, b] × [0, k]).
如果 Y
If Y < fx(x) then return X, otherwise go back to step 1.
rejectionK <- function(fx, a, b, K) {
# simulates from the pdf fx using the rejection algorithm
# assumes fx is 0 outside [a, b] and bounded by K
# note that we exit the infinite loop using the return statement
while (TRUE) {
x <- runif(1, a, b)
y <- runif(1, 0, K)
if (y < fx(x)) return(x)
}
}
我不明白为什么 while (TRUE)
中的这个 TRUE
是?
I have not understood why is this TRUE
in while (TRUE)
?
如果 (y < fx(x)) 不为真,则该方法建议再次重复循环以再次生成统一数.(y
if (y < fx(x)) is not true then the method suggests to repeat the loop again to generate the uniform number again. (y < fx(x)) is not true=FALSE. So why will not the condition be while (FALSE)
?
我将在哪个基础上再次进入 while 循环?也就是说,我习惯了这个
Again in which basis will i get enter into the while loop ? That is, i am accustomed with this
a=5
while(a<7){
a=a+1
}
这里我在写条件 (a<7) 之前定义了 a .
here i define a before writing the condition (a<7) .
但是在 while (TRUE)
中,哪个语句是正确的?
But in while (TRUE)
, which statement is true ?
另外:
你可以运行代码
rejectionK <- function(fx, a, b, K) {
# simulates from the pdf fx using the rejection algorithm
# assumes fx is 0 outside [a, b] and bounded by K
# note that we exit the infinite loop using the return statement
while (TRUE) {
x <- runif(1, a, b)
y <- runif(1, 0, K)
cat("y=",y,"fx=",fx(x),"",y < fx(x),"
")
if (y < fx(x)) return(x)
}
}
fx<-function(x){
# triangular density
if ((0<x) && (x<1)) {
return(x)
} else if ((1<x) && (x<2)) {
return(2-x)
} else {
return(0)
}
}
set.seed(123)
rejectionK(fx, 0, 2, 1)
推荐答案
这是一个无限循环.只要条件计算结果为 TRUE
,表达式就会执行,它总是会这样做.然而,在表达式中有一个 return
,当调用它时(例如,如果 y
It's an infinite loop. The expression is executed as long as the condition evaluates to TRUE
, which it will always do. However, in the expression there is a return
, which when called (e.g., if y < fx(x)
), breaks out of the function and thus stops the loop.
这是一个更简单的例子:
Here is a simpler example:
fun <- function(n) {
i <- 1
while (TRUE) {
if (i>n) return("stopped") else print(i)
i <- i+1
}
}
fun(3)
#[1] 1
#[1] 2
#[1] 3
#[1] "stopped"
当这个函数被调用时会发生什么?
What happens when this function is called?
i
设置为 1.- 测试
while
循环的条件.因为它是TRUE
,所以它的表达式被评估. - 测试
if
结构的条件.由于它是FALSE
,else
表达式被计算并打印i
. i
加 1.- 重复第 3 步和第 4 步.
- 当
i
达到 4 时,if
构造的条件是TRUE
和return("stopped")
被评估.这将停止整个函数并返回值已停止".
i
is set to 1.- The condition of the
while
loop is tested. Because it isTRUE
, it's expression is evaluated. - The condition of the
if
construct is tested. Since it isFALSE
theelse
expression is evaluated andi
is printed. i
is increased by 1.- Steps 3 and 4 are repeated.
- When
i
reaches the value of 4, the condition of theif
construct isTRUE
andreturn("stopped")
is evaluated. This stops the whole function and returns the value "stopped".
这篇关于R,while (TRUE) 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!