-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Doris
committed
Oct 2, 2019
1 parent
72512fc
commit 91569e4
Showing
15 changed files
with
396 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# conversion | ||
float(x::CLogarithmic) = Complex(exp(Complex(float(x.abs.log), float(x.angle)))) | ||
(::Type{T})(x::CLogarithmic) where {T<:AbstractFloat} = (y=float(x.abs); T(iszero(x.angle) ? y : x.angle≈π ? -y : throw(DomainError(x)))) | ||
(::Type{T})(x::CLogarithmic) where {T<:Complex} = T(exp(Complex(x.abs.log, x.angle))) | ||
big(x::CLogarithmic) = CLogarithmic(big(x.abs), big(x.angle)) | ||
|
||
# type functions | ||
widen(::Type{CLogarithmic{T}}) where {T} = CLogarithmic{widen(T)} | ||
big(::Type{CLogarithmic{T}}) where {T} = CLogarithmic{big(T)} | ||
|
||
# special values | ||
zero(::Type{CLogarithmic{T}}) where {T} = CLogarithmic(zero(ULogarithmic{T})) | ||
one(::Type{CLogarithmic{T}}) where {T} = CLogarithmic(one(ULogarithmic{T})) | ||
|
||
# predicates | ||
iszero(x::CLogarithmic) = iszero(x.abs) | ||
isone(x::CLogarithmic) = isone(x.abs) && iszero(x.angle) | ||
isinf(x::CLogarithmic) = isinf(x.abs) | ||
isfinite(x::CLogarithmic) = isfinite(x.abs) | ||
isnan(x::CLogarithmic) = isnan(x.abs) | ||
|
||
# sign | ||
sign(x::CLogarithmic{T}) where {T} = iszero(x) ? zero(x) : CLogarithmic{T}(uexp(T,0), x.angle) | ||
abs(x::CLogarithmic) = x.abs | ||
angle(x::CLogarithmic) = x.angle | ||
real(x::CLogarithmic) = x.abs * cos(x.angle) | ||
imag(x::CLogarithmic) = x.abs * sin(x.angle) | ||
|
||
# ordering | ||
(==)(x::CLogarithmic, y::CLogarithmic) = x.abs==y.abs && x.angle==y.angle | ||
isequal(x::CLogarithmic, y::CLogarithmic) = isequal(x.abs, y.abs) && isequal(x.angle, y.angle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,78 @@ | ||
# construct by exponentiation | ||
""" | ||
exp(T<:AbstractLogarithmic, x) | ||
exp(T, x) | ||
The number `exp(x)` represented as a `T`. | ||
""" | ||
exp(::Type{E} where {E<:(AbstractLogarithmic{T} where {T})}, x::Real) | ||
exp(::Type{T} where {T<:AnyLog}, x::Real) | ||
|
||
exp(::Type{ULogarithmic{T}}, x::Real) where {T<:Real} = exp(ULogarithmic{T}, convert(T, x)) | ||
exp(::Type{ULogarithmic}, x::T) where {T<:Real} = exp(ULogarithmic{T}, x) | ||
exp(::Type{ULogarithmic{T}}, x::Real) where {T<:Real} = | ||
exp(ULogarithmic{T}, convert(T, x)) | ||
|
||
exp(::Type{Logarithmic{T}}, x::Real) where {T<:Real} = Logarithmic{T}(exp(ULogarithmic{T}, x)) | ||
exp(::Type{Logarithmic}, x::T) where {T<:Real} = Logarithmic{T}(exp(ULogarithmic{T}, x)) | ||
exp(::Type{ULogarithmic}, x::T) where {T<:Real} = | ||
exp(ULogarithmic{T}, x) | ||
|
||
exp(::Type{AbstractLogarithmic{T}}, x::Real) where {T<:Real} = exp(ULogarithmic{T}, x) | ||
exp(::Type{AbstractLogarithmic}, x::Real) = exp(ULogarithmic, x) | ||
exp(::Type{Logarithmic{T}}, x::Real) where {T<:Real} = | ||
Logarithmic{T}(exp(ULogarithmic{T}, x)) | ||
|
||
exp(::Type{Logarithmic}, x::T) where {T<:Real} = | ||
exp(Logarithmic{T}, x) | ||
|
||
exp(::Type{CLogarithmic{T}}, x::Union{Real,Complex}) where {T<:Real} = | ||
CLogarithmic{T}(exp(ULogarithmic{T}, real(x)), imag(x)) | ||
|
||
exp(::Type{CLogarithmic}, x::T) where {T<:Real} = | ||
exp(CLogarithmic{T}, x) | ||
|
||
exp(::Type{CLogarithmic}, x::Complex{T}) where {T<:Real} = | ||
exp(CLogarithmic{T}, x) | ||
|
||
# convenience | ||
uexp(x) = exp(ULogarithmic, x) | ||
uexp(T,x) = exp(ULogarithmic{T}, x) | ||
|
||
# convert to ULogarithmic | ||
ULogarithmic{T}(x::Real) where {T<:Real} = exp(ULogarithmic{T}, log(x)) | ||
|
||
ULogarithmic{T}(x::ULogarithmic{T}) where {T<:Real} = x | ||
|
||
ULogarithmic(x::Real) = exp(ULogarithmic, log(x)) | ||
|
||
AbstractLogarithmic{T}(x::Real) where {T<:Real} = ULogarithmic{T}(x) | ||
AbstractLogarithmic(x::T) where {T<:Real} = ULogarithmic(x) | ||
ULogarithmic(x::ULogarithmic) = x | ||
|
||
# convert to Logarithmic | ||
Logarithmic{T}(x::Real) where {T<:Real} = Logarithmic{T}(ULogarithmic{T}(abs(x)), signbit(x)) | ||
|
||
Logarithmic{T}(x::Real) where {T<:Real} = | ||
Logarithmic{T}(ULogarithmic{T}(abs(x)), signbit(x)) | ||
|
||
Logarithmic{T}(x::Logarithmic{T}) where {T<:Real} = x | ||
Logarithmic{T}(abs::ULogarithmic, signbit::Bool=false) where {T<:Real} = Logarithmic{T}(ULogarithmic{T},(abs), signbit) | ||
|
||
Logarithmic(x::Real) where {T<:Real} = Logarithmic(ULogarithmic(abs(x)), signbit(x)) | ||
Logarithmic(abs::ULogarithmic{T}, signbit::Bool=false) where {T<:Real} = Logarithmic{T}(abs, signbit) | ||
Logarithmic{T}(abs::ULogarithmic, signbit::Bool=false) where {T<:Real} = | ||
Logarithmic{T}(ULogarithmic{T}(abs), signbit) | ||
|
||
Logarithmic(x::Real) = | ||
Logarithmic(ULogarithmic(abs(x)), signbit(x)) | ||
|
||
Logarithmic(abs::ULogarithmic{T}, signbit::Bool=false) where {T<:Real} = | ||
Logarithmic{T}(abs, signbit) | ||
|
||
Logarithmic(x::Logarithmic) = x | ||
|
||
# convert to CLogarithmic | ||
|
||
CLogarithmic{T}(x::Union{Real,Complex}) where {T<:Real} = | ||
CLogarithmic{T}(ULogarithmic{T}(abs(x)), convert(T, angle(x))) | ||
|
||
CLogarithmic{T}(x::CLogarithmic{T}) where {T<:Real} = x | ||
|
||
CLogarithmic{T}(x::CLogarithmic) where {T<:Real} = | ||
CLogarithmic{T}(x.abs, x.angle) | ||
|
||
CLogarithmic{T}(abs::ULogarithmic, angle::Real=zero(T)) where {T<:Real} = | ||
CLogarithmic{T}(ULogarithmic{T}(abs), convert(T, angle)) | ||
|
||
CLogarithmic(x::Union{Real,Complex}) = | ||
CLogarithmic(ULogarithmic(abs(x)), angle(x)) | ||
|
||
CLogarithmic(abs::ULogarithmic{T}, angle::A=zero(T)) where {T<:Real, A<:Real} = | ||
CLogarithmic{promote_type(T,A)}(abs, angle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import .Distributions | ||
|
||
Distributions.cdf(::Type{E}, args...; opts...) where {E<:ALog} = exp(E, Distributions.logcdf(args...; opts...)) | ||
Distributions.ccdf(::Type{E}, args...; opts...) where {E<:ALog} = exp(E, Distributions.logccdf(args...; opts...)) | ||
Distributions.pdf(::Type{E}, args...; opts...) where {E<:ALog} = exp(E, Distributions.logpdf(args...; opts...)) | ||
Distributions.cdf(::Type{E}, args...; opts...) where {E<:AnyLog} = exp(E, Distributions.logcdf(args...; opts...)) | ||
Distributions.ccdf(::Type{E}, args...; opts...) where {E<:AnyLog} = exp(E, Distributions.logccdf(args...; opts...)) | ||
Distributions.pdf(::Type{E}, args...; opts...) where {E<:AnyLog} = exp(E, Distributions.logpdf(args...; opts...)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
promote_rule(::Type{ULogarithmic{T}}, ::Type{ULogarithmic{R}}) where {T,R} = ULogarithmic{promote_type(T,R)} | ||
promote_rule(::Type{ULogarithmic{T}}, ::Type{Logarithmic{R}}) where {T,R} = Logarithmic{promote_type(T,R)} | ||
promote_rule(::Type{Logarithmic{T}}, ::Type{Logarithmic{R}}) where {T,R} = Logarithmic{promote_type(T,R)} | ||
promote_rule(::Type{Logarithmic{T}}, ::Type{ULogarithmic{R}}) where {T,R} = Logarithmic{promote_type(T,R)} | ||
promote_rule(::Type{ULogarithmic{T}}, ::Type{R}) where {T,R<:Real} = try ULogarithmic{promote_type(T,typeof(log(one(R))))}; catch; Union{}; end | ||
promote_rule(::Type{Logarithmic{T}}, ::Type{R}) where {T,R<:Real} = try ULogarithmic{promote_type(T,typeof(log(one(R))))}; catch; Union{}; end | ||
atypes = (ULogarithmic, Logarithmic, CLogarithmic) | ||
|
||
# logarithmic + logarithmic | ||
|
||
for (i,A) in enumerate(atypes) | ||
for (j,B) in enumerate(atypes) | ||
C = i<j ? B : A | ||
@eval promote_rule(::Type{$A}, ::Type{$B}) = $C | ||
@eval promote_rule(::Type{$A}, ::Type{$B{T}}) where {T} = $C{T} | ||
@eval promote_rule(::Type{$A{T}}, ::Type{$B}) where {T} = $C{T} | ||
@eval promote_rule(::Type{$A{S}}, ::Type{$B{T}}) where {S,T} = | ||
$C{promote_type(S,T)} | ||
end | ||
end | ||
|
||
# logarithmic + real | ||
|
||
# generated for type-stability | ||
@generated promote_rule(::Type{ULogarithmic}, ::Type{R}) where {R<:Real} = | ||
try | ||
:($(typeof(ULogarithmic(one(R))))) | ||
catch | ||
:(Union{}) | ||
end | ||
|
||
promote_rule(::Type{A}, ::Type{R}) where {A<:AnyLog, R<:Real} = | ||
promote_type(A, promote_type(ULogarithmic, R)) | ||
|
||
# logarithmic + complex | ||
# only CLogarithmic+Complex is promoted, so that Logarithmic+Complex is promoted to Complex{Logarithmic} | ||
|
||
# generated for type-stability | ||
@generated promote_rule(::Type{CLogarithmic}, ::Type{C}) where {C<:Complex} = | ||
try | ||
:($(typeof(CLogarithmic(one(C))))) | ||
catch | ||
:(Union{}) | ||
end | ||
|
||
promote_rule(::Type{CLogarithmic{T}}, ::Type{C}) where {T, C<:Complex} = | ||
promote_type(CLogarithmic{T}, promote_type(CLogarithmic, C)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
rand(rng::AbstractRNG, ::Random.SamplerType{E}) where {T<:AbstractFloat, E<:AbstractLogarithmic{T}} = exp(E, -randexp(rng, T)) | ||
rand(rng::AbstractRNG, ::Random.SamplerType{E}) where {E<:ALog} = exp(E, -randexp(rng)) | ||
rand(rng::AbstractRNG, ::Random.SamplerType{E}) where {T<:AbstractFloat, E<:RealLog{T}} = | ||
exp(E, -randexp(rng, T)) | ||
rand(rng::AbstractRNG, ::Random.SamplerType{E}) where {E<:RealLog} = | ||
exp(E, -randexp(rng)) | ||
|
||
# todo: sample CLogarithmic | ||
# (note that rand(Complex) samples uniformly from the unit square) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.