Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/DualNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module DualNumbers
using SpecialFunctions
import NaNMath
import Calculus
import Random

include("dual.jl")

Expand Down
11 changes: 11 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ Base.ceil( ::Type{T}, z::Dual) where {T<:Real} = ceil( T, value(z))
Base.trunc(::Type{T}, z::Dual) where {T<:Real} = trunc(T, value(z))
Base.round(::Type{T}, z::Dual) where {T<:Real} = round(T, value(z))

Base.zero(::Type{Dual{T}}) where {T} = Dual(zero(T), zero(T))
Base.zero(x::Dual{T}) where {T} = zero(T)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should these two versions of zero return different types?

Base.iszero(z::Dual{T}) where {T} = iszero(value(z)) & iszero(epsilon(z))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to use short-circuiting && here and for isone?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definition of iszero is inconsistent with how == 0 is defined for duals:

Base.:(==)(z::Dual, x::Number) = value(z) == x


Base.one(::Type{Dual{T}}) where {T} = Dual(one(T), zero(T))
Base.one(x::Dual{T}) where {T} = one(T)
Base.isone(z::Dual{T}) where {T} = isone(value(z)) & iszero(epsilon(z))

Base.rand(r::Random.AbstractRNG, ::Random.SamplerType{Dual{T}}) where {T} = Dual{T}(rand(r, T), rand(r, T))
Base.randn(r::Random.AbstractRNG, ::Type{Dual{T}}) where {T} = Dual{T}(randn(r, T), randn(r, T))

for op in (:real, :imag, :conj, :float, :complex)
@eval Base.$op(z::Dual) = Dual($op(value(z)), $op(epsilon(z)))
end
Expand Down