Skip to content

Commit f9b6aa9

Browse files
added scalar_rule for copysign and tests.
1 parent 262be3c commit f9b6aa9

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Development/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
3+
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"

src/rulesets/Base/fastmath_able.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ let
184184
@scalar_rule max(x, y) @setup(gt = x > y) (gt, !gt)
185185
@scalar_rule min(x, y) @setup(gt = x > y) (!gt, gt)
186186

187+
@scalar_rule copysign(y, x) (ifelse(signbit(x)!=signbit(y), -one(y), +one(y)), NoTangent())
188+
187189
# Unary functions
188190
@scalar_rule +x true
189191
@scalar_rule -x -1

test/rulesets/Base/fastmath_able.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ const FASTABLE_AST = quote
192192
end
193193
end
194194

195+
@testset "copysign" begin
196+
# don't go too close to zero as the numerics may jump over it yielding wrong results
197+
@testset "at $y" for y in (-1.1, 0.1, 100.0)
198+
@testset "at $x" for x in (-1.1, -0.1, 33.0)
199+
test_frule(copysign, y, x)
200+
test_rrule(copysign, y, x)
201+
end
202+
end
203+
end
204+
195205
@testset "sign" begin
196206
@testset "real" begin
197207
@testset "at $x" for x in (-1.1, -1.1, 0.5, 100.0)

0 commit comments

Comments
 (0)