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 the getindex bug for abstract arrays #100

Merged
merged 1 commit into from
May 18, 2022
Merged

fix the getindex bug for abstract arrays #100

merged 1 commit into from
May 18, 2022

Conversation

mzgubic
Copy link
Collaborator

@mzgubic mzgubic commented May 17, 2022

Fixes the bug in the test (introduced in #88) which gives

julia> b = BlockDiagonal(AbstractMatrix{Float64}[rand(2, 2)]);

julia> b[1]
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/missing.jl:105
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Dates/src/periods.jl:53
  zero(::FillArrays.Ones{T, N}) where {T, N} at ~/.julia/packages/FillArrays/7oBjc/src/FillArrays.jl:539
  ...
Stacktrace:
  [1] zero(#unused#::Type{Any})
    @ Base ./missing.jl:106
  [2] reduce_empty(#unused#::typeof(+), #unused#::Type{Any})
    @ Base ./reduce.jl:313
  [3] reduce_empty(#unused#::typeof(Base.add_sum), #unused#::Type{Any})
    @ Base ./reduce.jl:322
  [4] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type)
    @ Base ./reduce.jl:345
  [5] reduce_empty(op::Base.MappingRF{typeof(identity), typeof(Base.add_sum)}, #unused#::Type{Any})
    @ Base ./reduce.jl:331
  [6] reduce_empty_iter
    @ ./reduce.jl:357 [inlined]
  [7] mapreduce_empty_iter(f::Function, op::Function, itr::Vector{Any}, ItrEltype::Base.HasEltype)
    @ Base ./reduce.jl:353
  [8] _mapreduce(f::typeof(identity), op::typeof(Base.add_sum), #unused#::IndexLinear, A::Vector{Any})
    @ Base ./reduce.jl:402
  [9] _mapreduce_dim
    @ ./reducedim.jl:330 [inlined]
 [10] #mapreduce#725
    @ ./reducedim.jl:322 [inlined]
 [11] mapreduce
    @ ./reducedim.jl:322 [inlined]
 [12] #_sum#735
    @ ./reducedim.jl:894 [inlined]
 [13] _sum
    @ ./reducedim.jl:894 [inlined]
 [14] #_sum#734
    @ ./reducedim.jl:893 [inlined]
 [15] _sum
    @ ./reducedim.jl:893 [inlined]
 [16] #sum#732
    @ ./reducedim.jl:889 [inlined]
 [17] sum(a::Vector{Any})
    @ Base ./reducedim.jl:889
 [18] _block_indices(B::BlockDiagonal{Float64, AbstractMatrix{Float64}}, i::Int64, j::Int64)
    @ BlockDiagonals ~/JuliaEnvs/GPForecasting.jl/dev/BlockDiagonals/src/blockdiagonal.jl:158
 [19] getindex(B::BlockDiagonal{Float64, AbstractMatrix{Float64}}, i::Int64, j::Int64)
    @ BlockDiagonals ~/JuliaEnvs/GPForecasting.jl/dev/BlockDiagonals/src/blockdiagonal.jl:140
 [20] _getindex
    @ ./abstractarray.jl:1257 [inlined]
 [21] getindex(A::BlockDiagonal{Float64, AbstractMatrix{Float64}}, I::Int64)
    @ Base ./abstractarray.jl:1218
 [22] top-level scope
    @ REPL[84]:1

@@ -152,7 +152,8 @@ function _block_indices(B::BlockDiagonal, i::Integer, j::Integer)
p += 1
j -= blocksize(B, p)[2]
end
@views @inbounds i -= sum(size.(blocks(B)[1:(p-1)], 1))
# isempty to avoid reducing over an empty collection
@views @inbounds i -= isempty(1:(p-1)) ? 0 : sum(size.(blocks(B)[1:(p-1)], 1))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The alternative is

Suggested change
@views @inbounds i -= isempty(1:(p-1)) ? 0 : sum(size.(blocks(B)[1:(p-1)], 1))
@views @inbounds i -= sum(size.(blocks(B), 1)[1:(p-1)])

which is cleaner but it allocates more since is builds the array of length nblocks rather than just the subset

@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

Merging #100 (d77c395) into master (fc727ab) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #100   +/-   ##
=======================================
  Coverage   95.92%   95.92%           
=======================================
  Files           5        5           
  Lines         319      319           
=======================================
  Hits          306      306           
  Misses         13       13           
Impacted Files Coverage Δ
src/blockdiagonal.jl 85.71% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc727ab...d77c395. Read the comment docs.

Copy link

@BaileyBox BaileyBox left a comment

Choose a reason for hiding this comment

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

Looks good to me 🙏

@mzgubic mzgubic merged commit b2ee90a into master May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants