Skip to content

Commit

Permalink
Merge pull request #68 from ararslan/aa/some-fixes
Browse files Browse the repository at this point in the history
Grab bag of minor changes
  • Loading branch information
dlfivefifty committed Oct 16, 2018
2 parents c77ee18 + 36715a9 commit 6c2d651
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ git:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("DualNumbers"); Pkg.test("DualNumbers"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("DualNumbers")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
7 changes: 6 additions & 1 deletion src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Base.widen(::Type{Dual{T}}) where {T} = Dual{widen(T)}
value(z::Dual) = z.value
epsilon(z::Dual) = z.epsilon

value(x::Number) = x
epsilon(x::Number) = zero(typeof(x))

dual(x::ReComp, y::ReComp) = Dual(x, y)
dual(x::ReComp) = Dual(x)
dual(z::Dual) = z
Expand Down Expand Up @@ -264,7 +267,7 @@ Base.:^(z::Dual, n::Number) = Dual(value(z)^n, epsilon(z)*n*value(z)^(n-1))
NaNMath.pow(z::Dual, n::Number) = Dual(NaNMath.pow(value(z),n), epsilon(z)*n*NaNMath.pow(value(z),n-1))
NaNMath.pow(z::Number, w::Dual) = Dual(NaNMath.pow(z,value(w)), epsilon(w)*NaNMath.pow(z,value(w))*log(z))

inv(z::Dual) = dual(inv(value(z)),-epsilon(z)/value(z)^2)
Base.inv(z::Dual) = dual(inv(value(z)),-epsilon(z)/value(z)^2)

# force use of NaNMath functions in derivative calculations
function to_nanmath(x::Expr)
Expand Down Expand Up @@ -313,6 +316,8 @@ end
Base.exp(z::Dual) = (expval = exp(value(z)); Dual(expval, epsilon(z)*expval))
Base.cis(z::Dual) = (cisval = cis(value(z)); Dual(cisval, im*epsilon(z)*cisval))

Base.exp10(x::Dual) = (y = exp10(value(x)); Dual(y, y * log(10) * epsilon(x)))

## TODO: should be generated in Calculus
Base.sinpi(z::Dual) = Dual(sinpi(value(z)),epsilon(z)*cospi(value(z))*π)
Base.cospi(z::Dual) = Dual(cospi(value(z)),-epsilon(z)*sinpi(value(z))*π)
Expand Down
9 changes: 9 additions & 0 deletions test/automatic_differentiation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,12 @@ flipsign(-1.0,Dual(1.0,1.0)) == -1.0
# test SpecialFunctions
@test erf(dual(1.0,1.0)) == dual(erf(1.0), 2exp(-1.0^2)/sqrt(π))
@test gamma(dual(1.,1)) == dual(gamma(1.0),polygamma(0,1.0))


let x = exp10(Dual(2, 0.01))
@test value(x) 100.0
@test epsilon(x) log(10)
end

@test value(3) == 3
@test epsilon(44.0) 0.0

0 comments on commit 6c2d651

Please sign in to comment.