【发布时间】:2011-12-17 00:16:20
【问题描述】:
我想在一维中执行数值积分,其中被积函数是向量值。 integrate() 只允许标量被积函数,因此我需要多次调用它。 cubature 包似乎很适合,但它似乎对一维积分表现很差。考虑以下示例(标量被积函数和一维积分),
library(cubature)
integrand <- function(x, a=0.01) exp(-x^2/a^2)*cos(x)
Nmax <- 1e3
tolerance <- 1e-4
# using cubature's adaptIntegrate
time1 <- system.time(replicate(1e3, {
a <<- adaptIntegrate(integrand, -1, 1, tol=tolerance, fDim=1, maxEval=Nmax)
}) )
# using integrate
time2 <- system.time(replicate(1e3, {
b <<- integrate(integrand, -1, 1, rel.tol=tolerance, subdivisions=Nmax)
}) )
time1
user system elapsed
2.398 0.004 2.403
time2
user system elapsed
0.204 0.004 0.208
a$integral
> [1] 0.0177241
b$value
> [1] 0.0177241
a$functionEvaluations
> [1] 345
b$subdivisions
> [1] 10
不知何故,adaptIntegrate 似乎正在使用更多的函数评估来实现类似的精度。这两种方法显然都使用了 Gauss-Kronrod 求积法(一维情况:15 点高斯求积法则),尽管?integrate 添加了“Wynn 的 Epsilon 算法”。这能解释巨大的时间差异吗?
我愿意接受有关处理向量值被积函数的替代方法的建议,例如
integrand <- function(x, a = 0.01) c(exp(-x^2/a^2), cos(x))
adaptIntegrate(integrand, -1, 1, tol=tolerance, fDim=2, maxEval=Nmax)
$integral
[1] 0.01772454 1.68294197
$error
[1] 2.034608e-08 1.868441e-14
$functionEvaluations
[1] 345
谢谢。
【问题讨论】:
-
我不明白,对不起;我对标量值被积函数进行的一对一比较有什么问题?
-
我用
fDim=2进行了测试(最后一个例子,345 次评估),比较只是调用integrate两次的情况,str(lapply(c(integrand1, integrand2), integrate, -1,1, rel.tol=tolerance, subdivisions=Nmax))给出 10+1 = 11 次评估。我的观点是,是的,adaptIntegrate的目标是多维积分,并且可以选择向量值被积函数,但是一维积分的情况比反复调用integrate效率低得多,但幅度很大(~30 倍在这里)。 -
我没有,谢谢指点。
-
@Hemmo 是否愿意将其转化为答案并在它被浪费之前获得赏金?