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

fix weighted computations for non-real arrays #649

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ Base.:(==)(x::AbstractWeights, y::AbstractWeights) = false

Compute the weighted sum of an array `v` with weights `w`, optionally over the dimension `dim`.
"""
wsum(v::AbstractVector, w::AbstractVector) = dot(v, w)
wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w)
wsum(v::AbstractVector, w::AbstractVector) = dot(w, v)
wsum(v::AbstractArray, w::AbstractVector) = dot(w, vec(v))
wsum(v::AbstractArray, w::AbstractVector, dims::Colon) = wsum(v, w)

## wsum along dimension
Expand Down
2 changes: 2 additions & 0 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ a = reshape(1.0:27.0, 3, 3, 3)
@testset "Sum $f" for f in weight_funcs
@test sum([1.0, 2.0, 3.0], f([1.0, 0.5, 0.5])) ≈ 3.5
@test sum(1:3, f([1.0, 1.0, 0.5])) ≈ 4.5
@test sum([1 + 2im, 2 + 3im], f([1.0, 0.5])) ≈ 2 + 3.5im

for wt in ([1.0, 1.0, 1.0], [1.0, 0.2, 0.0], [0.2, 0.0, 1.0])
@test sum(a, f(wt), dims=1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims=1)
Expand All @@ -250,6 +251,7 @@ end
@testset "Mean $f" for f in weight_funcs
@test mean([1:3;], f([1.0, 1.0, 0.5])) ≈ 1.8
@test mean(1:3, f([1.0, 1.0, 0.5])) ≈ 1.8
@test mean([1 + 2im, 4 + 5im], f([1.0, 0.5])) ≈ 2 + 3im

for wt in ([1.0, 1.0, 1.0], [1.0, 0.2, 0.0], [0.2, 0.0, 1.0])
@test mean(a, f(wt), dims=1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims=1)/sum(wt)
Expand Down