You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use data.table::fifelse to implement a recursive function to calculate greatest common divisors mentioned in this StackOverflow. However, I found that in this case, data.table::fifelse cannot be a replacement for base::ifelse.
Minimal reproducible
# use base::ifelsegcd_base<-function(x,y) {
r<-x%%y;
return(ifelse(r, gcd_base(y, r), y))
}
gcd_dt<-function(x,y) {
r<-x%%y;
return(data.table::fifelse(r, gcd_dt(y, r), y))
}
gcd_base(10, 1)
# [1] 1
gcd_dt(10, 1)
# Error: C stack usage 7971780 is too close to the limit
Base ifelse() evaluates yes and no only if either condition is applicable. This is just a snippet of ifelse which is applicable if the length of test is greater than 1:
ans<-testlen<- length(ans)
ypos<- which(test)
npos<- which(!test)
if (length(ypos) >0L)
ans[ypos] <- rep(yes, length.out=len)[ypos]
if (length(npos) >0L)
ans[npos] <- rep(no, length.out=len)[npos]
For fifelse(), the yes or no appear to be evaluated during the call to the C API - I recompiled the fifelse with Rprintf() at the top of the function and it never printed anything. So in this case, we have a perpetual loop because gcd_dt() will keep calling itself.
We could address it by incorporating some of ifelse() into the fifelse() wrapper when the length of test is 1:
fifelse2=function(test, yes, no, na) {
if (is.atomic(test)) {
if (typeof(test) !="logical")
storage.mode(test) ="logical"if (length(test) ==1L) {
if (is.na(test)) {
if (missing(na))
return (NA)
elsereturn(na)
}
if (test)
return(yes)
elsereturn(no)
} else {
.Call(data.table:::CfifelseR, test, yes, no, na)
}
} else {
stop("Argument 'test' must be logical")
}
}
n_dt2=0gcd_dt2<-function(x,y) {
cat('Recursion level:', n_dt<<-n_dt+1, '\n')
r<-x%%yreturn(fifelse2(r, gcd_dt(y, r), y))
}
gcd_dt2(10, 1)
##Recursion level: 1##[1] 1
Note, if this were implemented, this would not check the type of yes, no, and na, so to allow for a recursive function we would likely miss out on that restriction. Also, for recursive functions that take a vector, we would also get similar errors.
Alternatively we would have to substitute input, pass together with parent.frame to C, unevaluated, which complicates a lot. I don't like to use lazy evaluation when it is not really necessary.
Both issues were closed due to concerns for type stability (e.g., both yes and no need evaluated to make sure they are both integer results). I checked, dplyr::if_else also errors out using this. I did not report up to dplyr due to those recent issues being closed.
Regardless, I believe we have some data.table issues which mention optimizing to automatically use fifelse. This use case would not work if ifelse were optimized within data.table.
I think what should be done in this issue is to document (if not already documented) that fifelse, contrary to base ifelse, evaluates both arguments. Then mention fcase which provides similar functionality (+ ensure examples included). Then add unit test (and ideally examples) for fcase to confirm such usage works fine.
NB. I was just looking for fast vectorized GCD and there doesn't seem anything nice out there. @hongyuanjia you may want to fill FR for that particularly, you will get my upvote, but not guaranteed it to get to in-scope of DT.
@jangorecki, I confirmed that this behavior has not yet been documented. Also, for a fast vectorized GCD, {collapse} has one, i.e. collapse::vgcd() which is implemented in C. However, it did not support interger64 (see fastverse/collapse#318).
Activity
MichaelChirico commentedon Jun 16, 2020
Thanks for the MRE! Not sure why it's recursing so much in the first place...
ColeMiller1 commentedon Jun 17, 2020
Base
ifelse()evaluatesyesandnoonly if either condition is applicable. This is just a snippet ofifelsewhich is applicable if the length oftestis greater than 1:For
fifelse(), theyesornoappear to be evaluated during the call to the C API - I recompiled thefifelsewithRprintf()at the top of the function and it never printed anything. So in this case, we have a perpetual loop becausegcd_dt()will keep calling itself.We could address it by incorporating some of
ifelse()into thefifelse()wrapper when the length oftestis 1:Note, if this were implemented, this would not check the type of
yes,no, andna, so to allow for a recursive function we would likely miss out on that restriction. Also, for recursive functions that take a vector, we would also get similar errors.jangorecki commentedon Jun 17, 2020
Alternatively we would have to substitute input, pass together with parent.frame to C, unevaluated, which complicates a lot. I don't like to use lazy evaluation when it is not really necessary.
ColeMiller1 commentedon Jun 22, 2020
dplyrrecently had two similar issues:tidyverse/dplyr#5341
tidyverse/dplyr#5321
Both issues were closed due to concerns for type stability (e.g., both
yesandnoneed evaluated to make sure they are both integer results). I checked,dplyr::if_elsealso errors out using this. I did not report up todplyrdue to those recent issues being closed.Regardless, I believe we have some
data.tableissues which mention optimizing to automatically usefifelse. This use case would not work ififelsewere optimized withindata.table.jangorecki commentedon Dec 15, 2023
I think what should be done in this issue is to document (if not already documented) that fifelse, contrary to base ifelse, evaluates both arguments. Then mention fcase which provides similar functionality (+ ensure examples included). Then add unit test (and ideally examples) for fcase to confirm such usage works fine.
NB. I was just looking for fast vectorized GCD and there doesn't seem anything nice out there. @hongyuanjia you may want to fill FR for that particularly, you will get my upvote, but not guaranteed it to get to in-scope of DT.
hongyuanjia commentedon Dec 17, 2023
@jangorecki, I confirmed that this behavior has not yet been documented. Also, for a fast vectorized GCD, {collapse} has one, i.e.
collapse::vgcd()which is implemented in C. However, it did not supportinterger64(see fastverse/collapse#318).