diff --git a/src/ForwardDiffNumber.jl b/src/ForwardDiffNumber.jl index 29d8af9c..b7166c60 100644 --- a/src/ForwardDiffNumber.jl +++ b/src/ForwardDiffNumber.jl @@ -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) diff --git a/src/GradientNumber.jl b/src/GradientNumber.jl index 11dc5eeb..5aa7e082 100644 --- a/src/GradientNumber.jl +++ b/src/GradientNumber.jl @@ -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) diff --git a/test/test_gradients.jl b/test/test_gradients.jl index aac1521b..a55719aa 100644 --- a/test/test_gradients.jl +++ b/test/test_gradients.jl @@ -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 diff --git a/test/test_hessians.jl b/test/test_hessians.jl index 10c81677..9de7cf29 100644 --- a/test/test_hessians.jl +++ b/test/test_hessians.jl @@ -203,6 +203,10 @@ 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) @@ -210,8 +214,8 @@ atanyx_hess = atan(test_hess/rand_hess) @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) diff --git a/test/test_tensors.jl b/test/test_tensors.jl index 4ecdb530..cfcd20ba 100644 --- a/test/test_tensors.jl +++ b/test/test_tensors.jl @@ -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) @@ -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)