Skip to content
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

rework result_type #140

Merged
merged 3 commits into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/generic.jl
Expand Up @@ -24,7 +24,15 @@ abstract type Metric <: SemiMetric end

# Generic functions

result_type(::PreMetric, ::AbstractArray, ::AbstractArray) = Float64
"""
result_type(dist::PreMetric, Ta::Type, Tb::Type) -> T
result_type(dist::PreMetric, a::AbstractArray, b::AbstractArray) -> T

Infer the result type of metric `dist` with input type `Ta` and `Tb`, or input
data `a` and `b`.
"""
result_type(::PreMetric, ::Type, ::Type) = Float64 # fallback
result_type(dist::PreMetric, a::AbstractArray, b::AbstractArray) = result_type(dist, eltype(a), eltype(b))


# Generic column-wise evaluation
Expand Down
4 changes: 2 additions & 2 deletions src/mahalanobis.jl
Expand Up @@ -8,8 +8,8 @@ struct SqMahalanobis{T} <: SemiMetric
qmat::Matrix{T}
end

result_type(::Mahalanobis{T}, ::AbstractArray, ::AbstractArray) where {T} = T
result_type(::SqMahalanobis{T}, ::AbstractArray, ::AbstractArray) where {T} = T
result_type(::Mahalanobis{T}, ::Type, ::Type) where {T} = T
result_type(::SqMahalanobis{T}, ::Type, ::Type) where {T} = T

# SqMahalanobis

Expand Down
12 changes: 5 additions & 7 deletions src/metrics.jl
Expand Up @@ -252,9 +252,8 @@ end
end
return eval_end(d, s)
end
result_type(dist::UnionMetrics, a::AbstractArray, b::AbstractArray) =
typeof(evaluate(dist, oneunit(eltype(a)), oneunit(eltype(b))))

result_type(dist::UnionMetrics, Ta::Type, Tb::Type) =
typeof(evaluate(dist, oneunit(Ta), oneunit(Tb)))
eval_start(d::UnionMetrics, a::AbstractArray, b::AbstractArray) =
zero(result_type(d, a, b))
eval_end(d::UnionMetrics, s) = s
Expand Down Expand Up @@ -352,7 +351,7 @@ evaluate(::CorrDist, a::AbstractArray, b::AbstractArray) = cosine_dist(_centrali
# Ambiguity resolution
evaluate(::CorrDist, a::Array, b::Array) = cosine_dist(_centralize(a), _centralize(b))
corr_dist(a::AbstractArray, b::AbstractArray) = evaluate(CorrDist(), a, b)
result_type(::CorrDist, a::AbstractArray, b::AbstractArray) = result_type(CosineDist(), a, b)
result_type(::CorrDist, Ta::Type, Tb::Type) = result_type(CosineDist(), Ta, Tb)

# ChiSqDist
@inline eval_op(::ChiSqDist, ai, bi) = (d = abs2(ai - bi) / (ai + bi); ifelse(ai != bi, d, zero(d)))
Expand Down Expand Up @@ -452,9 +451,8 @@ end

eval_end(::SpanNormDist, s) = s[2] - s[1]
spannorm_dist(a::AbstractArray, b::AbstractArray) = evaluate(SpanNormDist(), a, b)
result_type(dist::SpanNormDist, a::AbstractArray, b::AbstractArray) =
typeof(eval_op(dist, oneunit(eltype(a)), oneunit(eltype(b))))

result_type(dist::SpanNormDist, Ta::Type, Tb::Type) =
typeof(eval_op(dist, oneunit(Ta), oneunit(Tb)))

# Jaccard

Expand Down
4 changes: 2 additions & 2 deletions src/wmetrics.jl
Expand Up @@ -42,8 +42,8 @@ Base.eltype(x::UnionWeightedMetrics) = eltype(x.weights)
function evaluate(dist::UnionWeightedMetrics, a::Number, b::Number)
eval_end(dist, eval_op(dist, a, b, oneunit(eltype(dist))))
end
result_type(dist::UnionWeightedMetrics, a::AbstractArray, b::AbstractArray) =
typeof(evaluate(dist, oneunit(eltype(a)), oneunit(eltype(b))))
result_type(dist::UnionWeightedMetrics, Ta::Type, Tb::Type) =
typeof(evaluate(dist, oneunit(Ta), oneunit(Tb)))

@inline function eval_start(d::UnionWeightedMetrics, a::AbstractArray, b::AbstractArray)
zero(result_type(d, a, b))
Expand Down