Skip to content

Conversation

@twadleigh
Copy link
Contributor

Addresses #15.

src/rules.jl Outdated
@define_diffrule atan2(x, y) = :($y / ($x*$x + $y*$y)), :(-$x / ($x*$x + $y*$y))
@define_diffrule hypot(x, y) = :($x / hypot($x, $y)), :($y / hypot($x, $y))
@define_diffrule mod(x, y) = :(1), :(-trunc($x / $y))
@define_diffrule rem(x, y) = :(1), :(-trunc($x / $y))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need fixing. mod and rem are not identical. Besides, contrary to my belief following an initial quick study of the cases, these rules don't work for all sign combinations for either function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I was wrong about being wrong. 😄 The rule works for rem, but trunc needs to be changed to floor for mod.

@twadleigh
Copy link
Contributor Author

The rules for mod and rem given here are valid for the partial derivatives when x/y is not an integer. They are valid on at least one side when y != 0.

Should they be modified to give NaN when x/y is integral? Are there cases where that sort of safeguard ends up being important (say, in optimizers)? My gut says that optimizers are going to have a hard time near discontinuities regardless, though.

@jrevels
Copy link
Contributor

jrevels commented Aug 14, 2017

Should they be modified to give NaN when x/y is integral

I think so, might as well have them be correct. We could change the rule to something like :(z = $x / $y; first(promote(ifelse(isinteger(z), NaN, -floor(z)), NaN))), which avoids a branch and maintains type stability.

Thanks for this!

@twadleigh
Copy link
Contributor Author

Oh, forgot to do the same for the first partial.

@twadleigh
Copy link
Contributor Author

I'll squash shortly.

src/rules.jl Outdated
# mod
# hypot
# atan2
@define_diffrule atan2(x, y) = :($y / ($x*$x + $y*$y)), :(-$x / ($x*$x + $y*$y))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we switch x * x and y * y to x^2 and y^2, just to keep in style with the other rules? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K

@twadleigh twadleigh force-pushed the pull-request/9edaf9ec branch from 59b55ba to 6220c2f Compare August 14, 2017 16:43
@jrevels jrevels merged commit fa19c5a into JuliaAttic:master Aug 14, 2017
@twadleigh twadleigh deleted the pull-request/9edaf9ec branch August 14, 2017 18:39
@jrevels jrevels mentioned this pull request Aug 31, 2017
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 this pull request may close these issues.

2 participants