【问题标题】:What argument to give to the function to return results for more than one value?给函数什么参数以返回多个值的结果?
【发布时间】:2021-10-13 03:25:31
【问题描述】:

我需要模拟使用 isingLenzMC 包中的函数 transitionProbability1D 计算的概率。我想一次模拟 10 个 bF 值并接收结果向量,但仍然只收到一个数字,我不知道为什么。这是我的代码

N <- 100
conf0 <- genConfig1D(N)
conf1 <- flipConfig1D(conf0)


# transition probability at J=H=1/kBT=1.0, 1= p-ty metropolis 2=glauber
bF <- 1:10
J <- h <- rep(1,10)

# HERE IT DOESNT WORK EVEN THOUGHT bF IS A VECTOR 
transitionProbability1D(bF, conf0, conf1, J, h, 1)
>> 0.298615

【问题讨论】:

    标签: r function plot vector


    【解决方案1】:

    您可能想了解如何向量化函数。 在您的示例中,以下内容可能会满足您的期望:

    library(isingLenzMC)
    N <- 100
    conf0 <- genConfig1D(N)
    conf1 <- flipConfig1D(conf0)
    
    
    # transition probability at J=H=1/kBT=1.0, 1= p-ty metropolis 2=glauber
    bF <- 1:10
    # Here I changed these inputs to single values
    J <- h <- 1
    
    # HERE IT DOESNT WORK EVEN THOUGHT bF IS A VECTOR 
    transitionProbability1D(bF, conf0, conf1, J, h, 1)
    # Vectorize on the first argument
    transitionProbability1D_vectorized <- Vectorize(transitionProbability1D, vectorize.args = "ikBT")
    # Now there are as many results as input values
    transitionProbability1D_vectorized(ikBT = bF, x = conf0, xflip = conf1, J = J, H = h, probSel = 1)
    

    您也可以使用 (for) 循环!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2019-07-31
      相关资源
      最近更新 更多