Skip to content

Commit

Permalink
use sincos in rules for sin and cos (#296)
Browse files Browse the repository at this point in the history
* use `sincos` in rules for `sin` and `cos`

* add rules for `sin` and `cos` to fastmath_able

* conjugte the rrule derivative values but not the frule derivatives
  • Loading branch information
gxyd committed Nov 2, 2020
1 parent 13b11bd commit 43d4a3d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "0.7.31"
version = "0.7.32"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
29 changes: 27 additions & 2 deletions src/rulesets/Base/fastmath_able.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ let
# e.g. do not add `foo` unless `Base.FastMath.foo_fast` exists.
fastable_ast = quote
# Trig-Basics
@scalar_rule cos(x) -(sin(x))
@scalar_rule sin(x) cos(x)
## use `sincos` to compute `sin` and `cos` at the same time
## for the rules for `sin` and `cos`
## See issue: https://github.com/JuliaDiff/ChainRules.jl/issues/291
## sin
function rrule(::typeof(sin), x::Number)
sinx, cosx = sincos(x)
sin_pullback(Δy) = (NO_FIELDS, cosx' * Δy)
return (sinx, sin_pullback)
end

function frule((_, Δx), ::typeof(sin), x::Number)
sinx, cosx = sincos(x)
return (sinx, cosx * Δx)
end

## cos
function rrule(::typeof(cos), x::Number)
sinx, cosx = sincos(x)
cos_pullback(Δy) = (NO_FIELDS, -sinx' * Δy)
return (cosx, cos_pullback)
end

function frule((_, Δx), ::typeof(cos), x::Number)
sinx, cosx = sincos(x)
return (cosx, -sinx * Δx)
end

@scalar_rule tan(x) 1 + Ω ^ 2


Expand Down

2 comments on commit 43d4a3d

@oxinabox
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/24044

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.32 -m "<description of version>" 43d4a3d2544065f461ff3ba5a79d82dd45de60d0
git push origin v0.7.32

Please sign in to comment.