Dual number 'magic'?

I have been reading about dual numbers and wanted to learn a bit more about them and about Julia at the same time. So I implemented a simple dual number type below (tips to improve?). Comparing to the official DualNumbers package (https://github.com/JuliaDiff/DualNumbers.jl) I find that mine is limited. For example, I define methods for basic arithmetic with dual numbers and a few trigonometric methods (sin and cos). However, I wonder how I can make this generalize without writing a new method for every single mathematical function possible. For instance with the official DualNumbers package I can take the tan or the sinh of a Dual, but I don’t see this implemented anywhere explicitly in the authors code. How is this ‘magic’ achieved?

module Dual

# Imports
import Base: show, +, *, -, /, sin, cos

"""
    DualNumber{T<:Real}

A dual number, with real part and epsilon part of type `T`.
"""
struct DualNumber{T<:Real}
    x::T
    y::T
    DualNumber{T}(x,y) where {T<:Real} = new{T}(x,y)
    DualNumber{T}(x) where {T<:Real} = new{T}(x,one(x))
end
DualNumber(x::T, y::T) where {T<:Real} = DualNumber{T}(x,y)
DualNumber(x::Real, y::Real) = DualNumber(promote(x,y)...)
DualNumber(x::T) where {T<:Real} = DualNumber{T}(x)

"""
The element ϵ, such that ϵ^2 = 0. Note: this is a DualNumber{Float64}.
"""
const ϵ = DualNumber{Float64}(0.0, 1.0)

#####################
# Dual/Dual methods #
#####################

+(a::DualNumber, b::DualNumber) = DualNumber(a.x + b.x, a.y + b.y)

*(a::DualNumber, b::DualNumber) = DualNumber(a.x * b.x, a.x * b.y + a.y * b.x)

-(a::DualNumber, b::DualNumber) = DualNumber(a.x - b.x, a.y - b.y)

/(a::DualNumber, b::DualNumber) = DualNumber(a.x / b.x, a.y/b.x - a.x * b.y/(b.x^2))

#######################
# Dual/Scalar methods #
#######################

+(a::T, b::DualNumber) where {T<:Real} = DualNumber(a + b.x, b.y)
+(a::DualNumber, b::T) where {T<:Real}  = b + a

*(a::T, b::DualNumber) where {T<:Real} = DualNumber(a * b.x, a * b.y)
*(a::DualNumber, b::T) where {T<:Real}  = b * a

-(a::T, b::DualNumber) where {T<:Real} = DualNumber(a - b.x, -b.y)
-(a::DualNumber, b::T) where {T<:Real}  = DualNumber(a.x - b, a.y)

/(a::T, b::DualNumber) where {T<:Real} = DualNumber(a / b.x, -a * b.y/(b.x^2))
/(a::DualNumber, b::T) where {T<:Real}  = DualNumber(a.x / b, a.y / b)

#############
# Unary ops #
#############

sin(a::DualNumber) = DualNumber(sin(a.x), a.y*cos(a.x))
cos(a::DualNumber) = DualNumber(cos(a.x), -a.y*sin(a.x))

# Exports
export DualNumber, ϵ, grad

end

I’m fairly certain this piece of the code does the relevant meta-programming magic.

for (funsym, exp) in Calculus.symbolic_derivatives_1arg()
    funsym == :exp && continue
    funsym == :abs2 && continue
    @eval function $(funsym)(z::Dual)
        x = value(z)
        xp = epsilon(z)
        Dual($(funsym)(x),xp*$exp)
    end
1 Like

Thanks @GunnarFarneback that’s very useful. I thought it would be some meta-programming trick but that is a bit new to me so it’s great to have that clarified. I add some ‘magic’:

#########
# Magic #
#########

for (function_symbol, derivative_expression) in Calculus.symbolic_derivatives_1arg()
    @eval function $(function_symbol)(a::DualNumber)
        x = a.x
        y = a.y
        return DualNumber($(function_symbol)(x), y * $derivative_expression)
    end
end

and add these imports at the module start:

# Imports
importall Base
import Calculus

The methods seem to work now which is cool, however, I get a lot of warnings about overwritten methods when I use the module, although I don’t understand why, and I wonder is you have any idea how to fix this?

WARNING: Method definition (::Type{DomainError})(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:503 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:503.
WARNING: Method definition (::Type{DomainError})(Any, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:504 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:504.
WARNING: Method definition (::Type{OverflowError})(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:509 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:509.
WARNING: Method definition (::Type{InexactError})(Symbol, Any, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:498 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:498.
WARNING: Method definition (::Type{Main.Base.IOContext{IO_t} where IO_t<:IO})(Main.Base.IOContext{IO_t} where IO_t<:IO, Main.Base.Pair{A, B} where B where A, Main.Base.Pair{A, B} where B where A) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1003 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1003.
WARNING: Method definition (::Type{Main.Base.IOContext{IO_t} where IO_t<:IO})(IO, Main.Base.Pair{A, B} where B where A, Main.Base.Pair{A, B} where B where A, Main.Base.Pair{A, B} where B where A...) inmodule Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1001 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1001.
WARNING: Method definition (::Type{Main.Base.Val{T} where T})(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:440 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:440.
WARNING: Method definition (::Type{Array{T, 2}})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Tuple{Int64, Int64}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:973 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:973.
WARNING: Method definition (::Type{Array{T, 2}})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Integer, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:974 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:974.
WARNING: Method definition (::Type{Main.Base.SparseArrays.SparseMatrixCSC{Tv, Ti} where Ti<:Integer})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Integer, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:977 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:977.
WARNING: Method definition (::Type{Main.Base.SparseArrays.SparseMatrixCSC{Tv, Ti}})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Integer, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:976 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:976.
WARNING: Method definition (::Type{Main.Base.SparseArrays.SparseMatrixCSC{Tv, Ti} where Ti<:Integer})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Tuple{Int64, Int64}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:978 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:978.
WARNING: Method definition (::Type{Main.Base.SparseArrays.SparseMatrixCSC{Tv, Ti}})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Tuple{Int64, Int64}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:980 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:980.
WARNING: Method definition (::Type{Array{T, N} where N})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Tuple{Int64, Int64}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:992 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:992.
WARNING: Method definition (::Type{Array{T, N} where N})(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Integer, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:993 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:993.
WARNING: Method definition (::Type{Array{T, 2} where T})(Main.Base.LinAlg.UniformScaling{T}, Any...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:996 overwritten in module Compat at/home/peter/.julia/v0.6/Compat/src/Compat.jl:996.
WARNING: Method definition findfirst(Main.Base.Regex, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1532 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1532.
WARNING: Method definition findfirst(AbstractString, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1547 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1547.
WARNING: Method definition ==(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:880 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:880.
WARNING: Method definition squeeze(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1819 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1819.
WARNING: Method definition expand(Module, ANY) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:69 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:69.
WARNING: Method definition round(Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Main.Base.Rounding.RoundingMode{:NearestTiesUp}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:778 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:778.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Main.Base.Dates.Period) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:787 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:787.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Main.Base.Dates.Period, Main.Base.Rounding.RoundingMode{:Down}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:783 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:783.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Main.Base.Dates.Period, Main.Base.Rounding.RoundingMode{:Up}) inmodule Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:784 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:784.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Main.Base.Dates.Period, Main.Base.Rounding.RoundingMode{T} whereT) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:786 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:786.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Type{P<:Main.Base.Dates.Period}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:791 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:791.
WARNING: Method definition round(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Type{P<:Main.Base.Dates.Period}, Main.Base.Rounding.RoundingMode{T} where T) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:791 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:791.
WARNING: Method definition floor(Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, T<:Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}) in moduleCompat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:705 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:705.
WARNING: Method definition floor(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Type{P<:Main.Base.Dates.Period}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:788 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:788.
WARNING: Method definition length(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition start(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition values(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:614 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:614.
WARNING: Method definition chol!(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:457 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:457.
WARNING: Method definition diagm(Main.Base.Pair{A, B} where B where A...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:952 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:952.
WARNING: Method definition chol(Main.Base.LinAlg.UniformScaling{T} where T<:Number, Any...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:458 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:458.
WARNING: Method definition logdet(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:451 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:451.
WARNING: Method definition next(Main.Base.Cmd, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419.
WARNING: Method definition contains(AbstractString, Main.Base.Regex) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1197 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1197.
WARNING: Method definition repr(Union{AbstractString, Main.Base.MIME{mime} where mime}, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1576 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1576.
WARNING: Method definition eltype(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition ntuple(F, Main.Base.Val{N}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:445 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:445.
WARNING: Method definition done(Main.Base.Cmd, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419.
WARNING: Method definition endof(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition last(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition first(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition #squeeze(Array{Any, 1}, typeof(Main.Base.squeeze), Any) in module Compat overwritten in module Compat.
WARNING: Method definition _redirect_stdin(IO) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18.
WARNING: Method definition #replace(Array{Any, 1}, typeof(Main.Base.replace), AbstractString, Main.Base.Pair{A, B} where B where A) in module Compat overwritten in module Compat.
WARNING: Method definition read(Main.Base.Cmd, Type{String}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:493 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:493.
WARNING: Method definition read(AbstractString, Type{String}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:492 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:492.
WARNING: Method definition read(IO, Type{String}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:491 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:491.
WARNING: Method definition replace(AbstractString, Main.Base.Pair{A, B} where B where A) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1184 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1184.
WARNING: Method definition findprev(AbstractString, AbstractString, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1562 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1562.
WARNING: Method definition macroexpand(Module, ANY) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:70 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:70.
WARNING: Method definition in(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:889 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:889.
WARNING: Method definition getindex(Main.Base.Cmd, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:419.
WARNING: Method definition include_string(Module, String, String) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:71 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:71.
WARNING: Method definition include_string(Module, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:73 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:73.
WARNING: Method definition include_string(Module, AbstractString, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:73 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:73.
WARNING: Method definition reshape(AbstractArray{T, N} where N where T, Main.Base.Val{N}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:443 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:443.
WARNING: Method definition eachindex(Main.Base.Cmd) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:416.
WARNING: Method definition ceil(Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Union{Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:734 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:734.
WARNING: Method definition ceil(Union{Main.Base.Dates.TimeType, Main.Base.Dates.TimePeriod, Main.Base.Dates.Week, Main.Base.Dates.Day}, Type{P<:Main.Base.Dates.Period}) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:789 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:789.
WARNING: Method definition #cov(Array{Any, 1}, typeof(Main.Base.cov), AbstractArray{T, 1} where T) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:530 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:530.
WARNING: Method definition #cov(Array{Any, 1}, typeof(Main.Base.cov), AbstractArray{T, 1} where T, AbstractArray{T, 1} where T) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:531 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:531.
WARNING: Method definition #cov(Array{Any, 1}, typeof(Main.Base.cov), AbstractArray{T, 2} where T, Int64) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:534 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:534.
WARNING: Method definition #cov(Array{Any, 1}, typeof(Main.Base.cov), Union{AbstractArray{T, 1}, AbstractArray{T, 2}} where T, Union{AbstractArray{T, 1}, AbstractArray{T, 2}} where T, Int64) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:537 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:537.
WARNING: Method definition get(Union{Function, Type{T} where T}, Main.Base.EnvHash, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1036 overwritten in module Compat at/home/peter/.julia/v0.6/Compat/src/Compat.jl:1036.
WARNING: Method definition spdiagm(Main.Base.Pair{A, B} where B where A...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:942 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:942.
WARNING: Method definition _redirect_stderr(IO) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18.
WARNING: Method definition convert(Type{Void}, Void) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1145 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1145.
WARNING: Method definition convert(Type{Void}, Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1144 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1144.
WARNING: Method definition convert(Type{Union{T, Void}}, Any) in module Compat at string:8 overwritten in module Compat at string:8.
WARNING: Method definition rtoldefault(Any, Any, Real) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:625 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:625.
WARNING: Method definition keys(AbstractArray{T, 1} where T) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:611 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:611.
WARNING: Method definition keys(AbstractArray{T, N} where N where T) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:610 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:610.
WARNING: Method definition keys(Main.Base.IndexStyle, AbstractArray{T, N} where N where T, AbstractArray{T, N} where N where T...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:612 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:612.
WARNING: Method definition isequal(Any) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:884 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:884.
WARNING: Method definition _redirect_stdout(IO) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:18.
WARNING: Method definition findnext(Main.Base.Regex, AbstractString, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1531 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1531.
WARNING: Method definition findnext(AbstractString, AbstractString, Integer) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1546 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1546.
WARNING: Method definition findlast(AbstractString, AbstractString) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1563 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:1563.
WARNING: Method definition *(Union{Char, AbstractString}, Union{Char, AbstractString}...) in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:929 overwritten in module Compat at /home/peter/.julia/v0.6/Compat/src/Compat.jl:929.
WARNING: Method definition gradient(Any) in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:24 overwritten in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:24.
WARNING: Method definition gradient(Any, Union{T<:Number, Array{T<:Number, 1}}) in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:17 overwritten in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:17.
WARNING: Method definition gradient(Any, Union{T<:Number, Array{T<:Number, 1}}, Symbol) in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:17 overwritten in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:17.
WARNING: Method definition gradient(Any, Symbol) in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:24 overwritten in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:24.
WARNING: Method definition ctranspose(Function) in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:33 overwritten in module Calculus at /home/peter/.julia/v0.6/Calculus/src/derivative.jl:33.

I can’t reproduce those warnings with the code you have shown. I can provoke similar warnings with

using Compat
include("/home/gunnar/.julia/v0.6/Compat/src/Compat.jl")

but that would be an odd thing to do.

Unrelatedly I would skip importall Base and instead do

    @eval function Base.$(function_symbol)(a::DualNumber)