Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loosen restriction on input array for cache type assertion #142

Merged
merged 3 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function JacobianCache{N}(x, chunk::Chunk{N})
return JacobianCache{N,T,typeof(duals)}(duals, seeds)
end

@inline jacobian_dual_type{T,M,N}(::Array{T,M}, ::Chunk{N}) = Array{Dual{N,T},M}
@inline jacobian_dual_type{N}(arr, ::Chunk{N}) = Array{Dual{N,eltype(arr)},ndims(arr)}
@inline jacobian_dual_type{T,M,N}(::AbstractArray{T,M}, ::Chunk{N}) = Array{Dual{N,T},M}

Base.copy(cache::JacobianCache) = JacobianCache(copy(cache.duals), cache.seeds)

Expand Down Expand Up @@ -56,7 +57,8 @@ function HessianCache{N}(x, chunk::Chunk{N})
return HessianCache{N,T,typeof(duals)}(duals, inseeds, outseeds)
end

@inline hessian_dual_type{T,M,N}(::Array{T,M}, ::Chunk{N}) = Array{Dual{N,Dual{N,T}},M}
@inline hessian_dual_type{N}(arr, ::Chunk{N}) = Array{Dual{Dual{N,eltype(arr)}},ndims(arr)}
@inline hessian_dual_type{T,M,N}(::AbstractArray{T,M}, ::Chunk{N}) = Array{Dual{N,Dual{N,T}},M}

Base.copy(cache::HessianCache) = HessianCache(copy(cache.duals), cache.inseeds, cache.outseeds)

Expand Down
2 changes: 1 addition & 1 deletion src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function jacobian_chunk_mode_expr(out_definition::Expr, cache_definition::Expr,

$(y_definition)

return out
return reshape(out_reshaped, size(out))
end
end

Expand Down
16 changes: 9 additions & 7 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module MiscTest

import NaNMath

using Compat
using Base.Test
using ForwardDiff

Expand Down Expand Up @@ -72,18 +73,19 @@ testf2 = x -> testdf(x[1]) * f(x[2])
# Higher-Dimensional Differentiation #
######################################

x = rand(3, 3)
x = rand(5, 5)

@test_approx_eq ForwardDiff.jacobian(inv, x) -kron(inv(x'), inv(x))

x = [1, 2, 3]
#########################################
# Differentiation with non-Array inputs #
#########################################

function vector_hessian(f, x)
n = length(x)
out = ForwardDiff.jacobian(x -> ForwardDiff.jacobian(f, x), x)
return reshape(out, n, n)
end
x = rand(5, 5)
jinvx = ForwardDiff.jacobian(inv, x)

@test_approx_eq jinvx ForwardDiff.jacobian(inv, sparse(x))
@test_approx_eq jinvx ForwardDiff.jacobian(inv, Compat.view(x, 1:5, 1:5))

########################
# Conversion/Promotion #
Expand Down