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

allow AbstractVector input for gradients #103

Merged
merged 1 commit into from
Feb 10, 2016
Merged
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
16 changes: 8 additions & 8 deletions src/api/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

# Exposed API methods #
#---------------------#
@generated function gradient!{T,A}(output::Vector{T}, f, x::Vector, ::Type{A}=Void;
@generated function gradient!{T,A}(output::AbstractVector{T}, f, x::AbstractVector, ::Type{A}=Void;
chunk_size::Int=default_chunk_size,
cache::ForwardDiffCache=dummy_cache)
if A <: Void
return_stmt = :(gradient!(output, result)::Vector{T})
return_stmt = :(gradient!(output, result)::typeof(output))
elseif A <: AllResults
return_stmt = :(gradient!(output, result)::Vector{T}, result)
return_stmt = :(gradient!(output, result)::typeof(output), result)
else
error("invalid argument $A passed to FowardDiff.gradient")
end
Expand All @@ -21,7 +21,7 @@
end
end

@generated function gradient{T,A}(f, x::Vector{T}, ::Type{A}=Void;
@generated function gradient{T,A}(f, x::AbstractVector{T}, ::Type{A}=Void;
chunk_size::Int=default_chunk_size,
cache::ForwardDiffCache=dummy_cache)
if A <: Void
Expand All @@ -43,14 +43,14 @@ function gradient{A}(f, ::Type{A}=Void;
chunk_size::Int=default_chunk_size,
cache::ForwardDiffCache=ForwardDiffCache())
if mutates
function g!(output::Vector, x::Vector)
function g!(output::AbstractVector, x::AbstractVector)
return ForwardDiff.gradient!(output, f, x, A;
chunk_size=chunk_size,
cache=cache)
end
return g!
else
function g(x::Vector)
function g(x::AbstractVector)
return ForwardDiff.gradient(f, x, A;
chunk_size=chunk_size,
cache=cache)
Expand All @@ -61,15 +61,15 @@ end

# Calculate gradient of a given function #
#----------------------------------------#
function _calc_gradient{S}(f, x::Vector, ::Type{S},
function _calc_gradient{S}(f, x::AbstractVector, ::Type{S},
chunk_size::Int,
cache::ForwardDiffCache)
X = Val{length(x)}
C = Val{chunk_size}
return _calc_gradient(f, x, S, X, C, cache)
end

@generated function _calc_gradient{T,S,xlen,chunk_size}(f, x::Vector{T}, ::Type{S},
@generated function _calc_gradient{T,S,xlen,chunk_size}(f, x::AbstractVector{T}, ::Type{S},
X::Type{Val{xlen}},
C::Type{Val{chunk_size}},
cache::ForwardDiffCache)
Expand Down