Skip to content

Commit d8339cc

Browse files
simeonschaubnickrobinson251
authored andcommitted
fix abs(::Complex) (#98)
1 parent f2e2c09 commit d8339cc

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/rulesets/Base/base.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
@scalar_rule(adjoint(x), Wirtinger(Zero(), One()))
5353
@scalar_rule(transpose(x), One())
5454

55-
@scalar_rule(abs(x), sign(x))
55+
@scalar_rule(abs(x::Real), sign(x))
56+
@scalar_rule(abs(x::Complex), Wirtinger(x' / 2Ω, x / 2Ω))
57+
@scalar_rule(hypot(x::Real), sign(x))
58+
@scalar_rule(hypot(x::Complex), Wirtinger(x' / 2Ω, x / 2Ω))
5659
@scalar_rule(rem2pi(x, r::RoundingMode), (One(), DNE()))
5760

5861
@scalar_rule(+(x), One())
@@ -81,7 +84,7 @@
8184
@scalar_rule(sech(x), -tanh(x) * Ω)
8285
@scalar_rule(csch(x), -coth(x) * Ω)
8386

84-
@scalar_rule(hypot(x, y), (x / Ω, y / Ω))
87+
@scalar_rule(hypot(x::Real, y::Real), (x / Ω, y / Ω))
8588
@scalar_rule(sincos(x), @setup((sinx, cosx) = Ω), cosx, -sinx)
8689
@scalar_rule(atan(y, x), @setup(u = x^2 + y^2), (x / u, -y / u))
8790

test/rulesets/Base/base.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@
104104
rtol = x isa Complex{Float32} ? 1e-6 : 1e-9
105105
test_scalar(real, x; rtol=rtol)
106106
test_scalar(imag, x; rtol=rtol)
107-
# TODO: implement correct complex derivative
108-
x isa Real && test_scalar(abs, x; rtol=rtol)
107+
108+
test_scalar(abs, x; rtol=rtol)
109+
test_scalar(hypot, x; rtol=rtol)
110+
109111
test_scalar(angle, x; rtol=rtol)
110112
test_scalar(abs2, x; rtol=rtol)
111113
test_scalar(conj, x; rtol=rtol)

0 commit comments

Comments
 (0)