Skip to content

Make duals' operations through steprange use linrange instead #618

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 11 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,17 @@ 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))

Base.:(:)(start::T, step::Dual, stop::T) where {T<:Real} = range(start, step, stop)

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

@testset "steprange" begin # issue #380
@test Dual(1,1) * (0:0.1:1) isa LinRange{Dual{Nothing, Float64, 1}, Int64}
@test (0:0.1:1) / Dual(1,1) isa LinRange{Dual{Nothing, Float64, 1}, Int64}
@test 0:Dual(1,1):2 isa LinRange{Dual{Nothing, Float64, 1}, Int64}
end




end # module