Skip to content

Commit

Permalink
loosen ForwardDiffResult restrictions to allow for constant Real retu…
Browse files Browse the repository at this point in the history
…rn values; fixes #71
  • Loading branch information
jrevels committed Dec 7, 2015
1 parent 32ae0d8 commit decce46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/api/ForwardDiffResult.jl
@@ -1,10 +1,10 @@
# This file contains methods to handle the results
# of ForwardDiff calculations (generally either
# ForwardDiffNumbers or Arrays of ForwardDiffNumbers.
# of ForwardDiff calculations (generally either
# ForwardDiffNumbers or Arrays of ForwardDiffNumbers.

immutable ForwardDiffResult{T}
data::T
ForwardDiffResult(data::ForwardDiffNumber) = new(data)
ForwardDiffResult(data::Real) = new(data)
ForwardDiffResult(data::Array) = new(data)
end

Expand All @@ -31,6 +31,7 @@ function get_value!(output::Array, arr::Array)
end

get_value{F}(arr::Array{F}) = _load_value!(similar(arr, eltype(F)), arr)
get_value(n::Real) = n
get_value(n::ForwardDiffNumber) = value(n)

###############
Expand All @@ -52,6 +53,7 @@ function get_derivative!(output::Array, arr::Array)
end

get_derivative{F}(arr::Array{F}) = _load_derivative!(similar(arr, eltype(F)), arr)
get_derivative(n::Real) = zero(n)
get_derivative(n::ForwardDiffNumber{1}) = first(grad(n))

#############
Expand Down Expand Up @@ -147,7 +149,7 @@ function _load_tensor!{N}(output, n::ForwardDiffNumber{N})
for k in 1:j
@inbounds output[j, k, i] = output[i, j, k]
end
end
end
for j in i:N
for k in 1:(i-1)
@inbounds output[j, k, i] = output[i, j, k]
Expand Down
12 changes: 12 additions & 0 deletions test/test_behaviors.jl
Expand Up @@ -36,3 +36,15 @@ g = ForwardDiff.gradient(f) # gradient in vector mode
j = x -> ForwardDiff.jacobian(g, x, chunk_size=2)/2 # jacobian in chunk_mode

@test_approx_eq ForwardDiff.hessian(f, x) 2*j(x)

#####################
# Conversion Issues #
#####################

# Target function returns a literal (Issue #71) #
#-----------------------------------------------#

@test ForwardDiff.derivative(x->zero(x), rand()) == ForwardDiff.derivative(x->1.0, rand())
@test ForwardDiff.gradient(x->zero(x[1]), [rand()]) == ForwardDiff.gradient(x->1.0, [rand()])
@test ForwardDiff.hessian(x->zero(x[1]), [rand()]) == ForwardDiff.hessian(x->1.0, [rand()])
@test ForwardDiff.jacobian(x->[zero(x[1])], [rand()]) == ForwardDiff.jacobian(x->[1.0], [rand()])

0 comments on commit decce46

Please sign in to comment.