Skip to content

Commit

Permalink
more robust abs definition in the case of negatively signed zero, ext…
Browse files Browse the repository at this point in the history
…ended abs/abs2 to all ForwardDiffNumbers
  • Loading branch information
jrevels committed Sep 18, 2015
1 parent d59a0cf commit 320312a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/ForwardDiffNumber.jl
Expand Up @@ -78,6 +78,9 @@ conj(n::ForwardDiffNumber) = n
transpose(n::ForwardDiffNumber) = n
ctranspose(n::ForwardDiffNumber) = n

@inline abs(n::ForwardDiffNumber) = signbit(value(n)) ? -n : n
@inline abs2(n::ForwardDiffNumber) = n*n

# NaNMath Helper Functions #
#--------------------------#
function to_nanmath(x::Expr)
Expand Down
3 changes: 0 additions & 3 deletions src/GradientNumber.jl
Expand Up @@ -210,9 +210,6 @@ end

# Other Functions #
#-----------------#
@inline abs(g::GradientNumber) = (value(g) >= 0) ? g : -g
@inline abs2(g::GradientNumber) = g*g

@inline calc_atan2(y::GradientNumber, x::GradientNumber) = atan2(value(y), value(x))
@inline calc_atan2(y::Real, x::GradientNumber) = atan2(y, value(x))
@inline calc_atan2(y::GradientNumber, x::Real) = atan2(value(y), x)
Expand Down
4 changes: 2 additions & 2 deletions test/test_gradients.jl
Expand Up @@ -175,8 +175,8 @@ for (test_partials, Grad) in ((test_partialstup, ForwardDiff.GradNumTup), (test_
grad_approx_eq(test_grad^rand_val, exp(rand_val * log(test_grad)))
grad_approx_eq(rand_val^test_grad, exp(test_grad * log(rand_val)))

# Univariate functions #
#----------------------#
# Unary functions #
#-----------------#
for (fsym, expr) in Calculus.symbolic_derivatives_1arg()
@eval begin
func = $fsym
Expand Down
8 changes: 6 additions & 2 deletions test/test_hessians.jl
Expand Up @@ -203,15 +203,19 @@ hess_approx_eq(rand_val^test_hess, exp(test_hess * log(rand_val)))

# Special Cases #
#---------------#
@test abs(test_hess) == test_hess
@test abs(-test_hess) == test_hess
hess_approx_eq(abs2(test_hess), test_hess*test_hess)

atan2_hess = atan2(test_hess, rand_hess)
atanyx_hess = atan(test_hess/rand_hess)

@test value(atan2_hess) == atan2(test_val, rand_val)
@test_approx_eq collect(grad(atan2_hess)) collect(grad(atanyx_hess))
@test_approx_eq hess(atan2_hess) hess(atanyx_hess)

# Univariate functions/API usage testing #
#----------------------------------------#
# Unary functions/API usage testing #
#-----------------------------------#
N = 6
testout = Array(Float64, N, N)

Expand Down
8 changes: 6 additions & 2 deletions test/test_tensors.jl
Expand Up @@ -221,6 +221,10 @@ tens_approx_eq(rand_val^test_tens, exp(test_tens * log(rand_val)))

# Special Cases #
#---------------#
@test abs(test_tens) == test_tens
@test abs(-test_tens) == test_tens
tens_approx_eq(abs2(test_tens), test_tens*test_tens)

atan2_tens = atan2(test_tens, rand_tens)
atanyx_tens = atan(test_tens/rand_tens)

Expand All @@ -229,8 +233,8 @@ atanyx_tens = atan(test_tens/rand_tens)
@test_approx_eq hess(atan2_tens) hess(atanyx_tens)
@test_approx_eq tens(atan2_tens) tens(atanyx_tens)

# Univariate functions/API usage testing #
#----------------------------------------#
# Unary functions/API usage testing #
#-----------------------------------#
testout = Array(Float64, N, N, N)

function tens_deriv_ijk(f_expr, x::Vector, i, j, k)
Expand Down

0 comments on commit 320312a

Please sign in to comment.