Skip to content

Drop v0.4 support #195

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

Merged
merged 5 commits into from
Feb 14, 2017
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
11 changes: 3 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
language: julia
julia:
- 0.4
- 0.5
- nightly
notifications:
email: false
sudo: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- if (julia -e 'VERSION < v"0.5" && exit(1)'); then
julia -e 'include(joinpath(JULIA_HOME, Base.DATAROOTDIR, "julia", "build_sysimg.jl")); build_sysimg(force=true)';
julia -e 'Pkg.clone(pwd()); Pkg.build("ForwardDiff"); Pkg.test("ForwardDiff"; coverage=true)';
julia -O3 -e 'include(joinpath(Pkg.dir("ForwardDiff"), "test/SIMDTest.jl"))';
else
julia -e 'Pkg.clone(pwd()); Pkg.build("ForwardDiff"); Pkg.test("ForwardDiff"; coverage=true)';
fi
- julia -e 'include(joinpath(JULIA_HOME, Base.DATAROOTDIR, "julia", "build_sysimg.jl")); build_sysimg(force=true)';
- julia -e 'Pkg.clone(pwd()); Pkg.build("ForwardDiff"); Pkg.test("ForwardDiff"; coverage=true)';
- julia -O3 -e 'include(joinpath(Pkg.dir("ForwardDiff"), "test/SIMDTest.jl"))';
after_success:
- julia -e 'cd(Pkg.dir("ForwardDiff")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.4
julia 0.5
DiffBase 0.0.3
Compat 0.8.6
Calculus 0.2.0
Expand Down
8 changes: 4 additions & 4 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ for Config in (:GradientConfig, :JacobianConfig)
# when predicting the final output type of API functions.
$Config(x::AbstractArray) = $Config{pickchunksize(length(x))}(x)

@compat (::Type{$Config{N}}){N,T}(x::AbstractArray{T}) = begin
function (::Type{$Config{N}}){N,T}(x::AbstractArray{T})
seeds = construct_seeds(Partials{N,T})
duals = similar(x, Dual{N,T})
return $Config{N,T,typeof(duals)}(seeds, duals)
Expand All @@ -39,7 +39,7 @@ end

JacobianConfig(y::AbstractArray, x::AbstractArray) = JacobianConfig{pickchunksize(length(x))}(y, x)

@compat (::Type{JacobianConfig{N}}){N,Y,X}(y::AbstractArray{Y}, x::AbstractArray{X}) = begin
function (::Type{JacobianConfig{N}}){N,Y,X}(y::AbstractArray{Y}, x::AbstractArray{X})
seeds = construct_seeds(Partials{N,X})
yduals = similar(y, Dual{N,Y})
xduals = similar(x, Dual{N,X})
Expand All @@ -59,13 +59,13 @@ end
HessianConfig(x::AbstractArray) = HessianConfig{pickchunksize(length(x))}(x)
HessianConfig(out, x::AbstractArray) = HessianConfig{pickchunksize(length(x))}(out, x)

@compat (::Type{HessianConfig{N}}){N}(x::AbstractArray) = begin
function (::Type{HessianConfig{N}}){N}(x::AbstractArray)
jacobian_config = JacobianConfig{N}(x)
gradient_config = GradientConfig{N}(jacobian_config.duals)
return HessianConfig(gradient_config, jacobian_config)
end

@compat (::Type{HessianConfig{N}}){N}(out::DiffResult, x::AbstractArray) = begin
function (::Type{HessianConfig{N}}){N}(out::DiffResult, x::AbstractArray)
jacobian_config = JacobianConfig{N}(DiffBase.gradient(out), x)
yduals, xduals = jacobian_config.duals
gradient_config = GradientConfig{N}(xduals)
Expand Down
50 changes: 25 additions & 25 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ end
isconstant(n::Dual) = iszero(partials(n))

@ambiguous Base.isequal{N}(a::Dual{N}, b::Dual{N}) = isequal(value(a), value(b))
@ambiguous @compat(Base.:(==)){N}(a::Dual{N}, b::Dual{N}) = value(a) == value(b)
@ambiguous Base.:(==){N}(a::Dual{N}, b::Dual{N}) = value(a) == value(b)
@ambiguous Base.isless{N}(a::Dual{N}, b::Dual{N}) = value(a) < value(b)
@ambiguous @compat(Base.:<){N}(a::Dual{N}, b::Dual{N}) = isless(a, b)
@ambiguous @compat(Base.:(<=)){N}(a::Dual{N}, b::Dual{N}) = <=(value(a), value(b))
@ambiguous Base.:<{N}(a::Dual{N}, b::Dual{N}) = isless(a, b)
@ambiguous Base.:(<=){N}(a::Dual{N}, b::Dual{N}) = <=(value(a), value(b))

for T in (AbstractFloat, Irrational, Real)
Base.isequal(n::Dual, x::T) = isequal(value(n), x)
Base.isequal(x::T, n::Dual) = isequal(n, x)

@compat(Base.:(==))(n::Dual, x::T) = (value(n) == x)
@compat(Base.:(==))(x::T, n::Dual) = ==(n, x)
Base.:(==)(n::Dual, x::T) = (value(n) == x)
Base.:(==)(x::T, n::Dual) = ==(n, x)

Base.isless(n::Dual, x::T) = value(n) < x
Base.isless(x::T, n::Dual) = x < value(n)

@compat(Base.:<)(n::Dual, x::T) = isless(n, x)
@compat(Base.:<)(x::T, n::Dual) = isless(x, n)
Base.:<(n::Dual, x::T) = isless(n, x)
Base.:<(x::T, n::Dual) = isless(x, n)

@compat(Base.:(<=))(n::Dual, x::T) = <=(value(n), x)
@compat(Base.:(<=))(x::T, n::Dual) = <=(x, value(n))
Base.:(<=)(n::Dual, x::T) = <=(value(n), x)
Base.:(<=)(x::T, n::Dual) = <=(x, value(n))
end

Base.isnan(n::Dual) = isnan(value(n))
Expand Down Expand Up @@ -210,49 +210,49 @@ Base.float{N,T}(n::Dual{N,T}) = Dual{N,promote_type(T, Float16)}(n)
# Addition/Subtraction #
#----------------------#

@ambiguous @inline @compat(Base.:+){N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) + value(n2), partials(n1) + partials(n2))
@ambiguous @inline @compat(Base.:+)(n::Dual, x::Real) = Dual(value(n) + x, partials(n))
@ambiguous @inline @compat(Base.:+)(x::Real, n::Dual) = n + x
@ambiguous @inline Base.:+{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) + value(n2), partials(n1) + partials(n2))
@ambiguous @inline Base.:+(n::Dual, x::Real) = Dual(value(n) + x, partials(n))
@ambiguous @inline Base.:+(x::Real, n::Dual) = n + x

@ambiguous @inline @compat(Base.:-){N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) - value(n2), partials(n1) - partials(n2))
@ambiguous @inline @compat(Base.:-)(n::Dual, x::Real) = Dual(value(n) - x, partials(n))
@ambiguous @inline @compat(Base.:-)(x::Real, n::Dual) = Dual(x - value(n), -(partials(n)))
@inline @compat(Base.:-)(n::Dual) = Dual(-(value(n)), -(partials(n)))
@ambiguous @inline Base.:-{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) - value(n2), partials(n1) - partials(n2))
@ambiguous @inline Base.:-(n::Dual, x::Real) = Dual(value(n) - x, partials(n))
@ambiguous @inline Base.:-(x::Real, n::Dual) = Dual(x - value(n), -(partials(n)))
@inline Base.:-(n::Dual) = Dual(-(value(n)), -(partials(n)))

# Multiplication #
#----------------#

@inline @compat(Base.:*)(n::Dual, x::Bool) = x ? n : (signbit(value(n))==0 ? zero(n) : -zero(n))
@inline @compat(Base.:*)(x::Bool, n::Dual) = n * x
@inline Base.:*(n::Dual, x::Bool) = x ? n : (signbit(value(n))==0 ? zero(n) : -zero(n))
@inline Base.:*(x::Bool, n::Dual) = n * x

@ambiguous @inline function @compat(Base.:*){N}(n1::Dual{N}, n2::Dual{N})
@ambiguous @inline function Base.:*{N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
return Dual(v1 * v2, _mul_partials(partials(n1), partials(n2), v2, v1))
end

@ambiguous @inline @compat(Base.:*)(n::Dual, x::Real) = Dual(value(n) * x, partials(n) * x)
@ambiguous @inline @compat(Base.:*)(x::Real, n::Dual) = n * x
@ambiguous @inline Base.:*(n::Dual, x::Real) = Dual(value(n) * x, partials(n) * x)
@ambiguous @inline Base.:*(x::Real, n::Dual) = n * x

# Division #
#----------#

@ambiguous @inline function @compat(Base.:/){N}(n1::Dual{N}, n2::Dual{N})
@ambiguous @inline function Base.:/{N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
return Dual(v1 / v2, _div_partials(partials(n1), partials(n2), v1, v2))
end

@ambiguous @inline function @compat(Base.:/)(x::Real, n::Dual)
@ambiguous @inline function Base.:/(x::Real, n::Dual)
v = value(n)
divv = x / v
return Dual(divv, -(divv / v) * partials(n))
end

@ambiguous @inline @compat(Base.:/)(n::Dual, x::Real) = Dual(value(n) / x, partials(n) / x)
@ambiguous @inline Base.:/(n::Dual, x::Real) = Dual(value(n) / x, partials(n) / x)

# Exponentiation #
#----------------#

for f in (macroexpand(:(@compat(Base.:^))), :(NaNMath.pow))
for f in (:(Base.:^), :(NaNMath.pow))
@eval begin
@ambiguous @inline function ($f){N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
Expand Down
30 changes: 15 additions & 15 deletions src/partials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Base.linearindexing(::Partials) = Base.LinearFast()
@inline Base.rand{N,T}(rng::AbstractRNG, ::Type{Partials{N,T}}) = Partials{N,T}(rand_tuple(rng, NTuple{N,T}))

Base.isequal{N}(a::Partials{N}, b::Partials{N}) = isequal(a.values, b.values)
@compat(Base.:(==)){N}(a::Partials{N}, b::Partials{N}) = a.values == b.values
Base.:(==){N}(a::Partials{N}, b::Partials{N}) = a.values == b.values

const PARTIALS_HASH = hash(Partials)

Expand Down Expand Up @@ -72,10 +72,10 @@ Base.convert{N,T}(::Type{Partials{N,T}}, partials::Partials{N,T}) = partials
# Arithmetic Functions #
########################

@inline @compat(Base.:+){N}(a::Partials{N}, b::Partials{N}) = Partials(add_tuples(a.values, b.values))
@inline @compat(Base.:-){N}(a::Partials{N}, b::Partials{N}) = Partials(sub_tuples(a.values, b.values))
@inline @compat(Base.:-)(partials::Partials) = Partials(minus_tuple(partials.values))
@inline @compat(Base.:*)(x::Real, partials::Partials) = partials*x
@inline Base.:+{N}(a::Partials{N}, b::Partials{N}) = Partials(add_tuples(a.values, b.values))
@inline Base.:-{N}(a::Partials{N}, b::Partials{N}) = Partials(sub_tuples(a.values, b.values))
@inline Base.:-(partials::Partials) = Partials(minus_tuple(partials.values))
@inline Base.:*(x::Real, partials::Partials) = partials*x

@inline function _div_partials(a::Partials, b::Partials, aval, bval)
return _mul_partials(a, b, inv(bval), -(aval / (bval*bval)))
Expand All @@ -85,12 +85,12 @@ end
#----------------------#

if NANSAFE_MODE_ENABLED
@inline function @compat(Base.:*)(partials::Partials, x::Real)
@inline function Base.:*(partials::Partials, x::Real)
x = ifelse(!isfinite(x) && iszero(partials), one(x), x)
return Partials(scale_tuple(partials.values, x))
end

@inline function @compat(Base.:/)(partials::Partials, x::Real)
@inline function Base.:/(partials::Partials, x::Real)
x = ifelse(x == zero(x) && iszero(partials), one(x), x)
return Partials(div_tuple_by_scalar(partials.values, x))
end
Expand All @@ -101,11 +101,11 @@ if NANSAFE_MODE_ENABLED
return Partials(mul_tuples(a.values, b.values, x_a, x_b))
end
else
@inline function @compat(Base.:*)(partials::Partials, x::Real)
@inline function Base.:*(partials::Partials, x::Real)
return Partials(scale_tuple(partials.values, x))
end

@inline function @compat(Base.:/)(partials::Partials, x::Real)
@inline function Base.:/(partials::Partials, x::Real)
return Partials(div_tuple_by_scalar(partials.values, x))
end

Expand All @@ -117,12 +117,12 @@ end
# edge cases where N == 0 #
#-------------------------#

@inline @compat(Base.:+){A,B}(a::Partials{0,A}, b::Partials{0,B}) = Partials{0,promote_type(A,B)}(tuple())
@inline @compat(Base.:-){A,B}(a::Partials{0,A}, b::Partials{0,B}) = Partials{0,promote_type(A,B)}(tuple())
@inline @compat(Base.:-){T}(partials::Partials{0,T}) = partials
@inline @compat(Base.:*){T}(partials::Partials{0,T}, x::Real) = Partials{0,promote_type(T,typeof(x))}(tuple())
@inline @compat(Base.:*){T}(x::Real, partials::Partials{0,T}) = Partials{0,promote_type(T,typeof(x))}(tuple())
@inline @compat(Base.:/){T}(partials::Partials{0,T}, x::Real) = Partials{0,promote_type(T,typeof(x))}(tuple())
@inline Base.:+{A,B}(a::Partials{0,A}, b::Partials{0,B}) = Partials{0,promote_type(A,B)}(tuple())
@inline Base.:-{A,B}(a::Partials{0,A}, b::Partials{0,B}) = Partials{0,promote_type(A,B)}(tuple())
@inline Base.:-{T}(partials::Partials{0,T}) = partials
@inline Base.:*{T}(partials::Partials{0,T}, x::Real) = Partials{0,promote_type(T,typeof(x))}(tuple())
@inline Base.:*{T}(x::Real, partials::Partials{0,T}) = Partials{0,promote_type(T,typeof(x))}(tuple())
@inline Base.:/{T}(partials::Partials{0,T}, x::Real) = Partials{0,promote_type(T,typeof(x))}(tuple())

@inline _mul_partials{A,B}(a::Partials{0,A}, b::Partials{0,B}, afactor, bfactor) = Partials{0,promote_type(A,B)}(tuple())
@inline _div_partials{A,B}(a::Partials{0,A}, b::Partials{0,B}, afactor, bfactor) = Partials{0,promote_type(A,B)}(tuple())
Expand Down