Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack rule for ifelse #47

Closed
wupeifan opened this issue Mar 31, 2020 · 5 comments · Fixed by #54
Closed

Lack rule for ifelse #47

wupeifan opened this issue Mar 31, 2020 · 5 comments · Fixed by #54

Comments

@wupeifan
Copy link

I was wondering whether a rule for ifelse(condition, exp1, exp2) can be created? Thanks a lot!

@JianghuiDu
Copy link

Most of the times ifelse can be replaced by combinations of max and mix which are included in DiffRules.jl.
Something like this https://exceljet.net/replace-ugly-ifs-with-max-or-min.

@wupeifan
Copy link
Author

wupeifan commented Jul 8, 2020

I raised this issue exactly because I want to avoid max and min combinations, but thanks for your suggestions.

@mcabbott
Copy link
Member

mcabbott commented Jul 8, 2020

Should be simple to add, and worth having IMO.

@wupeifan
Copy link
Author

wupeifan commented Jul 8, 2020

I think it should be something like D(ifelse(condition, exp1, exp2)) = ifelse(condition, D(exp1), D(exp2)). If it is just this simple then I can make a little PR, but I don't know whether there are other potential issues.

@mcabbott
Copy link
Member

mcabbott commented Jul 8, 2020

I was thinking it looks like this, but I'm not sure that false is actually the right thing... in Zygote etc. it would be nothing:

@define_diffrule Base.:ifelse(p, x, y) = :(false), :($p), :(!$p)

Trying this out with a package that uses this (old Zygote, maybe Tracker) would increase my confidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants