-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax type annotations in Jacobian output parsing (#217)
* Move output parsing into separate file * Rename functions * Loosen type restrictions * Add tests
- Loading branch information
Showing
5 changed files
with
151 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "SparseConnectivityTracer" | ||
uuid = "9f842d2f-2579-4b1d-911e-f412cf18a3f5" | ||
authors = ["Adrian Hill <[email protected]>"] | ||
version = "0.6.8" | ||
version = "0.6.9-DEV" | ||
|
||
[deps] | ||
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#= Parse tracers from function evaluation in `src/trace_functions.jl` into an output matrix. =# | ||
|
||
_tracer_or_number(x::Real) = x | ||
_tracer_or_number(d::Dual) = tracer(d) | ||
|
||
#==========# | ||
# Jacobian # | ||
#==========# | ||
|
||
function jacobian_tracers_to_matrix( | ||
xt::AbstractArray{T}, yt::AbstractArray | ||
) where {T<:GradientTracer} | ||
n, m = length(xt), length(yt) | ||
I = Int[] # row indices | ||
J = Int[] # column indices | ||
V = Bool[] # values | ||
for (i, y) in enumerate(yt) | ||
if y isa T && !isemptytracer(y) | ||
for j in gradient(y) | ||
push!(I, i) | ||
push!(J, j) | ||
push!(V, true) | ||
end | ||
end | ||
end | ||
return sparse(I, J, V, m, n) | ||
end | ||
|
||
function jacobian_tracers_to_matrix( | ||
xt::AbstractArray{D}, yt::AbstractArray | ||
) where {P,T<:GradientTracer,D<:Dual{P,T}} | ||
return jacobian_tracers_to_matrix(tracer.(xt), _tracer_or_number.(yt)) | ||
end | ||
|
||
#=========# | ||
# Hessian # | ||
#=========# | ||
|
||
function hessian_tracers_to_matrix(xt::AbstractArray{T}, yt::T) where {T<:HessianTracer} | ||
n = length(xt) | ||
I = Int[] # row indices | ||
J = Int[] # column indices | ||
V = Bool[] # values | ||
|
||
if !isemptytracer(yt) | ||
for (i, j) in tuple_set(hessian(yt)) | ||
push!(I, i) | ||
push!(J, j) | ||
push!(V, true) | ||
# TODO: return `Symmetric` instead on next breaking release | ||
push!(I, j) | ||
push!(J, i) | ||
push!(V, true) | ||
end | ||
end | ||
h = sparse(I, J, V, n, n) | ||
return h | ||
end | ||
|
||
function hessian_tracers_to_matrix( | ||
xt::AbstractArray{D1}, yt::D2 | ||
) where {P1,P2,T<:HessianTracer,D1<:Dual{P1,T},D2<:Dual{P2,T}} | ||
return hessian_tracers_to_matrix(tracer.(xt), tracer(yt)) | ||
end | ||
|
||
function hessian_tracers_to_matrix( | ||
xt::AbstractArray{T}, yt::Number | ||
) where {T<:HessianTracer} | ||
return hessian_tracers_to_matrix(xt, myempty(T)) | ||
end | ||
|
||
function hessian_tracers_to_matrix( | ||
xt::AbstractArray{D1}, yt::Number | ||
) where {P1,T<:HessianTracer,D1<:Dual{P1,T}} | ||
return hessian_tracers_to_matrix(tracer.(xt), myempty(T)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters