From dc81ca6dd5a625e29b342ffd9818097952095075 Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Tue, 23 Aug 2016 13:09:19 -0400 Subject: [PATCH 1/3] loosen restriction on input array for cache type assertion --- src/cache.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cache.jl b/src/cache.jl index b7ee6bd7..346f8b88 100644 --- a/src/cache.jl +++ b/src/cache.jl @@ -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) @@ -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) From 2a14fe0b372e2d3da172388df5c20f16ac14abb6 Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Tue, 23 Aug 2016 13:50:26 -0400 Subject: [PATCH 2/3] add test for non-Array inputs --- test/MiscTest.jl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/MiscTest.jl b/test/MiscTest.jl index 738c512b..8a00e241 100644 --- a/test/MiscTest.jl +++ b/test/MiscTest.jl @@ -2,6 +2,7 @@ module MiscTest import NaNMath +using Compat using Base.Test using ForwardDiff @@ -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 # From 0fb214f74cae4ab9dd7b62ccfac2862d109f9072 Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Tue, 23 Aug 2016 16:14:51 -0400 Subject: [PATCH 3/3] fix sparse matrix reshape issue on Julia v0.4 --- src/jacobian.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jacobian.jl b/src/jacobian.jl index 3fccce92..51820f34 100644 --- a/src/jacobian.jl +++ b/src/jacobian.jl @@ -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