-
Notifications
You must be signed in to change notification settings - Fork 36
Fixes return-types for multiple rules #55
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
Conversation
|
Thanks for fixing these. As a matter of style, I prefer to avoid explicit uses of |
src/rules.jl
Outdated
| @define_diffrule SpecialFunctions.erfi(x) = :( (2 / sqrt(oftype($x, π))) * exp($x * $x) ) | ||
| @define_diffrule SpecialFunctions.erfcx(x) = | ||
| :( (2 * $x * SpecialFunctions.erfcx($x)) - (2 / sqrt(π)) ) | ||
| :( (2 * $x * SpecialFunctions.erfcx($x)) - (2 / sqrt(oftype($x, π))) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could fail for integer inputs (unless there is some promotion before the rules are hit). Generally, it's safer to use "output" variables for the promotion so it would be something like
y = 2 * $x * SpecialFunctions.erfcx($x))
return y - oftype(y, 2)/πThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay here; been a busy couple of months and so this was pushed quite far down my priority list.
But yeah, this is a good point. Is the best solution then to go through all of the rules and manually make these changes, or is there a better way?
src/rules.jl
Outdated
| @define_diffrule Base.cotd(x) = :( -(π / 180) * (1 + cotd($x)^2) ) | ||
| @define_diffrule Base.sinpi(x) = :( π * cospi($x) ) | ||
| @define_diffrule Base.cospi(x) = :( -π * sinpi($x) ) | ||
| @define_diffrule Base.sind(x) = :( (oftype($x, π) / 180) * cosd($x) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be concerned what happens if x is a substantial expression? This might evaluate it twice, which could be avoided by something like
@gensym z
@define_diffrule Base.sind(x) = :( $z=$x; (oftype($z, π) / 180) * cosd($z) )
I see that a few existing rules do already violate this, e.g. with exp(-$x * $x).
It's possible that @define_diffrule could be extended to take care of this. It's also possible that every downstream use calls CSE which will clean this up anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware that x could potentially be an non-evaluated expression; if so, that's for sure an issue, but maybe a separate one from this PR?
|
Made some updates. There are a couple of things that:
|
|
Actually, would it be better if I just added the constants needed, e.g. copy-paste those we need from https://github.com/JuliaStats/StatsFuns.jl/blob/master/src/constants.jl ? That would solve almost all the promotion-bugs:) |
|
An update here: am currently trying to convince people that it's a good idea to make a MathConstants.jl package so that we can avoid explicit conversions here, in SpecialFunctions.jl, etc. |
|
I added the necessary irrationals to simplify the type-promotion. If you're happy to depend on IrrationalConstants.jl, we can make use of irrationals defined there instead:) |
|
Now that packages have started using IrrationalConstants.jl, maybe this is ready for a proper consideration? @andreasnoack @mcabbott |
Codecov Report
@@ Coverage Diff @@
## master #55 +/- ##
=======================================
Coverage 96.96% 96.96%
=======================================
Files 3 3
Lines 165 165
=======================================
Hits 160 160
Misses 5 5
Continue to review full report at Codecov.
|
devmotion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, only some minor questions/suggestions left 🙂
Co-authored-by: David Widmann <[email protected]>
Co-authored-by: David Widmann <[email protected]>
|
Btw, seems like the integration tests for Symbolics.jl are broken. |
Yeah, seems it's a Symbolics issue, the same errors show up on their master branch: https://github.com/JuliaSymbolics/Symbolics.jl/runs/4975659103?check_suite_focus=true |
devmotion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @torfjelde!
Motivation
Currently, almost all of the rules using
πcurrently has a return-type that differs from the input, e.g.SpecialFunctions.erfcxuses√πand soSpecialFunctions.erfcx(1f0)results inFloat64rather thanFloat32.Aim
This PR fixes these issue by replacing all usages of
πwithoftype($x, π). This is a rather naive and quick-fix.Up-for-discussion
√πis replaced byoftype($x, √π)instead of√oftype($x, π), but I don't know enough about LLVM to know if this makes a difference such simple computations, hence I just went with naive conversion ofπdirectly.