gpt4 book ai didi

r - 如何加快测试 R 中数字之间的相等性

转载 作者:行者123 更新时间:2023-12-01 02:23:02 25 4
gpt4 key购买 nike

目前这里是我测试数字相等性的方法,如果 x 有效是一个数字和 y一个向量。

almostEqual <- function(x, y, tolerance=1e-8) {
diff <- abs(x - y)
mag <- pmax( abs(x), abs(y) )
ifelse( mag > tolerance, diff/mag <= tolerance, diff <= tolerance)
}

示例 :
almostEqual(1,c(1,1.00000000000001,1.00002))
[1] TRUE TRUE FALSE

你能让它更快吗(只用基础 R)?

编辑 :
我建议这个我觉得有用
"%~=%" <- almostEqual;
"%~in%" <- function(x,y){ sapply(x,FUN=function(a,b){any(almostEqual(a,b))},y)};

最佳答案

裁剪ifelse首先会为您节省 57%...

almostEqual2 <- function(x, y, tolerance=1e-8) {
diff <- abs(x - y)
mag <- pmax( abs(x), abs(y) )
out <- logical(length(y))
out[ mag > tolerance ] <- (diff/mag <= tolerance)[ mag > tolerance]
out[ ! mag > tolerance ] <- (diff <= tolerance)[! mag > tolerance]
return( out )
}


require(microbenchmark)

set.seed(1)
x <- 1
y <- rnorm(1e6)

bm <- microbenchmark( almostEqual(x,y,tol=0.5) , almostEqual2(x,y,tol=0.5) , times = 25 )
print( bm , digits = 3 , unit = "relative" , order = "median" )
#Unit: relative
# expr min lq median uq max neval
# almostEqual2(x, y, tol = 0.5) 1.00 1.00 1.00 1.00 1.00 25
# almostEqual(x, y, tol = 0.5) 2.09 1.76 1.73 1.86 1.82 25

使用 Rcpp

我不明白为什么你不会在 base 之外使用 CRAN 中最依赖的包,但如果你愿意,你可以实现比我之前的努力(在 OP 上 10 倍)的 5 倍加速,并且它还可以优雅地处理 NA...
#include <Rcpp.h>

using namespace Rcpp;

//[[Rcpp::export]]


LogicalVector all_equalC( double x , NumericVector y , double tolerance ){
NumericVector diff = abs( x - y );
NumericVector mag = pmax( abs(x) , abs(y) );
LogicalVector res = ifelse( mag > tolerance , diff/mag <= tolerance , diff <= tolerance );
return( res );
}

使用 Rcpp::sourceCpp('path/to/file.cpp') 提供.结果...
bm <- microbenchmark( almostEqual(x,y,tol=0.5) , almostEqual2(x,y,tol=0.5) , all_equalC(x,y,tolerance=0.5) , times = 25 )
print( bm , digits = 3 , unit = "relative" , order = "median" )
#Unit: relative
# expr min lq median uq max neval
# all_equalC(x, y, tolerance = 0.5) 1.00 1.00 1.00 1.00 1.00 25
# almostEqual2(x, y, tol = 0.5) 4.50 4.39 5.39 5.24 7.32 25
# almostEqual(x, y, tol = 0.5) 8.69 9.34 9.24 9.96 10.91 25

关于r - 如何加快测试 R 中数字之间的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18851623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com