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

Make duals' operations through steprange use linrange instead #618

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,16 @@ function LinearAlgebra.eigen(A::SymTridiagonal{<:Dual{Tg,T,N}}) where {Tg,T<:Rea
Eigen(λ,Dual{Tg}.(Q, tuple.(parts...)))
end

# StepRange - default to operate with LinRange to avoid TwicePrecision #
#----------------------------------------------------------------------#

Base.:*(x::Dual, r::StepRangeLen{<:Real,<:Base.TwicePrecision}) =
LinRange(x * first(r), x * last(r), length(r))

Base.:/(r::StepRangeLen{<:Real,<:Base.TwicePrecision}, x::Dual) =
LinRange(first(r) / x, last(r) / x, length(r))


# Functions in SpecialFunctions which return tuples #
# Their derivatives are not defined in DiffRules #
#---------------------------------------------------#
Expand Down
55 changes: 55 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,59 @@ end
@test ForwardDiff.derivative(float, 1)::Float64 === 1.0
end

function deriv_check(f, x::Real; kw...)
fdual = f(ForwardDiff.Dual(x,1))
ty = fdual isa AbstractRange || !(f(x) isa AbstractRange)
t0 = isapprox(f(x), ForwardDiff.value.(fdual); kw...)
t1 = isapprox(ForwardDiff.derivative(f, x),
Calculus.derivative(f, x); kw...)
t1i = isapprox(ForwardDiff.derivative(x -> f(x)[1+end÷2], x),
Calculus.derivative(x -> f(x)[1+end÷2], x); kw...)
ty && t0 && t1 && t1i
end

@testset "ranges + Calculus" begin
# op(Dual, StepRangeLen) was broken, issue #380
@test (0:0.1:1) isa StepRangeLen

@test deriv_check(x -> (0:0.1:1) * x, 1.0)
@test deriv_check(x -> (0:0.1:1) .* x, 1.0)
@test deriv_check(x -> x * (0:0.1:1), 1.0)
@test deriv_check(x -> x .* (0:0.1:1), 1.0)

@test deriv_check(x -> (0:0.1:1) / x, 1.0) # makes a LinRange, after PR#618
@test deriv_check(x -> (0:0.1:1) ./ x, 1.0) # different path, at present a StepRangeLen

@test deriv_check(x -> (0:0.1:1) .+ x, 1.0)
@test deriv_check(x -> x .+ (0:0.1:1), 1.0)
@test deriv_check(x -> (0:0.1:1) .- x, 1.0)
@test deriv_check(x -> x .- (0:0.1:1), 1.0)

# op(Dual, StepRange) was never a problem
@test (2:3:11) isa StepRange

@test deriv_check(x -> (2:3:11) * x, 1.0)
@test deriv_check(x -> (2:3:11) / x, 1.0)
@test deriv_check(x -> (2:3:11) .+ x, 1.0)

# op(Dual, LinRange) was never a problem
@test deriv_check(x -> LinRange(1,2,3) * x, 1.0)
@test deriv_check(x -> LinRange(1,2,3) / x, 1.0)
@test deriv_check(x -> LinRange(1,2,3) .+ x, 1.0)

# Construction
@test deriv_check(x -> range(x, 3, length=5), 1.0)
@test deriv_check(x -> range(x, 3x, length=5), 1.0)
@test deriv_check(x -> range(x, step=2, length=5), 1.0)
@test deriv_check(x -> range(x, step=2x, length=5), 1.0)
@test deriv_check(x -> range(1, step=x, length=5), 1.0)

@test deriv_check(x -> x:2:6, 1.0) # perturbing x does not change the length
@test deriv_check(x -> 0.5:x:5, 1.0)
@test deriv_check(x -> 1:x:6, 2) # perturbing x does not change the length
@test deriv_check(x -> 1:2:x, 6) # perturbing x has no effect
@test deriv_check(x -> 1:2:x, 6.0) # perturbing x has no effect
end


end # module