Skip to content

Commit e493370

Browse files
Convert to floating point numbers where it makes sense
1 parent 2fee56d commit e493370

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

src/utils.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ _denom(A, ::Colon) = length(A)
1212
@inline _reducedsize(A::AbstractArray{T, N}, dims::NTuple{M, Int}) where {T, N, M} = ntuple(d -> d dims ? 1 : size(A, d), Val(N))
1313

1414
#### Numerical stability/limits
15-
_xlogx(x::T) where {T} = ifelse(iszero(x), zero(T), x * log(x))
16-
_xlogy(x::T, y::T) where {T} = ifelse(iszero(x) & !isnan(y), zero(T), x * log(y))
15+
function _xlogx(x::T) where {T<:AbstractFloat}
16+
ifelse(iszero(x), zero(T), x * log(x))
17+
end
18+
_xlogx(x::Real) = _xlogx(float(x))
19+
20+
function _xlogy(x::T, y::T) where {T<:AbstractFloat}
21+
ifelse(iszero(x) & !isnan(y), zero(T), x * log(y))
22+
end
23+
_xlogy(x::Real, y::Real) = _xlogy(float(x), float(y))
1724
# _xlogxdy(x::T, y::T) where {T} = _xlogy(x, ifelse(iszero(x) & iszero(y), zero(T), x / y))
18-
_klterm(x::T, y::T) where {T} = _xlogy(x, x) - _xlogy(x, y)
25+
function _klterm(x::T, y::T) where {T}
26+
xf = float(x)
27+
yf = float(y)
28+
_xlogy(xf, xf) - _xlogy(xf, yf)
29+
end

src/vdistance.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
# Distances
99
_vminkowski(x, y, p::T, dims=:) where {T<:Integer} =
10-
vmapreducethen((xᵢ, yᵢ) -> abs(xᵢ - yᵢ)^p, +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
10+
vmapreducethen((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ))^p, +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
1111
_vminkowski(x, y, p::T, dims=:) where {T<:AbstractFloat} =
12-
vmapreducethen((xᵢ, yᵢ) -> exp(p * log(abs(xᵢ - yᵢ))), +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
12+
vmapreducethen((xᵢ, yᵢ) -> exp(p * log(abs(float(xᵢ) - float(yᵢ)))), +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
1313
_vminkowski(x, y, p::Rational{T}, dims=:) where {T} = _vminkowski(x, y, float(p), dims=dims)
1414

1515
"""
@@ -39,9 +39,9 @@ function vminkowski(x, y, p::Real; dims=:)
3939
end
4040

4141
_vtminkowski(x, y, p::T, dims=:) where {T<:Integer} =
42-
vtmapreducethen((xᵢ, yᵢ) -> abs(xᵢ - yᵢ)^p, +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
42+
vtmapreducethen((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ))^p, +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
4343
_vtminkowski(x, y, p::T, dims=:) where {T<:AbstractFloat} =
44-
vtmapreducethen((xᵢ, yᵢ) -> exp(p * log(abs(xᵢ - yᵢ))), +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
44+
vtmapreducethen((xᵢ, yᵢ) -> exp(p * log(abs(float(xᵢ) - float(yᵢ)))), +, x -> exp((one(T)/p) * log(abs(x))), x, y, dims=dims)
4545
_vtminkowski(x, y, p::Rational{T}, dims=:) where {T} = _vtminkowski(x, y, float(p), dims=dims)
4646

4747
"""
@@ -78,7 +78,7 @@ dimensions `dims`.
7878
7979
See also: [`veuclidean`](@ref), [`vchebyshev`](@ref), [`vminkowski`](@ref)
8080
"""
81-
vmanhattan(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), +, x, y, dims=dims)
81+
vmanhattan(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), +, x, y, dims=dims)
8282

8383
"""
8484
vtmanhattan(x::AbstractArray, y::AbstractArray; dims=:)
@@ -88,7 +88,7 @@ dimensions `dims`. Threaded.
8888
8989
See also: [`vteuclidean`](@ref), [`vtchebyshev`](@ref), [`vtminkowski`](@ref)
9090
"""
91-
vtmanhattan(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), +, x, y, dims=dims)
91+
vtmanhattan(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), +, x, y, dims=dims)
9292

9393
"""
9494
veuclidean(x::AbstractArray, y::AbstractArray; dims=:)
@@ -98,7 +98,7 @@ dimensions `dims`.
9898
9999
See also: [`vmanhattan`](@ref), [`vchebyshev`](@ref), [`vminkowski`](@ref)
100100
"""
101-
veuclidean(x, y; dims=:) = vmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, , x, y, dims=dims)
101+
veuclidean(x, y; dims=:) = vmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, , x, y, dims=dims)
102102

103103
"""
104104
vteuclidean(x::AbstractArray, y::AbstractArray; dims=:)
@@ -108,7 +108,7 @@ dimensions `dims`. Threaded.
108108
109109
See also: [`vtmanhattan`](@ref), [`vtchebyshev`](@ref), [`vtminkowski`](@ref)
110110
"""
111-
vteuclidean(x, y; dims=:) = vtmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, , x, y, dims=dims)
111+
vteuclidean(x, y; dims=:) = vtmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, , x, y, dims=dims)
112112

113113
"""
114114
vchebyshev(x::AbstractArray, y::AbstractArray; dims=:)
@@ -118,8 +118,8 @@ dimensions `dims`.
118118
119119
See also: [`vmanhattan`](@ref), [`veuclidean`](@ref), [`vminkowski`](@ref)
120120
"""
121-
vchebyshev(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), max, x, y, dims=dims)
122-
vchebyshev₋(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), min, x, y, dims=dims)
121+
vchebyshev(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), max, x, y, dims=dims)
122+
vchebyshev₋(x, y; dims=:) = vvmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), min, x, y, dims=dims)
123123

124124
"""
125125
vtchebyshev(x::AbstractArray, y::AbstractArray; dims=:)
@@ -129,5 +129,5 @@ dimensions `dims`. Threaded.
129129
130130
See also: [`vtmanhattan`](@ref), [`vteuclidean`](@ref), [`vtminkowski`](@ref)
131131
"""
132-
vtchebyshev(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), max, x, y, dims=dims)
133-
vtchebyshev₋(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(xᵢ - yᵢ), min, x, y, dims=dims)
132+
vtchebyshev(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), max, x, y, dims=dims)
133+
vtchebyshev₋(x, y; dims=:) = vtmapreduce((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)), min, x, y, dims=dims)

src/vstats.jl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
#
66
############################################################################################
77
# Assorted functions from StatsBase, plus some of my own
8-
98
################
109
# Means
1110
function vmean(f::F, A; dims=:) where {F}
1211
c = 1 / _denom(A, dims)
13-
vmapreducethen(f, +, x -> c * x, A, dims=dims)
12+
vmapreducethen(x -> float(f(x)), +, x -> c * x, A, dims=dims)
1413
end
1514
vmean(A; dims=:) = vmean(identity, A, dims=dims)
1615

1716
function vtmean(f::F, A; dims=:) where {F}
1817
c = 1 / _denom(A, dims)
19-
vtmapreducethen(f, +, x -> c * x, A, dims=dims)
18+
vtmapreducethen(x -> float(f(x)), +, x -> c * x, A, dims=dims)
2019
end
2120
vtmean(A; dims=:) = vtmean(identity, A, dims=dims)
2221

@@ -97,13 +96,12 @@ _vmaxentropy(p, dims::NTuple{M, Int}) where {M} =
9796
_vmaxentropy(p, ::Colon) = log(length(p))
9897
vmaxentropy(p; dims=:) = _vmaxentropy(p, dims)
9998
vshannonentropy(p; dims=:) = vmapreducethen(_xlogx, +, -, p, dims=dims)
100-
vcollisionentropy(p; dims=:) = vmapreducethen(abs2, +, x -> -log(x), p, dims=dims)
99+
vcollisionentropy(p; dims=:) = vmapreducethen(abs2 float, +, x -> -log(x), p, dims=dims)
101100
vminentropy(p; dims=:) = vmapreducethen(identity, max, x -> -log(x), p, dims=dims)
102101

103-
_vrenyientropy(p, α::T, dims) where {T<:Integer} =
104-
(c = one(T) / (one(T) - α); vmapreducethen(x -> x^α, +, x -> c * log(x), p, dims=dims))
105102
_vrenyientropy(p, α::T, dims) where {T<:AbstractFloat} =
106103
(c = one(T) / (one(T) - α); vmapreducethen(x -> exp* log(x)), +, x -> c * log(x), p, dims=dims))
104+
_vrenyientropy(p, α::T, dims) where {T<:Integer} = _vrenyientryop(p, float(α), dims)
107105
_vrenyientropy(p, α::Rational{T}, dims) where {T} = _vrenyientropy(p, float(α), dims)
108106
function vrenyientropy(p, α::Real; dims=:)
109107
α < 0 && throw(ArgumentError("Order of Rényi entropy not legal, $(α) < 0."))
@@ -139,13 +137,12 @@ _vtmaxentropy(p, dims::NTuple{M, Int}) where {M} =
139137
_vtmaxentropy(p, ::Colon) = log(length(p))
140138
vtmaxentropy(p; dims=:) = _vtmaxentropy(p, dims)
141139
vtshannonentropy(p; dims=:) = vtmapreducethen(_xlogx, +, -, p, dims=dims)
142-
vtcollisionentropy(p; dims=:) = vtmapreducethen(abs2, +, x -> -log(x), p, dims=dims)
140+
vtcollisionentropy(p; dims=:) = vtmapreducethen(abs2 float, +, x -> -log(x), p, dims=dims)
143141
vtminentropy(p; dims=:) = vtmapreducethen(identity, max, x -> -log(x), p, dims=dims)
144142

145-
_vtrenyientropy(p, α::T, dims) where {T<:Integer} =
146-
(c = one(T) / (one(T) - α); vtmapreducethen(x -> x^α, +, x -> c * log(x), p, dims=dims))
147143
_vtrenyientropy(p, α::T, dims) where {T<:AbstractFloat} =
148144
(c = one(T) / (one(T) - α); vtmapreducethen(x -> exp* log(x)), +, x -> c * log(x), p, dims=dims))
145+
_vtrenyientropy(p, α::T, dims) where {T<:Integer} = _vrenyientryop(p, float(α), dims)
149146
_vtrenyientropy(p, α::Rational{T}, dims) where {T} = _vtrenyientropy(p, float(α), dims)
150147
function vtrenyientropy(p, α::Real; dims=:)
151148
α < 0 && throw(ArgumentError("Order of Rényi entropy not legal, $(α) < 0."))
@@ -274,7 +271,7 @@ See also: [`vmaxad`](@ref)
274271
"""
275272
function vmeanad(x, y; dims=:)
276273
c = 1 / _denom(x, dims)
277-
vmapreducethen((xᵢ, yᵢ) -> abs(xᵢ - yᵢ) , +, z -> c * z, x, y, dims=dims)
274+
vmapreducethen((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)) , +, z -> c * z, x, y, dims=dims)
278275
end
279276

280277
"""
@@ -287,7 +284,7 @@ See also: [`vtmaxad`](@ref)
287284
"""
288285
function vtmeanad(x, y; dims=:)
289286
c = 1 / _denom(x, dims)
290-
vtmapreducethen((xᵢ, yᵢ) -> abs(xᵢ - yᵢ) , +, z -> c * z, x, y, dims=dims)
287+
vtmapreducethen((xᵢ, yᵢ) -> abs(float(xᵢ) - float(yᵢ)) , +, z -> c * z, x, y, dims=dims)
291288
end
292289

293290
"""
@@ -320,7 +317,7 @@ See also: [`vrmse`](@ref)
320317
"""
321318
function vmse(x, y; dims=:)
322319
c = 1 / _denom(x, dims)
323-
vmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, z -> c * z, x, y, dims=dims)
320+
vmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, z -> c * z, x, y, dims=dims)
324321
end
325322

326323
"""
@@ -333,7 +330,7 @@ See also: [`vtrmse`](@ref)
333330
"""
334331
function vtmse(x, y; dims=:)
335332
c = 1 / _denom(x, dims)
336-
vtmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, z -> c * z, x, y, dims=dims)
333+
vtmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, z -> c * z, x, y, dims=dims)
337334
end
338335

339336
"""
@@ -348,7 +345,7 @@ See also: [`vmse`](@ref)
348345
"""
349346
function vrmse(x, y; dims=:)
350347
c = 1 / _denom(x, dims)
351-
vmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, z -> (c * z), x, y, dims=dims)
348+
vmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, z -> (c * z), x, y, dims=dims)
352349
end
353350

354351
"""
@@ -363,7 +360,7 @@ See also: [`vtmse`](@ref)
363360
"""
364361
function vtrmse(x, y; dims=:)
365362
c = 1 / _denom(x, dims)
366-
vtmapreducethen((xᵢ, yᵢ) -> abs2(xᵢ - yᵢ) , +, z -> (c * z), x, y, dims=dims)
363+
vtmapreducethen((xᵢ, yᵢ) -> abs2(float(xᵢ) - float(yᵢ)) , +, z -> (c * z), x, y, dims=dims)
367364
end
368365

369366
# Match names with StatsBase

test/vstats.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ end
3030
end
3131
end
3232

33-
meanad(x, y; dims=:) = mean(abs.(x .- y), dims=dims)
33+
meanad(x, y; dims=:) = mean(abs.(float.(x) .- float.(y)), dims=dims)
3434
maxad(x, y; dims=:) = maximum(abs.(x .- y), dims=dims)
35-
mse(x, y; dims=:) = mean(abs2.(x .- y), dims=dims)
36-
rmse(x, y; dims=:) = .√mean(abs2.(x .- y), dims=dims)
35+
mse(x, y; dims=:) = mean(abs2.(float.(x) .- float.(y)), dims=dims)
36+
rmse(x, y; dims=:) = .√mean(abs2.(float.(x) .- float.(y)), dims=dims)
3737

3838
@testset "meanad" begin
3939
for T (Float32, Float64, Int32)

0 commit comments

Comments
 (0)